Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Blind 75

Leetcode link to the post.

Total Completed: 31

Array (Solved: 4, Unsolved: 6)

  • Two Sum

    Approach: Solved using hash table, you can even solve this using 2 pointer approach where you'd have to sort the array, but then you won't be able to return index.

  • Three Sum
    1. Solved using 3 for loops: Time complexity O(n^3), Leetcode time limit exceeded.
    2. Solved using Recursion: Leetcode time limit exceeded.
    3. Solved using 2 pointers and a for loop: Time Complexity O(n^2), Leetcode accepted.
  • Container With Most Water

    Approach: 2 Pointers to keep track of left and right poles.

  • Best Time to Buy and Sell Stock

    Approach: O(n) approach, just keep track of lowest on left and compare with current price.

  • Contains Duplicate
  • Product of Array Except Self
  • Maximum Subarray
  • Maximum Product Subarray
  • Find Minimum in Rotated Sorted Array
  • Search in Rotated Sorted Array

Binary (Solved: 1, Unsolved: 4)

  • Sum of Two Integers

    Approach: We can add 2 integers without using any arithmetic operator by using half adder.

    We need XOR for sum and AND for carry.
    This is the truth table for it.

    a + b carry sum
    0 + 0 0 0
    0 + 1 0 1
    1 + 0 0 1
    1 + 1 1 0
    Here's a catch though, when you AND 01 with 01 it gives you 01.
    So how do you implement 1 ADDER 1 = 10 by using LEFT SHIFT <<

    Similarly we can subtract 2 numbers with the help of half subtractor.

    We need XOR for the difference but we need to compliment part of the carry.
    This is the truth table for it.
    a - b borrow difference
    0 - 0 0 0
    0 - 1 1 1
    1 - 0 0 1
    1 - 1 0 0
    We use similar logic that we used for half adder above and get subtraction.

  • Number of 1 Bits
  • Counting Bits
  • Missing Number
  • Reverse Bits

Dynamic Programming (Not started this)

  • Climbing Stairs
  • Coin Change
  • Longest Increasing Subsequence
  • Longest Common Subsequence
  • Word Break Problem
  • Combination Sum
  • House Robber
  • House Robber II
  • Decode Ways
  • Unique Paths
  • Jump Game

Graph (Solved: 4, Unsolved: 4)

  • Clone Graph

    Approach: Used both dfs and bfs to copy the graph. Leetcode accepted.

  • Course Schedule

    Approach: Used BFS topological sort. Understood the approach from this article.
    The following pseudo code helped.

    
                L = Empty list that will contain the sorted elements 
    S = Set of all nodes with no incoming edge

    While S is non-empty do:
      remove a node n from S
      add n to tail of L
      for each node m with an edge e from n to m do
        remove edge e from the graph
        if m has no other incoming edges then
          insert m into S

    if graph has edges then
      return error (graph has at least one cycle)
    else
      return L (a topologically sorted order)

  • Pacific Atlantic Water Flow

    Approach: Variation of island problem, solved using DFS. Can be solved using BFS

  • Number of Islands

    Approach: Solved using DFS

  • Longest Consecutive Sequence
  • Alien Dictionary
  • Graph Valid Tree
  • Number of Connected Components in an Undirected Graph

Interval (Not started this)

  • Insert Interval
  • Merge Intervals
  • Non-overlapping Intervals
  • Meeting Rooms
  • Meeting Rooms II

Linked List (Solved all)

  • Reverse Linked List

    Approach: Used 3 pointer approach

  • Linked List Cycle

    Approach: Used rabbit and tortoise method, where we move rabbit by 2 positions and tortoise by 1. If at any given point the 2 meet then we got a cycle. You can also solve this porblem by having a hash table and making note of all the nodes seen so far.

  • Merge Two Sorted Lists

    Approach: Simple O(n) merging, by traversing both lists at the same time.

  • Merge K Sorted Lists

    Approach: Use heap sort to merge k lists.

  • Remove Nth Node From End Of List

    Approach: Use rabbit and tortoise method. Move rabbit n spaces, then move tortoise along with rabbit unitl rabbit reaches end of the list.

  • Reorder List

    Approach: Detached list in the middle using rabbit and tortoise method. Put the second list in stack and detached it's nodes. Traversed the first list and added the stack nodes in between. Total time O(n), space O(n/2)

