1795. Rearrange Products Table LeetCode Solution
In this guide, you will get 1795. Rearrange Products Table LeetCode Solution with the best time and space complexity. The solution to Rearrange Products Table problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Rearrange Products Table solution in C++
- Rearrange Products Table solution in Java
- Rearrange Products Table solution in Python
- Additional Resources
Problem Statement of Rearrange Products Table
Table: Products
+————-+———+
| Column Name | Type |
+————-+———+
| product_id | int |
| store1 | int |
| store2 | int |
| store3 | int |
+————-+———+
product_id is the primary key (column with unique values) for this table.
Each row in this table indicates the product’s price in 3 different stores: store1, store2, and store3.
If the product is not available in a store, the price will be null in that store’s column.
Write a solution to rearrange the Products table so that each row has (product_id, store, price). If a product is not available in a store, do not include a row with that product_id and store combination in the result table.
Return the result table in any order.
The result format is in the following example.
Example not found
Constraints not found
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
1795. Rearrange Products Table LeetCode Solution in C++
SELECT
product_id,
'store1' AS store,
store1 AS price
FROM Products
WHERE store1 IS NOT NULL
UNION ALL
SELECT
product_id,
'store2',
store2
FROM Products
WHERE store2 IS NOT NULL
UNION ALL
SELECT
product_id,
'store3',
store3
FROM Products
WHERE store3 IS NOT NULL;
/* code provided by PROGIEZ */
1795. Rearrange Products Table LeetCode Solution in Java
N/A
// code provided by PROGIEZ
1795. Rearrange Products Table LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.