902. Numbers At Most N Given Digit Set Hard

@problem@discussion
#Array#Math#String#Binary Search#Dynamic Programming



1/**
2 * [902] Numbers At Most N Given Digit Set
3 *
4 * Given an array of digits which is sorted in non-decreasing order. 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'.
5 * Return the number of positive integers that can be generated that are less than or equal to a given integer n.
6 *  
7 * Example 1:
8 * 
9 * Input: digits = ["1","3","5","7"], n = 100
10 * Output: 20
11 * Explanation: 
12 * The 20 numbers that can be written are:
13 * 1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
14 * 
15 * Example 2:
16 * 
17 * Input: digits = ["1","4","9"], n = 1000000000
18 * Output: 29523
19 * Explanation: 
20 * We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers,
21 * 81 four digit numbers, 243 five digit numbers, 729 six digit numbers,
22 * 2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers.
23 * In total, this is 29523 integers that can be written using the digits array.
24 * 
25 * Example 3:
26 * 
27 * Input: digits = ["7"], n = 8
28 * Output: 1
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	1 <= digits.length <= 9
34 * 	digits[i].length == 1
35 * 	digits[i] is a digit from '1' to '9'.
36 * 	All the values in digits are unique.
37 * 	digits is sorted in non-decreasing order.
38 * 	1 <= n <= 10^9
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/numbers-at-most-n-given-digit-set/
44// discuss: https://leetcode.com/problems/numbers-at-most-n-given-digit-set/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn at_most_n_given_digit_set(digits: Vec<String>, n: i32) -> i32 {
50        0
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_902() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.