Skip to content

Commit a44daaf

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

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
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] |
8585
| [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-kotlin] |
86+
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [Easy][E] | [][121-java] | [][121-js]| [][121-kotlin] |
8787
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [Easy][E] | [][122-java] | | [][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] |
@@ -463,6 +463,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
463463
[112-js]: ./src/_112/Solution.js
464464
[118-js]: ./src/_118/Solution.js
465465
[119-js]: ./src/_119/Solution.js
466+
[121-js]: ./src/_121/Solution.js
466467
[226-js]: ./src/_226/Solution.js
467468
[561-js]: ./src/_561/Solution.js
468469
[643-js]: ./src/_643/Solution.js

src/_121/Solution.js

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

tips/121/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ class Solution {
6262
}
6363
```
6464

65+
```JavaScript
66+
var maxProfit = function(prices) {
67+
let finalProfit = 0
68+
for(var i = 0; i < prices.length; i++) {
69+
for(var j = 0; j < i; j++) {
70+
let profit = prices[i] - prices[j]
71+
if(profit > finalProfit) {
72+
finalProfit = profit
73+
}
74+
}
75+
}
76+
return finalProfit
77+
};
78+
```
6579
## 结语
6680

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

0 commit comments

Comments
 (0)