Skip to content

Commit 6d1567a

Browse files
committed
add DP questoins
1 parent 1fe1766 commit 6d1567a

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* lc338 counting bits (https://leetcode.com/problems/counting-bits/)
3+
* Given a non negative integer number num.
4+
* For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation
5+
* and return them as an array.
6+
* @param {number} num
7+
* @return {number[]}
8+
*/
9+
var countBits = function (num) {
10+
const result = [];
11+
result[0] = 0;
12+
let j = 1;
13+
// prefix, DP(x + b) = DP(x) + 1, b = 2^m > x
14+
while (j <= num) {
15+
let i = 0;
16+
while (i < j && i + j <= num) {
17+
result[i + j] = result[i] + 1;
18+
i += 1;
19+
}
20+
21+
j *= 2;
22+
}
23+
24+
return result;
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Given an unsorted array of integers, find the length of longest increasing subsequence.
3+
* lc300(https://leetcode.com/problems/longest-increasing-subsequence/)
4+
* Input: [10,9,2,5,3,7,101,18]
5+
* Output: 4
6+
* @param {number[]} nums
7+
* @return {number}
8+
*/
9+
var lengthOfLIS = function (nums) {
10+
let sub = [];
11+
let result = 0;
12+
13+
for (let num of nums) {
14+
let i = 0, j = result;
15+
while (i !== j) {
16+
const mid = Math.floor((i + j) / 2);
17+
if (sub[mid] < num) {
18+
i = mid + 1;
19+
} else {
20+
j = mid;
21+
}
22+
}
23+
sub[i] = num;
24+
if (i === result) result++;
25+
}
26+
return result
27+
};
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// lc1140 stone games II (https://leetcode.com/problems/stone-game-ii/)
2+
// Alex and Lee continue their games with piles of stones.There are a number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].The objective of the game is to end with the most stones.
3+
// Alex and Lee take turns, with Alex starting first.Initially, M = 1.
4+
// On each player's turn, that player can take all the stones in the first X remaining piles, where 1 <= X <= 2M. Then, we set M = max(M, X).
5+
// The game continues until all the stones have been taken.
6+
// Assuming Alex and Lee play optimally, return the maximum number of stones Alex can get.
7+
8+
/**
9+
* Basic idea: suffix, store M
10+
* @param {number[]} piles
11+
* @return {number}
12+
*/
13+
const dp = (piles, i, M, memo) => {
14+
15+
if (memo[i] === undefined) {
16+
memo[i] = new Map();
17+
}
18+
19+
if (memo[i].has(M)) {
20+
return memo[i].get(M);
21+
}
22+
23+
let sum = 0;
24+
for (let n = i; n < piles.length; n++) {
25+
sum += piles[n];
26+
}
27+
28+
if (piles.length - i <= 2 * M) {
29+
return sum;
30+
}
31+
32+
let min = Number.MAX_VALUE;
33+
for (let x = 1; x <= 2 * M; x++) {
34+
min = Math.min(min, dp(piles, i + x, Math.max(x, M), memo));
35+
}
36+
37+
memo[i].set(M, sum - min);
38+
39+
return sum - min;
40+
};
41+
42+
var stoneGameII = function (piles) {
43+
return dp(piles, 0, 1, []);
44+
};

0 commit comments

Comments
 (0)