#2886

Change Data Type

Easy
DataFrame manipulationType conversion
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(1)
💡

Intuition

Time O(n)Space O(1)

Using pandas built-in functionality allows us to efficiently convert the data type of the grade column in one step without manual iteration.

⚙️

Algorithm

3 steps
  1. 1Step 1: Import the pandas library.
  2. 2Step 2: Use the DataFrame's astype method to convert the grade column to int.
  3. 3Step 3: Return or display the modified DataFrame.
solution.py5 lines
1import pandas as pd
2
3def convert_grades_optimal(students):
4    students['grade'] = students['grade'].astype(int)
5    return students

Complexity note: This complexity is linear because we are directly converting the column in one operation without the need for additional storage.

  • 1Using built-in functions can greatly improve efficiency.
  • 2Understanding data types is crucial for data manipulation.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.