Skip to content

Commit a1b7fce

Browse files
committed
[Silver IV] Title: 숫자 카드 2, Time: 960 ms, Memory: 151616 KB -BaekjoonHub
1 parent 395db81 commit a1b7fce

2 files changed

Lines changed: 36 additions & 31 deletions

File tree

백준/Silver/10816. 숫자 카드 2/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 223728 KB, 시간: 1644 ms
7+
메모리: 151616 KB, 시간: 960 ms
88

99
### 분류
1010

1111
자료 구조, 정렬, 이분 탐색, 해시를 사용한 집합과 맵
1212

1313
### 제출 일자
1414

15-
2026년 2월 23일 21:56:34
15+
2026년 2월 24일 12:24:32
1616

1717
### 문제 설명
1818

백준/Silver/10816. 숫자 카드 2/숫자 카드 2.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,60 @@
1-
// /dev/stdin
2-
const fs = require("fs");
1+
/* /dev/stdin */
2+
let fs = require("fs");
33
let input = fs
44
.readFileSync("/dev/stdin")
55
.toString()
66
.split("\n")
7-
.map((x) => x.replace("\r", ""));
7+
.map((x) => x.replaceAll("\r", ""));
88

99
const N = Number(input[0]);
10-
const numberCards = input[1].split(" ").map(Number);
11-
const M = Number(input[2]);
12-
let myCards = input[3]
10+
const myCards = input[1]
1311
.split(" ")
1412
.map(Number)
1513
.sort((a, b) => a - b);
1614

17-
const maps = new Map();
15+
const M = Number(input[2]);
16+
const targetCards = input[3].split(" ").map(Number);
17+
const answer = [];
18+
19+
for (const targetCard of targetCards) {
20+
const lower = lowerBound(myCards, targetCard);
21+
const upper = upperBound(myCards, targetCard);
1822

19-
for (const key of myCards) {
20-
maps[key] = 0;
23+
const result = upper - lower;
24+
answer.push(result);
2125
}
26+
console.log(answer.join(" "));
2227

23-
let answer = "";
28+
function lowerBound(myCards, targetCard) {
29+
let left = 0;
30+
let right = myCards.length;
2431

25-
for (const x of numberCards) {
26-
const result = binarySearch(myCards, x);
27-
if (result === 1) {
28-
maps[x]++;
32+
while (left < right) {
33+
const mid = Math.floor((left + right) / 2);
34+
35+
if (myCards[mid] >= targetCard) {
36+
right = mid;
37+
} else {
38+
left = mid + 1;
39+
}
2940
}
41+
42+
return left;
3043
}
3144

32-
function binarySearch(cards, target) {
45+
function upperBound(myCards, targetCard) {
3346
let left = 0;
34-
let right = cards.length - 1;
47+
let right = myCards.length;
3548

36-
while (left <= right) {
49+
while (left < right) {
3750
const mid = Math.floor((left + right) / 2);
3851

39-
if (cards[mid] === target) {
40-
return 1;
41-
} else if (cards[mid] < target) {
42-
left = mid + 1;
52+
if (myCards[mid] > targetCard) {
53+
right = mid;
4354
} else {
44-
right = mid - 1;
55+
left = mid + 1;
4556
}
4657
}
4758

48-
return 0;
49-
}
50-
myCards = input[3].split(" ").map(Number);
51-
for (const number of myCards) {
52-
answer += maps[number] + " ";
59+
return left;
5360
}
54-
55-
console.log(answer);

0 commit comments

Comments
 (0)