Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/b3062b12a24b23173fbb009803e442b0 to your computer and use it in GitHub Desktop.
Save jianminchen/b3062b12a24b23173fbb009803e442b0 to your computer and use it in GitHub Desktop.
2.
Given an Array, replace each element in the Array with its Next Element(To its RHS) which is Larger than it. If no such element exists,
then no need to replace.
Ex:
i/p: {2,12,8,6,5,1,2,10,3,2}
o/p:{12,12,10,10,10,2,10,10,3,2}
keywords:
array, integer,
replace by next bigger value in the array after current index, if not found, no change
Go over example:
2 -> 12
12 -> 12
8 -> 10
6->10
5->10
1->2
2->10
10->10
3->3
2->2
brute force-> no preprocessing time complexity: n * n
preprocessing right -> left
element save left side last value
left -> right
2,12,8,6,5
1, 2, 3, 2, 1
->
left -> right
1,
->
,2,
3,2, 4
4,
?,
2, 1, 3, 4
3 3 4, 4
desc stack,
8 7
insc stack,
8:00 am - 9:30 am
interviewer gave me the link:
https://leetcode.com/problems/next-greater-element-i/description/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment