Rising Temperature
Easy
Topics
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
[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.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
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
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
- Build a map from
daytotemperatureover all rows. - For each row, look up
day - 1in the map. - If that day is present and its temperature is strictly less than this row’s, record this row’s
id. - 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)import java.util.*;
class Solution {
public java.util.List<Integer> risingTemperature(int[][] matrix) {
Map<Integer, Integer> tempOn = new HashMap<>();
for (int[] row : matrix) tempOn.put(row[1], row[2]);
List<Integer> out = new ArrayList<>();
for (int[] row : matrix) {
Integer yesterday = tempOn.get(row[1] - 1);
if (yesterday != null && yesterday < row[2]) out.add(row[0]);
}
Collections.sort(out);
return out;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: vector<int> risingTemperature(vector<vector<int>>& matrix) {
unordered_map<int, int> tempOn;
for (auto& row : matrix) tempOn[row[1]] = row[2];
vector<int> out;
for (auto& row : matrix) {
auto it = tempOn.find(row[1] - 1);
if (it != tempOn.end() && it->second < row[2]) out.push_back(row[0]);
}
sort(out.begin(), out.end());
return out;
}
};/**
* @param {number[][]} matrix
* @return {number[]}
*/ var risingTemperature = function(matrix) {
const tempOn = new Map();
for (const row of matrix) tempOn.set(row[1], row[2]);
const out = [];
for (const [rowId, day, temp] of matrix) {
const yesterday = tempOn.get(day - 1);
if (yesterday !== undefined && yesterday < temp) out.push(rowId);
}
return out.sort((x, y) => x - y);
};