Skip to content

Commit ed078d9

Browse files
committed
Feat: 7주차 문제
1 parent 7617a03 commit ed078d9

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

Diff for: 7주차/박종운/골드4__도서관.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const local_input = `
2+
7 2
3+
-37 2 -6 -39 -29 11 -28
4+
`;
5+
6+
const input = process.execArgv.includes("--stack-size=65536")
7+
? require("fs").readFileSync("dev/stdin").toString()
8+
: local_input;
9+
10+
const lines = input.trim().split("\n");
11+
const size = lines[0].split(' ').map(Number)[1];
12+
const positions = lines[1].split(' ').map(Number).sort((a,b) => a-b);
13+
14+
// console.log(positions);
15+
16+
const posNums = [];
17+
const negNums = [];
18+
19+
const steps = [];
20+
21+
positions.forEach(el => el > 0 ? posNums.push(el) : negNums.push(el));
22+
23+
negNums.reverse();
24+
25+
while(posNums.length) {
26+
const group = [posNums.pop()];
27+
28+
while(group.length < size && posNums.length) {
29+
group.push(posNums.pop());
30+
}
31+
32+
steps.push(group[0]);
33+
}
34+
35+
while(negNums.length) {
36+
const group = [negNums.pop()];
37+
38+
while(group.length < size && negNums.length) {
39+
group.push(negNums.pop());
40+
}
41+
42+
steps.push(Math.abs(group[0]))
43+
}
44+
45+
let sum = 0;
46+
47+
steps.forEach(el => sum+=el*2);
48+
49+
console.log(sum - Math.max(...steps ?? [0]))

Diff for: 7주차/박종운/골드5__강의실.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const local_input = `
2+
8
3+
6 15 21
4+
7 20 25
5+
1 3 8
6+
3 2 14
7+
8 6 27
8+
2 7 13
9+
4 12 18
10+
5 6 20
11+
`;
12+
13+
const input = process.execArgv.includes("--stack-size=65536")
14+
? require("fs").readFileSync("dev/stdin").toString()
15+
: local_input;
16+
17+
const lines = input.trim().split("\n");
18+
const sorted = lines
19+
.slice(1)
20+
.map(el => el.split(' ').map(Number))
21+
.sort((a, b) => a[1] - b[1]);
22+
23+
// console.log(sorted);
24+
25+
class MinHeap {
26+
constructor() {
27+
this.heap = [];
28+
}
29+
30+
push(value) {
31+
this.heap.push(value);
32+
let idx = this.heap.length - 1;
33+
34+
while (idx > 0 && this.heap[idx] < this.heap[this.getParent(idx)]) {
35+
this.swap(idx, this.getParent(idx));
36+
idx = this.getParent(idx);
37+
}
38+
}
39+
40+
getParent(index) {
41+
return Math.floor((index - 1) / 2);
42+
}
43+
44+
getLeft(index) {
45+
return index * 2 + 1;
46+
}
47+
48+
getRight(index) {
49+
return index * 2 + 2;
50+
}
51+
52+
swap(index1, index2) {
53+
const temp = this.heap[index1];
54+
this.heap[index1] = this.heap[index2];
55+
this.heap[index2] = temp;
56+
}
57+
58+
pop() {
59+
if (!this.heap.length) {
60+
return null;
61+
}
62+
63+
const min = this.heap[0];
64+
const last = this.heap.pop();
65+
66+
if (this.heap.length) {
67+
this.heap[0] = last;
68+
69+
let idx = 0;
70+
71+
while (this.getLeft(idx) < this.heap.length) {
72+
const left = this.getLeft(idx);
73+
const right = this.getRight(idx);
74+
75+
let minChild = left;
76+
77+
if (right < this.heap.length && this.heap[right] < this.heap[left]) {
78+
minChild = right;
79+
}
80+
81+
if (this.heap[idx] < this.heap[minChild]) {
82+
break;
83+
}
84+
85+
this.swap(idx, minChild);
86+
idx = minChild;
87+
}
88+
}
89+
90+
return min;
91+
}
92+
}
93+
94+
const minHeap = new MinHeap();
95+
96+
let result = 0;
97+
98+
sorted.forEach(el => {
99+
minHeap.push(el[2]);
100+
101+
if(minHeap.heap[0] <= el[1]) {
102+
minHeap.pop();
103+
} else {
104+
result++;
105+
}
106+
})
107+
108+
console.log(result)

Diff for: 7주차/박종운/실버1__접두사.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const local_input = `
2+
7
3+
ca
4+
cade
5+
caed
6+
cae
7+
coff
8+
c
9+
cb
10+
`;
11+
12+
const input = process.execArgv.includes("--stack-size=65536")
13+
? require("fs").readFileSync("dev/stdin").toString()
14+
: local_input;
15+
16+
const lines = input.trim().split("\n");
17+
const sorted = lines.slice(1).sort();
18+
19+
// console.log(sorted);
20+
21+
const result = [];
22+
23+
while(sorted.length) {
24+
const cur = sorted.pop();
25+
26+
if(!result.length) {
27+
result.push(cur);
28+
} else {
29+
const find = result.find(el => el.match(`^${cur}`));
30+
31+
if(!find) {
32+
result.push(cur);
33+
}
34+
}
35+
}
36+
37+
console.log(result.length);

0 commit comments

Comments
 (0)