Skip to content

Commit a2d2760

Browse files
committed
buy sell
1 parent f70937c commit a2d2760

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"initializer_list": "cpp",
77
"regex": "cpp",
88
"utility": "cpp",
9-
"valarray": "cpp"
9+
"valarray": "cpp",
10+
"chrono": "cpp",
11+
"random": "cpp",
12+
"limits": "cpp"
1013
}
1114
}

DSA Crack Sheet/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66

77
- [Reverse the array](https://www.geeksforgeeks.org/write-a-program-to-reverse-an-array-or-string/ "view topic")
88
- [Maximum and minimum of an array using minimum number of comparisons](https://www.geeksforgeeks.org/maximum-and-minimum-in-an-array/ "view topic")
9-
- [Kth smallest element ](https://practice.geeksforgeeks.org/problems/kth-smallest-element5635/1 "view questions") - [Cpp Solution](./solutions/Kth%20smallest%20element.cpp)
10-
- [Sort an array of 0s, 1s and 2s](https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1# "view questions") - [Cpp Solution](./solutions/Sort%20an%20array%20of%200s,%201s%20and%202s.cpp)
9+
- [Kth smallest element ](https://practice.geeksforgeeks.org/problems/kth-smallest-element5635/1 "view question") - [Cpp Solution](./solutions/Kth%20smallest%20element.cpp)
10+
- [Sort an array of 0s, 1s and 2s](https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1# "view question") - [Cpp Solution](./solutions/Sort%20an%20array%20of%200s,%201s%20and%202s.cpp)
1111
- [Move all negative numbers to beginning and positive to end with constant extra space](https://www.geeksforgeeks.org/move-negative-numbers-beginning-positive-end-constant-extra-space/ "view topic")
12-
- [Union of two arrays](https://practice.geeksforgeeks.org/problems/union-of-two-arrays3538/1# "view questions") - [Cpp Solution](./solutions/Union%20of%20two%20arrays.cpp)
13-
- [Cyclically rotate an array by one](https://practice.geeksforgeeks.org/problems/cyclically-rotate-an-array-by-one2614/1# "view questions") - [Cpp Solution](./solutions/Cyclically%20rotate%20an%20array%20by%20one.cpp)
14-
- [Kadane's Algorithm](https://practice.geeksforgeeks.org/problems/kadanes-algorithm-1587115620/1# "view questions") - [Cpp Solution](./solutions/Kadane's%20Algorithm.cpp)
15-
- [Minimum number of jumps](https://practice.geeksforgeeks.org/problems/minimum-number-of-jumps-1587115620/1# "view questions") - [Cpp Solution](./solutions/Minimum%20number%20of%20jumps.cpp)
16-
- [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/ "view questions") - [Cpp Solution](./solutions/Find%20the%20Duplicate%20Number.cpp)
17-
- [Merge Intervals](https://leetcode.com/problems/merge-intervals/ "view questions") - [Cpp Solution](./solutions/Merge%20Intervals.cpp)
18-
- [Next Permutation](https://leetcode.com/problems/next-permutation/ "view questions") - [Cpp Solution](./solutions/Next%20Permutation.cpp)
19-
- [Count Inversions](https://practice.geeksforgeeks.org/problems/inversion-of-array-1587115620/1# "view questions") - [Cpp Solution](./solutions/Count%20Inversions.cpp)
20-
- []( "view questions") - [Cpp Solution](./solutions/.cpp)
21-
22-
<!-- - []( "view questions") - [Cpp Solution](./solutions/) -->
12+
- [Union of two arrays](https://practice.geeksforgeeks.org/problems/union-of-two-arrays3538/1# "view question") - [Cpp Solution](./solutions/Union%20of%20two%20arrays.cpp)
13+
- [Cyclically rotate an array by one](https://practice.geeksforgeeks.org/problems/cyclically-rotate-an-array-by-one2614/1# "view question") - [Cpp Solution](./solutions/Cyclically%20rotate%20an%20array%20by%20one.cpp)
14+
- [Kadane's Algorithm](https://practice.geeksforgeeks.org/problems/kadanes-algorithm-1587115620/1# "view question") - [Cpp Solution](./solutions/Kadane's%20Algorithm.cpp)
15+
- [Minimum number of jumps](https://practice.geeksforgeeks.org/problems/minimum-number-of-jumps-1587115620/1# "view question") - [Cpp Solution](./solutions/Minimum%20number%20of%20jumps.cpp)
16+
- [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/ "view question") - [Cpp Solution](./solutions/Find%20the%20Duplicate%20Number.cpp)
17+
- [Merge Intervals](https://leetcode.com/problems/merge-intervals/ "view question") - [Cpp Solution](./solutions/Merge%20Intervals.cpp)
18+
- [Next Permutation](https://leetcode.com/problems/next-permutation/ "view question") - [Cpp Solution](./solutions/Next%20Permutation.cpp)
19+
- [Count Inversions](https://practice.geeksforgeeks.org/problems/inversion-of-array-1587115620/1# "view question") - [Cpp Solution](./solutions/Count%20Inversions.cpp)
20+
- [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ "view question") - [Cpp Solution](./solutions/Best%20Time%20to%20Buy%20and%20Sell%20Stock.cpp)
21+
- []( "view question") - [Cpp Solution](./solutions/.cpp)
22+
23+
<!-- - []( "view question") - [Cpp Solution](./solutions/) -->
2324

2425

2526
### Matrix
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Best Time to Buy and Sell Stock
3+
=================================
4+
5+
You are given an array prices where prices[i] is the price of a given stock on the ith day.
6+
7+
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.
8+
9+
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
10+
11+
Example 1:
12+
Input: prices = [7,1,5,3,6,4]
13+
Output: 5
14+
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
15+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
16+
17+
Example 2:
18+
Input: prices = [7,6,4,3,1]
19+
Output: 0
20+
Explanation: In this case, no transactions are done and the max profit = 0.
21+
22+
Constraints:
23+
1 <= prices.length <= 105
24+
0 <= prices[i] <= 104
25+
*/
26+
27+
class Solution
28+
{
29+
public:
30+
int maxProfit(vector<int> &prices)
31+
{
32+
int n = prices.size();
33+
if (n == 0)
34+
return 0;
35+
int ans = 0;
36+
int curr_min = prices[0];
37+
38+
for (int i = 1; i < n; ++i)
39+
{
40+
if (prices[i] - curr_min > 0)
41+
ans = max(ans, prices[i] - curr_min);
42+
curr_min = min(curr_min, prices[i]);
43+
}
44+
45+
return ans;
46+
}
47+
};

0 commit comments

Comments
 (0)