Matrix (Solved all)

  • Set Matrix Zeroes

    Approach: Set the row and col of the cell with zero to value "x". Then in second pass, change all the cells with "x" to zero. This ensures we use no extra space and do it in O(n) time.

  • Spiral Matrix

    Approach: Had to see this idea from leetcode solutions.
    First write a function that first prints the spiral of rows and cols, then just reduce the bounds and do the same.
    Don't forget the mark the cells visited to avoid duplicates.

  • Rotate Image

    Approach: First swap the elements under red triangle with the green triangle.

    Then flip the matrix upside down.

  • Word Search

    Approach: Similar to island problem, dfs and backtracking for visited cells.
    Make sure to check if all the letters in the word are present on the board before proceeding to find the word.

String (Solved: 2, Unsolve: 8)

  • Longest Substring Without Repeating Characters

    Approach: We use sliding window for this one. Use hash table to keep track of the letters in the window.

  • Longest Repeating Character Replacement

    Approach: Following formula used
    (length of substring - number of times of the maximum occurring character in the substring) <= k

  • Minimum Window Substring
  • Valid Anagram

    Approach: Used hash table.

  • Group Anagrams

    Approach: Sort the strings and store them in hash table.

  • Valid Parentheses
  • Valid Palindrome
  • Longest Palindromic Substring
  • Palindromic Substrings
  • Encode and Decode Strings (Leetcode Premium)

Tree (Solved: 8, Unsolved: 6)

  • Maximum Depth of Binary Tree

    Approach: done using simple (any)-order traversal.

  • Same Tree

    Approach: do any 2 traversals and compare them. I did in-order and post-order.

  • Invert/Flip Binary Tree

    Thoughts: My initial thought was to do level order traversal and then flip the nodes. But this approach was unnecessarily complicated.
    Approach: Perform a post-order traversal and flip left and right child of each node. What this creates is that even if you flip 1 node at a time, since you'll be flipping their parent too, all the nodes get flipped automatically.

  • Binary Tree Maximum Path Sum

    Approach: It's pretty similar to finding height of a tree. The only catch is what to return.

  • Binary Tree Level Order Traversal

    Approach: Used a modified iterative BFS

  • Serialize and Deserialize Binary Tree

    Approach: Use preorder to serialize it using comma as delimiter.
    Then use similar method we use to find height of tree to first add left child of sub tree and then add right child of sub tree, all while traversing the array.

    Thoughts: Initially I thought of using tries to do this, but it can create problems if all nodes of the tree have same value.
    My next thought was to do inOrder and preOrder traversal and then build the tree, but I was plagued by the same above problem, what if all tree nodes have same value.
    Then I thought about level order traversal but use delimiter for null nodes, this will waste a LOT of space (what if the tree is skewed), but I will end up with correct answer.

  • Subtree of Another Tree

    Approach: Recursive function similar to inorder traversal that checks if 2 sub trees are same or not. Another function that checks if the current node in the larger tree is same as the root of sub tree, then it calls the previous function to check if the subtrees are same or not.

  • Construct Binary Tree from Preorder and Inorder Traversal
  • Validate Binary Search Tree

    Approach: Do an inorder traversal of the BST.
    Then validate if the traversal is in ascending order or not.

  • Kth Smallest Element in a BST

    Approach: User inorder traversal to find the kth smallest element.

  • Lowest Common Ancestor of a Binary Search Tree

    Approach: First search for the nodes in left sub tree, and right sub tree. If found return the root.
    Don't forget to trickle the answer upwards if nothing is found. Approach similar to postOrder traversal of the tree.

  • Implement Trie (Prefix Tree)

    Thoughts: You can also use this approach to serialise and de-serialise an n-ary tree.

  • Add and Search Word

    Approach: Use trie for this.

  • Word Search II

Heap (Solved all)

  • Merge K Sorted Lists
  • Top K Frequent Elements

    Approach: First build a hash table with key: element, value: frequency.
    Then Create a heap and add all elements from the hash to the heap in form of tuple (python thing).
    Remove the top k elements from the heap.

  • Find Median from Data Stream

    Approach: Keep 2 heaps, max heap to hold all elements less than median and min heap to hold elements greater than the median.
    Finding median: which ever heap has more elements, the top element of that heap is the median.
    If both heaps have same elements, then median is (minHeapTop+maxHeapTop) / 2
    If at any point one heap has 2 elements more than other heap, pop an element from that heap and add it to another heap.