Monotone Increasing Digits LeetCode solution in Java

1
class Solution {
2
public int monotoneIncreasingDigits(int n) {
3
int position;
4
int digitInTheNextPosition;
5
while ((position = getThePositionNotSatisfied(n)) != -1) {
6
digitInTheNextPosition = ((int) (n / Math.pow(10, position - 1))) % 10;
7
n -= Math.pow(10, position - 1) * (digitInTheNextPosition + 1);
8
n -= n % Math.pow(10, position);
9
n += Math.pow(10, position) - 1;
10
}
11
return n;
12
}
13

14
public int getThePositionNotSatisfied(int n) {
15
int k = 10;
16
int position = 0;
17
while (n > 0) {
18
if (k < n % 10) {
19
return position;
20
} else {
21
k = n % 10;
22
n /= 10;
23
position++;
24
}
25
}
26
return -1;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0