Baseline Stack and Queues · Monotonic Stack and Queue O(n + m) · O(n)
A monitoring system tracks temperature probes across two racks. Rack B holds the full probe layout, and rack A carries a subset of those probe readings in the same order. For every probe on rack A, the dashboard must show the first probe to its right on rack B that reads strictly hotter — or a dash when none exists.
Input: Two distinct integer arrays, nums1 (the queries) and nums2 (the master layout), with nums1 a subset of nums2.
Output: Return an array where entry i answers the query for nums1[i]: the next strictly greater value to its right in nums2, or -1.
1 <= nums1.length <= nums2.length <= 10000 <= nums1[i], nums2[i] <= 10^4All values are distinct and nums1 is a subset of nums2Input: {"nums1":[4,1,2],"nums2":[1,3,4,2]}
Output: [-1,3,-1]
4 has nothing hotter to its right; 1 is followed by 3; 2 has nothing after it but 3 came earlier.
Input: {"nums1":[2,4],"nums2":[1,2,3,4]}
Output: [3,-1]
2's next greater is 3; the maximum 4 has no greater element anywhere to its right.