Skip to content

[재영] 징검다리 건너기, 테이블 해시 함수, 부대복귀, n^2 배열 자르기 #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions 황재영/n^2배열 자르기.md
Original file line number Diff line number Diff line change
@@ -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;
}
```
110 changes: 110 additions & 0 deletions 황재영/부대복귀.md
Original file line number Diff line number Diff line change
@@ -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
);
}
```
40 changes: 40 additions & 0 deletions 황재영/징검다리 건너기.md
Original file line number Diff line number Diff line change
@@ -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));
})();
```
20 changes: 20 additions & 0 deletions 황재영/테이블 해시 함수.md
Original file line number Diff line number Diff line change
@@ -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;
}
```