Skip to content

Commit 214c212

Browse files
authored
feat: add typescript solution to lc problem: No.0122.Best Time to Buy and Sell Stock II (doocs#525)
1 parent 7061247 commit 214c212

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ class Solution {
134134
}
135135
```
136136

137+
### **TypeScript**
138+
139+
```ts
140+
function maxProfit(prices: number[]): number {
141+
let ans = 0;
142+
for (let i = 1; i < prices.length; i++) {
143+
ans += Math.max(0, prices[i] - prices[i - 1]);
144+
}
145+
return ans;
146+
};
147+
```
148+
137149
### **C++**
138150

139151
贪心:

solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README_EN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ class Solution {
110110
}
111111
```
112112

113+
### **TypeScript**
114+
115+
```ts
116+
function maxProfit(prices: number[]): number {
117+
let ans = 0;
118+
for (let i = 1; i < prices.length; i++) {
119+
ans += Math.max(0, prices[i] - prices[i - 1]);
120+
}
121+
return ans;
122+
};
123+
```
124+
113125
### **C++**
114126

115127
Greedy:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function maxProfit(prices: number[]): number {
2+
let ans = 0;
3+
for (let i = 1; i < prices.length; i++) {
4+
ans += Math.max(0, prices[i] - prices[i - 1]);
5+
}
6+
return ans;
7+
};

0 commit comments

Comments
 (0)