-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmp.cpp
More file actions
42 lines (36 loc) · 905 Bytes
/
Copy pathtmp.cpp
File metadata and controls
42 lines (36 loc) · 905 Bytes
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
//
// Created by houruizhe on 2020/10/31.
//
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
class Solution {
public:
/**
* @param N: a positive integer N
* @return: return a maximum integer less than N, each digit of which must be monotonically increasing.
*/
long long getIncreasingNumber(long long N) {
// write your code here
if (N >= 123456789) return 123456789L;
LL res = 0;
int pre = 10, digit = 1;
while (N != 0) {
LL mod = N % 10;
LL small = mod < pre - 1 ? mod : pre - 1;
res += digit * small;
digit *= 10;
pre = small;
N /= 10;
}
return res;
}
};
int main() {
auto *sol = new Solution();
auto res = sol->getIncreasingNumber(998);
cout << res << endl;
return 0;
}