Skip to content

Commit 2e3944a

Browse files
author
Sanajit Jana
committed
Add new problem
1 parent 765bd95 commit 2e3944a

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
2+
3+
## Question Description
4+
You are given an array prices where prices[i] is the price of a given stock on the ith day.
5+
6+
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.
7+
8+
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
9+
10+
---
11+
12+
## Constraints
13+
- 1 <= prices.length <= 10^5
14+
- 0 <= prices[i] <= 10^4
15+
16+
---
17+
18+
## Brute Force
19+
20+
### Approach
21+
The brute force approach involves checking all possible pairs of buy and sell days. For each pair (i, j) where i < j, calculate the profit as prices[j] - prices[i] and keep track of the maximum profit.
22+
23+
This approach has a time complexity of O(n²) due to the nested loops, which is inefficient for large inputs.
24+
25+
### Dry Run
26+
Example Input: prices = [7,1,5,3,6,4]
27+
28+
- Check all pairs: (0,1): 1-7=-6, (0,2):5-7=-2, (0,3):3-7=-4, (0,4):6-7=-1, (0,5):4-7=-3, (1,2):5-1=4, (1,3):3-1=2, (1,4):6-1=5, (1,5):4-1=3, (2,3):3-5=-2, (2,4):6-5=1, (2,5):4-5=-1, (3,4):6-3=3, (3,5):4-3=1, (4,5):4-6=-2
29+
- Max profit: 6-1=5
30+
31+
Final Answer = 5
32+
33+
### Solution
34+
```java
35+
class Solution {
36+
public int maxProfit(int[] prices) {
37+
int max = 0;
38+
for (int i = 0; i < prices.length; i++) {
39+
for (int j = i + 1; j < prices.length; j++) {
40+
int diff = prices[j] - prices[i];
41+
max = Math.max(diff, max);
42+
}
43+
}
44+
return max;
45+
}
46+
}
47+
```
48+
49+
### Time Complexity
50+
O(n²)
51+
52+
---
53+
54+
## Optimized Solution
55+
56+
### Approach
57+
The optimized solution uses a single pass through the array. We keep track of the minimum price seen so far (buy) and the maximum profit achievable. For each day, if the current price is less than the buy price, update buy. Otherwise, calculate the profit and update max if it's greater.
58+
59+
### Dry Run
60+
Example Input: prices = [7,1,5,3,6,4]
61+
62+
- buy=7, max=0
63+
- i=1, prices[1]=1 < buy, buy=1
64+
- i=2, prices[2]=5 > buy, profit=5-1=4, max=4
65+
- i=3, prices[3]=3 > buy, profit=3-1=2, max=4
66+
- i=4, prices[4]=6 > buy, profit=6-1=5, max=5
67+
- i=5, prices[5]=4 > buy, profit=4-1=3, max=5
68+
69+
Final Answer = 5
70+
71+
### Solution
72+
```java
73+
class Solution {
74+
public int maxProfit(int[] prices) {
75+
int buy = prices[0];
76+
int max = 0;
77+
78+
for (int i = 0; i < prices.length; i++) {
79+
if (prices[i] < buy) buy = prices[i];
80+
else if (prices[i] - buy > max) max = prices[i] - buy;
81+
}
82+
return max;
83+
}
84+
}
85+
```
86+
87+
### Time and Space Complexity
88+
- Time Complexity: O(n)
89+
- Space Complexity: O(1)

p/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
0073: Set Matrix Zeroes - [`0073-set-matrix-zeroes/`](0073-set-matrix-zeroes/)
66

7+
0121: Best Time to Buy and Sell Stock - [`0121-best-time-to-buy-and-sell-stock/`](0121-best-time-to-buy-and-sell-stock/)
8+
79
0165: Compare Version Numbers - [`0165-compare-version-numbers/`](0165-compare-version-numbers/)
810

911
0169: Majority Element - [`0169-majority-element/`](0169-majority-element/)

0 commit comments

Comments
 (0)