Skip to content

Commit 287cfa5

Browse files
committed
update
1 parent efc749c commit 287cfa5

6 files changed

+135
-9
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export {};
2+
3+
// O(n) time, O(1) space
4+
function maxProfit(prices: number[]): number {
5+
const N = prices.length;
6+
7+
let hold = -prices[0];
8+
let release = Number.MIN_SAFE_INTEGER;
9+
let idle = 0;
10+
11+
if (N < 2) return 0;
12+
13+
for (let i = 1; i < N; i++) {
14+
const lastHold = hold;
15+
const lastRelease = release;
16+
const lastIdle = idle;
17+
hold = Math.max(lastHold, lastIdle - prices[i]);
18+
release = lastHold + prices[i];
19+
idle = Math.max(lastIdle, lastRelease);
20+
}
21+
22+
return Math.max(release, idle);
23+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export {};
2+
3+
/**
4+
For each day we have three different multi universe, which we
5+
1. just sell in this day
6+
2. just buy in this day
7+
3. just do noop this day
8+
9+
So how come we deal with each relation?
10+
11+
As the desc, we are
12+
1. not allowed to buy after we sell (cooldown)
13+
2. not allowed hold multiple shares
14+
15+
16+
So now we had three state:
17+
1. HOLD -> you had one share in your hand
18+
2. RELEASE -> you just sell that share (so I mean you need to at hold state in previous day)
19+
3. IDLE -> you do nothing, and you don't have the share in your hand.
20+
21+
The state machine be like
22+
23+
HOLD[i] = IDLE[i-1] - prices[i] vs HOLD[i-1]; // best buy point
24+
25+
RELEASE[i] = HOLD[i-1] + prices[i];
26+
27+
IDLE[i] = IDLE[i-1] vs RELEASE[i-1]
28+
29+
*/
30+
31+
// O(n) time, O(n) space, for O(1) space, check the `best-time-to-buy-and-sell-stock-with-cooldown-optimize`
32+
function maxProfit(prices: number[]): number {
33+
const N = prices.length;
34+
35+
const hold = Array.from({ length: N }, () => 0);
36+
const release = Array.from({ length: N }, () => 0);
37+
const idle = Array.from({ length: N }, () => 0);
38+
39+
if (N < 2) return 0;
40+
41+
hold[0] = -prices[0];
42+
release[0] = Number.MIN_SAFE_INTEGER;
43+
idle[0] = 0;
44+
45+
for (let i = 1; i < N; i++) {
46+
hold[i] = Math.max(hold[i - 1], idle[i - 1] - prices[i]);
47+
release[i] = hold[i - 1] + prices[i];
48+
idle[i] = Math.max(idle[i - 1], release[i - 1]);
49+
}
50+
51+
return Math.max(release[N - 1], idle[N - 1]);
52+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export {};
2+
function maxProfit(prices: number[], fee: number): number {
3+
const N = prices.length;
4+
5+
let hold = -prices[0];
6+
let release = Number.MIN_SAFE_INTEGER;
7+
let idle = 0;
8+
9+
for (let i = 1; i < N; i++) {
10+
const lastHold = hold,
11+
lastRelease = release,
12+
lastIdle = idle;
13+
14+
hold = Math.max(
15+
lastRelease - prices[i],
16+
lastIdle - prices[i],
17+
lastHold /*may not engage in multiple*/
18+
);
19+
release = lastHold + prices[i] - fee;
20+
idle = Math.max(lastRelease, lastIdle);
21+
}
22+
23+
return Math.max(release, idle);
24+
}

src/maximum-subarray-sum.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function maxSubArray(nums: number[]): number {
2+
const N = nums.length;
3+
let maxSum = 0;
4+
let ret = Number.MIN_SAFE_INTEGER;
5+
6+
for (let i = 0; i < N; i++) {
7+
if (maxSum < 0) maxSum = 0;
8+
maxSum += nums[i];
9+
ret = Math.max(ret, maxSum);
10+
}
11+
return ret;
12+
}

src/move-zeros.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
*/
44
function moveZeroes(nums: number[]): void {
55
const N = nums.length;
6-
let pos1 = 0;
7-
let pos0 = -1;
86

9-
while (pos1 < N) {
10-
if (nums[pos1] === 0) {
11-
pos0 = Math.max(pos0, pos1) + 1;
12-
while (pos0 < N && nums[pos0] === 0) pos0++;
13-
if (pos0 === N) break;
14-
[nums[pos1], nums[pos0]] = [nums[pos0], nums[pos1]];
7+
let ptr1 = 0,
8+
ptr2 = 0;
9+
while (true) {
10+
while (ptr2 < N && nums[ptr2] === 0) ptr2++;
11+
if (ptr2 === N) break;
12+
13+
if (ptr1 !== ptr2) {
14+
// prevent redundant swap
15+
[nums[ptr1], nums[ptr2]] = [nums[ptr2], nums[ptr1]]; // swap
1516
}
16-
pos1++;
17+
ptr1++;
18+
ptr2++;
1719
}
1820
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function twoSum(numbers: number[], target: number): number[] {
2+
const N = numbers.length;
3+
let lo = 0,
4+
hi = N - 1;
5+
6+
// exactly one solution
7+
while (numbers[lo] + numbers[hi] !== target) {
8+
if (numbers[lo] + numbers[hi] > target) hi--;
9+
else lo++;
10+
}
11+
12+
return [lo + 1, hi + 1];
13+
}

0 commit comments

Comments
 (0)