Skip to content

Commit

Permalink
feat: add typescript solution to lc problem: No.0122.Best Time to Buy…
Browse files Browse the repository at this point in the history
… and Sell Stock II (doocs#525)
  • Loading branch information
zhaocchen authored Jul 18, 2021
1 parent 7061247 commit 214c212
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ class Solution {
}
```

### **TypeScript**

```ts
function maxProfit(prices: number[]): number {
let ans = 0;
for (let i = 1; i < prices.length; i++) {
ans += Math.max(0, prices[i] - prices[i - 1]);
}
return ans;
};
```

### **C++**

贪心:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ class Solution {
}
```

### **TypeScript**

```ts
function maxProfit(prices: number[]): number {
let ans = 0;
for (let i = 1; i < prices.length; i++) {
ans += Math.max(0, prices[i] - prices[i - 1]);
}
return ans;
};
```

### **C++**

Greedy:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function maxProfit(prices: number[]): number {
let ans = 0;
for (let i = 1; i < prices.length; i++) {
ans += Math.max(0, prices[i] - prices[i - 1]);
}
return ans;
};

0 comments on commit 214c212

Please sign in to comment.