1321. Restaurant Growth LeetCode Solution

In this guide, you will get 1321. Restaurant Growth LeetCode Solution with the best time and space complexity. The solution to Restaurant Growth 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

  1. Problem Statement
  2. Complexity Analysis
  3. Restaurant Growth solution in C++
  4. Restaurant Growth solution in Java
  5. Restaurant Growth solution in Python
  6. Additional Resources
1321. Restaurant Growth LeetCode Solution image

Problem Statement of Restaurant Growth

Table: Customer

+—————+———+
| Column Name | Type |
+—————+———+
| customer_id | int |
| name | varchar |
| visited_on | date |
| amount | int |
+—————+———+
In SQL,(customer_id, visited_on) is the primary key for this table.
This table contains data about customer transactions in a restaurant.
visited_on is the date on which the customer with ID (customer_id) has visited the restaurant.
amount is the total paid by a customer.

You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day).
Compute the moving average of how much the customer paid in a seven days window (i.e., current day + 6 days before). average_amount should be rounded to two decimal places.
Return the result table ordered by visited_on in ascending 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

1321. Restaurant Growth LeetCode Solution in C++

WITH
  Dates AS (
    SELECT DISTINCT visited_on
    FROM Customer
    WHERE visited_on >= (
      SELECT DATE_ADD(MIN(visited_on), INTERVAL 6 DAY)
      FROM Customer
    )
  )
SELECT
  Dates.visited_on,
  SUM(Customer.amount) AS amount,
  ROUND(SUM(Customer.amount) / 7, 2) AS average_amount
FROM Dates
LEFT JOIN Customer
  ON (DATEDIFF(Dates.visited_on, Customer.visited_on) BETWEEN 0 AND 6)
GROUP BY 1;
/* code provided by PROGIEZ */

1321. Restaurant Growth LeetCode Solution in Java

N/A
// code provided by PROGIEZ

1321. Restaurant Growth LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.