Skip to content

Commit 2e7fec4

Browse files
authored
Merge pull request #198 from jeantimex/perfect-square
Perfect Square
2 parents 973b4ed + e08bd5b commit 2e7fec4

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ A collection of JavaScript problems and solutions for studying algorithms.
224224
- [Best Time to Buy and Sell Stock with Cooldown](src/dynamic-programming/best-time-to-buy-and-sell-stock-with-cooldown.js)
225225
- [Best Time to Buy and Sell Stock with Transaction Fee](src/dynamic-programming/best-time-to-buy-and-sell-stock-with-transaction-fee.js)
226226
- [Gas Station III](src/dynamic-programming/gas-station-iii.js)
227+
- [Perfect Squares](src/dynamic-programming/perfect-squares.js)
228+
- [Subset Sum Problem](src/dynamic-programming/subset-sum-problem.js)
227229

228230
### Greedy
229231

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Perfect Squares
3+
*
4+
* Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
5+
*
6+
* Example 1:
7+
*
8+
* Input: n = 12
9+
* Output: 3
10+
* Explanation: 12 = 4 + 4 + 4.
11+
* Example 2:
12+
*
13+
* Input: n = 13
14+
* Output: 2
15+
* Explanation: 13 = 4 + 9.
16+
*/
17+
18+
/**
19+
* @param {number} n
20+
* @return {number}
21+
*/
22+
const numSquares = n => {
23+
if (n <= 0) {
24+
return 0;
25+
}
26+
27+
const dp = Array(n + 1).fill(0);
28+
29+
for (let i = 1; i <= n; i++) {
30+
dp[i] = Number.MAX_SAFE_INTEGER;
31+
32+
for (let j = 1; j * j <= i; j++) {
33+
dp[i] = Math.min(dp[i], dp[i - j * j] + 1);
34+
}
35+
}
36+
37+
return dp[n];
38+
};
39+
40+
export { numSquares };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Subset Sum Problem
3+
*
4+
* Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum.
5+
*
6+
* Examples: set[] = {3, 34, 4, 12, 5, 2}, sum = 9
7+
* Output: True //There is a subset (4, 5) with sum 9.
8+
*/
9+
10+
/**
11+
* Recursion
12+
*
13+
* Returns true if there is a subset of set[] with sum equal to given sum
14+
*
15+
* @param {number[]} set
16+
* @param {number} n
17+
* @param {number} sum
18+
* @return {boolean}
19+
*/
20+
const isSubsetSumR = (set, n, sum) => {
21+
// Base Cases
22+
if (sum === 0) {
23+
return true;
24+
}
25+
26+
if (n === 0 && sum !== 0) {
27+
return false;
28+
}
29+
30+
// If last element is greater than
31+
// sum, then ignore it
32+
if (set[n - 1] > sum) {
33+
return isSubsetSumR(set, n - 1, sum);
34+
}
35+
36+
// else, check if sum can be obtained by any of the following
37+
// (a) including the last element
38+
// (b) excluding the last element
39+
return isSubsetSumR(set, n - 1, sum) || isSubsetSumR(set, n - 1, sum - set[n - 1]);
40+
};
41+
42+
/**
43+
* Dynamic Programming
44+
*
45+
* Returns true if there is a subset of set[] with sum equal to given sum
46+
*
47+
* @param {number[]} set
48+
* @param {number} n
49+
* @param {number} sum
50+
* @return {boolean}
51+
*/
52+
const isSubsetSum = (set, n, sum) => {
53+
// The value of subset[i][j] will be true if there is a subset of
54+
// set[0..j-1] with sum equal to i
55+
const dp = Array(sum + 1)
56+
.fill()
57+
.map(() => Array(n + 1));
58+
59+
// If sum is 0, then answer is true
60+
for (let i = 0; i <= n; i++) {
61+
dp[0][i] = true;
62+
}
63+
64+
// If sum is not 0 and set is empty,
65+
// then answer is false
66+
for (let i = 1; i <= sum; i++) {
67+
dp[i][0] = false;
68+
}
69+
70+
// Fill the subset table in botton up manner
71+
for (let i = 1; i <= sum; i++) {
72+
for (let j = 1; j <= n; j++) {
73+
dp[i][j] = dp[i][j - 1];
74+
75+
if (i >= set[j - 1]) {
76+
dp[i][j] = dp[i][j] || dp[i - set[j - 1]][j - 1];
77+
}
78+
}
79+
}
80+
81+
return dp[sum][n];
82+
};

0 commit comments

Comments
 (0)