Skip to content

Commit 33cffba

Browse files
authored
Merge pull request #2150 from a93a/main
Create 0496-next-greater-element-i.kt
2 parents 7409320 + 86d35d1 commit 33cffba

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

kotlin/0496-next-greater-element-i.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)