Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2376 (#3535)
Browse files Browse the repository at this point in the history
No.2376.Count Special Integers
  • Loading branch information
yanglbme authored Sep 19, 2024
1 parent de4ed82 commit 767a71d
Show file tree
Hide file tree
Showing 11 changed files with 408 additions and 824 deletions.
421 changes: 136 additions & 285 deletions solution/2300-2399/2376.Count Special Integers/README.md

Large diffs are not rendered by default.

402 changes: 139 additions & 263 deletions solution/2300-2399/2376.Count Special Integers/README_EN.md

Large diffs are not rendered by default.

56 changes: 27 additions & 29 deletions solution/2300-2399/2376.Count Special Integers/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
class Solution {
public:
int countSpecialNumbers(int n) {
int ans = 0;
vector<int> digits;
while (n) {
digits.push_back(n % 10);
n /= 10;
}
int m = digits.size();
vector<bool> vis(10);
for (int i = 1; i < m; ++i) {
ans += 9 * A(9, i - 1);
}
for (int i = m - 1; ~i; --i) {
int v = digits[i];
for (int j = i == m - 1 ? 1 : 0; j < v; ++j) {
if (!vis[j]) {
ans += A(10 - (m - i), i);
}
string s = to_string(n);
int m = s.size();
int f[m][1 << 10];
memset(f, -1, sizeof(f));
auto dfs = [&](auto&& dfs, int i, int mask, bool lead, bool limit) -> int {
if (i >= m) {
return lead ^ 1;
}
if (vis[v]) {
break;
if (!limit && !lead && f[i][mask] != -1) {
return f[i][mask];
}
vis[v] = true;
if (i == 0) {
++ans;
int up = limit ? s[i] - '0' : 9;
int ans = 0;
for (int j = 0; j <= up; ++j) {
if (mask >> j & 1) {
continue;
}
if (lead && j == 0) {
ans += dfs(dfs, i + 1, mask, true, limit && j == up);
} else {
ans += dfs(dfs, i + 1, mask | (1 << j), false, limit && j == up);
}
}
}
return ans;
}

int A(int m, int n) {
return n == 0 ? 1 : A(m, n - 1) * (m - n + 1);
if (!limit && !lead) {
f[i][mask] = ans;
}
return ans;
};
return dfs(dfs, 0, 0, true, true);
}
};
};
66 changes: 34 additions & 32 deletions solution/2300-2399/2376.Count Special Integers/Solution.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
func countSpecialNumbers(n int) int {
digits := []int{}
for n != 0 {
digits = append(digits, n%10)
n /= 10
}
m := len(digits)
vis := make([]bool, 10)
ans := 0
for i := 1; i < m; i++ {
ans += 9 * A(9, i-1)
}
for i := m - 1; i >= 0; i-- {
v := digits[i]
j := 0
if i == m-1 {
j = 1
s := strconv.Itoa(n)
m := len(s)
f := make([][1 << 10]int, m+1)
for i := range f {
for j := range f[i] {
f[i][j] = -1
}
for ; j < v; j++ {
if !vis[j] {
ans += A(10-(m-i), i)
}
var dfs func(int, int, bool, bool) int
dfs = func(i, mask int, lead, limit bool) int {
if i >= m {
if lead {
return 0
}
return 1
}
if !limit && !lead && f[i][mask] != -1 {
return f[i][mask]
}
if vis[v] {
break
up := 9
if limit {
up = int(s[i] - '0')
}
vis[v] = true
if i == 0 {
ans++
ans := 0
for j := 0; j <= up; j++ {
if mask>>j&1 == 1 {
continue
}
if lead && j == 0 {
ans += dfs(i+1, mask, true, limit && j == up)
} else {
ans += dfs(i+1, mask|1<<j, false, limit && j == up)
}
}
if !limit && !lead {
f[i][mask] = ans
}
return ans
}
return ans
return dfs(0, 0, true, true)
}

func A(m, n int) int {
if n == 0 {
return 1
}
return A(m, n-1) * (m - n + 1)
}
53 changes: 26 additions & 27 deletions solution/2300-2399/2376.Count Special Integers/Solution.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
class Solution {
private char[] s;
private Integer[][] f;

public int countSpecialNumbers(int n) {
List<Integer> digits = new ArrayList<>();
while (n != 0) {
digits.add(n % 10);
n /= 10;
s = String.valueOf(n).toCharArray();
f = new Integer[s.length][1 << 10];
return dfs(0, 0, true, true);
}

private int dfs(int i, int mask, boolean lead, boolean limit) {
if (i >= s.length) {
return lead ? 0 : 1;
}
int m = digits.size();
int ans = 0;
for (int i = 1; i < m; ++i) {
ans += 9 * A(9, i - 1);
if (!limit && !lead && f[i][mask] != null) {
return f[i][mask];
}
boolean[] vis = new boolean[10];
for (int i = m - 1; i >= 0; --i) {
int v = digits.get(i);
for (int j = i == m - 1 ? 1 : 0; j < v; ++j) {
if (vis[j]) {
continue;
}
ans += A(10 - (m - i), i);
}
if (vis[v]) {
break;
int up = limit ? s[i] - '0' : 9;
int ans = 0;
for (int j = 0; j <= up; ++j) {
if ((mask >> j & 1) == 1) {
continue;
}
vis[v] = true;
if (i == 0) {
++ans;
if (lead && j == 0) {
ans += dfs(i + 1, mask, true, limit && j == up);
} else {
ans += dfs(i + 1, mask | (1 << j), false, limit && j == up);
}
}
if (!limit && !lead) {
f[i][mask] = ans;
}
return ans;
}

private int A(int m, int n) {
return n == 0 ? 1 : A(m, n - 1) * (m - n + 1);
}
}
}
37 changes: 16 additions & 21 deletions solution/2300-2399/2376.Count Special Integers/Solution.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
class Solution:
def countSpecialNumbers(self, n: int) -> int:
def A(m, n):
return 1 if n == 0 else A(m, n - 1) * (m - n + 1)
@cache
def dfs(i: int, mask: int, lead: bool, limit: bool) -> int:
if i >= len(s):
return int(lead ^ 1)
up = int(s[i]) if limit else 9
ans = 0
for j in range(up + 1):
if mask >> j & 1:
continue
if lead and j == 0:
ans += dfs(i + 1, mask, True, limit and j == up)
else:
ans += dfs(i + 1, mask | 1 << j, False, limit and j == up)
return ans

vis = [False] * 10
ans = 0
digits = [int(c) for c in str(n)[::-1]]
m = len(digits)
for i in range(1, m):
ans += 9 * A(9, i - 1)
for i in range(m - 1, -1, -1):
v = digits[i]
j = 1 if i == m - 1 else 0
while j < v:
if not vis[j]:
ans += A(10 - (m - i), i)
j += 1
if vis[v]:
break
vis[v] = True
if i == 0:
ans += 1
return ans
s = str(n)
return dfs(0, 0, True, True)
30 changes: 30 additions & 0 deletions solution/2300-2399/2376.Count Special Integers/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function countSpecialNumbers(n: number): number {
const s = n.toString();
const m = s.length;
const f: number[][] = Array.from({ length: m }, () => Array(1 << 10).fill(-1));
const dfs = (i: number, mask: number, lead: boolean, limit: boolean): number => {
if (i >= m) {
return lead ? 0 : 1;
}
if (!limit && !lead && f[i][mask] !== -1) {
return f[i][mask];
}
const up = limit ? +s[i] : 9;
let ans = 0;
for (let j = 0; j <= up; ++j) {
if ((mask >> j) & 1) {
continue;
}
if (lead && j === 0) {
ans += dfs(i + 1, mask, true, limit && j === up);
} else {
ans += dfs(i + 1, mask | (1 << j), false, limit && j === up);
}
}
if (!limit && !lead) {
f[i][mask] = ans;
}
return ans;
};
return dfs(0, 0, true, true);
}
42 changes: 0 additions & 42 deletions solution/2300-2399/2376.Count Special Integers/Solution2.cpp

This file was deleted.

53 changes: 0 additions & 53 deletions solution/2300-2399/2376.Count Special Integers/Solution2.go

This file was deleted.

Loading

0 comments on commit 767a71d

Please sign in to comment.