-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2827-NumberOfBeautifulIntegersInTheRange.go
More file actions
128 lines (117 loc) · 4.92 KB
/
2827-NumberOfBeautifulIntegersInTheRange.go
File metadata and controls
128 lines (117 loc) · 4.92 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
// 2827. Number of Beautiful Integers in the Range
// You are given positive integers low, high, and k.
// A number is beautiful if it meets both of the following conditions:
// 1. The count of even digits in the number is equal to the count of odd digits.
// 2. The number is divisible by k.
// Return the number of beautiful integers in the range [low, high].
// Example 1:
// Input: low = 10, high = 20, k = 3
// Output: 2
// Explanation: There are 2 beautiful integers in the given range: [12,18].
// - 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
// - 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
// Additionally we can see that:
// - 16 is not beautiful because it is not divisible by k = 3.
// - 15 is not beautiful because it does not contain equal counts even and odd digits.
// It can be shown that there are only 2 beautiful integers in the given range.
// Example 2:
// Input: low = 1, high = 10, k = 1
// Output: 1
// Explanation: There is 1 beautiful integer in the given range: [10].
// - 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1.
// It can be shown that there is only 1 beautiful integer in the given range.
// Example 3:
// Input: low = 5, high = 5, k = 2
// Output: 0
// Explanation: There are 0 beautiful integers in the given range.
// - 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits.
// Constraints:
// 0 < low <= high <= 10^9
// 0 < k <= 20
import "fmt"
import "strconv"
func numberOfBeautifulIntegers(low int, high int, k int) int {
gen := func(m, n, k int) [][][]int {
res := make([][][]int, m)
for i := 0; i < m; i++ {
res[i] = make([][]int, n)
for j := 0; j < n; j++ {
res[i][j] = make([]int, k)
for d := 0; d < k; d++ {
res[i][j][d] = -1
}
}
}
return res
}
s := strconv.Itoa(high)
f := gen(len(s), k, 21)
var dfs func(pos, mod, diff int, lead, limit bool) int
dfs = func(pos, mod, diff int, lead, limit bool) int {
if pos >= len(s) {
if mod == 0 && diff == 10 { return 1 }
return 0
}
if !lead && !limit && f[pos][mod][diff] != -1 {
return f[pos][mod][diff]
}
up := 9
if limit {
up = int(s[pos] - '0')
}
res := 0
for i := 0; i <= up; i++ {
if i == 0 && lead {
res += dfs(pos + 1, mod, diff, true, limit && i == up)
} else {
next := diff + 1
if i%2 == 0 {
next -= 2
}
res += dfs(pos+1, (mod * 10 + i) % k, next, false, limit && i == up)
}
}
if !lead && !limit {
f[pos][mod][diff] = res
}
return res
}
a := dfs(0, 0, 10, true, true)
s = strconv.Itoa(low - 1)
f = gen(len(s), k, 21)
b := dfs(0, 0, 10, true, true)
return a - b
}
func main() {
// Example 1:
// Input: low = 10, high = 20, k = 3
// Output: 2
// Explanation: There are 2 beautiful integers in the given range: [12,18].
// - 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
// - 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
// Additionally we can see that:
// - 16 is not beautiful because it is not divisible by k = 3.
// - 15 is not beautiful because it does not contain equal counts even and odd digits.
// It can be shown that there are only 2 beautiful integers in the given range.
fmt.Println(numberOfBeautifulIntegers(10, 20, 3)) // 2
// Example 2:
// Input: low = 1, high = 10, k = 1
// Output: 1
// Explanation: There is 1 beautiful integer in the given range: [10].
// - 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1.
// It can be shown that there is only 1 beautiful integer in the given range.
fmt.Println(numberOfBeautifulIntegers(1, 10, 1)) // 1
// Example 3:
// Input: low = 5, high = 5, k = 2
// Output: 0
// Explanation: There are 0 beautiful integers in the given range.
// - 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits.
fmt.Println(numberOfBeautifulIntegers(5, 5, 2)) // 0
fmt.Println(numberOfBeautifulIntegers(1, 1, 1)) // 0
fmt.Println(numberOfBeautifulIntegers(1, 1, 20)) // 0
fmt.Println(numberOfBeautifulIntegers(1_000_000_000, 1_000_000_000, 20)) // 0
fmt.Println(numberOfBeautifulIntegers(1_000_000_000, 1_000_000_000, 1)) // 0
fmt.Println(numberOfBeautifulIntegers(1, 1_000_000_000, 20)) // 1105750
fmt.Println(numberOfBeautifulIntegers(1, 1_000_000_000, 1)) // 24894045
}