From 747445d292ee84d5044ea7d65dba40c1802f323a Mon Sep 17 00:00:00 2001 From: SinnoLn Date: Sun, 29 Nov 2026 20:28:12 +0900 Subject: [PATCH] 3272. Find the Count of Good Integers --- ...272. Find the Count of Good Integers.java" | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 "leetcode3/\354\235\264\354\247\204\355\235\254/3272. Find the Count of Good Integers.java" diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/3272. Find the Count of Good Integers.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/3272. Find the Count of Good Integers.java" new file mode 100644 index 00000000..09e65559 --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/3272. Find the Count of Good Integers.java" @@ -0,0 +1,84 @@ +/* + +1. 아이디어 : n자리 숫자의 펠린드롬을 구한다. 이때 조건은 아래와 같음 + 1. k로 나누어떨어질 것 + 2. 재정렬을 했을때 k로 나누어떨어져도 될 것 + + 중복순열로 짝수는 n/2, 홀수는 n/2+1 자릿수를 만든다 (펠린드롬이므로 나머지 절반은 붙이기) + 조합된 숫자로 펠린드롬을 만들고 k로 나누어떨어진다면 set에 저장한다 + + 마지막에 저장된 숫자를 꺼내어 계산한다 이때 펙토리얼을 미리 구해두면 계산이 줄어듬 + +2. 시간복잡도 : O(10^⌈n/2⌉*n) + +3. 자료구조/알고리즘 : 해쉬맵, 중복순열(백트랙킹), 팰린드롬 + + */ + +class Solution { + private Set set = new HashSet<>(); + private long[] fact; + private long ans; + + public long countGoodIntegers(int n, int k) { + int limit = (n%2==0) ? n/2 : n/2+1; + fact = new long[11]; + + fact[0] = 1; + for (int i=1; i<=10; i++) { + fact[i] = fact[i-1] * i; + } + + makePermutation(0, n, k, 0, limit); + + for(String sn : set) { + int[] cnt = new int[10]; + for(int i=0; i= '0' && c <= '9') { + cnt[c-'0']++; + } + } + + long ways = (n - cnt[0]) * fact[n - 1]; + + for (int i = 0; i < 10; i++) { + ways /= fact[cnt[i]]; + } + ans += ways; + } + return ans; + } + + private void makePermutation(int depth, int n, int k, int num, int limit) { + if(limit == depth) { + long palindrom = num; + + if(n%2==1) num/=10; + + while(num>0) { + palindrom = palindrom*10+num%10; + num/=10; + } + + // k로 나누어 떨어진다면 저장 + if(palindrom%k == 0) { + int[] nums = new int[n]; + int idx = 0; + while(palindrom > 0) { + nums[idx++] = (int)(palindrom%10); + palindrom/=10; + } + Arrays.sort(nums); + + set.add(Arrays.toString(nums)); + } + return; + } + + for(int i=0; i<10; i++) { + if(depth == 0 && i == 0) continue; + makePermutation(depth+1, n, k, num*10+i, limit); + } + } +} \ No newline at end of file