jas0nhuang

Don't LeetCode Yet - 8 - Algorithm Intro

Introduction to Algorithm

Algorithm efficiency

Time complexity VS Space complexity

Big O notation

O(n) processing steps increase linearly to the size of the input(amount of data).
O(2^n) yes/no questions
O(1) always 1
O(log n)

Most of the time we can’t get both Time and Space efficiency. But we can use one to exchange for the other.

How many time will it take to count 1 to 1 billion?

ONLY around 100ms. Computer is actually really powerful.

LIOJ 1035 sorting

Use .sort() method.

arr.sort(function(a,b){
return a-b
// a-b means sorting from low to high. b-a will sort from hight to low.
}
let [n, m] = lines[0].split(" ")

Is it a must to write a SEARCH function?

LIOJ 1048

Was also the problem with data type(string or number)

for(){
for(){
for(){
}
}
}

~= O(n^3)

LIOJ 1049

NEED TO .map(Number) when getting the array or else the array elements will be stored as strings, and get a Time Limit Excieded upon submission.
Even if you do Math.abs(Number(arrayX[i])-Number(arrayY[j])) inside the for loop.
O(n*m)
For more efficient solution, think of the “sorted” fact given in the description.

O(n^2) > O(n)
O(n) ~= O(100n)

LIOJ 1050

Brute forced!
Store the number array into an object:

{
1: index0,
3: index1,
5: index2,
7: index3
}

Search inside the object for the two sum.

LIOJ 1051

Merge Sort