Rising Temperature

Easy

Topics
ArrayHash TableSorting

Each row of matrix is a weather reading [id, day, temperature]. The day values are integer day numbers and every row has a distinct day.

Return the id of every reading whose temperature is higher than the temperature on the previous day, that is on day - 1. Return them in ascending order of id.

LeetCode poses this as a SQL question over a Weather table with a DATE column. Here the dates are integer day numbers and the table arrives as a matrix named matrix, which keeps the question identical while making it answerable in the four judge languages.

Example 1

01110122252332034430iddaytemp
warmer than the day beforenot counted
These 4 readings give [2,4]. A reading qualifies only against the reading for day - 1, and the rows arrive in no particular order, so the row above is not the previous day and cannot be used as one. Here 1 row has no reading for the day before it at all, and a missing day is no comparison rather than a free pass.
Days are integer day numbers here rather than dates, and no two rows share a day.
Input:  matrix = [[1,1,10],[2,2,25],[3,3,20],[4,4,30]]
Output: [2,4]
Explanation: Day 2 at 25 beats day 1 at 10, and day 4 at 30 beats day 3 at 20. Day 3 at 20 is colder than day 2.

Example 2

0111012320iddaytemp
not counted
These 2 readings give an empty answer.
Input:  matrix = [[1,1,10],[2,3,20]]
Output: []
Explanation: Day 3 has no reading for day 2, so there is nothing to compare it with. A gap is not a comparison.

Example 3

0523019130iddaytemp
not counted
Input:  matrix = [[5,2,30],[9,1,30]]
Output: []
Explanation: Equal temperatures do not count; the rule is strictly higher.

Constraints

  • 1 <= matrix.length <= 10^4
  • matrix[i].length == 3
  • 1 <= day <= 10^5
  • Days are distinct
  • -100 <= temperature <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

The rows arrive in no particular order, so "the previous day" is not "the previous row". That is the whole trap. Comparing each row with the one above it is wrong twice over: the order is arbitrary, and consecutive rows need not be consecutive days.

What is needed is a lookup from a day number to its temperature. Build that first, then every row can be answered on its own: look up day - 1, and if it exists and is colder, the row qualifies.

The missing-day case is not an edge case to patch afterwards, it is half the specification. A day whose predecessor is absent has no comparison to make and is therefore excluded — not included by default.

Sorting the rows by day and walking them also works, but then you still have to check that each neighbour really is the previous day rather than merely the next reading. The lookup makes that check impossible to forget.

Approach

  1. Build a map from day to temperature over all rows.
  2. For each row, look up day - 1 in the map.
  3. If that day is present and its temperature is strictly less than this row’s, record this row’s id.
  4. Sort the recorded ids ascending and return them.

Walkthrough

matrix = [[1,1,10],[2,2,25],[3,3,20],[4,4,30]]

day -> temperature
   1 -> 10
   2 -> 25
   3 -> 20
   4 -> 30

row by row
   id 1, day 1: day 0 absent          -> no
   id 2, day 2: day 1 is 10 < 25      -> YES
   id 3, day 3: day 2 is 25, not < 20 -> no
   id 4, day 4: day 3 is 20 < 30      -> YES

answer [2, 4]

the gap case
matrix = [[1,1,10],[2,3,20]]
   id 2, day 3: day 2 is absent       -> no
   answer []

   walking the rows in order would compare day 3 with
   day 1 and wrongly report id 2.

Complexity

  • Time — O(n log n): building and probing the map is linear; the final sort of the ids dominates.
  • Space — O(n): one map entry per reading, plus the output.

Common Pitfalls

  • Comparing each row with the previous row. The rows are unordered and consecutive rows need not be consecutive days.
  • Treating a missing previous day as a pass. It excludes the row.
  • Using >=. The rule is strictly higher, so equal temperatures do not qualify.
  • Returning the days or the temperatures instead of the ids.
  • Returning the ids in input order. The answer is sorted ascending by id.

Full Solution — all languages

class Solution:
    def risingTemperature(self, matrix):
        # day -> temperature, so "the previous day" is a lookup and not a
        # neighbouring row. The rows arrive in arbitrary order.
        temp_on = {}
        for row in matrix:
            temp_on[row[1]] = row[2]

        out = []
        for row_id, day, temp in matrix:
            yesterday = temp_on.get(day - 1)
            # An absent previous day means there is no comparison to make.
            if yesterday is not None and yesterday < temp:
                out.append(row_id)

        return sorted(out)
SensAI
Beta
Tap the mic to stop voice mode