Skip to content

Commit cb9c0a7

Browse files
authored
2025년 15주차 문제 풀이 (#40)
## 문제 풀이 목록 - [LeetCode 416. Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum) ✨ - [LeetCode 3396. Minimum Number of Operations to Make Elements in Array Distinct](https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct) - [LeetCode 3375. Minimum Operations to Make Array Values Equal to K](https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k) - [LeetCode 2999. Count the Number of Powerful Integers](https://leetcode.com/problems/count-the-number-of-powerful-integers) ✨ - [LeetCode 2843. Count Symmetric Integers](https://leetcode.com/problems/count-symmetric-integers) - [LeetCode 3272. Find the Count of Good Integers](https://leetcode.com/problems/find-the-count-of-good-integers) :sparkles: - [LeetCode 1922. Count Good Numbers](https://leetcode.com/problems/count-good-numbers) ✨
1 parent 6f3a03a commit cb9c0a7

File tree

25 files changed

+2071
-1381
lines changed

25 files changed

+2071
-1381
lines changed

solution/README.md

Lines changed: 1340 additions & 1304 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { canPartition } from './0416';
2+
3+
describe('LeetCode 0416', () => {
4+
test('Example 1', () => {
5+
expect(canPartition([1, 5, 11, 5])).toBe(true);
6+
});
7+
test('Example 2', () => {
8+
expect(canPartition([1, 2, 3, 5])).toBe(false);
9+
});
10+
});

solution/src/leetcode/0416/0416.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 416. Partition Equal Subset Sum
3+
* https://leetcode.com/problems/partition-equal-subset-sum
4+
*/
5+
export function canPartition(nums: number[]): boolean {
6+
const totalSum = nums.reduce((acc, num) => acc + num, 0);
7+
if (totalSum % 2 !== 0) {
8+
return false;
9+
}
10+
11+
const targetSum = totalSum / 2;
12+
const dp = new Array<boolean>(targetSum + 1).fill(false);
13+
dp[0] = true;
14+
for (const num of nums) {
15+
for (let sum = targetSum; sum >= num; sum--) {
16+
dp[sum] = dp[sum] || dp[sum - num];
17+
if (dp[targetSum]) {
18+
return true;
19+
}
20+
}
21+
}
22+
return dp[targetSum];
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "416",
3+
"url": "https://leetcode.com/problems/partition-equal-subset-sum",
4+
"title": "Partition Equal Subset Sum",
5+
"category": "Algorithms",
6+
"difficulty": "Medium",
7+
"tags": ["Array", "Dynamic Programming"]
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { countGoodNumbers } from './1922';
2+
3+
describe('LeetCode 1922', () => {
4+
test('Example 1', () => {
5+
expect(countGoodNumbers(1)).toBe(5);
6+
});
7+
test('Example 2', () => {
8+
expect(countGoodNumbers(4)).toBe(400);
9+
});
10+
test('Example 3', () => {
11+
expect(countGoodNumbers(50)).toBe(564908303);
12+
});
13+
});

solution/src/leetcode/1922/1922.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 1922. Count Good Numbers
3+
* https://leetcode.com/problems/count-good-numbers
4+
*/
5+
export function countGoodNumbers(n: number): number {
6+
const MOD = BigInt(10 ** 9 + 7);
7+
8+
function pow(x: bigint, y: bigint): bigint {
9+
let result = 1n;
10+
let [base, exponent] = [x, y];
11+
while (exponent > 0) {
12+
if (exponent % 2n === 1n) {
13+
result = (result * base) % MOD;
14+
}
15+
base = (base * base) % MOD;
16+
exponent = exponent / 2n;
17+
}
18+
return result;
19+
}
20+
21+
const even = BigInt(Math.floor((n + 1) / 2));
22+
const odd = BigInt(Math.floor(n / 2));
23+
return Number((pow(5n, even) * pow(4n, odd)) % MOD);
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "1922",
3+
"url": "https://leetcode.com/problems/count-good-numbers",
4+
"title": "Count Good Numbers",
5+
"category": "Algorithms",
6+
"difficulty": "Medium",
7+
"tags": ["Math", "Recursion"]
8+
}

solution/src/leetcode/2843/2843.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@
33
* https://leetcode.com/problems/count-symmetric-integers
44
*/
55
export function countSymmetricIntegers(low: number, high: number): number {
6-
function isSymmetric(num: number) {
7-
const str = num.toString();
8-
const n = str.length;
9-
if (n % 2 !== 0) {
10-
return false;
11-
}
12-
let [leftSum, rightSum] = [0, 0];
13-
for (let i = 0; i < Math.floor(n / 2); i++) {
14-
leftSum += parseInt(str[i]);
15-
rightSum += parseInt(str[n - i - 1]);
16-
}
17-
return leftSum === rightSum;
18-
}
19-
206
let answer = 0;
217
for (let num = low; num <= high; num++) {
22-
if (isSymmetric(num)) {
8+
if (isSymmetricInteger(num)) {
239
answer += 1;
2410
}
2511
}
2612
return answer;
2713
}
14+
15+
function isSymmetricInteger(num: number): boolean {
16+
const s = num.toString();
17+
if (s.length % 2 !== 0) {
18+
return false;
19+
}
20+
21+
const n = s.length / 2;
22+
let [left, right] = [0, 0];
23+
for (let i = 0; i < n; i++) {
24+
left += parseInt(s[i]);
25+
right += parseInt(s[s.length - i - 1]);
26+
}
27+
return left === right;
28+
}

solution/src/leetcode/2843/meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "2843",
33
"url": "https://leetcode.com/problems/count-symmetric-integers",
4-
"title": " Count Symmetric Integers",
4+
"title": "Count Symmetric Integers",
55
"category": "Algorithms",
66
"difficulty": "Easy",
77
"tags": ["Math", "Enumeration"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { numberOfPowerfulInt } from './2999';
2+
3+
describe('LeetCode 2999', () => {
4+
test('Example 1', () => {
5+
expect(numberOfPowerfulInt(1, 6000, 4, '124')).toBe(5);
6+
});
7+
test('Example 2', () => {
8+
expect(numberOfPowerfulInt(15, 215, 6, '10')).toBe(2);
9+
});
10+
test('Example 3', () => {
11+
expect(numberOfPowerfulInt(1000, 2000, 4, '3000')).toBe(0);
12+
});
13+
});

0 commit comments

Comments
 (0)