Skip to content

[재영] 시소 짝꿍, 택배 상자 #80

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 2 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
35 changes: 35 additions & 0 deletions 황재영/시소 짝꿍.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
```js
function solution(weights) {
let result = 0;

const map = new Map();

// 먼저 map에 weight 정보를 추가
weights.forEach((weight) => {
map.set(weight, (map.get(weight) ?? 0) + 1);
});

map.forEach((value, key) => {
// 상대편의 경우의 수를 모두 추려냄.
[key, (key * 3) / 2, (key * 4) / 3, key * 2].forEach((partnerWeight) => {
// 만약 상대방의 몸무게에 관하여 정보가 없으면, 짝꿍이 될 수 없음.
if (!map.has(partnerWeight)) {
return;
}

// 몸무게가 같은 경우 = N개 중 2개를 뽑아내는 조합
if (key === partnerWeight) {
result += (value * (value - 1)) / 2;
return;
}

const partnerWeightCount = map.get(partnerWeight);

// 몸무게가 다른 경우 = 서로의 곱만큼 경우의 수 가능
result += partnerWeightCount * value;
});
});

return result;
}
```
66 changes: 66 additions & 0 deletions 황재영/택배 상자.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
```js
class Queue {
constructor() {
this.queue = [];
this.front = 0;
this.rear = 0;
}
enqueue(value) {
this.queue[this.rear++] = value;
}
dequeue() {
const value = this.queue[this.front];
delete this.queue[this.front];
this.front += 1;
return value;
}
get peek() {
return this.queue[this.front];
}
get size() {
return this.rear - this.front;
}
}

/**
* 메인컨테이너벨트 - 맨 앞쪽부터 빼내는 방식 = 큐
* 보조컨테이너벨트 - 후입선출의 방식 = 스택
*/
function solution(order) {
let result = 0;

const mainContainerBelt = new Queue();
const subContainerBelt = [];

for (let i = 1; i <= order.length; i += 1) {
mainContainerBelt.enqueue(i);
}

for (let now of order) {
while (mainContainerBelt.size && mainContainerBelt.peek < now) {
const keep = mainContainerBelt.dequeue();
subContainerBelt.push(keep);
}

if (mainContainerBelt.peek === now) {
mainContainerBelt.dequeue();

result += 1;

continue;
}

if (subContainerBelt.at(-1) === now) {
subContainerBelt.pop();

result += 1;

continue;
}

break;
}

return result;
}
```