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'}. (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 toN.
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:
Dis a subset of digits'1'-'9'in sorted order.1 <= N <= 10^9
Approach #1: Math. [C++]
class Solution {
public:
int atMostNGivenDigitSet(vector<string>& D, int N) {
string s = to_string(N);
int n = s.length();
int ans = 0;
for (int i = 1; i < n; ++i)
ans += pow(D.size(), i);
for (int i = 0; i < n; ++i) {
bool prefix = false;
for (string d : D) {
if (d[0] < s[i]) {
ans += pow(D.size(), n-i-1);
} else if (d[0] == s[i]) {
prefix = true;
break;
}
}
if (!prefix) return ans;
}
return ans + 1;
}
};
Reference:
https://zxi.mytechroad.com/blog/math/leetcode-902-numbers-at-most-n-given-digit-set/
902. Numbers At Most N Given Digit Set的更多相关文章
- [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 ...
- [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 ...
- 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'}. ...
- 由最多N个给定数字集组成的数字 Numbers At Most N Given Digit Set
2019-10-14 22:21:29 问题描述: 问题求解: 暴力求解必然会超时,那么就需要考虑数学的方法来降低时间复杂度了. public int atMostNGivenDigitSet(Str ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- 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 ...
随机推荐
- 分享chrome清空缓存开发小技巧
在打开开发者工具的前提下,左键长按刷新页面小图标(左上角,地址栏左侧),可以调出清空缓存下拉选择项.
- NOIP 2016 游记
- FEATURE_MCT_READERDIRECT问题
刚才查了一下,好像与一个叫CCID的驱动有关.你把FEATURE_MCT_READEDIRECT定义成0x08,再make一下试试.
- 使用delphi 开发多层应用(二十四)KbmMW 的消息方式和创建WIB节点
KbmMW 中支持基于UDP的消息广播,也支持TCP/IP hub/spoke 方式,还有 基于UDP或者TCP/IP 的点对点的消息传输. 1.基于UDP的消息广播
- 2018.09.15 hdu1599find the mincost route(floyd求最小环)
传送门 floyd求最小环的板子题目. 就是枚举两个相邻的点求最小环就行了. 代码: #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f ...
- 2018.07.10NOIP模拟 Knapsack(单调队列优化dp)
Knapsack 题目背景 SOURCE:NOIP2016-RZZ-4 T2 题目描述 有 n 个物品,第 i 个物品的重量为 ai . 设 f(i,j,k,l,m) 为满足以下约束的物品集合数量: ...
- [GO]关于go的waitgroup
watigroup是用来控制一组goroutine的,用来等待一组goroutine结束 比如关于kafka的消费者代码除了生硬的让程序等待一个小时,也可以这样写 package main impor ...
- trsd_extract_EDSD_new
# -*- coding:utf-8 -*- import re ''' 适应新版本 ''' year='17A'#用户自定义 ss='./data/'#根目录 filename = ss+'EDSD ...
- (KMP 模板)Number Sequence -- Hdu -- 1711
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...
- (最短路 spfa)Wormholes -- poj -- 3259
http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions ...