Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

Leetcode Problem 400-499

402. Remove K Digits 🌟🌟


404. Sum of Left Leaves 🌟

Simple recursive dfs


413. Arithmetic Slices 🌟🌟

	array: [1,2,3,4]
	Here first 3 element:   [1,2,3] is arithmetic -> count=1
	also next 3 element:    [2,3,4] is arithmetic -> count=1
	now all together:     [1,2,3,4] is arithmetic -> count=1

429. N-ary Tree Level Order Traversal 🌟🌟

This question is in continuation with A general approach to level order traversal questions series.

Previous Questions

  1. Binary tree level order traversal
  2. Binary tree level order traversal - II
  3. Binary tree zig-zag level order traversal
  4. Average of levels
  5. Binary tree right side view
  6. largest value in each tree row
  7. Populating next right pointer

Recursive solution

Iterative solution


438. Find All Anagrams in a String 🌟🌟

sliding window + hashmap

sliding window + array [constant space]


441. Arranging Coins 🌟

Simulation

Math solution


448. Find All Numbers Disappeared in an Array 🌟

Brute force

Hash set

Space-Optimized using In-place modification


450. Delete Node in a BST 🌟🌟

Solution

  1. find the key node: there are 3 cases
    if(key < root) -> find in left subtree
    else if(key > root) -> find in right subtree
    else (key==root) -> delete the node
  1. Now delete the node: there are 3 cases
    if leaf node(0 child)  -> delete(node) and return NULL
    else if node having 1 child  -> delete(node) and return the child
    else  (node having 2 children){
      i) find smallest node (leftmost) in the right subtree of the key
      ii) copy the smallest node value with key node
      iii) delete that smallest node from right subtree (as it have only 1 or 0 child)}

Alternative: In the condition of node having 2 children, we can also find largest node (rightmost) of left subtree of the key.


452. Minimum Number of Arrows to Burst Balloons 🌟🌟

Solution


454. 4Sum II 🌟🌟

Brute force (TLE)

O(N^3) (TLE)

O(N^2) Optimized (AC)


461. Hamming Distance 🌟

Original Post - 4 Solutions

Converting Binary Form to String/Array & Iterating

Iterating & Comparing each Bit

XOR & count bits

Brian-Kernighan’s method

πŸ’‘ Note:

  1. The number of bits N for this problem is fixed to 32. So, strictly speaking, the time complexity of 1st three solutions is O(N) = O(32) = O(1). But to differentiate between time complexities of 1st three and last approach, I have denoted them as O(N).
  2. It’s likely that if you got such a question during an interview, you will probably be expected to come up with an approach similar to this one. This approach performs the least number of loops to find the number of set bits in a number which is equal to the number of set bits in the number itself.

485. Max Consecutive Ones 🌟

Simple iterative solution


492. Construct the Rectangle 🌟

Iterative Solution

Recursive Solution

493. Reverse Pairs 🌟🌟🌟

Brute force

Using Merge Sort modification