본문 바로가기

반응형

1. 출력
https://school.programmers.co.kr/learn/courses/30/lessons/250133

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        String msg = "Spring is beginning";
        int val1 = 3;
        String val2 = "3";

        System.out.println(msg);
        System.out.println(val1 + 10);
        System.out.println(val2 + "10");
    }
}

 

2. 피타고라스의 정리
https://school.programmers.co.kr/learn/courses/30/lessons/250132

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int c = sc.nextInt();

        int b_square = c*c - a*a;

        System.out.println(b_square);
    }
}

 

3. 나이 계산
https://school.programmers.co.kr/learn/courses/30/lessons/250131

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();
        String age_type = sc.next();
        int answer = 0;

        if (age_type.equals("Korea")) {
            answer = 2030-year+1;
        }
        else if (age_type.equals("Year")) {
            answer = 2030-year;
        }

        System.out.println(answer);
    }
}

 

4. 저축
https://school.programmers.co.kr/learn/courses/30/lessons/250130

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int start = sc.nextInt();
        int before = sc.nextInt();
        int after = sc.nextInt();

        int money = start;
        int month = 1;
        while (money < 70) {
            money += before;
            month++;
        }
        while (money < 100) {
            money += after;
            month++;
        }

        System.out.println(month);
    }
}

 

5. 산책
https://school.programmers.co.kr/learn/courses/30/lessons/250129

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

class Solution {
    public int[] solution(String route) {
        int east = 0;
        int north = 0;
        int[] answer = new int [2];
        for(int i=0; i<route.length(); i++){
            switch(route.charAt(i)){
                case 'N':
                    north++;
                    break;
                case 'S':
                    north--;
                    break;
                case 'E':
                    east++;
                    break;
                case 'W':
                    east--;
                    break;
            }
        }
        answer[0] = east;
        answer[1] = north;
        return answer;
    }
}

조금만 생각하면 아주 간단한 문제입니다. 북쪽과 동쪽을 기준으로 ±1을 해주면 해결되는 문제입니다.

6. 가습기
https://school.programmers.co.kr/learn/courses/30/lessons/250127

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

class Solution {
    public int func1(int humidity, int val_set){
        if(humidity < val_set)
            return 3;
        return 1;
    }

    public int func2(int humidity){
        if(humidity >= 50)
            return 0;
        else if (humidity >= 40)
            return 1;
        else if (humidity >= 30)
            return 2;
        else if (humidity >= 20)
            return 3;
        else if (humidity >= 10)
            return 4;
        else 
            return 5;
    }

    public int func3(int humidity, int val_set){
        if(humidity < val_set)
            return 1;
        return 0;
    }

    public int solution(String mode_type, int humidity, int val_set) {
        int answer = 0;

        if(mode_type.equals("auto")){
            answer = func2(humidity);
        }
        else if(mode_type.equals("target")){
            answer = func1(humidity, val_set);
        }
        else if(mode_type.equals("minimum")){
            answer = func3(humidity, val_set);
        }

        return answer;
    }
}

 

7. 창고 정리
https://school.programmers.co.kr/learn/courses/30/lessons/250126

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

class Solution {
    public String solution(String[] storage, int[] num) {
        int num_item = 0;
        String[] clean_storage = new String[storage.length];
        int[] clean_num = new int[num.length];
        
        for(int i=0; i<storage.length; i++){
            int clean_idx = -1;
            for(int j=0; j<num_item; j++){
                if(storage[i].equals(clean_storage[j])){
                    clean_idx = j;
                    break;
                }
            }
            if(clean_idx == -1){
                clean_storage[num_item] = storage[i];
                clean_num[num_item] = num[i];
                num_item += 1;
            }
            else{
                clean_num[clean_idx] += num[i];
            }
        }
        
        // 아래 코드에는 틀린 부분이 없습니다.
        
        int num_max = -1;
        String answer = "";
        for(int i=0; i<num_item; i++){
            if(clean_num[i] > num_max){
                num_max = clean_num[i];
                answer = clean_storage[i];
            }
        }
        return answer;
    }
}

