LeetCode 2881: Create a New Column Solution

Master LeetCode problem 2881 (Create a New Column), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2881. Create a New Column

Easy

Problem Explanation

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:

    1. Iterate through each row in the DataFrame.
    2. For each row, double the value in the "salary" column to calculate the bonus.
    3. 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).

Solution Code

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);
        }
    }
}

Try It Yourself

Loading code editor...

Frequently Asked Questions

How to solve LeetCode 2881 (Create a New Column)?

This page provides optimized solutions for LeetCode problem 2881 (Create a New Column) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2881 (Create a New Column)?

The time complexity for LeetCode 2881 (Create a New Column) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2881 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2881 in Java, C++, or Python.

Back to LeetCode Solutions