Skip to content

Commit a5f89b7

Browse files
committed
❌ [714] a1
1 parent 957ce48 commit a5f89b7

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

714/my_solution.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} prices
3+
* @param {number} fee
4+
* @return {number}
5+
*/
6+
const maxProfit = (prices, fee) => {
7+
let l = 0, r = 0,
8+
cost = 0, prevSm = Infinity;
9+
10+
console.log(prices)
11+
while (r < prices.length) {
12+
console.log(`> l:${l}, r:${r}, prices[l]:${prices[l]}, prices[r]:${prices[r]}, prevSm:${prevSm}`)
13+
if (prices[r + 1] === undefined || prices[r] > prices[r + 1]) {
14+
console.log(`1. ${((prices[r] - prevSm - fee))}`)
15+
console.log(`2. ${((prices[r] - prices[l] - fee) + cost)}`)
16+
cost = Math.max(cost, ((prices[r] - prevSm - fee)), ((prices[r] - prices[l] - fee) + cost));
17+
console.log("cost")
18+
console.log(cost)
19+
l = r + 1;
20+
}
21+
22+
r++;
23+
prevSm = Math.min(prices[l], prevSm);
24+
}
25+
26+
return cost;
27+
};
28+
29+
let x = 0;
30+
// x = maxProfit([1, 3, 2, 8, 4, 9], 2) // 8
31+
x = maxProfit([1, 3, 7, 5, 10, 3], 3) // 6
32+
console.log("Result")
33+
console.log(x)

714/solution.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} prices
3+
* @param {number} fee
4+
* @return {number}
5+
*/
6+
const maxProfit = (prices, fee) => {
7+
let l = 0, r = 0, cost = 0, prevSm = Infinity;
8+
9+
while (r < prices.length) {
10+
if (prices[r + 1] === undefined || prices[r] > prices[r + 1]) {
11+
cost = Math.max(cost, ((prices[r] - prevSm - fee)), ((prices[r] - prices[l] - fee) + cost));
12+
l = r + 1;
13+
}
14+
15+
r++;
16+
prevSm = Math.min(prices[l], prevSm);
17+
}
18+
19+
return cost;
20+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
- [530. Minimum Absolute Difference in BST](./530/)
7171
- [647. Palindromic Substrings](./647/)
7272
- [771. Jewels and Stones](./771/)
73+
- [714. Best Time to Buy and Sell Stock with Transaction Fee](./714/)
7374
- [832. Flipping an Image](./832/)
7475
- [876. Middle of the Linked List](./876/)
7576
- [997. Find the Town Judge](./997/)
@@ -135,7 +136,7 @@ Batch create:
135136
NOTE: JS IS HERE
136137
-->
137138
```ssh
138-
chapter=383 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
139+
chapter=714 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
139140
```
140141
> then you can use `x` for quick debug.
141142

0 commit comments

Comments
 (0)