
Sponsored
Sponsored
Most data manipulation libraries provide a direct method to access the top few rows of a DataFrame. This approach utilizes the built-in method to fetch the first three rows efficiently.
Time Complexity: O(1), since it directly accesses the specified rows.
Space Complexity: O(1), no additional space usage beyond the output.
1function getFirstThreeRows(employees) {
2 returnIn JavaScript, if you have a 2D array representing the DataFrame, the slice method helps to extract the first three elements efficiently.
This approach involves manually iterating over the DataFrame and extracting the first three rows. It's useful in languages or environments where built-in functionality might not exist.
Time Complexity: O(1), since we only loop through a fixed number of rows.
Space Complexity: O(1), as we only store a constant number of rows.
1#include <iostream>
2#include <vector>
3#include <string>
4
5using namespace std;
6
7typedef vector<vector<string>> DataFrame;
8
9DataFrame getFirstThreeRows(const DataFrame &employees) {
10 DataFrame result;
11 for (int i = 0; i < 3 && i < employees.size(); ++i) {
12 result.push_back(employees[i]);
13 }
14 return result;
15}This C++ solution manually iterates through the rows of a DataFrame using a for loop. We ensure to only access rows as long as they exist, preventing out-of-bound errors.