본문 바로가기

WINK-(Web & App)/알고리즘 스터디

[2025 1학기 알고리즘 스터디] 김민재 #3주차

반응형

동전 0

import java.util.Scanner;

public class makeZero {

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

        int n = sc.nextInt();
        int k = sc.nextInt();
        int cnt = 0;

        int[] coins = new int[n];

        for (int i = 0; i < n; i++) {
            coins[i] = sc.nextInt();
        }

        for (int i = n-1; i >= 0; i--) {

            if (coins[i] <= k){
                cnt += k / coins[i];
                k = k % coins[i];
            }
        }

        System.out.println(cnt);

    }
}

 

 

잃어버린 괄호

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class lostBrackets {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();

        // 식을 리스트에 넣기
        List<String> list = new ArrayList<>();
        StringBuilder num = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c == '+' || c == '-') {
                list.add(num.toString());
                list.add(String.valueOf(c));
                num.setLength(0);
            } else {
                num.append(c);
            }
        }
        list.add(num.toString());

        // "-"가 나오는 idx 찾기
        int idx = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals("-")) {
                idx = i;
                break;
            }
        }

        // "-"가 없을 때
        if (idx == 0) {
            int sum = Integer.parseInt(list.get(0));
            for (int i = 2; i < list.size(); i += 2) {
                sum += Integer.parseInt(list.get(i));
            }
            System.out.println(sum);
        // "-"가 있을 때
        } else {
            int firstSum = Integer.parseInt(list.get(0));
            for (int i = 2; i < idx; i += 2) {
                firstSum += Integer.parseInt(list.get(i));
            }

            int secondSum = 0;
            for (int i = idx + 1; i < list.size(); i += 2) {
                secondSum += Integer.parseInt(list.get(i));
            }

            int result = firstSum - secondSum;
            System.out.println(result);
        }


    }
}

 

 

회의실 배정

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

public class meetingRoom {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[][] rooms = new int[n][2];

        // 회의실 배정
        for (int i = 0; i < n; i++) {
            rooms[i][0] = scanner.nextInt();
            rooms[i][1] = scanner.nextInt();
        }

        // 오름 차순으로 정렬 (람다 함수)
        Arrays.sort(rooms, (a, b) -> {
            if (a[1] == b[1]) {
                return a[0] - b[0];
            }
            return a[1] - b[1];
        });

        int count = 0;
        int lastEndtime = 0;

        // 이전 회의의 종료 시간보다 이상이면 count +1, 종료 시간 업데이트
        for (int[] room : rooms) {
            if (room[0] >= lastEndtime) {
                count++;
                lastEndtime = room[1];
            }
        }

        System.out.println(count);
    }
}

 

 

반응형