EasyArrays & Hashing

Rising Temperature โ€” Solution

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:

  1. For each record (id, day, temp), iterate over all other records
  2. Find the record whose day is exactly current_day - 1
  3. If found and current_temp is strictly greater than that day's temperature, add id to result
  4. 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:

  1. Scan weather once, inserting day โ†’ temperature into a hash map
  2. For each record (id, day, temp), compute previous_day = day - 1
  3. If previous_day exists in the map and temp > map[previous_day], add id to result
  4. 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

ApproachTimeSpaceWhen to use
Brute ForceO(nยฒ)O(1)Only as a baseline; too slow for large datasets
Hash MapO(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 โ†’ id instead of day โ†’ 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 - 1 simply won't exist as a key.
  • Looking up day instead of day - 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

Ready to practice? Try it on SkillFlow

Adaptive problems, AI follow-up interviews, and a skill score that shows exactly where you need to improve.

Practice This Problem โ†’