Sponsored
Sponsored
This approach utilizes SQL window functions to help identify consecutive rows with the required conditions. By computing a lag on the 'people' field, we can identify groups of qualifying rows by checking the conditions on currently selected and previous rows.
The time complexity of this query is O(n), where n is the number of rows because each row is processed once. The space complexity is also O(n) due to the use of window functions.
1SELECT id, visit_date, people FROM (SELECT id, visit_date, people, LAG(people, 1) OVER (ORDER BY id) AS prev1, LAG(people, 2) OVER (ORDER BY id) AS prev2 FROM Stadium) WHERE people >= 100 AND prev1 >= 100 AND prev2 >= 100;
This SQL solution makes use of the LAG function to look into previous rows' people counts. The three consecutive rows meeting the criteria are filtered using WHERE conditions.
In this approach, we manually iterate over the records, maintaining a sliding window of three elements to check the condition on people counts. This involves checking the count of the previous two IDs.
The time complexity of this approach is O(n), where n is the length of the dataset, as each row is visited once. The space complexity is O(1) for storing temporary variables outside of external list storage.
1def find_consecutive(arr):
2 result = []
3 for i in
This approach utilizes a sliding window or similar mechanism to iterate over the rows and assess whether consecutive rows satisfy the conditions.
Time Complexity: O(n), where n is the number of records in the stadium table.
Space Complexity: O(n) due to the result storage.
1def human_traffic(stadium):
2 results = []
3 n = len(
This approach employs a segmenting strategy, similar to using SQL WHERE clauses, to filter and then sort the data based on conditions using language-specific features.
Time Complexity: O(n log n), primarily due to the sorting operation.
Space Complexity: O(n) as it stores the results list.
1function humanTraffic(stadium) {
2 let results = [];
3
This Python solution iterates over the input data with an index starting from the third row and checks if the current and the previous two rows have a people count of at least 100. If valid, the rows are then added to the result, ensuring no duplicates using dictionary keys to preserve order.
The solution traverses the list of visits and uses a sliding window approach to check for three or more consecutive days where the number of people in attendance is greater than or equal to 100. If the condition is met, it appends those days to the results list. Finally, the results are deduplicated and sorted by visit date.
The JS solution iterates over the stadium data and counts consecutive entries with more than or equal to 100 people. Once the count reaches three, it gathers these entries into a result list. Finally, the results are sorted based on date before returning.