Skip to content

Commit 7c50bc1

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 39.90 MB (Top 52.99%)
1 parent 9804c29 commit 7c50bc1

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 39.90 MB (Top 52.99%)
2+
13
class Solution {
2-
public int countNumbersWithUniqueDigits(int n) {
3-
// Logic -->
4-
/*
5-
if n == 0 --> the number is upto 10^0 --> 1 and 1 possibilites could be there
6-
if n == 1 --> the range is upto 10^1 --> 10(10 unique combinations from 1 to 10 can be there)
7-
so the bases cases are --> n == 0 and n == 1
4+
public int countNumbersWithUniqueDigits(int n)
5+
{
6+
/*
7+
9 * 9 + 10 for n = 2
8+
9 * 9 * 8 + 10 for n = 3
9+
9 * 9 * 8 * 7 + 10 for n = 4
10+
9 * 9 * 8 * 7 * 6 + 10 for n = 5
11+
*/
12+
if(n == 0)
13+
return 1;
814

15+
if(n == 1)
16+
return 10;
917

10-
Suppose the case of n = 3
11-
The upper limit is 10^3 --> 1000. So, _ _ _ be filled in following ways -->
12-
_ (9 choices except 0) _ (9 choices except the one filled in previous blank but 0 can be included) _ (8 choices). So for 3 digit, 9*9*8 + 9*9 choices for 2 digits+9(for 1 digit) + 1(0 digits)...
18+
int product =9;
19+
int result = 10;
1320

14-
*/
15-
int ans = 10;
16-
if(n == 0){
17-
return 1;
18-
}
19-
if(n == 1){
20-
return ans;
21+
for(int i=2; i<=n; i++)
22+
{
23+
product = product * (11-i);
24+
result += product;
2125
}
22-
int counter = 9;
23-
int choiceFiller = 9;
24-
while(n --> 1 && choiceFiller>0){
25-
counter *= choiceFiller--;
26-
ans += counter;
27-
}
28-
return ans;
26+
27+
return result;
2928
}
3029
}

0 commit comments

Comments
 (0)