Problem
Given a list of weather records โ each with an ID, a calendar day number, and a temperature reading โ return the IDs of all entries where the temperature was strictly higher than the temperature recorded on the immediately preceding calendar day. If no record exists for the previous day, that entry is excluded from the result.
Example:
- Input:
weather = [[1,1,10],[2,2,25],[3,3,20],[4,4,30]](each entry is[id, day, temperature]) - Output:
[2, 4] - Explanation: Day 2 (25ยฐ) beats day 1 (10ยฐ); day 3 (20ยฐ) is cooler than day 2 (25ยฐ); day 4 (30ยฐ) beats day 3 (20ยฐ).
Intuition
The challenge is connecting each record to the record from exactly one calendar day earlier โ records can appear in any order, and some days may be absent entirely. Storing a map from day number to temperature lets you answer "what was yesterday's temperature?" in O(1) for any record, without scanning the whole list each time.
Approach 1 โ Brute Force
For each record, scan the entire list looking for the record whose day equals current_day - 1, then compare temperatures.
Steps:
- For each record
(id, day, temp), iterate over all other records - Find the record whose day is exactly
current_day - 1 - If found and
current_tempis strictly greater than that day's temperature, addidto result - Break early after the first match (at most one record per day)
1def risingTemperature(weather):
2 result = []
3 for current_id, current_day, current_temp in weather:
4 for _, other_day, other_temp in weather:
5 if other_day == current_day - 1 and current_temp > other_temp:
6 result.append(current_id)
7 break # at most one record per day, so stop after first match
8 return result- Time: O(nยฒ) โ for each of n records, we scan all n records to locate the previous day's entry
- Space: O(1) โ no auxiliary data structures beyond the result list
Approach 2 โ Hash Map (Optimal)
Build a lookup from day number to temperature in one pass, then answer every "what was yesterday's temperature?" query in O(1).
Steps:
- Scan
weatheronce, insertingday โ temperatureinto a hash map - For each record
(id, day, temp), computeprevious_day = day - 1 - If
previous_dayexists in the map andtemp > map[previous_day], addidto result - Return result
1def risingTemperature(weather):
2 day_to_temp = {day: temp for _, day, temp in weather}
3
4 result = []
5 for record_id, day, temp in weather:
6 # previous_day may not exist if that calendar day has no record
7 if day - 1 in day_to_temp and temp > day_to_temp[day - 1]:
8 result.append(record_id)
9
10 return result- Time: O(n) โ two linear passes: one to build the map, one to query it; each lookup is O(1)
- Space: O(n) โ hash map stores one entry per record
Complexity Summary
| Approach | Time | Space | When to use |
|---|---|---|---|
| Brute Force | O(nยฒ) | O(1) | Only as a baseline; too slow for large datasets |
| Hash Map | O(n) | O(n) | Always โ the standard approach for "lookup by date or key" |
Common Mistakes
- Comparing records by array index instead of calendar date: Using
weather[i-1]fetches the previous element in the array, not the record from the previous calendar day. Records can arrive in any order, and days may be missing entirely. - Storing
day โ idinstead ofday โ temperature: The map needs to hold the temperature for comparison โ the outer loop already gives you the current ID directly, so the map only needs to answer "what was yesterday's temperature?". - Assuming all days are consecutive: If day 5 is in the dataset but day 4 is absent, day 5 must not appear in the result. The hash map handles this automatically โ
day - 1simply won't exist as a key. - Looking up
dayinstead ofday - 1: This compares a record's temperature against itself (always equal, never strictly greater). The subtraction happens on the key side when querying the map, not on the value side.
Related Problems
Two Sumโ same "build a hash map, then query it in O(1)" patternContains Duplicateโ hash set to track previously seen values in a single passSubarray Sum Equals Kโ prefix sums stored in a map for O(1) range queriesBest Time To Buy And Sell Stockโ comparing today's value against the best value from any previous dayContiguous Arrayโ hash map keyed by a derived value to find matching indices in O(n)