diff --git "a/\355\231\251\354\236\254\354\230\201/n^2\353\260\260\354\227\264 \354\236\220\353\245\264\352\270\260.md" "b/\355\231\251\354\236\254\354\230\201/n^2\353\260\260\354\227\264 \354\236\220\353\245\264\352\270\260.md" new file mode 100644 index 0000000..d551c96 --- /dev/null +++ "b/\355\231\251\354\236\254\354\230\201/n^2\353\260\260\354\227\264 \354\236\220\353\245\264\352\270\260.md" @@ -0,0 +1,16 @@ +```js +function solution(n, left, right) { + var answer = []; + + for (let i = left; i <= right; i += 1) { + const row = Math.floor(i / n); + const col = i % n; + + const value = Math.max(row, col) + 1; + + answer.push(value); + } + + return answer; +} +``` diff --git "a/\355\231\251\354\236\254\354\230\201/\353\266\200\353\214\200\353\263\265\352\267\200.md" "b/\355\231\251\354\236\254\354\230\201/\353\266\200\353\214\200\353\263\265\352\267\200.md" new file mode 100644 index 0000000..94ae1a8 --- /dev/null +++ "b/\355\231\251\354\236\254\354\230\201/\353\266\200\353\214\200\353\263\265\352\267\200.md" @@ -0,0 +1,110 @@ +```js +class MinHeap { + constructor() { + this.heap = [null]; + } + + heappush(value) { + this.heap.push(value); + let nowIndex = this.heap.length - 1; + let parentIndex = Math.floor(nowIndex / 2); + + while (nowIndex > 1 && this.heap[parentIndex][1] > this.heap[nowIndex][1]) { + this.swap(nowIndex, parentIndex); + nowIndex = parentIndex; + parentIndex = Math.floor(nowIndex / 2); + } + } + + heappop() { + if (this.length === 1) return this.heap.pop(); + const returnValue = this.heap[1]; + this.heap[1] = this.heap.pop(); + + let nowIndex = 1; + let leftIndex = nowIndex * 2; + let rightIndex = nowIndex * 2 + 1; + + if (!this.heap[rightIndex]) { + if ( + this.heap[leftIndex] && + this.heap[nowIndex][1] > this.heap[leftIndex][1] + ) { + this.swap(nowIndex, leftIndex); + return returnValue; + } + } + + while ( + this.heap[rightIndex] && + (this.heap[nowIndex][1] > this.heap[leftIndex][1] || + this.heap[nowIndex][1] > this.heap[rightIndex][1]) + ) { + if (this.heap[leftIndex][1] < this.heap[rightIndex][1]) { + this.swap(nowIndex, leftIndex); + nowIndex = leftIndex; + } else { + this.swap(nowIndex, rightIndex); + nowIndex = rightIndex; + } + + leftIndex = nowIndex * 2; + rightIndex = nowIndex * 2 + 1; + } + return returnValue; + } + + swap(a, b) { + [this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]]; + } + + get length() { + return this.heap.length - 1; + } +} + +function solution(n, roads, sources, destination) { + var answer = []; + + const graph = {}; + + roads.forEach(([v, e]) => { + graph[v] = [...(graph[v] ?? []), [e, 1]]; + graph[e] = [...(graph[e] ?? []), [v, 1]]; + }); + + const distance = new Array(n + 1).fill(Infinity); + + const minHeap = new MinHeap(); + + distance[destination] = 0; + + graph[destination].forEach(([to, cost]) => { + minHeap.heappush([to, cost]); + }); + + while (minHeap.length) { + const [now, cost] = minHeap.heappop(); + + if (distance[now] <= cost) { + continue; + } + + distance[now] = cost; + + if (graph[now]) { + graph[now].forEach(([next, nextCost]) => { + const dist = cost + nextCost; + + if (dist < distance[next]) { + minHeap.heappush([next, dist]); + } + }); + } + } + + return sources.map((source) => + distance[source] !== Infinity ? distance[source] : -1 + ); +} +``` diff --git "a/\355\231\251\354\236\254\354\230\201/\354\247\225\352\262\200\353\213\244\353\246\254 \352\261\264\353\204\210\352\270\260.md" "b/\355\231\251\354\236\254\354\230\201/\354\247\225\352\262\200\353\213\244\353\246\254 \352\261\264\353\204\210\352\270\260.md" new file mode 100644 index 0000000..4f85610 --- /dev/null +++ "b/\355\231\251\354\236\254\354\230\201/\354\247\225\352\262\200\353\213\244\353\246\254 \352\261\264\353\204\210\352\270\260.md" @@ -0,0 +1,40 @@ +```js +/* + 1. 이분탐색을 위한 함수 + 2. 체크할 때 true, false를 내뱉는 함수 +*/ + +const isAllCrossed = (stones, k, mid) => { + let cnt = 0; + let flag = true; + stones.forEach((stone) => { + if (!flag) return; + cnt = stone <= mid ? cnt + 1 : 0; + if (cnt >= k) { + flag = false; + } + }); + return flag; +}; + +const binarySearch = (arr, k) => { + let start = 1; + let end = 200000000; + while (start <= end) { + let mid = Math.floor((start + end) / 2); + if (isAllCrossed(arr, k, mid)) start = mid + 1; + else end = mid - 1; + } + return start; +}; + +const solution = (stones, k) => { + return binarySearch(stones, k); +}; + +(() => { + const stones = [2, 4, 5, 3, 2, 1, 4, 2, 5, 1]; + const k = 3; + console.log(solution(stones, k)); +})(); +``` diff --git "a/\355\231\251\354\236\254\354\230\201/\355\205\214\354\235\264\353\270\224 \355\225\264\354\213\234 \355\225\250\354\210\230.md" "b/\355\231\251\354\236\254\354\230\201/\355\205\214\354\235\264\353\270\224 \355\225\264\354\213\234 \355\225\250\354\210\230.md" new file mode 100644 index 0000000..21ba082 --- /dev/null +++ "b/\355\231\251\354\236\254\354\230\201/\355\205\214\354\235\264\353\270\224 \355\225\264\354\213\234 \355\225\250\354\210\230.md" @@ -0,0 +1,20 @@ +```js +function solution(data, col, row_begin, row_end) { + let answer = 0; + + const sortedData = data.sort((a, b) => { + const diff = a[col - 1] - b[col - 1]; + return diff ? diff : b[0] - a[0]; + }); + + for (let i = row_begin; i <= row_end; i += 1) { + const index = i; + const now = sortedData[i - 1]; + + const sum = now.reduce((acc, cur) => acc + (cur % index), 0); + answer ^= sum; + } + + return answer; +} +```