Skip to content

Commit 337c14f

Browse files
committed
feat: add solution of Best Time to Buy and Sell Stock II(122) with javascript.
1 parent a44daaf commit 337c14f

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@
8282
| [111][111-question] | [Minimum Depth of Binary Tree][111-tips] | [Easy][E] | [][111-java] | [][111-js] | [][111-kotlin] |
8383
| [112][112-question] | [Path Sum][112-tips] | [Easy][E] | [][112-java] | [][112-js] | [][112-kotlin] |
8484
| [118][118-question] | [Pascal's Triangle][118-tips] | [Easy][E] | [][118-java] | [][118-js] | [][118-kotlin] |
85-
| [119][119-question] | [Pascal's Triangle II][119-tips] | [Easy][E] | [][119-java] | [][119-js]| [][119-kotlin] |
86-
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [Easy][E] | [][121-java] | [][121-js]| [][121-kotlin] |
87-
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [Easy][E] | [][122-java] | | [][122-kotlin] |
85+
| [119][119-question] | [Pascal's Triangle II][119-tips] | [Easy][E] | [][119-java] | [][119-js] | [][119-kotlin] |
86+
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [Easy][E] | [][121-java] | [][121-js] | [][121-kotlin] |
87+
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [Easy][E] | [][122-java] | [][122-js] | [][122-kotlin] |
8888
| [125][125-question] | [Valid Palindrome][125-tips] | [Easy][E] | | | [][125-kotlin] |
8989
| [136][136-question] | [Single Number][136-tips] | [Easy][E] | | | [][136-kotlin] |
9090
| [141][141-question] | [Linked List Cycle][141-tips] | [Easy][E] | [][141-java] | | - |
@@ -464,6 +464,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
464464
[118-js]: ./src/_118/Solution.js
465465
[119-js]: ./src/_119/Solution.js
466466
[121-js]: ./src/_121/Solution.js
467+
[122-js]: ./src/_122/Solution.js
467468
[226-js]: ./src/_226/Solution.js
468469
[561-js]: ./src/_561/Solution.js
469470
[643-js]: ./src/_643/Solution.js

src/_122/Solution.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let profit = 0;
7+
for (let i = 0; i < prices.length - 1; i++) {
8+
const possibleProfit = prices[i + 1] - prices[i];
9+
profit = Math.max(profit + possibleProfit, profit);
10+
}
11+
return profit;
12+
};

tips/122/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ class Solution {
3939
}
4040
```
4141

42+
JavaScript
43+
```JavaScript
44+
var maxProfit = function(prices) {
45+
let profit = 0;
46+
for (let i = 0; i < prices.length - 1; i++) {
47+
const possibleProfit = prices[i + 1] - prices[i];
48+
profit = Math.max(profit + possibleProfit, profit);
49+
}
50+
return profit;
51+
};
52+
```
4253
## 结语
4354

4455
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]

0 commit comments

Comments
 (0)