Input: A = [1,1,2,2,2,2], target = 5Output: 12Explanation:
A[i] = 1, A[j] = A[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Note that '0' is not included.)
Now, we write numbers using these digits, using each digit as many times as we want. For example, if D = {'1','3','5'}, we may write numbers such as '13', '551', '1351315'.
Return the number of positive integers that can be written (using the digits of D) that are less than or equal to N.
Example 1:
Input: D = ["1","3","5","7"], N = 100Output: 20Explanation:
The 20 numbers that can be written are:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
Example 2:
Input: D = ["1","4","9"], N = 1000000000Output: 29523Explanation:
We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers,
81 four digit numbers, 243 five digit numbers, 729 six digit numbers,
2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers.
In total, this is 29523 integers that can be written using the digits of D.
Note:
D is a subset of digits '1'-'9' in sorted order.
1 <= N <= 10^9
Solution -1: DFS (TLE)
Time complexity: O(|D|^log10(N))
Space complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Author: Huahua
classSolution{
public:
intatMostNGivenDigitSet(vector<string>& D, int N) {
int ans = 0;
dfs(D,0,N,ans);
returnans;
}
private:
voiddfs(constvector<string>& D, int cur, int N, int& ans) {
if (cur > N) return;
if(cur>0&& cur <= N) ++ans;
for(conststring& d : D)
dfs(D, cur * 10 + d[0] - '0', N, ans);
}
};
Solution 1: Math
Time complexity: O(log10(N))
Space complexity: O(1)
Suppose N has n digits.
less than n digits
We can use all the numbers from D to construct numbers of with length 1,2,…,n-1 which are guaranteed to be less than N.
e.g. n = 52125, D = [1, 2, 5]
format X: e.g. 1, 2, 5 counts = |D| ^ 1
format XX: e.g. 11,12,15,21,22,25,51,52,55, counts = |D|^2
format XXX: counts = |D|^3
format XXXX: counts = |D|^4
exact n digits
if all numbers in D != N[0], counts = |d < N[0] | d in D| * |D|^(n-1), and we are done.
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Could you solve it without converting the integer to a string?
Solution 1: Convert to string (cheating)
Time complexity: O(log10(x))
Space complexity: O(log10(x))
C++
1
2
3
4
5
6
7
8
// Author: Huahua
classSolution{
public:
boolisPalindrome(intx){
strings=to_string(x);
returns==string(rbegin(s),rend(s));
}
};
Solution 2: Digit by Digit
Every time we compare the first and last digits of x, if they are not the same, return false. Otherwise, remove first and last digit and continue this process.
How can we achieve that via int math?
e.g. x = 9999, t = pow((10, int)log10(x)) = 1000
first digit: x / t, last digit: x % 10
then x = (x – x / t * t) / 10 removes first and last digits.