This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeId(length) { | |
let result = ""; | |
const characters = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
const charactersLength = characters.length; | |
for (let i = 0; i < length; i++) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//get the nth number in fibonacci sequence, base case is 0 and 1. | |
//solution 1, easier to understand but it's not good. It has a time complexity of O(2^n). | |
function fib(num) { | |
if (num === 2) return 1; | |
if (num === 1) return 0; | |
return fib(num - 1) + fib(num - 2); | |
} | |
fib(6) //returns 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Status": "Connected", | |
"Adminstration Control": { | |
"Connection": "stable", | |
//Total number 100. 70 means adminstration can control most of stuff of that device, but he is restricted on some of the part. | |
//will need to think more about it | |
"Permission": "70" //rank system. 70 out of 100 | |
}, | |
"Device": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//get the nth number in fibonacci sequence, base case is 0 and 1. | |
//solution 1, easier to understand but it's not good. It has a time complexity of O(2^n). | |
function fib(num) { | |
if (num === 2) return 1; | |
if (num === 1) return 0; | |
return fib(num - 1) + fib(num - 2); | |
} | |
fib(6) //returns 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get Fibonacci Sequence in an Array, base case is 0 and 1. | |
const fibSequence = (num) => { | |
if(num === 0) return 0; | |
const arr=[0,1]; | |
let counter=2; | |
while(counter <=num){ | |
arr[counter]=arr[counter -1] + arr[counter -2] | |
counter ++ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Solution 1 | |
const isValidBST = function(root, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER) { | |
if(root == null) return true | |
if(root.val >= max || root.val <= min) return false | |
return isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max) | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Dijkstra algorithm is used to find the shortest distance between two nodes inside a valid weighted graph. Often used in Google Maps, Network Router etc. | |
//helper class for PriorityQueue | |
class Node { | |
constructor(val, priority) { | |
this.val = val; | |
this.priority = priority; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Helper class. To get the most prioritize result out | |
class PriorityQueue { | |
constructor() { | |
this.values = []; | |
} | |
enqueue(val, priority) { | |
this.values.push({ val, priority }); | |
this.sort(); | |
} | |
dequeue() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In graph theory, a tree is an undirected graph in which any two vertices are connected by exactly one path, | |
or equivalently a connected acyclic undirected graph. | |
Which means, in a graph, we can reach a certain node in many different ways, but in a tree there should be only ONE way to reach that node. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The algorithm starts at the root node and explores as far as possible along each branch before backtracking | |
//To simplify, In Depth First Search in Binary Search Tree, travels through one side until it reaches its end and then tries the other side. | |
//============================================================================================================ | |
//DFS_PreOrder | |
//This method, visits the one specific side(either left or right) at once, and after the whole side is finished, then visits the other side. | |
// as it goes through the tree, it pushes visited node continuously. | |
//Example: 10 | |
// 6 15 | |
// 3 8 20 |