LeetCode 2877: Create a DataFrame from List Solution

Master LeetCode problem 2877 (Create a DataFrame from List), 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.

2877. Create a DataFrame from List

Easy

Problem Explanation

Explanation:

To create a DataFrame from a 2D list called student_data, we need to iterate over the list and extract the student ID and age for each student. We can then format this data into a tabular form with columns for student_id and age.

Algorithm:

  1. Create an empty DataFrame.
  2. For each row in student_data, extract the student ID and age.
  3. Add these values to the DataFrame under the corresponding columns.
  4. Display the DataFrame in the required format.

Time Complexity:

The time complexity of this algorithm is O(n), where n is the number of rows in the student_data list.

Space Complexity:

The space complexity is also O(n) since we are storing the data in a DataFrame.

:

Solution Code

import java.util.List;
import java.util.ArrayList;

public class Solution {
    public DataFrame createDataFrame(List<List<Integer>> student_data) {
        DataFrame df = new DataFrame();
        df.addColumn("student_id");
        df.addColumn("age");

        for (List<Integer> row : student_data) {
            df.addRow(row.get(0), row.get(1));
        }

        return df;
    }

    static class DataFrame {
        List<String> columns;
        List<List<Integer>> data;

        public DataFrame() {
            columns = new ArrayList<>();
            data = new ArrayList<>();
        }

        public void addColumn(String columnName) {
            columns.add(columnName);
        }

        public void addRow(int student_id, int age) {
            List<Integer> row = new ArrayList<>();
            row.add(student_id);
            row.add(age);
            data.add(row);
        }

        public void display() {
            System.out.println("+------------+-----+");
            System.out.println("| student_id | age |");
            System.out.println("+------------+-----+");

            for (List<Integer> row : data) {
                System.out.printf("| %-11d | %-3d |\n", row.get(0), row.get(1));
            }

            System.out.println("+------------+-----+");
        }
    }
}

Try It Yourself

Loading code editor...

Frequently Asked Questions

How to solve LeetCode 2877 (Create a DataFrame from List)?

This page provides optimized solutions for LeetCode problem 2877 (Create a DataFrame from List) 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 2877 (Create a DataFrame from List)?

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

Can I run code for LeetCode 2877 on DevExCode?

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

Back to LeetCode Solutions