Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions gyuiin/16472.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.io.*;

public class Main {
static int[] arr = new int[26]; // 각 알파벳 문자의 개수 저장 (a~z)
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String string = br.readLine();

int cnt = 0; // 윈도우 안에서 서로 다른 문자 종류의 수
int a = 0; // 조건을 만족하는 가장 긴 부분 문자열의 길이

int start = 0;
int end = 0;

for(start = 0, end = 0; end < string.length(); end++) { // 슬라이딩 윈도우
if(arr[string.charAt(end) - 'a']++ == 0) {
cnt++;
}

while (N < cnt && start < end) {
if (--arr[string.charAt(start++) - 'a'] == 0) cnt--;
}
a = Math.max(a, end - start + 1);
}
System.out.println(a);
}
}

34 changes: 34 additions & 0 deletions gyuiin/1806.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.io.*;
import java.util.*;

public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int n = Integer.parseInt(st.nextToken()); // 배열의 길이
int s = Integer.parseInt(st.nextToken()); // 목표 합

int[] arr = new int[n+1]; // 사용자의 입력을 저장
st = new StringTokenizer(br.readLine());
for(int i=0; i<n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}

int start = 0;
int end = 0;
int len = Integer.MAX_VALUE; // 부분 배열의 최소 길이
int sum = 0; // 시작과 끝 배열의 합

while(start <= end && end <= n) { // 슬라이딩 윈도우
if(sum < s) {
sum += arr[end++];
} else if(sum >= s) {
len = Math.min(len, end-start);
sum -= arr[start++];
}
}
System.out.println(len==Integer.MAX_VALUE ? 0 : len);
}
}

54 changes: 54 additions & 0 deletions gyuiin/2531.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.io.*;
import java.util.*;

public class Main {
static StringTokenizer st;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());

int N = Integer.parseInt(st.nextToken()); // 벨트에 놓인 접시의 수
int d = Integer.parseInt(st.nextToken()); // 초밥의 가짓수
int k = Integer.parseInt(st.nextToken()); // 연속해서 먹는 접시의 수
int c = Integer.parseInt(st.nextToken()); // 쿠폰 번호

int[] list=new int[N + k - 1];
for (int i = 0; i < N; i++) { // 벨트에 놓인 접시들 번호 입력 받기
list[i] = Integer.parseInt(br.readLine());
}

for (int i = 0; i < k-1; i++) { // 벨트의 원형으로 처리
list[N++] = list[i];
}

int[] eaten = new int[d + 1]; // 각 초밥 번호가 몇 번 먹혔는지
int max = 1; // 먹은 초밥의 최대 종류의 수
eaten[c] += 1;

int start = 0;
for (int i = start; i < k; i++) { // 초밥 확인, 종류 카운트 후 배열 업데이트
if(eaten[list[i]] == 0) {
max++;
}
eaten[list[i]] += 1;
}

// 슬라이딩 윈도우
start=0;
int end = k;

int result = max;
for (int i = end; i < list.length; i++) {
eaten[list[start]] -= 1;
if(eaten[list[start]] == 0) {
result -= 1;
}
if(eaten[list[i]] == 0) result += 1;
eaten[list[i]] += 1;
max = Math.max(max, result);
start++;
}

System.out.println(max);
}
}