DataFrame products
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| name | object |
| quantity | int |
| price | int |
+-------------+--------+
Write a solution to fill in the missing value as 0 in the quantity column.
The result format is in the following example.
Example 1: Input:+-----------------+----------+-------+ | name | quantity | price | +-----------------+----------+-------+ | Wristwatch | None | 135 | | WirelessEarbuds | None | 821 | | GolfClubs | 779 | 9319 | | Printer | 849 | 3051 | +-----------------+----------+-------+ Output: +-----------------+----------+-------+ | name | quantity | price | +-----------------+----------+-------+ | Wristwatch | 0 | 135 | | WirelessEarbuds | 0 | 821 | | GolfClubs | 779 | 9319 | | Printer | 849 | 3051 | +-----------------+----------+-------+ Explanation: The quantity for Wristwatch and WirelessEarbuds are filled by 0.
This method involves iterating over each row of the DataFrame. We check if the 'quantity' value is null (or equivalent) and set it to zero if it is. This is a straightforward approach that uses conditional checks within loops appropriate for each programming language.
In this C code, we define an array of structs, each representing a product. The quantity is assumed to be -1 when missing. The function fillMissingQuantities iterates over the products and sets any -1 quantity to 0. Finally, we print out the updated products.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the number of products.
Space Complexity: O(1), as we do not use additional space proportional to the input size.
This approach involves leveraging built-in functions or libraries available in programming languages, like pandas in Python or LINQ in C#, to perform the task of filling in missing values effectively and efficiently.
Here, we stick with the manual way as C does not have a specific library for DataFrame handling. We assume -1 indicates a missing value, replaced by 0.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the number of products.
Space Complexity: O(1), without additional space for the task.
| Approach | Complexity |
|---|---|
| Iterate and Replace Using Conditional Logic | Time Complexity: O(n), where n is the number of products. |
| Use Built-In Functions or Libraries | Time Complexity: O(n), where n is the number of products. |
Trapping Rain Water - Google Interview Question - Leetcode 42 • NeetCode • 558,064 views views
Watch 9 more video solutions →Practice Fill Missing Data with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor