-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path738-MonotoneIncreasingDigits.go
More file actions
58 lines (50 loc) · 1.26 KB
/
738-MonotoneIncreasingDigits.go
File metadata and controls
58 lines (50 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
// 738. Monotone Increasing Digits
// An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.
// Given an integer n, return the largest number that is less than or equal to n with monotone increasing digits.
// Example 1:
// Input: n = 10
// Output: 9
// Example 2:
// Input: n = 1234
// Output: 1234
// Example 3:
// Input: n = 332
// Output: 299
// Constraints:
// 0 <= n <= 10^9
import "fmt"
func monotoneIncreasingDigits(n int) int {
res, i, digits := 0, 0, []int{}
for n > 0 {
digits = append(digits, n % 10)
n = n / 10
}
for i < len(digits) - 1 {
if digits[i] < digits[i+1] {
for j := 0; j <= i; j++ {
digits[j] = 9
}
digits[i+1]--
}
i++
}
for i := len(digits) - 1; i >= 0; i-- {
res = res * 10 + digits[i]
}
return res
}
func main() {
// Example 1:
// Input: n = 10
// Output: 9
fmt.Println(monotoneIncreasingDigits(10)) // 9
// Example 2:
// Input: n = 1234
// Output: 1234
fmt.Println(monotoneIncreasingDigits(1234)) // 1234
// Example 3:
// Input: n = 332
// Output: 299
fmt.Println(monotoneIncreasingDigits(332)) // 299
}