Skip to content

[1주차] 배수빈 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 14, 2024
Prev Previous commit
Next Next commit
배수빈: [BOJ] 2531 회전초밥_240910
  • Loading branch information
baexxbin committed Sep 10, 2024
commit 935dff69672b727ea2b802b700980c4b2ac01267
53 changes: 53 additions & 0 deletions BOJ/1000-10000번/SB_2531_회전초밥.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/*
* 회전 초밥
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

초밥 먹고 싶어요

* 문제: 최대로 초밥먹을 수 있는 갯수 구하기
* */
public class SB_2531_회전초밥 {
static int N, D, K, C;
static int[] foods;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
D = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());

foods = new int[N];
for (int i = 0; i < N; i++) {
foods[i] = Integer.parseInt(br.readLine());
}

int left = 0;
int right = 0;
int mx = 0;

HashMap<Integer, Integer> eat = new HashMap<>();
eat.put(foods[left], 1);
while (left < N) {
while (right - left < K-1) {
right++;
if (foods[right%N]==C) continue;
eat.put(foods[right%N], eat.getOrDefault(foods[right%N], 0) + 1);
}
mx = Math.max(mx, eat.size());

eat.put(foods[left], eat.getOrDefault(foods[left], 0) - 1);
if (eat.get(foods[left]) <= 0) {
eat.remove(foods[left]);
}
left++;
}
System.out.println(mx+1);
}
}

/*
* 회전초밥 일자가 아니라 원형으로 되어있음
* */