Skip to content

Commit bbfd9f7

Browse files
committed
Best Time to Buy and Sell Stock
1 parent a31508c commit bbfd9f7

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

0121-best-time-to-buy-and-sell-stock/best-time-to-buy-and-sell-stock.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ This repository contains solutions to various LeetCode problems, organized by di
44

55
## 📊 Statistics
66

7-
- **Total Problems**: 143 solved
8-
- **Easy**: 73 problems
7+
- **Total Problems**: 144 solved
8+
- **Easy**: 74 problems
99
- **Medium**: 60 problems
1010
- **Hard**: 10 problems
1111

@@ -25,6 +25,7 @@ Problems are organized by:
2525
- [0001 - Two Sum](./leetcode/easy/0001-two-sum) ![Easy](https://img.shields.io/badge/Easy-green)
2626
- [0009 - Palindrome Number](./leetcode/easy/0009-palindrome-number) ![Easy](https://img.shields.io/badge/Easy-green)
2727
- [0020 - Valid Parentheses](./leetcode/easy/0020-valid-parentheses) ![Easy](https://img.shields.io/badge/Easy-green)
28+
- [0121 - Best Time to Buy and Sell Stock](./leetcode/easy/0121-best-time-to-buy-and-sell-stock) ![Easy](https://img.shields.io/badge/Easy-green)
2829
- [0169 - Majority Element](./leetcode/easy/0169-majority-element) ![Easy](https://img.shields.io/badge/Easy-green)
2930
- [0266 - Palindrome Permutation](./leetcode/easy/0266-palindrome-permutation) ![Easy](https://img.shields.io/badge/Easy-green)
3031
- [0205 - Isomorphic Strings](./leetcode/easy/0205-isomorphic-strings) ![Easy](https://img.shields.io/badge/Easy-green)

0121-best-time-to-buy-and-sell-stock/README.md renamed to leetcode/easy/0121-best-time-to-buy-and-sell-stock/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) ![](https://img.shields.io/badge/Easy-green)
32

43
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p>
@@ -32,5 +31,3 @@ Note that buying on day 2 and selling on day 1 is not allowed because you must b
3231
<li><code>1 &lt;= prices.length &lt;= 10<sup>5</sup></code></li>
3332
<li><code>0 &lt;= prices[i] &lt;= 10<sup>4</sup></code></li>
3433
</ul>
35-
36-
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Space - O(1), Time - O(n)
2+
function maxProfit(prices: number[]): number {
3+
let minPrice = Infinity;
4+
let maxProfit = 0;
5+
6+
for (const price of prices) {
7+
minPrice = Math.min(minPrice, price);
8+
maxProfit = Math.max(maxProfit, price - minPrice);
9+
}
10+
11+
return maxProfit;
12+
}

0 commit comments

Comments
 (0)