Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Monotonic stack & queue

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Monotonic stack & queue

Next Greater Element I

intermediatelayer A8 · Monotonic stack & queue5 tests

nums1 is a subset of nums2, and both contain distinct values. For each value in nums1, find it inside nums2 and return the first value to its right in nums2 that is greater than it.

  • If there is no such value, use -1
  • The result lines up positionally with nums1
  • Aim for O(n + m) rather than searching nums2 once per query

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen