Skip to content

Commit 1550f1f

Browse files
committed
day76
1 parent 8940540 commit 1550f1f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Day76/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var nextGreaterElement = function(n) {
6+
7+
let array = n.toString().split("");
8+
9+
let length = array.length-1;
10+
11+
let dIndex = -1;
12+
let d = -Infinity;
13+
14+
for(let i= length-1; i>=0; i--){
15+
if(array[i] < array[i+1]){
16+
d = array[i];
17+
dIndex = i;
18+
break;
19+
}
20+
}
21+
22+
//edge
23+
if(dIndex == -1)return -1;
24+
25+
for(let i = length; i>=0; i--){
26+
if(array[i] > d){
27+
//swap
28+
swap(array,i, dIndex);
29+
break
30+
}
31+
}
32+
33+
let restofData = array.slice(dIndex+1).sort((a,b)=> a-b);
34+
let res = [...array.slice(0, dIndex+1),...restofData].join("");
35+
36+
let max = Math.pow(2,31);
37+
if(res > max || res <= n)return -1;
38+
return res
39+
40+
41+
42+
function swap(array, i, j){
43+
return [array[i], array[j]] = [array[j],array[i]]
44+
}
45+
};

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,6 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
169169
|Day 74| [1602. Find Nearest Right Node in Binary Tree](https://leetcode.com/problems/find-nearest-right-node-in-binary-tree/) | [javascript]()|[:memo:](https://leetcode.com/problems/find-nearest-right-node-in-binary-tree/)|Medium|
170170

171171
|Day 75| [110. Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [javascript]()|[:memo:](https://leetcode.com/problems/balanced-binary-tree/)|Easy|
172+
173+
174+
|Day 76| [556. Next Greater Element III](https://leetcode.com/problems/next-greater-element-iii/) | [javascript]()|[:memo:](https://leetcode.com/problems/next-greater-element-iii/)|Medium|

0 commit comments

Comments
 (0)