This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
We can apply cycle sort approach in this question | |
first we try to find index and item is useful sorting logic. | |
should be : | |
index : 0 , item : 1 | |
index : 1, item : 2 | |
if we find any wrong match between index and item we can swap useful place in this array. | |
like : |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
We can apply fast and slow pointer approach for this question. | |
Slow pointer goes one by one acccording to nums index | |
slow = nums[slow] | |
Fast pointer goes twice according to nums inside nums index. | |
fast = nums[nums[fast]] | |
And we can find common intersection so they can reach in the same item. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
We can solve to seperate 2 parts in solving. | |
1 - We try to get ideal list (almost sorting list but not exactly sorted list) from original list | |
2 - find missing item | |
1 - we can iterate all list | |
as logic ideal list should be like : [1,2,3,4,5,6,7....n] , so means nums[i] ,nums[i+1] .... | |
we can check as first logic next item is usefull for ideal list. | |
for i range 0,n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
We think how to get a series sum? | |
If we a formula to get a series sum, we can match normal sum of this series. | |
Difference of this process we can get missing number | |
sum any series formula = n*(n+1)/2 | |
normal sum series = sum(nums) | |
get missing elem > sum_series - normal_sum | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
We can reduce time compl. with the a DP solution. | |
in the brute force we can use 2 loops them together. | |
in DP solution we don't need 2 loops used them together. | |
We can create left and right max 1D dp array. | |
we can use this arrays. | |
first, we can keep a left_max_array start to end troughout height array | |
secodn we can keep a right max array end to start troughout height array | |
finally we gonna filled up 2 dp array. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
if we can brute force : | |
first we need to go left side from current item. We need max item in the left side | |
second we can go to right side from the current item and we can get max right item | |
and finally we can get min(left,right) - current main item | |
if this difference grater than 0 we can add to total |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
football | |
each play can lead 2,3 or 7 points. | |
if we are given some score of p find out 2,3 and 7 | |
how many diff perm | |
p = 7 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
we can apply a DP solution for this question. | |
we can generate a DP table been length array size | |
first element will be 1 in DP | |
after we can iterate 2 loops. i start from 1 , j start from 0 | |
every item will check to start from j , to i | |
if nums[i] > nums[j] | |
we can find max_val = max(max_val, dp[j]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
we can apply AP and Bisect left. | |
first we can generate a dp = list() | |
iterate num in nums | |
if there is no any item in DP - or current val is grater than last item of DP | |
we can add num to DP directly | |
else | |
call bisect_left : will get left most item. | |
we can set directly dp[insert_point_from_bst] = val | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
First we need to understant logic : | |
* : matches zero or more occurence of character before * | |
. : matches any single character | |
like : | |
a.b = acb , aab, axb > True . Because we can put any single char beetween a and b | |
a.b = ab, acyb > False. |