All files / leetCode 0496.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19        2x 2x   2x 8x 5x   8x     2x 5x 2x    
export default function nextGreaterElement(
  nums1: number[],
  nums2: number[],
): number[] {
  const stack: number[] = []
  const greaterMap = new Map<number, number>()
 
  for (const num of nums2) {
    while (stack.length && stack[stack.length - 1] < num)
      greaterMap.set(stack.pop() as number, num)
 
    stack.push(num)
  }
 
  const result: number[] = []
  for (const num of nums1) result.push(greaterMap.get(num) ?? -1)
  return result
}