๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/131127
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
< ํ ์ธ ํ์ฌ >
๋ฌธ์ ํ์ด (Java)
import java.util.*;
class Solution {
public int solution(String[] want, int[] number, String[] discount) {
int answer = 0;
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < 10; i++) {
if (map.containsKey(discount[i])) {
map.put(discount[i], map.get(discount[i]) + 1);
} else {
map.put(discount[i], 1);
}
}
boolean flag = false;
for (int i = 0; i < want.length; i++) {
if (!map.containsKey(want[i]) || map.get(want[i]) != number[i]) {
flag = true;
break;
}
}
if (!flag) {
answer += 1;
}
for (int i = 10; i < discount.length; i++) {
map.put(discount[i - 10], map.get(discount[i - 10]) - 1);
if (map.containsKey(discount[i])) {
map.put(discount[i], map.get(discount[i]) + 1);
} else {
map.put(discount[i], 1);
}
flag = false;
for (int j = 0; j < want.length; j++) {
if (!map.containsKey(want[j]) || map.get(want[j]) != number[j]) {
flag = true;
break;
}
}
if (!flag) {
answer += 1;
}
}
return answer;
}
}
10์ผ ๋์ ํ์ ์๊ฒฉ์ ๋ถ์ฌํ๋ฏ๋ก ์ผ๋จ ์ฒ์ 10์ผ ๋์์ ํ ์ธํ๋ ์ ํ๊ณผ ๊ฐ์๋ฅผ ๊ตฌํด HashMap์ ์ ์ฅํ๋ค. ์ ํ์ด๊ฐ ์ํ๋ ์ ํ๊ณผ ์๋์ด ์ผ์นํ๋ค๋ฉด answer+1์ ํ๋ค. ์ดํ 10์ผ์ฉ ์ดํด๋ณด๊ธฐ ์ํด ๊ฐ์ฅ ์์ ํ๋ฃจ์ ํ ์ธํ๋ ์ ํ์ ๋นผ๊ณ ๋ค์ ํ๋ฃจ์ ํ ์ธํ๋ ์ ํ์ ๋ํ๋ค. ์ ํ์ด๊ฐ ์ํ๋ ์ ํ๊ณผ ์๋์ด ์ผ์นํ๋ค๋ฉด answer+1์ ํ๋ฉฐ, ์ด ๊ณผ์ ์ ๋ง์ง๋ง๋ ๊น์ง ๋ฐ๋ณตํ๋ค. ์ต์ข answer์ ๋ฐํํ๋ค.

์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉ ํ ์คํธ ์ฐ์ต,
https://school.programmers.co.kr/learn/challenges
'๐Algorithm > ๐ฅprogrammers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [programmers] ๋กค์ผ์ดํฌ ์๋ฅด๊ธฐ (0) | 2026.06.26 |
|---|---|
| [programmers] ์ซ์ ์ง๊ฟ (0) | 2026.06.25 |
| [programmers] ์ซ์์ ํํ (0) | 2026.06.23 |
| [programmers] ์ฐ์ ๋ถ๋ถ ์์ด ํฉ์ ๊ฐ์ (0) | 2026.06.22 |
| [programmers] ๋ค์ ํฐ ์ซ์ (0) | 2026.06.19 |