Skip to content

Instantly share code, notes, and snippets.

View zlrlo's full-sized avatar
🐥

jieunee zlrlo

🐥
  • Seoul, Republic of Korea
View GitHub Profile
@zlrlo
zlrlo / min_heap.js
Created May 23, 2024 16:38
[JS] Min Heap 구현
class MinHeap {
constructor() {
this.heap = [null];
}
push(value) {
this.heap.push(value);
let currentIndex = this.heap.length - 1;
let parentIndex = Math.floor(currentIndex / 2);
@zlrlo
zlrlo / combinations.js
Created October 3, 2023 02:41
[JS] 조합 구현
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);
@zlrlo
zlrlo / permutations.js
Created October 3, 2023 02:40
[JS] 순열 구현
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);
@zlrlo
zlrlo / getPrime.js
Last active October 3, 2023 02:26
[JS] 소수 구하는 가장 빠른 방법
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;
}
@zlrlo
zlrlo / max_heap.js
Last active July 29, 2023 03:06
[JS] Max Heap 구현
class MaxHeap {
constructor() {
this.heap = [null];
}
push(value) {
this.heap.push(value);
let currentIndex = this.heap.length - 1;
let parentIndex = Math.floor(currentIndex / 2);
@zlrlo
zlrlo / sort.js
Created July 26, 2023 09:58
[JS] 정렬
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;
}
@zlrlo
zlrlo / slice.js
Created July 26, 2023 09:52
[JS] slice 얕은 복사
// 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)
@zlrlo
zlrlo / flatMap.js
Created July 26, 2023 09:39
[JS] 한 레벨만 평탄화 flatMap
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"]
@zlrlo
zlrlo / padStart.js
Last active July 26, 2023 09:39
[JS] 문자열의 시작을 다른 문자열로 채우기
const hour = '21';
const min = '53';
const sec = '5';
const time = `${hour}:${min}:${sec.padStart(2, '0')}`
console.log(time); // 21:53:05
@zlrlo
zlrlo / queue.js
Last active August 23, 2023 09:41
[JS] Queue javascript
// LinkedList
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {