Best Time to Buy and Sell Stock – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
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.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0
.
Example 1:
Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
1 <= prices.length <= 105
0 <= prices[i] <= 104
C++ Best Time to Buy and Sell StockLeetCode Solution
class Solution {
public:
int maxProfit(vector<int>& prices) {
int lsf = INT_MAX;
int op = 0;
int pist = 0;
for(int i = 0; i < prices.size(); i++){
if(prices[i] < lsf){
lsf = prices[i];
}
pist = prices[i] - lsf;
if(op < pist){
op = pist;
}
}
return op;
}
};
Java Best Time to Buy and Sell StockLeetCode Solution
class Solution {
public int maxProfit(int[] prices) {
int lsf = Integer.MAX_VALUE;
int op = 0;
int pist = 0;
for(int i = 0; i < prices.length; i++){
if(prices[i] < lsf){
lsf = prices[i];
}
pist = prices[i] - lsf;
if(op < pist){
op = pist;
}
}
return op;
}
}
Python 3 Best Time to Buy and Sell Stock LeetCode Solution
class Solution:
def maxProfit(self,prices):
left = 0 #Buy
right = 1 #Sell
max_profit = 0
while right < len(prices):
currentProfit = prices[right] - prices[left] #our current Profit
if prices[left] < prices[right]:
max_profit =max(currentProfit,max_profit)
else:
left = right
right += 1
return max_profit
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1
Leave a comment below