Python leetcode solution 4
Python leetcode solution 4
High-level details
More Details…
Overview
The input given is an array where
each element represents the price
of a stock on that particular day, for
example, prices[0] represent the
price of the stock on the first day,
prices[1] represent the price of the
Overview
stock on the second day and so on.
Points to remember:
• You can only make one
transaction
• Only one stock is allowed to be
bought or sold
• You can only sell after buying
Example 1
The input is [7,1,5,3,6,4] , to make
the maximum profit we buy one
stock on the second day with the
Background
cost of 1 and sell it on the 5th day
with a price of 6, making a profit of
(6-1 = 5) 5.
Example 2:
The input given here [7, 6, 4, 3, 2, 1] ,
You can notice that the prices are
always decreasing, It doesn't
Background
matter on which day we buy or sell
we will always make a negative
profit, that is a loss. So the greatest
profit we can make is when we do
no transactions, 0 profit. So we
return 0.
Example 2:
The input given here [7, 6, 4, 3, 2, 1] ,
You can notice that the prices are
always decreasing, It doesn't
Background
matter on which day we buy or sell
we will always make a negative
profit, that is a loss. So the greatest
profit we can make is when we do
no transactions, 0 profit. So we
return 0.
Any Ideas ?
Attempt 1
This is a trivial solution where we are using two
nested for loops to find the maximum profit
between two days, the outer for loop iterates
through the array and returns the value and the
index of the current element using enumerate
function, the inner for loop slices the array from
the current element till the last element. And
Attempt 1
profit is equal to max of the current profit and
the profit between the two days in the loop.
The Challenge
the most optimal solution
You can the test case that it failed,
it starts from 10000 and goes all
the way to 0 in descending order
and the price of the stock
continues to be 0 until the length of
the array is 26004
Attempt 2
Since the test case we failed was in descending
order and the profit is 0, I thought that sorting
Attempt 2
the array prices and comparing it with the
original array will tell us if the prices are always
descending, considering the sort(function) is
faster than the nested loops
Time Limit Exceeded
The Challenge
Time limit error, 198/211 test cases
checked and passed, the code is
still not the most optimal solution
Other Trivial Attempt:
The big O notation:
“Big O notation is a mathematical notation that describes the
limiting behavior of a function when the argument tends towards a
particular value or infinity. It is a member of a family of notations
In basic words, the Big O notation tells us about the time complexity of a
given operation or a function of a code in terms of algebraic terms.
The big O notation:
O(1/n) < O(log n) < O(n) < O(n log n) < O(n²) < O(2^n) < O(n!)
The big O notation:
Accessing an array is O(1)
Iterating through an array once is O(n),
Iterating through an array twice in a nested manner is O(n²)
a=[1,2,3,4,5,6,7,8] a=[1,2,3,4,5,6,7,8]
target=6 for i in a:
for i in a: if i==8:
for j in a: return True
if i+j==6:
return i,j
The big O notation:
The time complexity of the above function is
O(n^2).The outer for loop has a time
complexity of O(n), as it iterates through the
Therefore, the overall time complexity of the function is O(n * n) = O(n^2). This means
that the running time of the function grows quadratically with the size of the input list.
The challenge
Optimising the Code:
So the challenge is to make the code more time efficient, the most optimal solution will
be O(n)
The Challenge
This means that we should iterate through the array only once, and return the maximum
profit than can be made with one transaction.
6
6
Start Again
5
4
4
I plotted a graph with the prices on
3
the y-axis and the days on the x-axis
to better understand how to solve
2
the code better 1
0
1 2 3 4 5 6
Going back to the drawing board:
8
7
6
6
Start Again
5
4
4
I plotted a graph with the prices on
3
the y-axis and the days on the x-axis
to better understand how to solve
2
the code better 1
0
1 2 3 4 5 6
Two Pointer Approach (Algorithm):
• We initialise two variables left and right with values 0 and 1, the pointer
point to the index of the array
Problem Statement
High-level details
Overview
The input given is an array where
each element represents the price
of a stock on that particular day, for
example, prices[0] represent the
price of the stock on the first day,
prices[1] represent the price of the
Overview
stock on the second day and so on.
Points to remember:
• You can make any number of
transactions
• Only one stock is allowed to be
bought or sold
• You can only sell after buying
Example 1
The input is [7,1,5,3,6,4] , to make the most
profit, we buy on day 2 at the price of 1 and
sell on day 3 for the price of 5 with a profit of
4, we again buy on day 4 at the price 3 and
sell it on day 5 with the price of 6, bringing
Example Testcases
the total profit to 7
Example 2
The input is [1,2,3,4,5], to make the most
profit we buy the stock on day 1 at another
price of 1, and sell it on day 5 at the price of
5 making a total profit of 4
Note: Here even if you buy and sell the stock
every other day you will get the profit of 4
Example 1
Here the input is [ 7, 6, 4, 3, 1] , the prices are
always decreasing, so we cannot make any
profit, the best case is making 0 profit, so we
Example Testcases
return 0
Constraints
Explanation
2 and sell at every local
1
maximum
0
1 2 3 4 5 6
• In the above graph, we buy on • Instead of finding the maxima and the
days 2 and 4 and sell the minima, we can just check if the prices are
stocks on days 3 and 5 increasing and add the profit to the total
profit on each day
Code :
• We initialise the variables total_profit, left,
right with the values 0,0,1, respectively
• While right is not at the end of the array, if
the value of the stock at the right pointer
Phase 1
is greater than the value of the left pointer,
it means that the price is increasing, so we
add the difference to the total_profit
• If the price of the left pointer is greater than the price of the right pointer, the price is
decreasing, we won't do any transaction in that case
• Increase both left and right by 1, and continue the loop, until after the the right pointer
points to the last element
Result :
Result
Better Code :
Better Code
Credits :