forked from black-shadows/LeetCode-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfusing-number-ii.cpp
200 lines (188 loc) · 7.21 KB
/
confusing-number-ii.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Time: O(logn)
// Space: O(logn)
class Solution {
public:
int confusingNumberII(int n) {
return totalCount(n) - validCountInLessLength(n) - validCountInFullLength(n);
}
private:
int totalCount(int n) { // count all numbers in the pattern of [01689]{1,len(n)} in the range of [1, n]
const auto& s = to_string(n);
int total = 0;
int p = pow(lookup.size(), s.length() - 1);
for (int i = 0; i <= s.length(); ++i, p /= lookup.size()) {
if (i == s.length()) {
++total;
break;
}
int smaller = 0;
for (const auto& kvp : lookup) {
smaller += int(kvp.first < s[i]);
}
total += smaller * p;
if (!lookup.count(s[i])) {
break;
}
}
return total - 1; // exclude 0
}
int validCountInLessLength(int n) { // count unconfusing numbers in the pattern of [01689]{1,len(n)-1} in the range of [1, n]
const auto& s = to_string(n);
int valid = 0;
int total = centers.size();
for (int i = 1; i < s.length(); i += 2) { // count unconfusing numbers for each odd length less than s
if (i == 1) {
valid += accumulate(cbegin(centers), cend(centers), 0,
[](const auto& total, const auto& kvp) {
return total + (kvp.first != '0');
});
} else {
valid += total * (lookup.size() - 1);
total *= lookup.size();
}
}
total = 1;
for (int i = 2; i < s.length(); i += 2) { // count unconfusing numbers for each even length less than s
valid += total * (lookup.size() - 1);
total *= lookup.size();
}
return valid;
}
int validCountInFullLength(int n) { // count unconfusing numbers in the pattern of [01689]{len(n)} in the range of [1, n]
const auto& s = to_string(n);
const auto& half_s = s.substr(0, (s.length() + 1) / 2);
int total = 0;
const auto& choices = (s.length() % 2) ? centers : lookup;
int p = pow(lookup.size(), int(half_s.length()) - 2) * choices.size();
for (int i = 0; i < half_s.length(); ++i, p /= lookup.size()) {
if (i == half_s.length() - 1) {
for (const auto& kvp : choices) {
if (kvp.first == '0' && i == 0) {
continue;
}
total += int(kvp.first < half_s[i]);
}
if (!choices.count(half_s[i])) {
break;
}
string tmp(half_s);
for (int i = half_s.length() - 1 - (s.length() % 2); i >= 0; --i) {
tmp.push_back(lookup.at(half_s[i]));
}
if (stoull(tmp) <= n && n != 0) {
++total;
}
break;
}
int smaller = 0;
for (const auto& kvp : lookup) {
if (kvp.first == '0' && i == 0) {
continue;
}
smaller += int(kvp.first < half_s[i]);
}
total += smaller * p;
if (!lookup.count(half_s[i])) {
break;
}
}
return total;
}
const unordered_map<char, char> lookup = {{'0', '0'}, {'1', '1'}, {'6', '9'},
{'8', '8'}, {'9', '6'}};
const unordered_map<char, char> centers = {{'0', '0'}, {'1', '1'}, {'8', '8'}};
};
// Time: O(logn)
// Space: O(logn)
class Solution2 {
public:
int confusingNumberII(int n) {
return f(n) - f(0); // f(0) is always 0 and could be ignored
}
private:
int f(int n) { // count confusing numbers in the range of [0, n]
return totalCount(n) - validCountInLessLength(n) - validCountInFullLength(n);
}
int totalCount(int n) { // count all numbers in the pattern of [01689]{1,len(n)} in the range of [0, n]
const auto& s = to_string(n);
int total = 0;
int p = pow(lookup.size(), s.length() - 1);
for (int i = 0; i <= s.length(); ++i, p /= lookup.size()) {
if (i == s.length()) {
++total;
break;
}
int smaller = 0;
for (const auto& kvp : lookup) {
smaller += int(kvp.first < s[i]);
}
total += smaller * p;
if (!lookup.count(s[i])) {
break;
}
}
return total;
}
int validCountInLessLength(int n) { // count unconfusing numbers in the pattern of [01689]{1,len(n)-1} in the range of [0, n]
const auto& s = to_string(n);
int valid = 0;
int total = centers.size();
for (int i = 1; i < s.length(); i += 2) { // count unconfusing numbers for each odd length less than s
if (i == 1) {
valid += centers.size();
} else {
valid += total * (lookup.size() - 1);
total *= lookup.size();
}
}
total = 1;
for (int i = 2; i < s.length(); i += 2) { // count unconfusing numbers for each even length less than s
valid += total * (lookup.size() - 1);
total *= lookup.size();
}
return valid;
}
int validCountInFullLength(int n) { // count unconfusing numbers in the pattern of [01689]{len(n)} in the range of [0, n]
const auto& s = to_string(n);
const auto& half_s = s.substr(0, (s.length() + 1) / 2);
int total = 0;
const auto& choices = (s.length() % 2) ? centers : lookup;
int p = pow(lookup.size(), int(half_s.length()) - 2) * choices.size();
for (int i = 0; i < half_s.length(); ++i, p /= lookup.size()) {
if (i == half_s.length() - 1) {
for (const auto& kvp : choices) {
if (kvp.first == '0' && s.length() == 2) {
continue;
}
total += int(kvp.first < half_s[i]);
}
if (!choices.count(half_s[i])) {
break;
}
string tmp(half_s);
for (int i = half_s.length() - 1 - (s.length() % 2); i >= 0; --i) {
tmp.push_back(lookup.at(half_s[i]));
}
if (stoull(tmp) <= n) {
++total;
}
break;
}
int smaller = 0;
for (const auto& kvp : lookup) {
if (kvp.first == '0' && i == 0) {
continue;
}
smaller += int(kvp.first < half_s[i]);
}
total += smaller * p;
if (!lookup.count(half_s[i])) {
break;
}
}
return total;
}
const unordered_map<char, char> lookup = {{'0', '0'}, {'1', '1'}, {'6', '9'},
{'8', '8'}, {'9', '6'}};
const unordered_map<char, char> centers = {{'0', '0'}, {'1', '1'}, {'8', '8'}};
};