16번 중만 저렇게 바꿔주면 됩니다. 원래는 num[i]를 String으로 변환해주고 넣어줬지만, 문제에서 원하는 답은 "숫자"가 아니라 "문자" 니까요.

8. 이웃한 칸 (Lv 1)
https://school.programmers.co.kr/learn/courses/30/lessons/250125

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

class Solution {
    public int solution(String[][] board, int h, int w) {
        int answer = 0;
        
        int hMax = board.length-1;
        int wMax = board[0].length-1;
        
        if(h != 0 && board[h-1][w].equals(board[h][w])){
            answer++;
        }
        
        if(h != hMax && board[h+1][w].equals(board[h][w])){
            answer++;
        }
        
        if(w != 0 && board[h][w-1].equals(board[h][w])){
            answer++;
        }
        
        if(w != wMax && board[h][w+1].equals(board[h][w])){
            answer++;
        }
        
        return answer;
    }
}

우리가 원하는 답은 결국 인접한 같은 색상의 개수입니다. 각 방향으로 인접한 칸이 없는 경우와 같은 색상만 고려하면 되는 것이죠. 처음에 저는 하마터면 면과 모서리에 있는 모든 경우를 조건으로 줄 뻔 했습니다 ㅎㅎ;;

 

9. 데이터 분석 (Lv 1)
https://school.programmers.co.kr/learn/courses/30/lessons/250121

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

import java.util.Arrays;
import java.util.Comparator;

class Solution {
    public int[][] solution(int[][] data, String ext, int val_ext, String sort_by) {
        int hIdx = 0;
        
        if("code".equals(ext)){
            hIdx = 0;
        } else if("date".equals(ext)){
            hIdx = 1;
        } else if("maximum".equals(ext)){
            hIdx = 2;
        } else if("remain".equals(ext)){
            hIdx = 3;
        }
        
        int count = 0;
        for(int i = 0; i < data.length; i++){
            if(data[i][hIdx] < val_ext) count++;
        }
        
        int[][] answer = new int[count][4];
        
        int ansIdx = 0;
        for(int i = 0; i < data.length; i++){
            int cC = 0;
            for(int j = 0; j < 4; j++){
                if(data[i][hIdx] < val_ext){
                    answer[ansIdx][j] = data[i][j];
                    cC++;
                }
            }
            if(cC > 0) ansIdx++;
        }
        
        int sIdx = 0;
        
        if("code".equals(sort_by)){
            sIdx = 0;
        } else if("date".equals(sort_by)){
            sIdx = 1;
        } else if("maximum".equals(sort_by)){
            sIdx = 2;
        } else if("remain".equals(sort_by)){
            sIdx = 3;
        }
        
        final int S_Idx = sIdx;
        
        // Final 변수를 사용해야 함
        Arrays.sort(answer, Comparator.comparingInt(a -> a[S_Idx]));
        
        return answer;
    }
}

제 코드의 흐름은 다음과 같습니다.
1. 주어진 조건(ext)에 맞는 1차원 배열의 개수를 센다.
2. 1번 숫자 크기의 반환할 2차원 배열을 선언한다.
3. 2차원 배열에 주어진 조건에 맞는 배열을 대입한다.
4. 주어진 조건(sort_by)을 기준으로 오름차순 정렬을 한다.

제가 이 문제를 풀며 고민했던 부분은 어떻게 과정을 더 줄일 수 있을까? 였습니다. 배열의 크기는 변하지 않으므로 먼저 for문을 사용해서 크기를 정하고 배열의 크기를 지정해주고 다시 또 똑같지만 내부 내용이 살짝 다른 for문을 돌려줘야 하죠. 저는 이게 낭비라고 생각했습니다. for문 한번에 할 수 없을까? 라는 고민 때문에 낮은 레벨에도 불구하고 시간을 오래 쓴 문제가 되었습니다. 

그리고 고생했던 부분은 제가 아직 람다식 표현을 잘 몰라서 final을 사용하지 않고 쓰려고 해서 생긴 에러였습니다.

반응형
댓글