-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumbersAtMostNGivenDigit.java
50 lines (38 loc) · 1.38 KB
/
NumbersAtMostNGivenDigit.java
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
/**
*
* Given an array of digits, you can write numbers using each digits[i] as many times as we want. For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'.
*
* Return the number of positive integers that can be generated that are less than or equal to a given integer n.
Input: digits = ["1","3","5","7"], n = 100
Output: 20
Explanation:
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.
*/
public class NumbersAtMostNGivenDigit {
public static int atMostNGivenDigitSet(String[] D, int N) {
String S = String.valueOf(N);
int K = S.length();
int[] dp = new int[K+1];
dp[K] = 1;
for (int i = K-1; i >= 0; --i) {
// compute dp[i]
int Si = S.charAt(i) - '0';
for (String d: D) {
if (Integer.valueOf(d) < Si)
dp[i] += Math.pow(D.length, K-i-1);
else if (Integer.valueOf(d) == Si)
dp[i] += dp[i+1];
}
}
for (int i = 1; i < K; ++i)
dp[0] += Math.pow(D.length, i);
return dp[0];
}
public static void main(String[] args) {
String[] digits = {"1","3","5","7"};
int n=100;
int res = atMostNGivenDigitSet(digits, n);
System.out.println(res);
}
}