LeetCode902. Numbers At Most N Given Digit Set

题目:

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 = 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.

Example 2:

Input: D = ["1","4","9"], N = 1000000000
Output: 29523
Explanation: 
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:

  1. D is a subset of digits '1'-'9' in sorted order.
  2. 1 <= N <= 10^9

题意理解:

给了你一个string数组D,和一个数字N。D中每个string包含的是一个数字字符。相当于就是D中包含了LD个一位数字,然后用这些数字进行组合,只要比N小就满足条件,问一共有多少种满足条件的组合可能。

DP思想:

对于D的组合,当组合的数字长度小于N的长度时,很好理解,就是D的size的i次幂相加,1 <= i < D;

当组合的数字长度等于N的长度时,情况就有点复杂了,但是我们可以通过DP的思想来理解。

即对于数字N从右往左扫描,如果对于一个扫描的点,再遍历D。

1.如果D中的一个数字小于N中的当前值,那么当前位置的组合数就加上D长度的一个次幂,具体多少次幂,取决于N中当前位置,相当于就是 1XXX, 2abc 组合数就是D长度的3次幂。

2.如果D中的一个数字等于N中的当前值,那么就加上前一个位置的组合数,相当于就是 1XXXX 和 1XXXX;组合数取决于后4位。

3.如果D中的一个数字比N中的当前值大,其实不用处理,因为我们设定的DP数组初始值都是0;

需要注意的就是DP数组要多开一个位置,存储最后一个值1,自己随便写一个就知道原因了。

代码:

class Solution {
public:
    int atMostNGivenDigitSet(vector<string>& D, int N) {
        string s = to_string(N);
        int LD = D.size();
        int LN = s.size();
        int ans = 0;
        int DP[LN+1];
        memset(DP, 0, sizeof(DP));
        DP[LN] = 1;
        
        
        for(int i = LN-1; i >= 0; i--)
        {
            int nt = s[i] - '0';
            for(int j = 0; j < LD; j++)
            {
                if(D[j][0] - '0' == nt)
                    DP[i] += DP[i+1];
                else if(D[j][0] - '0' < nt)
                    DP[i] += pow(LD, LN - i - 1);
            }
            
        }
        
        for(int j = 1; j < LN; j++)
        {
                
                DP[0] += pow(LD, j);
        }
        
        return DP[0];
    }
};

  

转载于:https://www.cnblogs.com/dybala21/p/9668676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值