Uploaded by Kenan Rustamov

Leetcode 2022

advertisement
Blind 75
**Approach**
**Big O Analysis**
Leave a like if this helped! If you have any questions/issues with the code, let me know
in the comments!
121. Best Time to Buy and Sell Stock
Approach
In terms of approach, we want to find the maximum profit possible and the way we're
going to do that as we can keep track of the minimum value we’ve encountered thus far.
By comparing the profit of “selling”(current value - current minimum value) to the max
profit, we can continually update the max profit. Then, at the end of the iteration we get
the final Max profit possible.
Code break down
MinValue is equal to 10^5 because that is greater than any value you can encounter in
the list. Max profit is set to 0 because that ensures that if nothing changes the max
profit, it'll yield 0. Now we can continually update the min value if a lower price is
encountered and update the max profit if a greater profit is encountered.
Big O break down
So this is clearly O(n) time because we are iterating through the list one time and every
iteration has constant time calculations.
217. Contains Duplicate
Approach
so sure we can check if the length of the list numbs is equal to the length of a set of the
list nums. However this is probably not how this is meant to be salt. Instead, we can
keep track of previous values that have been encountered with a set, and every time
we encounter a new element check if it is found within that set.
Big'o break down
This is Big O of n because adding to a set end checking if a value is in a set is constant
time. in the worst case alliterate through the entire array.
152. Maximum Product Subarray
Approach
So, the first thing that comes to mind is using the same approach as the maximum sum
subarray, however, this breaks down with multiplying negative numbers because a
negative number can lead you to a maximum later down the line. This means we must
keep track of both the current maximum product and the current minimum product. As
we iterate through the list, we find the current minimum by seeing if the current element
is smaller than either the previous maximum*the current element or the previous
minimum *current element because both of them could lead to minimums. We do the
same for the current maximum because multiplication could lead to both maximums or
minimums. Then, we can update the total maximum by seeing if the current maximum is
the greatest we have encountered thus far.
Big O Break Down
This is Big O N because we are iterating through the list one time and every iteration
has constant time calculations.
322. Coin Change
Approach
Must start on a single coin and for each recursive call try to find the min number of coins
to make amount - current coin by looping through all the numbers. Then return this
number. Can use memoization to improve the speed and memory. Keep track of the min
number of coins for the current amount, can remove branches in the overall recursive
stack to improve speed and memory.
Big O break down
With memoization the runtime is O(n*m).
Others
108. Convert Sorted Array to Binary Search Tree
Hello Saudi Arabia is already sorted which means to start populating the tree the binary
search tree we have to find the root note and once we find the node then we could set
the value to the current value and then at the left note then we can add the right note
but in order to add love note we need to know what the weather is equal to. What does
equal to the root of that of its own tree which because it's a binary search tree must be
in the middle of all the values of that subarray. this means we have to find multiple
subarrays and then Subways in those subarrays to calculate the middle notes in each of
them. This is a recursive pattern. So the easiest way to solve this is a recursive function
that calculates the middle which is the route and then calls the recursive function again
to find the value of the left node using a separate subarray then calling the crucifixion to
find the value of the right note using a separate Subway and finally once we've got
those returning the route so the value is set in those notes once it back propagates the
recursive function. new one
Big O break down new one
Dysfunction this solution is Big O time a van. this is because every value in the list must
be visited which is and different values.
Download