Skip to content

Commit e1d98ad

Browse files
committed
123차 2번 문제풀이
1 parent 6b29244 commit e1d98ad

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const input = require('fs')
2+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
3+
.toString()
4+
.trim()
5+
.split('\n')
6+
.map((el) => el.split(' ').map(Number));
7+
8+
const N = input[0][0];
9+
const lines = input.slice(1).sort((a, b) => a[0] - b[0]);
10+
11+
let totalLength = 0;
12+
let currentStart = lines[0][0];
13+
let currentEnd = lines[0][1];
14+
15+
for (let i = 1; i < N; i++) {
16+
const [start, end] = lines[i];
17+
18+
if (start <= currentEnd) {
19+
currentEnd = Math.max(currentEnd, end);
20+
} else {
21+
totalLength += currentEnd - currentStart;
22+
currentStart = start;
23+
currentEnd = end;
24+
}
25+
}
26+
27+
totalLength += currentEnd - currentStart;
28+
29+
console.log(totalLength);

0 commit comments

Comments
 (0)