매일 Algorithm

신재원·2023년 4월 29일
0

Algorithm

목록 보기
110/243

프로그래머스 (명예의 전당)

import java.util.*;
public class problem359 {
    class Solution {
        public int[] solution(int k, int[] score) {
            int[] answer = new int[score.length];
            List<Integer> scoreList = new ArrayList<>();


            for(int i = 0; i < score.length; i++){
                scoreList.add(score[i]);

                // scoreList의 길이가 k와 같거나 큰경우
                // List의 최소값을 제거해줍니다.
                if(scoreList.size() > k) {
                    scoreList.remove(Collections.min(scoreList));
                }
                answer[i] = Collections.min(scoreList);
            }


            return answer;
        }
    }
}

백준 8595번 (Bronze 1)

import java.util.Scanner;

public class problem360 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        String inputString = in.next();
        long sum = 0;
        long result = 0;
        for (int i = 0; i < size; i++) {
            char temp = inputString.charAt(i);

            // 숫자인지 검증
            if (Character.isDigit(temp)) {
                sum = sum * 10 + (temp - '0');
            }
            // 숫자가 아닐경우
            else {
                result += sum;
                sum = 0; // 초기화
            }
        }

        // 반복문의 길이에 조건을 걸었기때문에 마지막 숫자가 더해지지않는다.
        result += sum;
        System.out.println(result);
    }
}

백준 9455번 (Bronze 1)

import java.util.Scanner;

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

        int test = in.nextInt(); // 테스트 갯수

        for (int i = 0; i < test; i++) {
            int m = in.nextInt(); // 세로 (열)
            int n = in.nextInt(); // 가로 (행)

            int[][] box = new int[m][n];

            for (int j = 0; j < m; j++) {
                for (int k = 0; k < n; k++) {
                    box[j][k] = in.nextInt(); // 박스 칸 입력
                }
            }

            int moveCount = 0;

            for (int j = 0; j < n; j++) {
                int flag = m - 1; // 세로칸에서 0이 있는지 flag
                // 아래에서 부터 1이 있는지 검증
                for (int k = m - 1; k >= 0; k--) {
                    if (box[k][j] == 1) {
                        // 0이 있는 칸까지 끌어 내려야된다.
                        moveCount += flag - k;
                        flag--; // 현재 아래에 1이 있으니 감소
                    }
                }
            }
            System.out.println(moveCount);
        }
    }
}

0개의 댓글