๐ŸŒžAlgorithm/๐Ÿ”ฅprogrammers

[programmers] ๋กค์ผ€์ดํฌ ์ž๋ฅด๊ธฐ

๋ฟŒ์•ผ._. 2026. 6. 26. 10:39
๋ฌธ์ œ
https://school.programmers.co.kr/learn/courses/30/lessons/132265
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก์˜ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 


< ๋กค์ผ€์ดํฌ ์ž๋ฅด๊ธฐ >

 

๋ฌธ์ œ ํ’€์ด (Java)

class Solution {
	public int solution(int[] topping) {
		int answer = 0;

		int arr1[] = new int[10001];
		int arr2[] = new int[10001];

		int cnt1 = 0;
		int cnt2 = 0;

		for (int i = 0; i < topping.length; i++) {
			if (arr1[topping[i]] == 0) {
				cnt1 += 1;
			}
			arr1[topping[i]] += 1;
		}

		for (int i = 0; i < topping.length; i++) {
			arr1[topping[i]] -= 1;
			if (arr1[topping[i]] == 0) {
				cnt1 -= 1;
			}

			if (arr2[topping[i]] == 0) {
				cnt2 += 1;
			}
			arr2[topping[i]] += 1;

			if (cnt1 == cnt2) {
				answer += 1;
			}
		}
		return answer;
	}
}

 

์ผ๋‹จ ๋กค์ผ€์ดํฌ ์ž๋ฅด๊ธฐ ์ „ ์ „์ฒด๋ฅผ ์ฒ ์ˆ˜๊บผ๋ผ ์ƒ๊ฐํ•˜๊ณ  ํ† ํ•‘ ์ˆ˜๋ฅผ ์„ผ๋‹ค. ๊ทธ ํ›„ ์•ž์—์„œ๋ถ€ํ„ฐ ํ† ํ•‘ ํ•˜๋‚˜์”ฉ ๋™์ƒํ•œํ…Œ ์ฃผ๋ฉด์„œ ํ† ํ•‘ ์ˆ˜๊ฐ€ ๊ฐ™์„ ๋•Œ์˜ ์ˆ˜๋ฅผ ์„ผ๋‹ค. 

 



 

์ถœ์ฒ˜: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ์—ฐ์Šต, 
https://school.programmers.co.kr/learn/challenges