전공공부
[BOJ_20365] 블로그2 본문
설명
간단한 그리디 문제였습니다. 연속되는 알파벳의 갯수를 구한 후 연속되는 알파벳의 List 설정을 연속된 경우를 한 경우로 치고 이를 List에 관리하여 List Size로 연속된 알파벳 갯수를 구하고 한 번에 칠해지는 경우 1 경우를 체크하여 이에 대해서 각각 B의 경우 R의 경우를 나누어서 마지막에 대소비교해서 최소로 작은 경우의 수를 출력하게끔 구성해서 풀었습니다.
코드
package BOJ.greedy;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class BOJ_20365 {
public static void main(String[] args) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(in.readLine());
String str = in.readLine();
int Bcnt = 0;
int Acnt = 0;
List<Integer> aList = new ArrayList<>();
List<Integer> bList = new ArrayList<>();
for (int i = 0; i < N; i++){
if(str.charAt(i) == 'B'){
Bcnt++;
if(i - 1 >= 0 && str.charAt(i - 1) == 'B'){
int bIdx = (bList.size() - 1);
bList.set(bIdx,bList.get(bIdx) + 1);
}else{
bList.add(1);
}
}else{
Acnt++;
if(i - 1 >= 0 && str.charAt(i - 1) == 'R'){
int aIdx = (aList.size() - 1);
aList.set(aIdx,aList.get(aIdx) + 1);
}else{
aList.add(1);
}
}
}
System.out.println(Math.min(1 + bList.size(), 1 + aList.size()));
}
}
20365번: 블로그2
neighbor 블로그를 운영하는 일우는 매일 아침 풀고 싶은 문제를 미리 정해놓고 글을 올린다. 그리고 매일 밤 각각의 문제에 대하여, 해결한 경우 파란색, 해결하지 못한 경우 빨간색으로 칠한
www.acmicpc.net
'Study > Problem Solving' 카테고리의 다른 글
[BOJ_20438] 출석 체크 (0) | 2024.04.28 |
---|---|
[BOJ_11000] 강의실 배정 (0) | 2024.04.07 |
[BOJ_13305] 주유소 (0) | 2024.04.01 |
[BOJ_1758] 알바생 강호 (1) | 2024.03.29 |
[BOJ_1343] 폴리오미노 (0) | 2024.03.27 |