├── Day16 ├── KMP algo.py ├── Count and Say.py ├── Compare version numbers.py ├── Prefix Function or Z-Function.py ├── Minimum characters needed to be inserted in the beginning to make it palindromic.py └── Check for Anagrams.py ├── Day23 ├── Topo Sort.py ├── Bipartite Check.py ├── Clone a graph (Not that easy as it looks).py ├── Number of islands (Do in Grid and Graph both).py ├── Detect A cycle in Undirected Graph or Directed Graph.py ├── DFS.py └── BFS.py ├── Day10 ├── Rat in a Maze.py ├── Sudoku Solver.py ├── M coloring Problem.py ├── N queens Problem.py ├── Word Break (print all ways) .py └── Print all Permutations of a array.py ├── Day21 ├── BST iterator.py ├── Floor and Ceil in a BST.py ├── Find a pair with a given sum in BST.py ├── Serialize and deserialize Binary Tree.py ├── Size of the largest BST in a Binary Tree.py └── Find K-th smallest and K-th largest element in BST (2 different Questions).py ├── Day26 ├── Word Break.py ├── Maximum profit in Job scheduling.py ├── Palindrome Partitioning (MCM Variation).py ├── Egg Dropping.py ├── Subset Sum.py ├── Maximum sum path in matrix, (count paths, and similar type do, also backtrack to find the maximum path).py ├── Rod Cutting.py └── Coin change.py ├── Day11 ├── Aggressive Cows.py ├── Median of 2 sorted arrays.py ├── Allocate Minimum Number of Pages.py ├── K-th element of two sorted arrays.py ├── Search element in a sorted and rotated array find pivot where it is rotated.py ├── Find the element that appears once in sorted array, and rest element appears twice (Binary search).py ├── Matrix Median.py └── N-th root of an integer (use binary search) (square root, cube root, ..).py ├── Day14 ├── Implement Min Stack.py ├── LRU cache (vvvv. imp).py ├── Next Smaller Element.py ├── Sliding Window maximum.py ├── Rotten Orange (Using BFS).py └── Largest rectangle in histogram.py ├── Day24 ├── Bellman Ford Algo.py ├── Djisktra’s Algorithm.py ├── Floyd Warshall Algorithm.py ├── MST using Kruskal’s Algo.py ├── MST using Prim’s Algo.py └── SCC(using KosaRaju’s algo).py ├── Day13 ├── Next Greater Element.py ├── Implement Queue Using Arrays.py ├── Implement Stack Using Arrays.py ├── Check for balanced parentheses.py ├── Implement Stack using Queue (using single queue).py └── Implement Queue using Stack (0(1) amortised method).py ├── Day15 ├── Implement ATOI or STRSTR.py ├── Roman Number to Integer and vice versa.py ├── Reverse Words in a String.py ├── Longest Common Prefix.py ├── Rabin Karp.py └── Longest Palindrome in a string.py ├── Day22 ├── Flood-fill Algorithm.py ├── Distinct numbers in Window.py ├── Binary Tree to Double Linked List.py ├── K-th largest element in a stream.py ├── Find median in a stream of running integers.py └── K-th largest element in an unsorted array.py ├── Day20 ├── Construct BST from given keys.py ├── Populate Next Right pointers of Tree.py ├── Find the inorder predecessor or successor of a given Key in BST.py ├── Search given Key in BST.py ├── Check is a BT is BST or not.py └── Find LCA of two nodes in BST.py ├── Day25 ├── Matrix Chain Multiplication.py ├── Maximum sum increasing subsequence.py ├── Longest Increasing Subsequence.py ├── Max Product Subarray.py ├── 0-1 Knapsack.py ├── Edit Distance.py └── Longest Common Subsequence.py ├── Day 5 ├── Delete a given Node when a node is given.py ├── Reverse a LinkedList.py ├── Find middle of LinkedList.py ├── Merge two sorted Linked List.py ├── Remove N-th node from back of LinkedList.py └── Add two numbers as LinkedList.py ├── Day12 ├── Check if a number if a power of 2 or not in O(1).py ├── Power Set (this is very important).py ├── Divide Integers without operator.py ├── Find MSB in o(1).py ├── Find square of a number without using multiplication or division operators.py └── Count total set bits.py ├── Day 1 ├── Kadane’s Algorithm.py ├── Sort an array of 0’s 1’s 2’s without using extra space or sorting algo.py ├── Repeat and Missing Number.py ├── Merge Overlapping Subintervals.py ├── Find the duplicate in an array of N+1 integers.py └── Merge two sorted Arrays without extra space.py ├── Day 2 ├── Stock Buy and Sell.py ├── Rotate Matrix.py ├── Next Permutation.py ├── Pascal Triangle.py ├── Inversion of Array.py └── Set Matrix Zeros.py ├── Day 4 ├── 2 Sum problem.py ├── Largest Subarray with 0 sum.py ├── Count number of subarrays with given XOR.py ├── Longest substring without repeat.py ├── 4 Sum problem.py └── Longest Consecutive Sequence.py ├── Day 7 ├── Max consecutive ones.py ├── Remove Duplicate from Sorted array.py ├── 3 sum.py ├── Trapping rainwater.py └── Clone a Linked List with random and next pointer.py ├── Day 3 ├── Majority Element N2.py ├── Pow(X,n).py ├── Majority Element N3.py ├── Search in a 2D matrix.py ├── Grid Unique Paths.py └── Reverse Pairs (Leetcode).py ├── Day18 ├── Check if two trees are identical or not.py ├── Height of a Binary Tree.py ├── Diameter of Binary Tree.py ├── Check if Binary tree is height balanced or not.py ├── LCA in Binary Tree.py └── Level order Traversal or Level order traversal in spiral form.py ├── Day 8 ├── Minimum number of platforms required for a railway.py ├── N meeting in one room.py ├── Fractional Knapsack Problem.py ├── Activity Selection (it is same as N meeting in one room).py ├── Greedy algorithm to find minimum number of coins.py └── Job sequencing Problem.py ├── Day 6 ├── Detect a cycle in Linked List.py ├── Check if a LinkedList is palindrome or not.py ├── Find the starting point of the Loop of LinkedList.py ├── Reverse a LinkedList in groups of size k.py ├── Rotate a LinkedList.py ├── Find intersection point of Y LinkedList.py └── Flattening of a LinkedList.py ├── Day19 ├── Symmetric Binary Tree.py ├── Flatten Binary Tree to LinkedList.py ├── Maximum path sum.py ├── Check if Binary Tree is mirror of itself or not.py ├── Construct Binary Tree from Inorder and Postorder.py └── Construct Binary Tree from inorder and preorder.py ├── Day 9 ├── K-th permutation Sequence.py ├── Subset-II.py ├── Combination sum-1.py ├── Palindrome Partitioning.py ├── Combination sum-2.py └── Subset Sums.py ├── Day17 ├── Top View of Binary Tree.py ├── Bottom View of Binary Tree.py ├── Preorder Traversal (with recursion and without recursion).py ├── Inorder Traversal (with recursion and without recursion).py ├── LeftView Of Binary Tree.py └── Postorder Traversal (with recursion and without recursion).py └── README.md /Day16/KMP algo.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day23/Topo Sort.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day10/Rat in a Maze.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day10/Sudoku Solver.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day16/Count and Say.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day21/BST iterator.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day26/Word Break.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day10/M coloring Problem.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day10/N queens Problem.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day11/Aggressive Cows.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day14/Implement Min Stack.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day23/Bipartite Check.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day24/Bellman Ford Algo.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day13/Next Greater Element.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day14/LRU cache (vvvv. imp).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day14/Next Smaller Element.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day14/Sliding Window maximum.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day15/Implement ATOI or STRSTR.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day16/Compare version numbers.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day21/Floor and Ceil in a BST.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day22/Flood-fill Algorithm.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day24/Djisktra’s Algorithm.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day24/Floyd Warshall Algorithm.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day24/MST using Kruskal’s Algo.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day24/MST using Prim’s Algo.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day10/Word Break (print all ways) .py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day11/Median of 2 sorted arrays.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day13/Implement Queue Using Arrays.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day13/Implement Stack Using Arrays.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day14/Rotten Orange (Using BFS).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day16/Prefix Function or Z-Function.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day20/Construct BST from given keys.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day22/Distinct numbers in Window.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day24/SCC(using KosaRaju’s algo).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day25/Matrix Chain Multiplication.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day11/Allocate Minimum Number of Pages.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day11/K-th element of two sorted arrays.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day13/Check for balanced parentheses.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day14/Largest rectangle in histogram.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day22/Binary Tree to Double Linked List.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day22/K-th largest element in a stream.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day26/Maximum profit in Job scheduling.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day15/Roman Number to Integer and vice versa.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day20/Populate Next Right pointers of Tree.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day21/Find a pair with a given sum in BST.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day21/Serialize and deserialize Binary Tree.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day26/Palindrome Partitioning (MCM Variation).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day21/Size of the largest BST in a Binary Tree.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day22/Find median in a stream of running integers.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day22/K-th largest element in an unsorted array.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day23/Clone a graph (Not that easy as it looks).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day13/Implement Stack using Queue (using single queue).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day23/Number of islands (Do in Grid and Graph both).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day13/Implement Queue using Stack (0(1) amortised method).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day23/Detect A cycle in Undirected Graph or Directed Graph.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day20/Find the inorder predecessor or successor of a given Key in BST.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day21/Find K-th smallest and K-th largest element in BST (2 different Questions).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day11/Search element in a sorted and rotated array find pivot where it is rotated.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day16/Minimum characters needed to be inserted in the beginning to make it palindromic.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day11/Find the element that appears once in sorted array, and rest element appears twice (Binary search).py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Day 5/Delete a given Node when a node is given.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def deleteNode(self, node): 3 | node.val=node.next.val 4 | node.next=node.next.next 5 | -------------------------------------------------------------------------------- /Day11/Matrix Median.py: -------------------------------------------------------------------------------- 1 | def search(nums,low,high): 2 | if low>high: 3 | return 4 | if low==high: 5 | return nums[low] 6 | mid=(high-low+1) 7 | l=list(map(int,input().split())) 8 | -------------------------------------------------------------------------------- /Day12/Check if a number if a power of 2 or not in O(1).py: -------------------------------------------------------------------------------- 1 | for i in range(int(input())): 2 | n=int(input()) 3 | if n¬(n&(n-1)): 4 | print('YES') 5 | else: 6 | print('NO') 7 | -------------------------------------------------------------------------------- /Day12/Power Set (this is very important).py: -------------------------------------------------------------------------------- 1 | l=list(map(int,input().split())) 2 | n=len(l) 3 | size=1<0: 7 | c+=l[i] 8 | else: 9 | c=0 10 | s=max(c,s) 11 | print(s) 12 | 13 | -------------------------------------------------------------------------------- /Day 2/Stock Buy and Sell.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, l: List[int]) -> int: 3 | m,q=0,0 4 | for i in range(1,len(l)): 5 | q=max(0,l[i]-l[i-1]+q) 6 | m=max(m,q) 7 | return (m) 8 | 9 | 10 | -------------------------------------------------------------------------------- /Day23/DFS.py: -------------------------------------------------------------------------------- 1 | def dfsutil(d,x,visited): 2 | visited.add(x) 3 | print(x,end=' ') 4 | for i in d[x]: 5 | if i not in visited: 6 | dfsutil(d,i,visited) 7 | def dfs(d,x): 8 | visited=set() 9 | dfsutil(d,x,visited) 10 | print() 11 | -------------------------------------------------------------------------------- /Day12/Find MSB in o(1).py: -------------------------------------------------------------------------------- 1 | #method 1 2 | from math import log 3 | n=int(input()) 4 | print(1<>1 9 | n|=n>>2 10 | n|=n>>4 11 | n|=n>>8 12 | n|=n>>16 13 | n=n+1 14 | return n>>1 15 | 16 | -------------------------------------------------------------------------------- /Day12/Find square of a number without using multiplication or division operators.py: -------------------------------------------------------------------------------- 1 | def square(n): 2 | if n==1: 3 | return 1 4 | x=n>>1 5 | if n&1: 6 | return ((square(x)<<2) + (x<<2) + 1) 7 | else: 8 | return (square(x)<<2) 9 | 10 | 11 | -------------------------------------------------------------------------------- /Day16/Check for Anagrams.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isAnagram(self, s, t): 3 | dic1, dic2 = [0]*26, [0]*26 4 | for item in s: 5 | dic1[ord(item)-ord('a')] += 1 6 | for item in t: 7 | dic2[ord(item)-ord('a')] += 1 8 | return dic1 == dic2 9 | -------------------------------------------------------------------------------- /Day26/Egg Dropping.py: -------------------------------------------------------------------------------- 1 | from sys import maxsize 2 | # Exponential complexity 3 | def egg(n,k): 4 | if n==1 or k==1 or k==0: 5 | return k 6 | mini=maxsize 7 | for i in range(1,k+1): 8 | mini=min(mini,max(egg(n-1,i-1),egg(n,k-i))) 9 | return mini+1 10 | 11 | 12 | -------------------------------------------------------------------------------- /Day 4/2 Sum problem.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums: List[int], target: int) -> List[int]: 3 | d={} 4 | for i in range(len(nums)): 5 | if nums[i] in d: 6 | return [d[nums[i]],i] 7 | else: 8 | d[target-nums[i]]=i 9 | -------------------------------------------------------------------------------- /Day15/Reverse Words in a String.py: -------------------------------------------------------------------------------- 1 | def reverse(word): 2 | i,j=0,len(word)-1 3 | while inums[j]: 7 | dp[i]=max(dp[i],dp[j]+nums[i]) 8 | return max(dp) 9 | -------------------------------------------------------------------------------- /Day 7/Max consecutive ones.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMaxConsecutiveOnes(self, nums: List[int]) -> int: 3 | s,m=0,0 4 | for i in nums: 5 | if i==0: 6 | s=0 7 | else: 8 | s+=1 9 | m=max(s,m) 10 | return m 11 | -------------------------------------------------------------------------------- /Day 3/Majority Element N2.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def majorityElement(self, nums): 3 | x,count=nums[0],0 4 | for i in nums: 5 | if x==i: 6 | count+=1 7 | elif count==0: 8 | x,count=i,1 9 | else: 10 | count-=1 11 | return x 12 | -------------------------------------------------------------------------------- /Day 5/Reverse a LinkedList.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseList(self, head: ListNode) -> ListNode: 3 | prev = None 4 | curr = head 5 | while curr is not None: 6 | head = curr.next 7 | curr.next = prev 8 | prev = curr 9 | curr = head 10 | return prev 11 | -------------------------------------------------------------------------------- /Day18/Check if two trees are identical or not.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: 3 | if not p and not q: 4 | return True 5 | if p and q: 6 | return p.val==q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right) 7 | return False 8 | -------------------------------------------------------------------------------- /Day 5/Find middle of LinkedList.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def middleNode(self, head: ListNode) -> ListNode: 3 | if head.next==None: 4 | return head 5 | slow=head 6 | fast=head 7 | while fast and fast.next: 8 | slow=slow.next 9 | fast=fast.next.next 10 | return slow 11 | -------------------------------------------------------------------------------- /Day 8/Minimum number of platforms required for a railway.py: -------------------------------------------------------------------------------- 1 | def check(a,d,n): 2 | a.sort() 3 | d.sort() 4 | i,j,m,p=0,0,0,1 5 | while i bool: 3 | fast=head 4 | slow=head 5 | while fast and fast.next: 6 | fast=fast.next.next 7 | slow=slow.next 8 | if fast==slow: 9 | return True 10 | else: 11 | return False 12 | -------------------------------------------------------------------------------- /Day23/BFS.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict as df 2 | def bfs(d,x): 3 | visited,q=set(),[] 4 | q.append(x) 5 | visited.add(x) 6 | while q: 7 | s=q.pop(0) 8 | print(s,end=' ') 9 | for i in d[s]: 10 | if i not in visited: 11 | q.append(i) 12 | visited.add(i) 13 | print() 14 | 15 | -------------------------------------------------------------------------------- /Day19/Symmetric Binary Tree.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isSymmetric(self, root: TreeNode) -> bool: 3 | def check(a,b): 4 | if not a and not b: 5 | return True 6 | if a and b and a.val==b.val: 7 | return (check(a.left,b.right) and check(a.right,b.left)) 8 | return False 9 | return check(root,root) 10 | -------------------------------------------------------------------------------- /Day20/Search given Key in BST.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def searchBST(self, root: TreeNode, val: int) -> TreeNode: 3 | if not root: 4 | return 5 | elif root.val==val: 6 | return root 7 | elif root.val str: 3 | k-=1 4 | out='' 5 | num=[i for i in range(1,n+1)] 6 | while n: 7 | n-=1 8 | q,k=divmod(k,math.factorial(n)) 9 | out+=str(num[q]) 10 | num.remove(num[q]) 11 | return out 12 | 13 | 14 | -------------------------------------------------------------------------------- /Day 4/Count number of subarrays with given XOR.py: -------------------------------------------------------------------------------- 1 | def total(nums,x): 2 | n=len(nums) 3 | count=0 4 | xor=0 5 | d={} 6 | for i in range(n): 7 | xor^=nums[i] 8 | if xor^x in d: 9 | count+=d[xor^x] 10 | if xor==x: 11 | count+=1 12 | if xor in d: 13 | d[xor]+=1 14 | else: 15 | d[xor]=1 16 | return count 17 | -------------------------------------------------------------------------------- /Day 9/Subset-II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: 3 | def bfs(k,l,path,out): 4 | out.append(path) 5 | for i in range(k,len(l)): 6 | if i>0 and l[i]==l[i-1]: 7 | continue 8 | bfs(i+1,l,path+[l[i]],out) 9 | out=[] 10 | bfs(0,nums,[],out) 11 | return out 12 | -------------------------------------------------------------------------------- /Day 7/Remove Duplicate from Sorted array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, nums: List[int]) -> int: 3 | n=len(nums) 4 | if n==0: 5 | return 0 6 | j=0 7 | for i in range(1,n): 8 | if nums[i]!=nums[j]: 9 | j+=1 10 | nums[i],nums[j]=nums[j],nums[i] 11 | return j+1 12 | 13 | 14 | -------------------------------------------------------------------------------- /Day18/Height of a Binary Tree.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxDepth(self, root: TreeNode) -> int: 3 | def height(root): 4 | if not root: 5 | return 0 6 | left=height(root.left) 7 | right=height(root.right) 8 | if left>right: 9 | return left+1 10 | else: 11 | return right+1 12 | return height(root) 13 | -------------------------------------------------------------------------------- /Day25/Longest Increasing Subsequence.py: -------------------------------------------------------------------------------- 1 | #https://leetcode.com/problems/longest-increasing-subsequence/submissions/ 2 | 3 | class Solution: 4 | def lengthOfLIS(self, nums: List[int]) -> int: 5 | n=len(nums) 6 | dp=[1]*n 7 | for i in range(1,n): 8 | for j in range(0,i): 9 | if nums[i]>nums[j]: 10 | dp[i]=max(dp[i],dp[j]+1) 11 | return max(dp) 12 | -------------------------------------------------------------------------------- /Day19/Flatten Binary Tree to LinkedList.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def flatten(self, root: TreeNode) -> None: 3 | curr=root 4 | while curr: 5 | if curr.left: 6 | p=curr.left 7 | while p.right: 8 | p=p.right 9 | p.right=curr.right 10 | curr.right=curr.left 11 | curr.left=None 12 | curr=curr.right 13 | -------------------------------------------------------------------------------- /Day 2/Rotate Matrix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, matrix: List[List[int]]) -> None: 3 | n=len(matrix) 4 | for i in range(n): 5 | for j in range(i,n): 6 | matrix[i][j],matrix[j][i]=matrix[j][i],matrix[i][j] 7 | for i in range(n//2): 8 | for j in range(n): 9 | matrix[j][i],matrix[j][n-i-1]=matrix[j][n-i-1],matrix[j][i] 10 | 11 | 12 | -------------------------------------------------------------------------------- /Day 4/Longest substring without repeat.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict as df 2 | class Solution: 3 | def lengthOfLongestSubstring(self, s: str) -> int: 4 | n=len(s) 5 | m=0 6 | l=0 7 | d={} 8 | for i in range(n): 9 | if s[i] in d and d[s[i]]>=l: 10 | l=d[s[i]]+1 11 | else: 12 | m=max(m,i-l+1) 13 | d[s[i]]=i 14 | return m 15 | -------------------------------------------------------------------------------- /Day20/Check is a BT is BST or not.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isValidBST(self, root: Optional[TreeNode]) -> bool: 3 | def isbst(root,maxi,mini): 4 | if not root: 5 | return True 6 | if root.valmaxi: 7 | return False 8 | return isbst(root.left,root.val-1,mini) and isbst(root.right,maxi,root.val+1) 9 | return isbst(root,float('inf'),float('-inf')) 10 | -------------------------------------------------------------------------------- /Day25/Max Product Subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProduct(self, nums: List[int]) -> int: 3 | ma=nums[0] 4 | mi=nums[0] 5 | ans=nums[0] 6 | for i in range(1,len(nums)): 7 | choice1=ma*nums[i] 8 | choice2=mi*nums[i] 9 | ma=max(choice1,choice2,nums[i]) 10 | mi=min(choice1,choice2,nums[i]) 11 | ans=max(ans,ma) 12 | return ans 13 | 14 | -------------------------------------------------------------------------------- /Day18/Diameter of Binary Tree.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def diameterOfBinaryTree(self, root: TreeNode) -> int: 3 | def height(root, ans): 4 | if not root: 5 | return 0 6 | left=height(root.left,ans) 7 | right=height(root.right,ans) 8 | ans[0]=max(ans[0],left+right) 9 | return 1+max(left,right) 10 | ans=[0] 11 | height(root,ans) 12 | return ans[0] 13 | -------------------------------------------------------------------------------- /Day20/Find LCA of two nodes in BST.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': 3 | if root is None: 4 | return None 5 | if root.val>p.val and root.val>q.val: 6 | return self.lowestCommonAncestor(root.left,p,q) 7 | if root.val int: 3 | def pathsum(root,ans): 4 | if not root: 5 | return 0 6 | l=pathsum(root.left,ans) 7 | r=pathsum(root.right,ans) 8 | m=max(max(l,r)+root.val,root.val) 9 | ans[0]=max(m,ans[0],l+r+root.val) 10 | return m 11 | ans=[-1000000] 12 | pathsum(root,ans) 13 | return ans[0] 14 | -------------------------------------------------------------------------------- /Day 8/N meeting in one room.py: -------------------------------------------------------------------------------- 1 | def meeting(nums): 2 | out=[] 3 | for i in range(len(nums)): 4 | out.append(nums[i]+[i+1]) 5 | out.sort(key=lambda x:x[1]) 6 | res=[out[0][-1]] 7 | curr=out[0][-2] 8 | for i in range(1,len(out)): 9 | if curr bool: 3 | def height(root,count): 4 | if not root: 5 | return 0 6 | left=height(root.left,count) 7 | right=height(root.right,count) 8 | if abs(left-right)>1: 9 | count[0]=0 10 | return 1+max(left,right) 11 | count=[1] 12 | height(root,count) 13 | return count[0] 14 | -------------------------------------------------------------------------------- /Day 1/Sort an array of 0’s 1’s 2’s without using extra space or sorting algo.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortColors(self, nums: List[int]) -> None: 3 | i,m,j=0,0,len(nums)-1 4 | while m<=j: 5 | if nums[m]==0: 6 | nums[m],nums[i]=nums[i],nums[m] 7 | m+=1 8 | i+=1 9 | elif nums[m]==1: 10 | m+=1 11 | elif nums[m]==2: 12 | nums[m],nums[j]=nums[j],nums[m] 13 | j-=1 14 | -------------------------------------------------------------------------------- /Day 5/Merge two sorted Linked List.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: 3 | if l1==None and l2==None: 4 | return 5 | if l1==None: 6 | return l2 7 | if l2==None and l1: 8 | return l1 9 | if l1.valitem[i][0]: 10 | tot-=item[i][0] 11 | sum+=item[i][1] 12 | else: 13 | sum+=((tot/item[i][0])*item[i][1]) 14 | break 15 | print(sum) 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Day 9/Combination sum-1.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: 3 | def dfs(l,target,out,curr): 4 | if target<0: 5 | return 6 | if target==0: 7 | out.append(curr) 8 | return 9 | for i in range(len(l)): 10 | dfs(l[i:],target-l[i],out,curr+[l[i]]) 11 | out=[] 12 | l=candidates 13 | dfs(l,target,out,[]) 14 | return out 15 | -------------------------------------------------------------------------------- /Day 8/Activity Selection (it is same as N meeting in one room).py: -------------------------------------------------------------------------------- 1 | 2 | def meeting(nums): 3 | out=[] 4 | for i in range(len(nums)): 5 | out.append(nums[i]+[i+1]) 6 | out.sort(key=lambda x:x[1]) 7 | res=[out[0][-1]] 8 | curr=out[0][-2] 9 | for i in range(1,len(out)): 10 | if curr List[List[str]]: 3 | path=[] 4 | result=[] 5 | self.dfs(path,result,s) 6 | return result 7 | def dfs(self,path,result,s): 8 | if not s: 9 | result.append(path) 10 | return 11 | for i in range(1,len(s)+1): 12 | if self.palindrome(s[:i]): 13 | self.dfs(path+[s[:i]],result,s[i:]) 14 | def palindrome(self,s): 15 | return s[::-1]==s 16 | -------------------------------------------------------------------------------- /Day 1/Repeat and Missing Number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def missing(self, nums): 3 | n=len(nums) 4 | x=0 5 | for i in range(n): 6 | x=x^nums[i]^(i+1) 7 | a,b=0,0 8 | x=x& ~(x-1) 9 | for i in range(n): 10 | if x&nums[i]: 11 | a=a^nums[i] 12 | else: 13 | b=b^nums[i] 14 | if x&(i+1): 15 | a=a^(i+1) 16 | else: 17 | b=b^(i+1) 18 | print(a,b) 19 | 20 | -------------------------------------------------------------------------------- /Day 1/Merge Overlapping Subintervals.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def merge(self, intervals: List[List[int]]) -> List[List[int]]: 3 | n=len(intervals) 4 | intervals.sort() 5 | out=intervals[0] 6 | s=[] 7 | for i in range(n): 8 | temp=intervals[i] 9 | if temp[0]<=out[1]: 10 | out[1]=max(temp[1],out[1]) 11 | else: 12 | s.append(out) 13 | out=temp 14 | s.append(out) 15 | return s 16 | 17 | 18 | -------------------------------------------------------------------------------- /Day 8/Greedy algorithm to find minimum number of coins.py: -------------------------------------------------------------------------------- 1 | #Copied from gfg 2 | 3 | 4 | def findMin(V): 5 | deno = [1, 2, 5, 10, 20, 50, 6 | 100, 500, 1000] 7 | n = len(deno) 8 | ans = [] 9 | i = n - 1 10 | while(i >= 0): 11 | while (V >= deno[i]): 12 | V -= deno[i] 13 | ans.append(deno[i]) 14 | 15 | i -= 1 16 | 17 | for i in range(len(ans)): 18 | print(ans[i], end = " ") 19 | 20 | if __name__ == '__main__': 21 | n = 93 22 | print("Following is minimal number", 23 | "of change for", n, ": ", end = "") 24 | findMin(n) 25 | 26 | -------------------------------------------------------------------------------- /Day11/N-th root of an integer (use binary search) (square root, cube root, ..).py: -------------------------------------------------------------------------------- 1 | def power(x,n): 2 | if x>=0 and x<1: 3 | low=x 4 | high=1 5 | else: 6 | low=1 7 | high=x 8 | correction=10**(-6) 9 | mid=(low+high)/2 10 | 11 | while abs(mid**n-x)>=correction: 12 | if mid**n>x: 13 | high=mid 14 | else: 15 | low=mid 16 | mid=(high+low)/2 17 | print(mid) 18 | for i in range(int(input())): 19 | n,m=map(float,input().split()) 20 | power(n,m) 21 | -------------------------------------------------------------------------------- /Day15/Longest Common Prefix.py: -------------------------------------------------------------------------------- 1 | #https://leetcode.com/problems/longest-common-prefix/ 2 | class Solution: 3 | def longestCommonPrefix(self, strs: List[str]) -> str: 4 | short=min(strs,key=len) 5 | c=0 6 | if short=='': 7 | return '' 8 | for i,j in enumerate(short): 9 | for k in strs: 10 | if k[i]!=j: 11 | c=1 12 | break 13 | if c==1: 14 | break 15 | if not(c): 16 | i+=1 17 | return short[:i] 18 | -------------------------------------------------------------------------------- /Day26/Subset Sum.py: -------------------------------------------------------------------------------- 1 | def subset(l,s,n): 2 | if s==0: 3 | return True 4 | if n==0: 5 | return False 6 | return subset(l,s-l[n-1],n-1) or subset(l,s,n-1) 7 | 8 | 9 | 10 | def check(subset,amount): 11 | dp=[True]+[0]*amount 12 | print(dp) 13 | for i in subset: 14 | for j in range(amount,i-1,-1): 15 | if j-i>=0: 16 | dp[j]=dp[j-i] or dp[j] 17 | return dp[-1] 18 | 19 | 20 | l=list(map(int,input().split())) 21 | sum=int(input()) 22 | print(subset(l,sum,len(l))) 23 | print(check(l,sum)) 24 | -------------------------------------------------------------------------------- /Day 6/Check if a LinkedList is palindrome or not.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, head: ListNode) -> bool: 3 | if not head: 4 | return True 5 | 6 | rev = None 7 | slow = fast = head 8 | while fast and fast.next: 9 | fast = fast.next.next 10 | rev, rev.next, slow = slow, rev, slow.next 11 | if fast: 12 | slow = slow.next 13 | 14 | while rev and rev.val == slow.val: 15 | rev, slow = rev.next, slow.next 16 | return not rev 17 | -------------------------------------------------------------------------------- /Day 5/Remove N-th node from back of LinkedList.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: 3 | if head.next==None: 4 | return None 5 | temp=head 6 | while n: 7 | temp=temp.next 8 | n-=1 9 | current=head 10 | if temp==None: 11 | head=head.next 12 | return head 13 | while temp.next: 14 | current=current.next 15 | temp=temp.next 16 | current.next=current.next.next 17 | return head 18 | -------------------------------------------------------------------------------- /Day 6/Find the starting point of the Loop of LinkedList.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def detectCycle(self, head: ListNode) -> ListNode: 3 | if head==None or head.next==None: 4 | return 5 | slow=head 6 | fast=head 7 | while fast and fast.next: 8 | slow=slow.next 9 | fast=fast.next.next 10 | if slow==fast: 11 | break 12 | if slow!=fast: 13 | return 14 | slow=head 15 | while slow!=fast: 16 | slow=slow.next 17 | fast=fast.next 18 | return slow 19 | -------------------------------------------------------------------------------- /Day 6/Reverse a LinkedList in groups of size k.py: -------------------------------------------------------------------------------- 1 | # Recursion 2 | class Solution: 3 | def reverseKGroup(self, head: ListNode, k: int) -> ListNode: 4 | curr=head 5 | for i in range(k): 6 | if not curr: 7 | return head 8 | curr=curr.next 9 | prev=None 10 | current=head 11 | count=0 12 | for _ in range(k): 13 | next=current.next 14 | current.next=prev 15 | prev=current 16 | current=next 17 | head.next=self.reverseKGroup(next,k) 18 | return prev 19 | 20 | -------------------------------------------------------------------------------- /Day 9/Combination sum-2.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: 3 | def dfs(l,target,out,curr): 4 | if target<0: 5 | return 6 | if target==0: 7 | out.append(curr) 8 | return 9 | for i in range(1,len(l)+1): 10 | if i>1 and l[i-1]==l[i-2]: 11 | continue 12 | dfs(l[i:],target-l[i-1],out,curr+[l[i-1]]) 13 | out=[] 14 | l=candidates 15 | l.sort() 16 | dfs(l,target,out,[]) 17 | return out 18 | -------------------------------------------------------------------------------- /Day 2/Next Permutation.py: -------------------------------------------------------------------------------- 1 | def permutation(nums): 2 | n=len(nums) 3 | i=n-1 4 | while i>0: 5 | if nums[i]>nums[i-1]: 6 | break 7 | i-=1 8 | if i==0: 9 | nums.reverse() 10 | else: 11 | prev=i-1 12 | j=n-1 13 | while nums[prev]>=nums[j]: 14 | j-=1 15 | nums[prev],nums[j]=nums[j],nums[prev] 16 | prev=i 17 | j=n-1 18 | while prev float: 4 | if n==0: 5 | return 1 6 | if n<0: 7 | n=-n 8 | x=1/x 9 | if n%2: 10 | return x*self.myPow(x*x,n//2) 11 | else: 12 | return self.myPow(x*x,n//2) 13 | 14 | #Iteration 15 | class Solution: 16 | def myPow(self, x: float, n: int) -> float: 17 | if n==0: 18 | return 1 19 | if n<0: 20 | n=-n 21 | x=1/x 22 | res=1 23 | while n: 24 | if n&1: 25 | res=res*x 26 | n=n>>1 27 | x=x*x 28 | return res 29 | 30 | -------------------------------------------------------------------------------- /Day25/0-1 Knapsack.py: -------------------------------------------------------------------------------- 1 | #O(2**n) 2 | def knapsack(am,wt,val,n): 3 | if n==0 or am==0: 4 | return 0 5 | if am=wt[i-1]: 18 | dp[i][j]=max(val[i-1]+dp[i-1][j-wt[i-1]],dp[i-1][j]) 19 | else: 20 | dp[i][j]=dp[i-1][j] 21 | return dp[n][am] 22 | 23 | -------------------------------------------------------------------------------- /Day 2/Pascal Triangle.py: -------------------------------------------------------------------------------- 1 | #O(n2) 2 | class Solution: 3 | def generate(self, numRows: int) -> List[List[int]]: 4 | out=[[1],[1,1]] 5 | if numRows==1 or numRows==2: 6 | return out[:numRows] 7 | for i in range(numRows-2): 8 | temp=[1] 9 | l=out[i+1] 10 | for j in range(i+1): 11 | temp.append(l[j]+l[j+1]) 12 | temp.append(1) 13 | out.append(temp) 14 | return out 15 | 16 | 17 | #O(n) 18 | def generateNthRow (N): 19 | prev = 1 20 | print(prev, end = '') 21 | 22 | for i in range(1, N + 1): 23 | curr = (prev * (N - i + 1)) // i 24 | print(",", curr, end = '') 25 | prev = curr 26 | -------------------------------------------------------------------------------- /Day 6/Rotate a LinkedList.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotateRight(self, head: ListNode, k: int) -> ListNode: 3 | if head==None: 4 | return None 5 | temp1=temp2=head 6 | count=0 7 | prev=None 8 | while temp1: 9 | count+=1 10 | prev=temp1 11 | temp1=temp1.next 12 | k=k%count 13 | while k: 14 | k-=1 15 | temp2=temp2.next 16 | temp1=head 17 | if temp2==None: 18 | return 19 | while temp2.next: 20 | temp2=temp2.next 21 | temp1=temp1.next 22 | prev.next=head 23 | head=temp1.next 24 | temp1.next=None 25 | return head 26 | 27 | -------------------------------------------------------------------------------- /Day26/Maximum sum path in matrix, (count paths, and similar type do, also backtrack to find the maximum path).py: -------------------------------------------------------------------------------- 1 | def maxpath(mat): 2 | n=len(mat) 3 | m=len(mat[0]) 4 | for i in range(n-1,0,-1): 5 | for j in range(m): 6 | if j>0 and j1: 9 | mat[i-1][j]=mat[i-1][j]+max(mat[i][j],mat[i][j-1]) 10 | else: 11 | mat[i-1][j]=mat[i-1][j]+mat[i][j] 12 | mi=0 13 | for i in range(m): 14 | mi=max(mat[0][i],mi) 15 | print(mi) 16 | 17 | mat=[] 18 | for i in range(int(input())): 19 | mat.append(list(map(int,input().split()))) 20 | maxpath(mat) 21 | 22 | -------------------------------------------------------------------------------- /Day26/Rod Cutting.py: -------------------------------------------------------------------------------- 1 | # O(2**n) 2 | def rod(price,n): 3 | if n<=0: 4 | return 0 5 | max_val=-1 6 | for i in range(n): 7 | max_val=max(max_val,price[i]+rod(price,n-i-1)) 8 | return max_val 9 | 10 | 11 | def rodd(price,n,val): 12 | if n<=0: 13 | return 0 14 | if val[n]: 15 | return val[n] 16 | max_val=-1 17 | for i in range(n): 18 | max_val=max(max_val,price[i]+rodd(price,n-i-1,val)) 19 | val[n]=max_val 20 | return max_val 21 | 22 | 23 | # O(n**2) 24 | def cutrod(price,n): 25 | val=[0]*(n+1) 26 | for i in range(1,n+1): 27 | maxi=-1 28 | for j in range(i): 29 | maxi=max(maxi,price[j]+val[i-j-1]) 30 | val[i]=maxi 31 | return val[n] 32 | 33 | 34 | -------------------------------------------------------------------------------- /Day19/Check if Binary Tree is mirror of itself or not.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self,data): 3 | self.left=None 4 | self.right=None 5 | self.val=data 6 | 7 | def checkmirror(root1,root2): 8 | if not root1 and not root2: 9 | return True 10 | if not root1 or not root2: 11 | return False 12 | if root1.val==root2.val: 13 | return (checkmirror(root1.left,root2.right) and checkmirror(root1.left,root2.right)) 14 | 15 | root1 = Node(1) 16 | root2 = Node(1) 17 | 18 | root1.left = Node(2) 19 | root1.right = Node(3) 20 | root1.left.left = Node(4) 21 | root1.left.right = Node(5) 22 | 23 | root2.left = Node(3) 24 | root2.right = Node(2) 25 | root2.right.left = Node(5) 26 | root2.right.right = Node(4) 27 | print(checkmirror(root1,root2)) 28 | -------------------------------------------------------------------------------- /Day 1/Find the duplicate in an array of N+1 integers.py: -------------------------------------------------------------------------------- 1 | #Cycle method 2 | class Solution: 3 | def findDuplicate(self, nums: List[int]) -> int: 4 | x=nums[0] 5 | y=nums[0] 6 | while(True): 7 | x=nums[x] 8 | y=nums[nums[y]] 9 | print(x,y) 10 | if x==y: 11 | break 12 | x=nums[0] 13 | while x!=y: 14 | x=nums[x] 15 | y=nums[y] 16 | else: 17 | return x 18 | 19 | #Visited array 20 | class Solution: 21 | def findDuplicate(self, nums: List[int]) -> int: 22 | for i in range(len(nums)): 23 | c=abs(nums[i]) 24 | if nums[c]>=0: 25 | nums[c]=-nums[c] 26 | else: 27 | return abs(nums[i]) 28 | 29 | -------------------------------------------------------------------------------- /Day 5/Add two numbers as LinkedList.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addTwoNumbers(self, root1: ListNode, root2: ListNode) -> ListNode: 3 | first=root1 4 | second=root2 5 | carry=0 6 | root=temp=node(0) 7 | while first or second: 8 | v1,v2=0,0 9 | if first: 10 | v1=first.data 11 | first=first.next 12 | if second: 13 | v2=second.data 14 | second=second.next 15 | sum1=v1+v2+carry 16 | if sum1>=10: 17 | carry=1 18 | sum1%=10 19 | else: 20 | carry=0 21 | temp.next=node(sum1) 22 | temp=temp.next 23 | if carry: 24 | temp.next=node(carry) 25 | return root.next 26 | -------------------------------------------------------------------------------- /Day26/Coin change.py: -------------------------------------------------------------------------------- 1 | #https://leetcode.com/problems/coin-change/submissions/ 2 | class Solution: 3 | def coinChange(self, coins: List[int], amount: int) -> int: 4 | inf=float('inf') 5 | dp=[0]+[inf]*amount 6 | for i in range(1,amount+1): 7 | for j in coins: 8 | if i-j>=0: 9 | dp[i]=min(dp[i-j],dp[i]) 10 | dp[i]+=1 11 | if dp[-1]!=inf: 12 | return dp[-1] 13 | return -1 14 | 15 | #https://leetcode.com/problems/coin-change-2/submissions/ 16 | def count(coins,amount): 17 | dp=[1]+[0]*amount 18 | print(dp) 19 | for i in coins: 20 | for j in range(1,amount+1): 21 | if j-i>=0: 22 | dp[j]+=dp[j-i] 23 | print(dp) 24 | return dp[-1] 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Day 3/Majority Element N3.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def majorityElement(self, nums: List[int]) -> List[int]: 3 | count1,count2,x,y=0,0,0,1 4 | for i in nums: 5 | if x==i: 6 | count1+=1 7 | elif y==i: 8 | count2+=1 9 | elif count1==0: 10 | count1,x=1,i 11 | elif count2==0: 12 | count2,y=1,i 13 | else: 14 | count1-=1 15 | count2-=1 16 | c1=c2=0 17 | for i in nums: 18 | if i==x: 19 | c1+=1 20 | elif i==y: 21 | c2+=1 22 | out=[] 23 | n=len(nums) 24 | if c1>n/3: 25 | out.append(x) 26 | if c2>n/3: 27 | out.append(y) 28 | return out 29 | -------------------------------------------------------------------------------- /Day25/Edit Distance.py: -------------------------------------------------------------------------------- 1 | #O(3**n) 2 | def edit(s1,s2,m,n): 3 | if m==0: 4 | return n 5 | if n==0: 6 | return m 7 | if s1[m-1]==s2[n-1]: 8 | return edit(s1,s2,m-1,n-1) 9 | return 1+min(edit(s1,s2,m,n-1),edit(s1,s2,m-1,n-1),edit(s1,s2,m-1,n)) 10 | 11 | 12 | 13 | #O(n*m) 14 | def editt(s1,s2,m,n): 15 | if m==0: 16 | return n 17 | if n==0: 18 | return m 19 | dp=[[0]*(n+1) for i in range(m+1)] 20 | for i in range(m+1): 21 | for j in range(n+1): 22 | if i==0: 23 | dp[i][j]=j 24 | elif j==0: 25 | dp[i][j]=i 26 | elif s1[i-1]==s2[j-1]: 27 | dp[i][j]=dp[i-1][j-1] 28 | else: 29 | dp[i][j]=1+min(dp[i][j-1],dp[i-1][j-1],dp[i-1][j]) 30 | return dp[m][n] 31 | -------------------------------------------------------------------------------- /Day 4/4 Sum problem.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fourSum(self, nums: List[int], target: int) -> List[List[int]]: 3 | d={} 4 | n=len(nums) 5 | for i in range(n-1): 6 | for j in range(i+1,n): 7 | if nums[i]+nums[j] in d: 8 | d[nums[i]+nums[j]].append((i,j)) 9 | else: 10 | d[nums[i]+nums[j]]=[(i,j)] 11 | out=set() 12 | for x in d: 13 | y=target-x 14 | if y in d: 15 | l1=d[x] 16 | l2=d[y] 17 | for i,j in l1: 18 | for k,l in l2: 19 | if i!=k and i!=l and j!=k and j!=l: 20 | out.add(tuple(sorted([nums[i],nums[j],nums[k],nums[l]]))) 21 | return out 22 | 23 | 24 | -------------------------------------------------------------------------------- /Day 9/Subset Sums.py: -------------------------------------------------------------------------------- 1 | # DP 2 | class Solution: 3 | def isSubsetSum (self, N, arr, sum): 4 | check=[[0 for i in range(sum+1)] for j in range(N+1)] 5 | for i in range(N+1): 6 | check[i][0]=1 7 | for i in range(1,sum+1): 8 | check[0][i]=0 9 | for i in range(1,N+1): 10 | for j in range(1,sum+1): 11 | if j int: 4 | d={} 5 | m=0 6 | for i in nums: 7 | d[i]=1 8 | for i in nums: 9 | if i-1 not in d: 10 | j=i 11 | while j in d: 12 | j+=1 13 | m=max(j-i,m) 14 | return m 15 | 16 | #O(n*log(n)) 17 | class Solution: 18 | def longestConsecutive(self, nums: List[int]) -> int: 19 | if not nums: 20 | return 0 21 | nums=list(set(nums)) 22 | nums.sort() 23 | 24 | m=1 25 | c=0 26 | for i in range(1,len(nums)): 27 | if nums[i-1]+1==nums[i]: 28 | m+=1 29 | else: 30 | c=max(c,m) 31 | m=1 32 | c=max(c,m) 33 | return c 34 | -------------------------------------------------------------------------------- /Day10/Print all Permutations of a array.py: -------------------------------------------------------------------------------- 1 | #Unique elements 2 | class Solution: 3 | def permute(self, nums: List[int]) -> List[List[int]]: 4 | def permute(s,answer,c): 5 | if len(s)==0: 6 | answer.append(c) 7 | return 8 | for i in range(len(s)): 9 | permute(s[:i]+s[i+1:],answer,c+[s[i]]) 10 | answer=[] 11 | permute(nums,answer,[]) 12 | return answer 13 | 14 | 15 | #Not unique elements 16 | class Solution: 17 | def permuteUnique(self, nums: List[int]) -> List[List[int]]: 18 | def permute(s,answer,c): 19 | if len(s)==0: 20 | answer.append(c) 21 | return 22 | for i in range(len(s)): 23 | if i>0 and s[i]==s[i-1]: 24 | continue 25 | permute(s[:i]+s[i+1:],answer,c+[s[i]]) 26 | answer=[] 27 | nums.sort() 28 | permute(nums,answer,[]) 29 | return answer 30 | -------------------------------------------------------------------------------- /Day25/Longest Common Subsequence.py: -------------------------------------------------------------------------------- 1 | #O(2**(n+m)) 2 | def lcs(s1,s2,m,n): 3 | if m==0 or n==0: 4 | return 0 5 | if s1[m-1]==s2[n-1]: 6 | return 1+lcs(s1,s2,m-1,n-1) 7 | return max(lcs(s1,s2,m,n-1),lcs(s1,s2,m-1,n)) 8 | 9 | 10 | #O(n*m) 11 | def lcss(s1,s2): 12 | m=len(s1) 13 | n=len(s2) 14 | l=[[0]*(n+1) for i in range(m+1)] 15 | for i in range(m+1): 16 | for j in range(n+1): 17 | if i==0 or j==0: 18 | l[i][j]=0 19 | elif s1[i-1]==s2[j-1]: 20 | l[i][j]=l[i-1][j-1]+1 21 | else: 22 | l[i][j]=max(l[i-1][j],l[i][j-1]) 23 | x= l[m][n] 24 | print(x) 25 | res=['']*(x) 26 | i=m 27 | j=n 28 | while i>0 and j>0: 29 | if s1[i-1]==s2[j-1]: 30 | res[x-1]=s1[i-1] 31 | i-=1 32 | j-=1 33 | x-=1 34 | elif l[i-1][j]>l[i][j-1]: 35 | i-=1 36 | else: 37 | j-=1 38 | print(''.join(res)) 39 | 40 | -------------------------------------------------------------------------------- /Day18/Level order Traversal or Level order traversal in spiral form.py: -------------------------------------------------------------------------------- 1 | class newNode: 2 | def __init__(self,data): 3 | self.left=None 4 | self.right=None 5 | self.data=data 6 | 7 | def levelspiral(root): 8 | if not root: 9 | return root 10 | curr=[root] 11 | next=[] 12 | temp=False 13 | while curr: 14 | x=curr.pop(-1) 15 | print(x.data,end=' ') 16 | if temp: 17 | if x.left: 18 | next.append(x.left) 19 | if x.right: 20 | next.append(x.right) 21 | else: 22 | if x.right: 23 | next.append(x.right) 24 | if x.left: 25 | next.append(x.left) 26 | if not curr: 27 | temp=not temp 28 | next,curr=curr,next 29 | 30 | root = newNode(1) 31 | root.left = newNode(2) 32 | root.right = newNode(3) 33 | root.left.left = newNode(7) 34 | root.left.right = newNode(6) 35 | root.right.left = newNode(5) 36 | root.right.right = newNode(4) 37 | levelspiral(root) 38 | 39 | 40 | -------------------------------------------------------------------------------- /Day 7/3 sum.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict as df 2 | class Solution: 3 | def threeSum(self, nums: List[int]) -> List[List[int]]: 4 | nums.sort() 5 | n=len(nums) 6 | out=[] 7 | for i in range(n-2): 8 | if i==0 or nums[i]!=nums[i-1]: 9 | low=i+1 10 | high=n-1 11 | sum=-nums[i] 12 | while lowsum: 22 | high-=1 23 | else: 24 | low+=1 25 | return out 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Day15/Rabin Karp.py: -------------------------------------------------------------------------------- 1 | def asci(s): 2 | return ord(s)-ord('a')+1 3 | def check(s,t): 4 | m=len(s) 5 | for i in range(m): 6 | if s[i]!=t[i]: 7 | return 0 8 | return 1 9 | def search(s,t): 10 | x=100 11 | #base=10**10 12 | if s=='' or t=='': 13 | return -1 14 | m=len(t) 15 | n=len(s) 16 | target=0 17 | current=0 18 | for i in range(m): 19 | #target=(target*x+asci(t[i]))%base 20 | #current=(current*x+asci(s[i]))%base 21 | #print(target) 22 | target=(target*x+asci(t[i])) 23 | current=(current*x+asci(s[i])) 24 | for i in range(n-m): 25 | #print(current,target) 26 | if target==current: 27 | if check(s[i:i+m],t): 28 | print('Pattern found at',i) 29 | current=current-((x**(m-1))*asci(s[i])) 30 | current=current*x+asci(s[i+m]) 31 | #print(current) 32 | #current=current-(x*m*asci(s[i]))+(x*m*asci(s[i+m-1])) 33 | if current==target: 34 | print('Pattern found at',i+1) 35 | #current=(current-(x*m*asci(s[i]))+(x*m*asci(s[i+m-1])))%base 36 | -------------------------------------------------------------------------------- /Day 2/Inversion of Array.py: -------------------------------------------------------------------------------- 1 | def merge(nums,temp,left,mid,right): 2 | i=left 3 | j=mid+1 4 | k=left 5 | invcount=0 6 | while i<=mid and j<=right: 7 | if nums[i]<=nums[j]: 8 | temp[k]=nums[i] 9 | i+=1 10 | k+=1 11 | 12 | else: 13 | temp[k]=nums[j] 14 | k+=1 15 | j+=1 16 | invcount+=(mid-i+1) 17 | while i<=mid: 18 | temp[k]=nums[i] 19 | k+=1 20 | i+=1 21 | while j<=right: 22 | temp[k]=nums[j] 23 | k+=1 24 | j+=1 25 | for i in range(left,right+1): 26 | nums[i]=temp[i] 27 | return invcount 28 | 29 | def checkmerge(nums,temp,left,right): 30 | invcount=0 31 | if right>left: 32 | mid=(right+left)//2 33 | invcount+=checkmerge(nums,temp,left,mid) 34 | invcount+=checkmerge(nums,temp,mid+1,right) 35 | invcount+=merge(nums,temp,left,mid,right) 36 | return invcount 37 | 38 | def countinversion(nums): 39 | n=len(nums) 40 | temp=[0]*n 41 | return checkmerge(nums,temp,0,n-1) 42 | 43 | -------------------------------------------------------------------------------- /Day 3/Search in a 2D matrix.py: -------------------------------------------------------------------------------- 1 | #O(nlog(m)) 2 | class Solution: 3 | def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: 4 | def binarysearch(l,i,j,x): 5 | if i<=j: 6 | mid=(i+j)//2 7 | if l[mid]>x: 8 | return binarysearch(l,i,mid-1,x) 9 | elif l[mid] bool: 25 | n=len(matrix) 26 | m=len(matrix[0]) 27 | i,j=0,m-1 28 | while i=0: 29 | if matrix[i][j]==target: 30 | return True 31 | elif matrix[i][j]>target: 32 | j-=1 33 | else: 34 | i+=1 35 | return False 36 | -------------------------------------------------------------------------------- /Day17/Top View of Binary Tree.py: -------------------------------------------------------------------------------- 1 | class node: 2 | def __init__(self,data): 3 | self.left=None 4 | self.right=None 5 | self.data=data 6 | 7 | def push(root,data): 8 | if root==None: 9 | root=node(data) 10 | return 11 | q=[root] 12 | while True: 13 | x=q.pop(0) 14 | if x.left==None: 15 | x.left=node(data) 16 | break 17 | else: 18 | q.append(x.left) 19 | if x.right==None: 20 | x.right=node(data) 21 | break 22 | else: 23 | q.append(x.right) 24 | 25 | def top(root): 26 | if not root: 27 | return root 28 | m={} 29 | q=[(root,0)] 30 | while q: 31 | x,v=q.pop(0) 32 | if v not in m: 33 | m[v]=x.data 34 | if x.left: 35 | q.append((x.left,v-1)) 36 | if x.right: 37 | q.append((x.right,v+1)) 38 | for i in sorted(m.keys()): 39 | print(m[i],end=' ') 40 | 41 | 42 | root = node(1) 43 | root.left = node(2) 44 | root.right = node(3) 45 | root.left.right = node(4) 46 | root.left.right.right = node(5) 47 | root.left.right.right.right = node(6) 48 | 49 | top(root) 50 | -------------------------------------------------------------------------------- /Day 7/Trapping rainwater.py: -------------------------------------------------------------------------------- 1 | #O(n) space 2 | class Solution: 3 | def trap(self, height: List[int]) -> int: 4 | n=len(height) 5 | su=[0]*n 6 | pre=[0]*n 7 | m=0 8 | for i in range(n): 9 | m=max(height[i],m) 10 | pre[i]=m 11 | m=0 12 | for i in range(n-1,-1,-1): 13 | m=max(height[i],m) 14 | su[i]=m 15 | count=0 16 | for i in range(n): 17 | count+=min(pre[i],su[i])-height[i] 18 | return count 19 | 20 | #O(1) space 21 | class Solution: 22 | def trap(self, height: List[int]) -> int: 23 | n=len(height) 24 | count,leftmax,rightmax,i,j=0,0,0,0,n-1 25 | while i<=j: 26 | if height[i]leftmax: 28 | leftmax=height[i] 29 | else: 30 | count+=leftmax-height[i] 31 | i+=1 32 | else: 33 | if height[j]>rightmax: 34 | rightmax=height[j] 35 | else: 36 | count+=rightmax-height[j] 37 | j-=1 38 | return count 39 | 40 | 41 | -------------------------------------------------------------------------------- /Day 6/Find intersection point of Y LinkedList.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: 3 | temp1=headA 4 | temp2=headB 5 | count1,count2=0,0 6 | while temp1: 7 | temp1=temp1.next 8 | count1+=1 9 | while temp2: 10 | temp2=temp2.next 11 | count2+=1 12 | diff=abs(count1-count2) 13 | if count1>count2: 14 | temp1=headA 15 | temp2=headB 16 | else: 17 | temp1=headB 18 | temp2=headA 19 | while diff: 20 | temp1=temp1.next 21 | diff-=1 22 | while temp1 and temp2 and temp1!=temp2: 23 | temp1=temp1.next 24 | temp2=temp2.next 25 | return temp1 26 | 27 | #Circular linked list 28 | class Solution: 29 | def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: 30 | 31 | linkA, linkB = headA, headB 32 | 33 | while ( linkA != linkB): 34 | 35 | linkA = headB if not linkA else linkA.next 36 | linkB = headA if not linkB else linkB.next 37 | return linkA 38 | -------------------------------------------------------------------------------- /Day 7/Clone a Linked List with random and next pointer.py: -------------------------------------------------------------------------------- 1 | #O(n) Space 2 | class Solution: 3 | def copyRandomList(self, head: 'Node') -> 'Node': 4 | d={} 5 | d[None]=None 6 | temp=head 7 | while temp: 8 | d[temp]=Node(temp.val) 9 | temp=temp.next 10 | temp=head 11 | while temp: 12 | d[temp].next=d[temp.next] 13 | d[temp].random=d[temp.random] 14 | temp=temp.next 15 | return d[head] 16 | 17 | #O(1) Space 18 | class Solution: 19 | def copyRandomList(self, head: 'Node') -> 'Node': 20 | temp=head 21 | while temp: 22 | next=temp.next 23 | temp.next=Node(temp.val) 24 | temp.next.next=next 25 | temp=temp.next.next 26 | temp=head 27 | while temp: 28 | if temp.random: 29 | temp.next.random=temp.random.next 30 | temp=temp.next.next 31 | dummy=Node(1) 32 | out=dummy 33 | temp=head 34 | while temp: 35 | out.next=temp.next 36 | temp.next=temp.next.next 37 | temp=temp.next 38 | out=out.next 39 | return dummy.next 40 | -------------------------------------------------------------------------------- /Day15/Longest Palindrome in a string.py: -------------------------------------------------------------------------------- 1 | #Brute Force 2 | def check(s): 3 | if s[::-1]==s: 4 | return True 5 | else: 6 | return False 7 | def palindrome(s): 8 | n=len(s) 9 | c=1 10 | out=s[0] 11 | for i in range(n,1,-1): 12 | for j in range(0,n-i+1): 13 | if check(s[j:j+i]): 14 | c=i 15 | out=s[j:j+i] 16 | break 17 | if c!=1: 18 | break 19 | print(c,out) 20 | 21 | #Dp 22 | class Solution: 23 | def longestPalindrome(self, s: str) -> str: 24 | n=len(s) 25 | l=[[0 for i in range(n)] for j in range(n)] 26 | a=s[0] 27 | c=1 28 | for i in range(n): 29 | l[i][i]=1 30 | for i in range(n-1): 31 | if s[i]==s[i+1]: 32 | if c<2: 33 | a=s[i:i+2] 34 | c=2 35 | l[i][i+1]=1 36 | for i in range(2,n): 37 | for j in range(n-i): 38 | if s[j]==s[j+i] and l[j+1][j+i-1]: 39 | if i+1>c: 40 | c=i+1 41 | a=s[j:j+i+1] 42 | l[j][j+i]=1 43 | return a 44 | 45 | # 46 | -------------------------------------------------------------------------------- /Day17/Bottom View of Binary Tree.py: -------------------------------------------------------------------------------- 1 | class node: 2 | def __init__(self,data): 3 | self.left=None 4 | self.right=None 5 | self.data=data 6 | 7 | def push(root,data): 8 | if root==None: 9 | root=node(data) 10 | return 11 | q=[root] 12 | while True: 13 | x=q.pop(0) 14 | if x.left==None: 15 | x.left=node(data) 16 | break 17 | else: 18 | q.append(x.left) 19 | if x.right==None: 20 | x.right=node(data) 21 | break 22 | else: 23 | q.append(x.right) 24 | 25 | def bottom(root): 26 | if not root: 27 | return root 28 | m={} 29 | q=[(root,0)] 30 | while q: 31 | x,v=q.pop(0) 32 | m[v]=x.data 33 | if x.left: 34 | q.append((x.left,v-1)) 35 | if x.right: 36 | q.append((x.right,v+1)) 37 | for i in sorted(m.keys()): 38 | print(m[i],end=' ') 39 | 40 | 41 | root = node(20) 42 | root.left = node(8) 43 | root.right = node(22) 44 | root.left.left = node(5) 45 | root.left.right = node(3) 46 | root.right.left = node(4) 47 | root.right.right = node(25) 48 | root.left.right.left = node(10) 49 | root.left.right.right = node(14) 50 | 51 | bottom(root) 52 | -------------------------------------------------------------------------------- /Day17/Preorder Traversal (with recursion and without recursion).py: -------------------------------------------------------------------------------- 1 | class node: 2 | def __init__(self,data): 3 | self.left=None 4 | self.right=None 5 | self.data=data 6 | 7 | def push(root,data): 8 | if root==None: 9 | root=node(data) 10 | return 11 | q=[root] 12 | while True: 13 | x=q.pop(0) 14 | if x.left==None: 15 | x.left=node(data) 16 | break 17 | else: 18 | q.append(x.left) 19 | if x.right==None: 20 | x.right=node(data) 21 | break 22 | else: 23 | q.append(x.right) 24 | 25 | #Recursion 26 | def preorderr(root): 27 | if root: 28 | print(root.data,end=' ') 29 | preorderr(root.left) 30 | preorderr(root.right) 31 | 32 | 33 | #Interative 34 | def preorder(root): 35 | temp=root 36 | stack=[] 37 | while True: 38 | if temp: 39 | print(temp.data,end=' ') 40 | stack.append(temp) 41 | temp=temp.left 42 | elif stack: 43 | temp=stack.pop() 44 | temp=temp.right 45 | else: 46 | break 47 | 48 | 49 | root=node(5) 50 | push(root,5) 51 | push(root,6) 52 | push(root,7) 53 | push(root,8) 54 | preorder(root) 55 | print() 56 | preorderr(root) 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Day 2/Set Matrix Zeros.py: -------------------------------------------------------------------------------- 1 | #O(n+m) space 2 | class Solution: 3 | def setZeroes(self, matrix: List[List[int]]) -> None: 4 | m=len(matrix[0]) 5 | n=len(matrix) 6 | row=[1]*n 7 | col=[1]*m 8 | for i in range(n): 9 | for j in range(m): 10 | if matrix[i][j]==0: 11 | row[i]=0 12 | col[j]=0 13 | for i in range(n): 14 | if row[i]==0: 15 | for j in range(m): 16 | matrix[i][j]=0 17 | for i in range(m): 18 | if col[i]==0: 19 | for j in range(n): 20 | matrix[j][i]=0 21 | 22 | #O(1) space 23 | class Solution: 24 | def setZeroes(self, matrix: List[List[int]]) -> None: 25 | m=len(matrix[0]) 26 | n=len(matrix) 27 | x=0 28 | for i in range(n): 29 | if matrix[i][0]==0: 30 | x=1 31 | for j in range(1,m): 32 | if matrix[i][j]==0: 33 | matrix[i][0]=0 34 | matrix[0][j]=0 35 | for i in range(n-1,-1,-1): 36 | for j in range(m-1,0,-1): 37 | if matrix[i][0]==0 or matrix[0][j]==0: 38 | matrix[i][j]=0 39 | if x: 40 | matrix[i][0]=0 41 | 42 | -------------------------------------------------------------------------------- /Day17/Inorder Traversal (with recursion and without recursion).py: -------------------------------------------------------------------------------- 1 | class node: 2 | def __init__(self,data): 3 | self.left=None 4 | self.right=None 5 | self.data=data 6 | 7 | def push(root,data): 8 | if root==None: 9 | root=node(data) 10 | return 11 | q=[root] 12 | while True: 13 | x=q.pop(0) 14 | if x.left==None: 15 | x.left=node(data) 16 | break 17 | else: 18 | q.append(x.left) 19 | if x.right==None: 20 | x.right=node(data) 21 | break 22 | else: 23 | q.append(x.right) 24 | 25 | #Recursion 26 | def inorderr(root): 27 | if root: 28 | inorderr(root.left) 29 | print(root.data,end=' ') 30 | inorderr(root.right) 31 | 32 | #Iterative 33 | def inorder(root): 34 | stack=[] 35 | temp=root 36 | while True: 37 | if temp: 38 | stack.append(temp) 39 | temp=temp.left 40 | elif stack: 41 | temp=stack.pop() 42 | print(temp.data,end=' ') 43 | temp=temp.right 44 | else: 45 | break 46 | 47 | 48 | 49 | root=node(5) 50 | push(root,5) 51 | push(root,6) 52 | push(root,7) 53 | push(root,8) 54 | inorder(root) 55 | print() 56 | inorderr(root) 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Day 3/Grid Unique Paths.py: -------------------------------------------------------------------------------- 1 | #By using dp 2 | class Solution: 3 | def uniquePaths(self, m: int, n: int) -> int: 4 | dp=[[-1 for i in range(n)] for i in range(m)] 5 | print(dp) 6 | def count(i,j,m,n): 7 | if i==m-1 and j==n-1: 8 | return 1 9 | if i>=m or j>=n: 10 | return 0 11 | if dp[i][j]!=-1: 12 | return dp[i][j] 13 | dp[i][j]=count(i+1,j,m,n)+count(i,j+1,m,n) 14 | return dp[i][j] 15 | 16 | return count(0,0,m,n) 17 | 18 | #Recursion 19 | class Solution: 20 | def uniquePaths(self, m: int, n: int) -> int: 21 | def count(i,j,m,n): 22 | if i==m-1 and j==n-1: 23 | return 1 24 | if i>=m or j>=n: 25 | return 0 26 | return count(i+1,j,m,n)+count(i,j+1,m,n) 27 | return count(0,0,m,n) 28 | # O(n*m) space 29 | class Solution 30 | def uniquePaths(self, m, n): 31 | aux = [[1 for x in range(n)] for x in range(m)] 32 | for i in range(1, m): 33 | for j in range(1, n): 34 | aux[i][j] = aux[i][j-1]+aux[i-1][j] 35 | return aux[-1][-1] 36 | # O(n) space 37 | def uniquePaths(self, m, n): 38 | if not m or not n: 39 | return 0 40 | cur = [1] * n 41 | for i in xrange(1, m): 42 | for j in xrange(1, n): 43 | cur[j] += cur[j-1] 44 | return cur[-1] 45 | -------------------------------------------------------------------------------- /Day 1/Merge two sorted Arrays without extra space.py: -------------------------------------------------------------------------------- 1 | # O(n*m) time, O(1) space 2 | class Solution: 3 | def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: 4 | if n==0: 5 | return 6 | i,j=0,0 7 | while inums2[j+1]): 11 | nums2[j],nums2[j+1]=nums2[j+1],nums2[j] 12 | j+=1 13 | j=0 14 | 15 | i+=1 16 | for i in range(n): 17 | nums1[m+i]=nums2[i] 18 | 19 | 20 | # O(log(n+m)) time, O(1) space 21 | import math 22 | def gapf(n): 23 | if n==1: 24 | return 0 25 | return math.ceil(n/2) 26 | def sort(l1,l2): 27 | n=len(l1) 28 | m=len(l2) 29 | gap=n+m 30 | gap=gapf(gap) 31 | while gap>0: 32 | i=0 33 | while i+gapl1[i+gap]: 35 | l1[i],l1[i+gap]=l1[i+gap],l1[i] 36 | i+=1 37 | j=gap-n if gap>n else 0 38 | while il2[j]: 40 | l1[i],l2[j]=l2[j],l1[i] 41 | i+=1 42 | j+=1 43 | if jl2[j+gap]: 47 | l2[j],l2[j+gap]=l2[j+gap],l2[j] 48 | j+=1 49 | gap=gapf(gap) 50 | 51 | 52 | -------------------------------------------------------------------------------- /Day19/Construct Binary Tree from Inorder and Postorder.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self,data): 3 | self.left=None 4 | self.right=None 5 | self.val=data 6 | 7 | 8 | def search(x,nums,start,end): 9 | for i in range(start,end+1): 10 | if nums[i]==x: 11 | return i 12 | #O(n**2) 13 | def construct(start,end,postorder,inorder,curr): 14 | if start>end: 15 | return 16 | node=Node(postorder[curr[0]]) 17 | curr[0]-=1 18 | x=search(node.val,inorder,start,end) 19 | node.right= construct(x+1,end,postorder,inorder,curr) 20 | node.left= construct(start,x-1,postorder,inorder,curr) 21 | return node 22 | 23 | #O(n) 24 | def constructn(start,end,postorder,inorder,curr,d): 25 | if start>end: 26 | return 27 | node=Node(postorder[curr[0]]) 28 | curr[0]-=1 29 | x=d[node.val] 30 | node.right= constructn(x+1,end,postorder,inorder,curr,d) 31 | node.left= constructn(start,x-1,postorder,inorder,curr,d) 32 | return node 33 | 34 | 35 | def printpreorder(node): 36 | if node: 37 | print(node.val,end=' ') 38 | printpreorder(node.left) 39 | printpreorder(node.right) 40 | 41 | inorder = [4, 8, 2, 5, 1, 6, 3, 7] 42 | postorder = [8, 4, 5, 2, 6, 7, 3, 1] 43 | n=len(inorder) 44 | curr=[n-1] 45 | root=construct(0,n-1,postorder,inorder,curr) 46 | printpreorder(root) 47 | print() 48 | d={} 49 | for i in range(n): 50 | d[inorder[i]]=i 51 | curr=[n-1] 52 | root=constructn(0,n-1,postorder,inorder,curr,d) 53 | printpreorder(root) 54 | print() 55 | -------------------------------------------------------------------------------- /Day19/Construct Binary Tree from inorder and preorder.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self,data): 3 | self.left=None 4 | self.right=None 5 | self.val=data 6 | 7 | 8 | def search(x,nums,start,end): 9 | for i in range(start,end+1): 10 | if nums[i]==x: 11 | return i 12 | #O(n**2) 13 | def construct(start,end,preorder,inorder,curr): 14 | if start>end: 15 | return 16 | node=Node(preorder[curr[0]]) 17 | curr[0]+=1 18 | x=search(node.val,inorder,start,end) 19 | node.left= construct(start,x-1,preorder,inorder,curr) 20 | node.right= construct(x+1,end,preorder,inorder,curr) 21 | return node 22 | 23 | #O(n) 24 | def constructn(start,end,preorder,inorder,curr,d): 25 | if start>end: 26 | return 27 | node=Node(preorder[curr[0]]) 28 | curr[0]+=1 29 | x=d[node.val] 30 | node.left= constructn(start,x-1,preorder,inorder,curr,d) 31 | node.right= constructn(x+1,end,preorder,inorder,curr,d) 32 | return node 33 | 34 | def printInorder(node): 35 | if node is None: 36 | return 37 | printInorder(node.left) 38 | print(node.val,end=' ') 39 | printInorder(node.right) 40 | 41 | inorder = ['D', 'B', 'E', 'A', 'F', 'C'] 42 | preorder = ['A', 'B', 'D', 'E', 'C', 'F'] 43 | curr=[0] 44 | root=construct(0,len(inorder)-1,preorder,inorder,curr) 45 | printInorder(root) 46 | print() 47 | d={} 48 | for i in range(len(inorder)): 49 | d[inorder[i]]=i 50 | curr=[0] 51 | root=constructn(0,len(inorder)-1,preorder,inorder,curr,d) 52 | printInorder(root) 53 | print() 54 | -------------------------------------------------------------------------------- /Day 3/Reverse Pairs (Leetcode).py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reversePairs(self, nums: List[int]) -> int: 3 | def merge(nums,temp,left,right,mid): 4 | count=0 5 | j=mid+1 6 | 7 | for i in range(left,mid+1): 8 | while j<=right and nums[i]>2*nums[j]: 9 | j+=1 10 | count+=(j-mid-1) 11 | i=left 12 | j=mid+1 13 | k=left 14 | while i<=mid and j<=right: 15 | if nums[i] 10 -> 19 -> 28 48 | | | | | 49 | V V V V 50 | 7 20 22 35 51 | | | | 52 | V V V 53 | 8 50 40 54 | | | 55 | V V 56 | 30 45 57 | ''' 58 | root=Node(30) 59 | root=push(root,8) 60 | root=push(root,7) 61 | root=push(root,5) 62 | root.next=Node(20) 63 | root.next=push(root.next,10) 64 | root.next.next=Node(50) 65 | root.next.next=push(root.next.next,22) 66 | root.next.next=push(root.next.next,19) 67 | root.next.next.next=Node(45) 68 | root.next.next.next=push(root.next.next.next,40) 69 | root.next.next.next=push(root.next.next.next,35) 70 | root.next.next.next=push(root.next.next.next,28) 71 | root=flatten(root) 72 | 73 | printList(root) 74 | 75 | 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Striver SDE Sheet [30 Days Challenge] 2 | 3 | Link to the SDE Sheet: [view](https://docs.google.com/document/d/1SM92efk8oDl8nyVw8NHPnbGexTS9W-1gmTEYfEurLWQ/edit) 4 | 5 | Collection of the solutions in Python 6 | 7 | ## Important Questions 8 | 9 | S.No. | Day | Topic | Date | 10 | ------|---------------|-------|------| 11 | 1 | [Day 1](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day%201) | Arrays | 16 June 21 | 12 | 2 | [Day 2](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day%202) | Arrays | 18 June 21 | 13 | 3 | [Day 3](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day%203) | Arrays/Maths | 21 June 21 | 14 | 4 | [Day 4](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day%204) | Hashing | 02 July 21 | 15 | 5 | [Day 5](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day%205) | Linked List | 17 June 21 | 16 | 6 | [Day 6](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day%206) | Linked List | 20 June 21 | 17 | 7 | [Day 7](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day%207) | 2-Pointer | 22 June 21 | 18 | 8 | [Day 8](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day%208) | Greedy | 8 August 21 | 19 | 9 | [Day 9](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day%209) | Recursion | 19 August 21 | 20 | 10 | [Day 10](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day10) | Recursion and Backtracking | Not started | 21 | 11 | [Day 11](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day11) | Binary Search | Not started | 22 | 12 | [Day 12](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day12) | Bits | 02 July 21 | 23 | 13 | [Day 13](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day13) | Stack and Queue | Not started | 24 | 14 | [Day 14](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day14) | 2-Pointer | Not started | 25 | 15 | [Day 15](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day15) | String | Not started | 26 | 16 | [Day 16](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day16) | String | Not started | 27 | 17 | [Day 17](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day17) | Binary Tree | 01 July 21 | 28 | 18 | [Day 18](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day18) | Binary Tree | 03 July 21 | 29 | 19 | [Day 19](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day19) | Binary Tree | 11 July 21 | 30 | 20 | [Day 20](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day20) | Binary Search Tree | Not started | 31 | 21 | [Day 21](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day21) | Binary Search Tree | Not started | 32 | 22 | [Day 22](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day22) | Mixed Question | Not started | 33 | 23 | [Day 23](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day23) | Graph | Not started | 34 | 24 | [Day 24](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Da24) | Graph | Not started | 35 | 25 | [Day 25](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day25) | Dynamic Programming | Not started | 36 | 26 | [Day 26](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day26) | Dynamic Programming | Not started | 37 | 27 | [Day 27](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day27) | OS | Not started | 38 | 28 | [Day 28](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day28) | DBMS | Not started | 39 | 29 | [Day 29](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day29) | CN | Not started | 40 | 30 | [Day 30](https://github.com/Shwetank2101/STRIVER-SDE-SHEET/tree/main/Day30) | Project | Not started | 41 | --------------------------------------------------------------------------------