매일 Algorithm

신재원·2023년 7월 7일
0

Algorithm

목록 보기
160/243

백준 20300번

import java.util.Arrays;
import java.util.Scanner;

public class problem505 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        long[] arr = new long[size];

        for (int i = 0; i < size; i++) {
            arr[i] = in.nextLong();
        }
        Arrays.sort(arr);

		// 길이가 홀수인 경우 최소 근손실은 배열의 마지막 값
        long result = arr[size - 1]; 

        // 길이가 짝수일 경우
        if (size % 2 == 0) {
            for (int i = 0; i < size / 2; i++) {
                long value = arr[i] + arr[size - i - 1];
                if(result < value) result = value;
            }
        }
        // 길이가 홀수일 경우
        else{
            for (int i = 0; i < size / 2; i++) {
                long value = arr[i] + arr[size - i - 2];
                if(result < value) result = value;
            }
        }
        System.out.println(result);
    }
}

백준 20365번

import java.util.Scanner;

public class problem506 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        in.nextLine();
        String str = in.next();

        int bCount = 0;
        int rCount = 0;
        char compare = ' '; // 문자를 담을 변수
        for (int i = 0; i < size; i++) {
            char ch = str.charAt(i);
            // 연속된 문자인지 확인
            if (ch != compare) {
                if (ch == 'B') bCount++;
                else rCount++;
            }
            compare = ch;
        }

        System.out.println(Math.min(bCount, rCount) + 1);
    }
}

0개의 댓글