728x90
https://school.programmers.co.kr/learn/courses/30/lessons/132265
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
메모리: 119 MB, 시간: 27.84 ms
사용 알고리즘: 구현
class Solution {
public int solution(int[] topping) {
int answer = 0;
// 형과 동생의 각 토핑 개수
int[] t1 = new int[10_001];
int[] t2 = new int[10_001];
// 형과 동생의 토핑 가짓수
int t1Count = 0;
int t2Count = 0;
// 모든 토핑을 형이 가졌을 때, 각 토핑 개수와 가짓수 구하기
for(int i = 0; i < topping.length; i++) {
if(t1[topping[i]] == 0) t1Count++;
t1[topping[i]]++;
}
// 하나씩 동생 주기
for(int i = 0; i < topping.length; i++) {
if(t1[topping[i]] == 1) t1Count--;
t1[topping[i]]--;
if(t2[topping[i]] == 0) t2Count++;
t2[topping[i]]++;
if(t1Count == t2Count) answer++;
}
return answer;
}
}728x90
'Problem Solving > Programmers' 카테고리의 다른 글
| [프로그래머스, 64065] 튜플 (java) (0) | 2025.12.15 |
|---|---|
| [프로그래머스, 12949] 행렬의 곱셈 (java) (0) | 2025.12.15 |
| [프로그래머스, 87390] n^2 배열 자르기 (java) (0) | 2025.12.13 |
| [프로그래머스, 76502] 괄호 회전하기 (java) (0) | 2025.12.12 |
| [프로그래머스, 131701] 연속 부분 수열 합의 개수 (java) (0) | 2025.12.11 |