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:
D
is a subset of digits'1'-'9'
in sorted order.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];
- }
- };
LeetCode902. Numbers At Most N Given Digit Set的更多相关文章
- [Swift]LeetCode902. 最大为 N 的数字组合 | 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'}. (Not ...
- 902. 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'}. (Not ...
- [LeetCode] 902. Numbers At Most N Given Digit Set 最大为 N 的数字组合
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Not ...
- LeetCode 902. Numbers At Most N Given Digit Set
应该是常数 N的位数时间级别 我的这个方法超时很严重...但是特此记录 费劲巴拉写的... 超时: int atMostNGivenDigitSet(char** D, int DSize, int ...
- 由最多N个给定数字集组成的数字 Numbers At Most N Given Digit Set
2019-10-14 22:21:29 问题描述: 问题求解: 暴力求解必然会超时,那么就需要考虑数学的方法来降低时间复杂度了. public int atMostNGivenDigitSet(Str ...
- F. Igor and Interesting Numbers
http://codeforces.com/contest/747/problem/F cf #387 div2 problem f 非常好的一道题.看完题,然后就不知道怎么做,感觉是dp,但是不知道 ...
- About Intel® Processor Numbers
http://www.intel.com/content/www/us/en/processors/processor-numbers.html About Intel® Processor Numb ...
- ural 1289. One Way Ticket
1289. One Way Ticket Time limit: 1.0 secondMemory limit: 64 MB A crowed of volunteers dressed in the ...
- CSUFT 1003 All Your Base
1003: All Your Base Time Limit: 1 Sec Memory Limit: 128 MB Submit: 4 Solved: 2 Description ...
随机推荐
- C++中内存区域的划分
栈存储区 那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参数等. 堆存储区(自由存储区) 那些由new或者malloc分配的内存块,他们的释放编译器 ...
- mongo状态查看方法
列举一些常用的mongodb状态查看方法. 1.mongostat 是mongdb自带的状态检测工具, inserts/s 每秒插入次数 query/s 每秒查询次数 update/s 每秒更新次数 ...
- EZOJ #202
传送门 分析 我们知道选一个点的代价就是他所有出边边权的异或和 由于一条边如果两个端点均选边权会异或两次变回0,所以不必担心重复的情况 于是直接跑线性基即可 代码 #include<bits/s ...
- 黑盒测试实践--Day4 11.28
黑盒测试实践--Day4 11.28 今天完成任务情况: 分块明确自己部分的工作,并做前期准备 完成被测系统--学生管理系统的需求规格说明书 完成Mook上高级测试课程的第六章在线学习,观看自动化测试 ...
- URAL 1133 Fibonacci Sequence(数论)
题目链接 题意 :给你第 i 项的值fi,第 j 项的值是 fj 让你求第n项的值,这个数列满足斐波那契的性质,每一项的值是前两项的值得和. 思路 :知道了第 i 项第j项,而且还知道了每个数的范围, ...
- 通达OA整合教程
资源下载地址: 通达OA 2015:http://pan.baidu.com/s/1qYMxsZU mysql下载:http://pan.baidu.com/s/1c2oVI5y 整合文件:htt ...
- 解决jquery操作checkbox火狐下第二次无法勾选问题
最近在学习jQuery(版本jquery-1.9.1.js),要求用jQuery实现全选/全不选.反选,在IE(IE8)中没有问题,但在火狐浏览器中调试的时候出现了一些小问题,达不到效果. html代 ...
- mysql数据库学习小结
数据库的学习可以从以下几个层次了解掌握,这样思路清晰后后面不管怎么变化都可以随时应变: 1.mysql基础知识 2.操作数据库的方法,增 删 改 查 3.jdbc连接数据库,工作原理 难点重点,如:P ...
- HTML <area> 标签区域map标签
1.距形:(左上角顶点坐标为(x1,y1),右下角顶点坐标为(x2,y2)) <area shape="rect" coords="x1,y1,x2,y2" ...
- Boosting and Its Application in LTR
1 Boosting概述 2 Classification and Regression Tree 3 AdaBoost 3.1 算法框架 3.2 原理:Additive Modeling 4 Gra ...