Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Comparison order is not matching up

karrad

Sep 16, 2023

  • Input: nums1 = [9,7,1]nums2 = [1,7,9,5,4,3]
  • Output: [-1,9,7]

In this example, we first take 9 and compare to every number to right of 9 in nums2. No number >9 so -1

Then 7. The 1st number to right of 7 (i.e only consider last 4 numbers in nums2) is 9

Now we consider 1. If we go by the same logic, we should only consider 5,4,3 in nums2. Why is the answer 7? Should it not be 5?

Thanks

M

2

0

Comments
Comments
K
karrad 2 years ago

Ok. I think I got it. So the order is all within the nums2 list. nums1 just gives the identity.

So the next greatest number of 1 will be 7 as 7 is after 1 in the nums2 array.

On this page

Definition of Monotonic Stack

Types of Monotonic Stacks

Identifying Problems Suitable for Monotonic Stack

Constructing Monotonic Stacks

Understanding Time Complexity