Skip to content

flxchen/leetcode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 

Repository files navigation

About

This is ongoing project which records my leetcode problem process. It also summarizes the common data structure, algorithm, and gives a template.

Auto load web page

function autoLoadTrades() {
    setInterval(() => {
        window.scrollTo(0, document.body.scrollHeight);
    }, 2); // Scroll every .002 seconds
}

find gcd

function gcd(int a,int b){
    int r = 0;
    while( b != 0){
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

Cpp iterator

unordered_map<int, int> m;
for(unordered_map<int, int>::iterator it=m.begin();it!=m.end();it++){
    cout<<"("<<it->first<<","<<it->second<<")"<<",";
}

Cpp containers

Sequence container

name description
array fixed size static array
vector resizable dynamic array
deque Dynamic array allows fast insertions and deletions at both ends.
list Doubly Linked List
forward-list Singly Linked List

Assoiciative container

name description
set Collection of unique elements sorted on values
map Collection of key-value pairs sorted on keys where no two pairs have same keys
multiset Collection of elements sorted on values but allows copies of values
multimap Collection of key-value pairs sorted on keys where multiple pairs can have same keys

Unordered Associative Containers

name description
unordered set Collection of unique elements hashed by values
unordered map Collection of key-value pairs that are hashed by keys where no two pairs have same keys
unordered multiset Collection of elements hashed by values and allows multiple copies of values
unordered multimap Collection of key-value pairs that are hashed by keys where multiple pairs can have same keys

Array

Easy

  1. Two sum
  2. Minimum differences

String

Easy

  1. Valid Palindrome

Linkedlist

Stack

Queue

Heap/priority queue

K closest points

Binary search tree

Hash table:Hashing

Backtracking

generate parenthesis

Sorting

name worst case best case additional space(in place) stable(relative order)
1. bubble sort O(n2) O(n) O(1) yes
2. selection sort O(n2) O(n2) O(1) no
3. insertion sort O(n2) O(n) O(1) yes
4. merge sort O(nlogn) O(nlogn) O(n) yes
5. quick sort O(n2) O(nlogn) O(logn) no
6. heap sort O(nlogn) O(nlogn) O(1) no
7. shell sort O(nlogn2) O(n) O(1) no
8. bucket sort O(n2) O(n+k) (k:bucket size) O(n+k) yes
9. radix sort O(n*k/d) (d:number of largest digit) O(n*k/d) O(n+2d) yes
10. Tim sort O(nlogn) O(nlogn) n yes

Binary Search

  1. min number of days to make bouquets
  2. 703 Binary search
    Template
int left = 0, right = int(nums.size()) - 1;
while (left <= right) {
// Get the middle index and the middle value. 
   int mid = left + (right - left) / 2;            
   // Case 1, return the middle index.
   if (nums[mid] == target) {
     return mid;
   } 
   // Case 2, discard the smaller half.
   else if (nums[mid] < target) {
     left = mid + 1;   
   } 
   // Case 3, discard the larger half.
   else {
     right = mid - 1;
   }
}        
// If we finish the search without finding target, return -1.
return -1;

Two pointers/sliding window

fruit into baskets

Matrix

Math

  1. calculate power

Bit manipulation

>> signed right shift(divide the number by power of 2) e.g. (1000)_2>>2=(1110)_2 -2 << signed left shift(preserve the sign)
>>> unsigned right shift <<< unsigned left shift
bitwise and & bitwise or |
bitwise XOR ^ (result is 0 if both bits are the same) bitwise coomplement ~
1's complement: invert all bits, add up to 1 e.g. (1000)2=8 (0111)2=-7 2's complement: invert all bits and add one, msb is signed e.g. (0110)2=6 (1001)2+1=(1010)2=-1x23+1x2=-6

Backtrack

Tree

Graph

1.number of islands

BFS(G,s) G:graph s:start node

for each vertex u in G.V-{s}:#initialize
   u.color = WHITE #not visited
   u.d = inf #distance from s to u
   u.pred = NIL #predecessor
s.color = GRAY # visiting
s.d = 0
s.pred = NIL
Q = Enqueue(Q,s);
while Q not empty:
   u = Dequeue(Q)
   for each v in G.Adj[u]:
      v.color = Gray
      v.d = u.d + 1
      v.pred = u
      Enqueue(Q,v)
   u.color = Black # finish visiting u

Time:O(V+E) Space:O(V)

DFS(G) G:graph

for each vertex u in G.V: #initialize
   u.color = White
   u.pred = NIL
time = 0
for each vertex u in G.V:
   if u.color = White:
      DFS-VISIT(G,u)

DFS-VISIT(G,u):
   time = time + 1
   u.d = time #start time when discovering u
   u.color = Gray
for each v in G.Adj[u]:
   if v.color == White:
      v.pred = u
      DFS-VISIT(G,v)
u.color = Black
time = time + 1
u.f = time #time after finish visiting u

time:O(V+E) space:O(V)

Greedy/optimization

DP

  1. odd even jump
  2. Longest increasing subsequence
  3. coin change

Reference

  1. leetcode-master
  2. neetcode

About

leetcode common data structure and algorithm

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published