We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 7409320 + 86d35d1 commit 33cffbaCopy full SHA for 33cffba
kotlin/0496-next-greater-element-i.kt
@@ -0,0 +1,22 @@
1
+class Solution {
2
+ fun nextGreaterElement(nums1: IntArray, nums2: IntArray): IntArray {
3
+ val res = IntArray(nums1.size){ -1 }
4
+
5
+ val hm = HashMap<Int, Int>()
6
+ for(i in 0 until nums1.size)
7
+ hm[nums1[i]] = i
8
9
+ val stack = ArrayDeque<Int>()
10
+ for(current in nums2){
11
+ while(stack.isNotEmpty() && current > stack.getLast()){
12
+ val element = stack.removeLast()
13
+ val index = hm[element]!!
14
+ res[index] = current
15
+ }
16
+ if(current in hm)
17
+ stack.addLast(current)
18
19
20
+ return res
21
22
+}
0 commit comments