롤케이크 자르기
철수는 롤케이크를 두 조각으로 잘라서 동생과 한 조각씩 나눠 먹으려고 합니다.
이 롤케이크에는 여러가지 토핑들이 일렬로 올려져 있습니다.
철수와 동생은 롤케이크를 공평하게 나눠먹으려 하는데,
그들은 롤케이크의 크기보다 롤케이크 위에 올려진 토핑들의 종류에 더 관심이 많습니다.
그래서 잘린 조각들의 크기와 올려진 토핑의 개수에 상관없이
각 조각에 동일한 가짓수의 토핑이 올라가면
공평하게 롤케이크가 나누어진 것으로 생각합니다.
예를 들어,
롤케이크에 4가지 종류의 토핑이 올려져 있다고 합시다.
토핑들을 1, 2, 3, 4와 같이 번호로 표시했을 때,
케이크 위에 토핑들이 [1, 2, 1, 3, 1, 4, 1, 2] 순서로 올려져 있습니다.
만약 세 번째 토핑(1)과 네 번째 토핑(3) 사이를 자르면
롤케이크의 토핑은 [1, 2, 1], [3, 1, 4, 1, 2]로 나뉘게 됩니다.
철수가 [1, 2, 1]이 놓인 조각을,
동생이 [3, 1, 4, 1, 2]가 놓인 조각을 먹게 되면
철수는 두 가지 토핑(1, 2)을 맛볼 수 있지만,
동생은 네 가지 토핑(1, 2, 3, 4)을 맛볼 수 있으므로,
이는 공평하게 나누어진 것이 아닙니다.
만약 롤케이크의 네 번째 토핑(3)과 다섯 번째 토핑(1)
사이를 자르면 [1, 2, 1, 3], [1, 4, 1, 2]로 나뉘게 됩니다.
이 경우 철수는 세 가지 토핑(1, 2, 3)을,
동생도 세 가지 토핑(1, 2, 4)을 맛볼 수 있으므로,
이는 공평하게 나누어진 것입니다.
공평하게 롤케이크를 자르는 방법은 여러가지 일 수 있습니다.
위의 롤케이크를 [1, 2, 1, 3, 1],
[4, 1, 2]으로 잘라도 공평하게 나뉩니다.
어떤 경우에는 롤케이크를 공평하게 나누지 못할 수도 있습니다.
롤케이크에 올려진 토핑들의 번호를 저장한 정수 배열 topping이 매개변수로 주어질 때,
롤케이크를 공평하게 자르는 방법의 수를 return 하도록 solution 함수를 완성해주세요.
제한사항
- 1 ≤ topping의 길이 ≤ 1,000,000
- 1 ≤ topping의 원소 ≤ 10,000
알고리즘
X
아이디어
- Map으로 미리 카운트 해서 그 결과를 정리하면서 keySet이 같아질 경우를 세면 되지 않을까?
- 배열 두 개를 두고, 각 요소까지 잘라졌을 때 원소의 개수를 기록한다. 이후 두 배열을 비교한다.
풀이
class RollCakeCutting {
@Nested
public class TestCases {
@Test
public void case1 () {
int[] topping = new int[]{1, 2, 1, 3, 1, 4, 1, 2};
int result = 2;
Assertions.assertEquals(result, solution(topping));
}
@Test
public void case2 () {
int[] topping = new int[]{1, 2, 3, 1, 4};
int result = 0;
Assertions.assertEquals(result, solution1(topping));
Assertions.assertEquals(result, solution2(topping));
}
@Test
public void case3 () {
int[] topping = new int[]{1, 2, 3, 4};
int result = 1;
Assertions.assertEquals(result, solution1(topping));
Assertions.assertEquals(result, solution2(topping));
}
@Test
public void case4 () {
int[] topping = new int[]{2, 1, 1, 1, 3};
int result = 2;
Assertions.assertEquals(result, solution1(topping));
Assertions.assertEquals(result, solution2(topping));
}
}
//1. useMap
public int solution1(int[] topping) {
Map<Integer, Integer> leftSide = new HashMap<>();
Map<Integer, Integer> rightSide = Arrays.stream(topping).boxed()
.collect(Collectors.toMap(Function.identity(), integer -> 1, (integer, integer2) -> integer + integer2));
int count = 0;
for( int i = 0; i < topping.length; i ++ ) {
Integer number = topping[i];
leftSide.putIfAbsent(number, 1);
leftSide.computeIfPresent(number, (k, v) -> leftSide.get(k) + 1);
Integer right = rightSide.get(number);
if(right <= 1) rightSide.remove(number);
else rightSide.put(number, right - 1);
if( leftSide.keySet().size() == rightSide.keySet().size()) count ++ ;
}
return count;
}
//2. useSet
public int solution2(int[] topping) {
int answer = 0;
Set<Integer> set = new HashSet<>();
int[] partA = new int[topping.length];
int[] partB = new int[topping.length];
for ( int i = 0; i < topping.length; i ++ ) {
set.add(topping[i]);
partA[i] = set.size();
}
set.clear();
for ( int i = topping.length - 1; i >= 0 ; i -- ) {
set.add(topping[i]);
partB[i] = set.size();
}
for ( int i = 0; i < partA.length - 1; i ++ ) {
if( partA[i] == partB[i + 1] ) answer += 1;
}
return answer;
}
}