Skip to content

Learning plan for top 16 algorithms and data structures.

Notifications You must be signed in to change notification settings

lywangj/Algorithms

Repository files navigation

Algorithms

Learning plan for top 16 algorithms and data structures. The goal is to develop an understanding of the underlying pattern and be able to solve a specific category of problems.

______ 01. Sliding Window
______ 02. Two Pointers
______ 03. Fast & Slow Pointers
______ 04. Merge Intervals
______ 05. Cyclic Sort
______ 06. In-place Reversal of a LinkedList
______ 07. Breadth First Search
______ 08. Depth First Search
______ 09. Two Heaps
______ 10. Subsets
______ 11. Binary Search
______ 12. Bitwise
______ 13. Top Kth Elements
______ 14. K-way Merge

Chapter 01. Sliding Window

A sliding window pattern will solve this problem in an more effecient way, time complexity O(n) - n as the size of the given array. Compared with this, a basic brute force solution will be with time complexity O(nk) - k as the number of the targeted elements.

《TOP》

Chapter 02. Two Pointers

If input array is sorted, an efficient way, time complexity O(n), would be to with one pointer in the beginning and another pointer at the end. At every step, pointers iterate through the remaining elements, until these two pointers pointing to the same elements.

Compared with this, a basic brute force solution will be with time complexity O(n*n), and a heapsort solution will be with O(n*logn).

  • Exercise: Dutch National Flag Problem
  • Input array = [2, 0, 1, 1, 1]
  • Output array = [0, 1, 1, 1, 2]
  • Explain: incrementally sort an array containing only three objects (labled as 0, 1 & 2)

《TOP》

Chapter 03. Fast & Slow Pointers

Setting two pointers with different move speed would be time complexity O(n) and with space complexity O(1), no extra space.
  • Exercise: Start of LinkedList Cycle
  • LinkedList = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 3
  • starting node of cycle: 3
  • Explain: Node 3 is start of cycle (3 -> 4 -> 5 -> 6 -> 3)

《TOP》

Chapter 04. Merge Intervals

This would be an effecient way to deal with overlapping intervals with time complexity O(n) and space complexity O(1), no extra space.
  • Exercise: Intervals Intersection
  • arr1 = [1, 3], [5, 6], [7, 9]
  • arr2 = [2, 3], [5, 7]
  • output : [2, 3], [5, 6], [7, 7]

《TOP》

Chapter 05. Cyclic Sort

With a given unsorted array, this would be an effecient way to sort and store all duplicate numbers with time complexity O(n) and space complexity O(1), no extra space.

《TOP》

Chapter 06. In-place Reversal of a LinkedList

An efficient pattern to implemnt in-place revere sub-list without extra space.
  • Exercise: Reverse a Sub-list
  • start to revere, p = 2
  • end to reverse, q = 4
  • input-list : 1 -> 2 -> 3 -> 4 -> 5 -> null
  • output-list : 1 -> 4 -> 3 -> 2 -> 5 -> null

《TOP》

Chapter 07. Breadth First Search

Breadth First Search (BFS) technique provides a way to traverse a tree in a level-by-level order, and a Queue is used to keep track of all the nodes of a level.

《TOP》

Chapter 08. Depth First Search

Using recursion, Depth First Search (DFS) is the approach to keep track of all the nodes while traversing.

《TOP》

Chapter 09. Two Heaps

Creating two heaps to store the given number stream, the time complexity of this approach will be only O(nlogn), rather than brute-force solution with time complexity O(n*n)

  • Exercise: Find the Median of a Number Stream
    1. insertNum(3)
    1. insertNum(1)
    1. findMedian() -> output: 3
    1. insertNum(2)
    1. findMedian() -> output: 2
    1. insertNum(4)
    1. findMedian() -> output: 2.5

《TOP》

Chapter 10. Subsets

Similar with breadth-first search, this subsets approach provides a solution with time complexity O(n*n!)

  • Exercise: Permutations
  • Input: nums = [1,3,5]
  • Output: [1,3,5], [1,5,3], [3,1,5], [3,5,1], [5,1,3], [5,3,1]

《TOP》

Chapter 11. Binary Search

As the efficient searching algorithm, binary search provide a solution with time complexity, O(logn), for finding an item from a sorted list of items.

  • Exercise: Number Range
  • arr = [4, 6, 6, 6, 9], key = 6
  • Output: [1, 3]

《TOP》

Chapter 12. Bitwise

With time complexity O(n), bitwise operatioin provides a method for these problems without extra space. Compared with it, another common method is to add every numbers in the given array, but the sum will be overflowed, if the numbers in the given array are large.

《TOP》

Chapter 13. Top Kth Elements

《TOP》

Chapter 14. K-way Merge

《TOP》

About

Learning plan for top 16 algorithms and data structures.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published