LeetCode 2881: Create a New Column
Problem Description
Explanation
To create a new column named "bonus" in the DataFrame employees containing doubled values of the "salary" column, we simply need to iterate through each row of the DataFrame and calculate the bonus value by doubling the salary value. We can then add this bonus value as a new column in the DataFrame.
-
Algorithm:
- Iterate through each row in the DataFrame.
- For each row, double the value in the "salary" column to calculate the bonus.
- Add the calculated bonus as a new column named "bonus" in the DataFrame.
-
Time Complexity: O(n), where n is the number of rows in the DataFrame.
-
Space Complexity: O(1) (not considering the space required for the DataFrame itself).
Solutions
import java.util.*;
class Solution {
public void createNewColumn(DataFrame employees) {
for (int i = 0; i < employees.size(); i++) {
int salary = employees.get(i).get("salary");
employees.get(i).put("bonus", salary * 2);
}
}
}
Loading editor...