Skip to content

Commit 76581dc

Browse files
committed
feat: add Best Time to Buy and Sell Stock
1 parent 0ed0402 commit 76581dc

File tree

7 files changed

+208
-1
lines changed

7 files changed

+208
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"problem_name": "best_time_to_buy_and_sell_stock",
3+
"solution_class_name": "Solution",
4+
"problem_number": "121",
5+
"problem_title": "Best Time to Buy and Sell Stock",
6+
"difficulty": "Easy",
7+
"topics": "Array, Dynamic Programming",
8+
"tags": ["grind-75"],
9+
"readme_description": "You are given an array `prices` where `prices[i]` is the price of a given stock on the ith day.\n\nYou want to maximize your profit by choosing a **single day** to buy one stock and choosing a **different day in the future** to sell that stock.\n\nReturn *the maximum profit you can achieve from this transaction*. If you cannot achieve any profit, return `0`.",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: prices = [7,1,5,3,6,4]\nOutput: 5\n```\n**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.\nNote that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell."
13+
},
14+
{
15+
"content": "```\nInput: prices = [7,6,4,3,1]\nOutput: 0\n```\n**Explanation:** In this case, no transactions are done and the max profit = 0."
16+
}
17+
],
18+
"readme_constraints": "- 1 <= prices.length <= 10^5\n- 0 <= prices[i] <= 10^4",
19+
"readme_additional": "",
20+
"solution_imports": "",
21+
"solution_methods": [
22+
{
23+
"name": "max_profit",
24+
"parameters": "prices: list[int]",
25+
"return_type": "int",
26+
"dummy_return": "0"
27+
}
28+
],
29+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
30+
"test_class_name": "BestTimeToBuyAndSellStock",
31+
"test_helper_methods": [
32+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
33+
],
34+
"test_methods": [
35+
{
36+
"name": "test_max_profit",
37+
"parametrize": "prices, expected",
38+
"parametrize_typed": "prices: list[int], expected: int",
39+
"test_cases": "[([7, 1, 5, 3, 6, 4], 5), ([7, 6, 4, 3, 1], 0), ([1, 2, 3, 4, 5], 4), ([5, 4, 3, 2, 1], 0), ([1], 0), ([2, 1], 0), ([1, 2], 1), ([3, 2, 6, 5, 0, 3], 4)]",
40+
"body": "result = self.solution.max_profit(prices)\nassert result == expected"
41+
}
42+
],
43+
"playground_imports": "from solution import Solution",
44+
"playground_test_case": "# Example test case\nprices = [7, 1, 5, 3, 6, 4]\nexpected = 5",
45+
"playground_execution": "result = Solution().max_profit(prices)\nresult",
46+
"playground_assertion": "assert result == expected"
47+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= merge_two_sorted_lists
2+
PROBLEM ?= best_time_to_buy_and_sell_stock
33
FORCE ?= 0
44
COMMA := ,
55

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Best Time to Buy and Sell Stock
2+
3+
**Difficulty:** Easy
4+
**Topics:** Array, Dynamic Programming
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
8+
9+
## Problem Description
10+
11+
You are given an array `prices` where `prices[i]` is the price of a given stock on the ith day.
12+
13+
You want to maximize your profit by choosing a **single day** to buy one stock and choosing a **different day in the future** to sell that stock.
14+
15+
Return _the maximum profit you can achieve from this transaction_. If you cannot achieve any profit, return `0`.
16+
17+
## Examples
18+
19+
### Example 1:
20+
21+
```
22+
Input: prices = [7,1,5,3,6,4]
23+
Output: 5
24+
```
25+
26+
**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
27+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
28+
29+
### Example 2:
30+
31+
```
32+
Input: prices = [7,6,4,3,1]
33+
Output: 0
34+
```
35+
36+
**Explanation:** In this case, no transactions are done and the max profit = 0.
37+
38+
## Constraints
39+
40+
- 1 <= prices.length <= 10^5
41+
- 0 <= prices[i] <= 10^4

leetcode/best_time_to_buy_and_sell_stock/__init__.py

Whitespace-only changes.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "setup",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"prices = [7, 1, 5, 3, 6, 4]\n",
22+
"expected = 5"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"id": "execute",
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"data": {
33+
"text/plain": [
34+
"5"
35+
]
36+
},
37+
"execution_count": 3,
38+
"metadata": {},
39+
"output_type": "execute_result"
40+
}
41+
],
42+
"source": [
43+
"result = Solution().max_profit(prices)\n",
44+
"result"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"id": "test",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"assert result == expected"
55+
]
56+
}
57+
],
58+
"metadata": {
59+
"kernelspec": {
60+
"display_name": "leetcode-py-py3.13",
61+
"language": "python",
62+
"name": "python3"
63+
},
64+
"language_info": {
65+
"codemirror_mode": {
66+
"name": "ipython",
67+
"version": 3
68+
},
69+
"file_extension": ".py",
70+
"mimetype": "text/x-python",
71+
"name": "python",
72+
"nbconvert_exporter": "python",
73+
"pygments_lexer": "ipython3",
74+
"version": "3.13.7"
75+
}
76+
},
77+
"nbformat": 4,
78+
"nbformat_minor": 5
79+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
# Time: O(n)
3+
# Space: O(1)
4+
def max_profit(self, prices: list[int]) -> int:
5+
min_price = prices[0]
6+
max_profit = 0
7+
8+
for price in prices[1:]:
9+
max_profit = max(max_profit, price - min_price)
10+
min_price = min(min_price, price)
11+
12+
return max_profit
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .solution import Solution
6+
7+
8+
class TestBestTimeToBuyAndSellStock:
9+
def setup_method(self):
10+
self.solution = Solution()
11+
12+
@pytest.mark.parametrize(
13+
"prices, expected",
14+
[
15+
([7, 1, 5, 3, 6, 4], 5),
16+
([7, 6, 4, 3, 1], 0),
17+
([1, 2, 3, 4, 5], 4),
18+
([5, 4, 3, 2, 1], 0),
19+
([1], 0),
20+
([2, 1], 0),
21+
([1, 2], 1),
22+
([3, 2, 6, 5, 0, 3], 4),
23+
],
24+
)
25+
@logged_test
26+
def test_max_profit(self, prices: list[int], expected: int):
27+
result = self.solution.max_profit(prices)
28+
assert result == expected

0 commit comments

Comments
 (0)