This file contains hidden or 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
| class MinHeap { | |
| constructor() { | |
| this.heap = [null]; | |
| } | |
| push(value) { | |
| this.heap.push(value); | |
| let currentIndex = this.heap.length - 1; | |
| let parentIndex = Math.floor(currentIndex / 2); |
This file contains hidden or 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 combinations(arr, n) { | |
| // 1개만 뽑는다면 그대로 조합을 반환한다. 탈출 조건으로도 사용된다. | |
| if (n === 1) return arr.map((v) => [v]); | |
| const result = []; | |
| // 요소를 순환한다 | |
| arr.forEach((fixed, idx, arr) => { | |
| // 현재 index 이후 요소를 추출한다. | |
| // index번째는 선택된 요소 | |
| const rest = arr.slice(idx + 1); |
This file contains hidden or 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 permutations(arr, n) { | |
| // 1개만 뽑는다면 그대로 순열을 반환한다. 탈출 조건으로도 사용된다. | |
| if (n === 1) return arr.map((v) => [v]); | |
| let result = []; | |
| // 요소를 순환한다 | |
| arr.forEach((fixed, idx, arr) => { | |
| // 현재 index를 제외한 요소를 추출한다. | |
| // index번째는 선택된 요소 | |
| const rest = arr.filter((_, index) => index !== idx); |
This file contains hidden or 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 getPrime(n) { | |
| let answer = 0; | |
| let prime = [false, false, ...Array(n - 1).fill(true)]; | |
| for(let i = 2; i * i <= n; i++) { | |
| if(prime[i]) { | |
| for(let j = i * 2; j <= n; j += i) { | |
| prime[j] = false; | |
| } |
This file contains hidden or 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
| class MaxHeap { | |
| constructor() { | |
| this.heap = [null]; | |
| } | |
| push(value) { | |
| this.heap.push(value); | |
| let currentIndex = this.heap.length - 1; | |
| let parentIndex = Math.floor(currentIndex / 2); |
This file contains hidden or 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 compare(a, b) { | |
| if (a is less than b by some ordering criterion) { | |
| return -1; | |
| } | |
| if (a is greater than b by the ordering criterion) { | |
| return 1; | |
| } | |
| // a must be equal to b | |
| return 0; | |
| } |
This file contains hidden or 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
| // Using slice, create newCar from myCar. | |
| let myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } } | |
| let myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'] | |
| let newCar = myCar.slice(0, 2) | |
| // Display the values of myCar, newCar, and the color of myHonda | |
| // referenced from both arrays. | |
| console.log('myCar = ' + JSON.stringify(myCar)) | |
| console.log('newCar = ' + JSON.stringify(newCar)) | |
| console.log('myCar[0].color = ' + myCar[0].color) |
This file contains hidden or 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
| let arr1 = ["it's Sunny in", "", "California"]; | |
| arr1.map(x=>x.split("")); | |
| // [["it's","Sunny","in"],[""],["California"]] | |
| arr1.flatMap(x => x.split(" ")); | |
| // ["it's","Sunny","in","","California"] |
This file contains hidden or 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
| const hour = '21'; | |
| const min = '53'; | |
| const sec = '5'; | |
| const time = `${hour}:${min}:${sec.padStart(2, '0')}` | |
| console.log(time); // 21:53:05 |
This file contains hidden or 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
| // LinkedList | |
| class Node { | |
| constructor(value) { | |
| this.value = value; | |
| this.next = null; | |
| } | |
| } | |
| class Queue { |
NewerOlder