문제 - 1052
처음에 어떻게 접근을 해야될지 몰라서 고민을 하다가 힌트와 topic을 보고 window sliding이라는 것을 알게되었다.
그래서 윈도우의 크기는 minutes으로 고정설정하여 불만족하는 고객들의 수를 탐색하였다.
그림과 같이 예시에서 불만족하는 최대의 고객 수는 6명인걸 알 수 있다.
해당 6명과 현재 행복한 고객의 수 10명을 더하여 출력
import java.util.*;
class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
int curW = 0, maxW =0,happy = 0;
int n = customers.length;
//minutes안에 불만족하는 고객들의 최대를 추출
for(int i=0;i<n;i++)
{
//불만족하는 고객들
curW += grumpy[i]*customers[i];
happy += grumpy[i] == 0 ? customers[i] : 0; //만족하는 고객들
//범위 밖에 있는 고객들 제외
if(i >= minutes)
{
curW -= grumpy[i-minutes]*customers[i-minutes];
}
maxW = Math.max(curW,maxW);
}
//불만족하는 고객들을 만족하게 바꾸고 기존 만족했던 고객들과 더해서 결과 추출
return happy + maxW;
}
}