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
| #-------------------------------------------------------------- | |
| #--------Move all occurrences of given element to last---------- | |
| #-------------------------------------------------------------- | |
| ''' | |
| Question : Move all occurrences of given element to the last and we don't care about other | |
| element's order | |
| We could sort the array and then move the element to the last but it will take O(nlogn) time | |
| That is not the best solution to do this |
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
| #------------------------------------------- | |
| #------------Simple Linked List------------- | |
| #------------------------------------------- | |
| # Node Class | |
| class Node: | |
| def __init__(self, data): | |
| self.data = data | |
| self.link = None | |
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
| #----------------------------------------------------- | |
| #--------Minimum Number Of Coins For Change----------- | |
| #----------------------------------------------------- | |
| ''' | |
| Logic | |
| Suppose we are asked to make change of 24 and denomination given are [10,5,2,1] | |
| Let total_coins = 0 |
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
| #--------------------------------------------------------- | |
| #---------Three Largest Numbers---------- | |
| #---------------------------------------------------------- | |
| # this function returns three largest numbers | |
| # complexity O(2n) = O(n) where n is length of array | |
| def threeLargestNumbers(numbers): | |
| if len(numbers) < 4: | |
| return numbers | |
| largest = [float("-inf")]*3 |
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
| #------------------------------------------- | |
| #------------Palindrome--------------------- | |
| #------------------------------------------- | |
| # this function check whether given string is palindrome or not | |
| def palindrome(string): | |
| length = len(string) | |
| mid = length//2 | |
| for i in range(mid): | |
| if string[i] != string[length -1 -i]: |
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
| #--------------------------------------- | |
| #-----------Caesar Cipher--------------- | |
| #--------------------------------------- | |
| # Ceaser Cipher class | |
| class CaesarCipher: | |
| # this method encrypt the plain text | |
| @staticmethod | |
| def encrypt(string,key): | |
| result = "" |
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
| #-------------------------------------------- | |
| #------------MinMax Stack-------------------- | |
| #-------------------------------------------- | |
| ''' | |
| This is similar to the normal stack with small modification. | |
| MinMax Stack can return min and max value of the stack in O(1) time | |
| Below code is the list/array implementation of stack | |
| Fore linked list implementation in C : https://gist.github.com/jatinsharrma/47b0c3ab06856563b890e400338956da |
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
| #----------------------------------------------------------------- | |
| #-------------------------Min Heap-------------------------------- | |
| #----------------------------------------------------------------- | |
| # Min Heap class | |
| class MinHeap: | |
| def __init__ (self, array): | |
| self.heap = self.bulidHeap(array) | |
| # This method take an array and build a heap from it |
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
| #------------------------------------------------------------ | |
| #----------Finding maximum sum (Non Adjacent)---------------- | |
| #------------------------------------------------------------ | |
| ''' | |
| Logic: | |
| Suppose we have given [4,8,2,10,15,9,3,11] |
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
| #--------- Longest Palindromic Substring------------------ | |
| ''' | |
| Logic: | |
| we know that a palindrome can be of even or odd length | |
| For example "aba" here b is center and in "abba" center is between 2 "b"s | |
| In below logic we will be using this knowledge | |
| Everytime we will make 2 assumptions |