LeetCode 2890: Reshape Data: Melt Solution
Master LeetCode problem 2890 (Reshape Data: Melt), 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.
2890. Reshape Data: Melt
Problem Explanation
Explanation:
To reshape the data from wide to long format, we need to iterate over the columns representing the quarters and create a new row for each product and quarter combination.
- Iterate over each product.
- For each product, iterate over the quarters and create a new row with the product name, quarter name, and sales value.
- Repeat this process for all products and quarters to reshape the data.
Time Complexity: O(n*m) where n is the number of products and m is the number of quarters.
Space Complexity: O(n*m) for the reshaped data.
:
Solution Code
public List<List<String>> reshapeData(List<List<String>> data) {
List<List<String>> reshapedData = new ArrayList<>();
reshapedData.add(Arrays.asList("product", "quarter", "sales"));
for (int i = 1; i < data.get(0).size(); i++) {
String quarter = "quarter_" + i;
for (int j = 1; j < data.size(); j++) {
List<String> row = new ArrayList<>();
row.add(data.get(j).get(0)); // product
row.add(quarter);
row.add(data.get(j).get(i)); // sales
reshapedData.add(row);
}
}
return reshapedData;
}Try It Yourself
Loading code editor...
Frequently Asked Questions
How to solve LeetCode 2890 (Reshape Data: Melt)?
This page provides optimized solutions for LeetCode problem 2890 (Reshape Data: Melt) 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 2890 (Reshape Data: Melt)?
The time complexity for LeetCode 2890 (Reshape Data: Melt) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2890 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2890 in Java, C++, or Python.