코딩테스트-알고리즘/도전

2024.01.09 - 9문제

닉네임생각즁 2024. 1. 9. 23:37

 

주사위 게임 3

https://school.programmers.co.kr/learn/courses/30/lessons/181916

 

프로그래머스

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

programmers.co.kr

import java.util.*;

class Solution {
    public int solution(int a, int b, int c, int d) {
        int answer = 0;
        int[] dice = {a, b, c, d};
        Arrays.sort(dice);
        
        if(dice[0] == dice[3]) { // 모두 p
            answer = 1111 * dice[0];
        } else if(dice[0] == dice[2]) { // dice[0] : p, dice[3] : q
            answer = (10 * dice[0] + dice[3]) * (10 * dice[0] + dice[3]);
        } else if(dice[1] == dice[3]) { // diece[1] : p, dice[0] : q
            answer = (10 * dice[1] + dice[0]) * (10 * dice[1] + dice[0]);
        } else if(dice[0] == dice[1] && dice[2] == dice[3]) { // dice[0] : p, dice[2] : q
            answer = (dice[0] + dice[2]) * Math.abs(dice[0] - dice[2]);
        } else if (dice[0] == dice[1]) { // dice[0] : p, dice[2] : q, dice[3] : r
            answer = dice[2] * dice[3];
        } else if (dice[1] == dice[2]) { // dice[1] : p, dice[0] : q, dice[3] : r
            answer = dice[0] * dice[3];
        } else if (dice[2] == dice[3]) { // dice[2] : p, dice[0] : q, dice[1] : r
            answer = dice[0] * dice[1];
        } else {
            answer = dice[0];
        }
        
        
        
        return answer;
    }
}

많은 if문을 쓰고도 또 생각해야될것들이 생겨서 풀다가미루고 풀다가미루고 했는데

오늘 🍊씨랑 미니스터디하면서 오름차순으로 정렬하고 이렇게저렇게하면 풀린다는 예기를 들고 헐...??!! 을 외쳤다 왜 그런 생각을 못했을까ㅏㅏㅏ

오름차순으로 정렬하고나면 비교하는게 훨씬 쉬워졌다 배워갑니당

 

 

2의 영역

https://school.programmers.co.kr/learn/courses/30/lessons/181894

 

프로그래머스

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

programmers.co.kr

class Solution {
    public int[] solution(int[] arr) {
        int[] answer = {};
        int n = -1;
        int m = -1;
        for(int i=0; i<arr.length; i++){
            if(arr[i] == 2) {
                n = i;
                break;
            }
        }
        for(int i=arr.length-1; i>=0; i--){
            if(arr[i] == 2) {
                m = i;
                break;
            }
        }
        
        int index = 0;
        if(n != -1) {
            answer = new int[m-n+1];
            for(int i=n; i<m+1; i++) {
                answer[index] = arr[i];
                index++;
            }
        } else {
            answer = new int[1];
            answer[0] = -1;
        }
        
        return answer;
    }
}

 

 

세 개의 구분자

https://school.programmers.co.kr/learn/courses/30/lessons/181862

 

프로그래머스

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

programmers.co.kr

import java.util.*;

class Solution {
    public String[] solution(String myStr) {
        List<String> list = new ArrayList<>();
        String[] answer;
        myStr = myStr.replaceAll("b","a");
        myStr = myStr.replaceAll("c","a");
        String[] str = myStr.split("a");
        
        for(int i=0; i<str.length; i++) {
                if(str[i].equals("")) continue;
                else list.add(str[i]);
        }
        
        answer = new String[list.size()];
        if(list.isEmpty()) {
            answer = new String[1];
            answer[0] = "EMPTY";
        } else {
            for(int i=0; i<list.size(); i++) {
            answer[i] = list.get(i);
            }
        }
        return answer;
    }
}

다른 사람 풀이

myStr.replaceAll("a|b|c", " ");

나는 잘 몰라서 각각 나눠서 썼는데 이렇게 | 을 써서 or 을 나타낼 수 있었구나,,!!!

 

 

문자열이 몇 번 등장하는지 세기

https://school.programmers.co.kr/learn/courses/30/lessons/181871

 

프로그래머스

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

programmers.co.kr

class Solution {
    public int solution(String myString, String pat) {
        int answer = 0;
        int num = pat.length();
        for(int i=0; i<=myString.length()-num; i++) {
            if(myString.substring(i, i+num).equals(pat)) answer++;
        }
        return answer;
    }
}

 

 

문자열 묶기

https://school.programmers.co.kr/learn/courses/30/lessons/181855

 

프로그래머스

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

programmers.co.kr

class Solution {
    public int solution(String[] strArr) {
        int[] len = new int[31];
        for(String s : strArr) {
            len[s.length()] += 1;
        }
        
        int max = Integer.MIN_VALUE;
        for(int i=0; i<len.length; i++) {
            max = Math.max(max, len[i]);
        }
        
        return max;
    }
}

 

 

1로 만들기

https://school.programmers.co.kr/learn/courses/30/lessons/181880

 

프로그래머스

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

programmers.co.kr

class Solution {
    public int solution(int[] num_list) {
        int answer = 0;
        for(int n : num_list) {
            while(n>1) {
                if(n % 2 == 0) {
                    n = n/2;
                    answer++;
                 } else {
                    n = (n-1)/2;
                    answer++;
                }
            }
        }
        return answer;
    }
}

 

 

특정 문자열로 끝나는 가장 긴 부분 문자열 찾기

https://school.programmers.co.kr/learn/courses/30/lessons/181872

 

프로그래머스

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

programmers.co.kr

class Solution {
    public String solution(String myString, String pat) {
        String answer = "";
        for(int i=myString.length(); i>=0; i--) {
            if(myString.substring(0, i).endsWith(pat)) {
                answer = myString.substring(0, i);
                break;
            } else continue;
        }
        return answer;
    }
}

 

 

문자 개수 세기

https://school.programmers.co.kr/learn/courses/30/lessons/181902

 

프로그래머스

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

programmers.co.kr

import java.util.*;
    
    class Solution {
    public int[] solution(String my_string) {
        int[] answer = new int[52];
        int[] list = new int[52];
        int count = 0;
        int index = 0;
        for(int i=65; i<=90; i++) {
            list[count] = i;
            count++;
        }
        for(int i=97; i<=122; i++) {
            list[count] = i;
            count++;
        }
        
        for(int i=0; i<my_string.length(); i++) {
            int n = (int) my_string.charAt(i);
            for(int j=0; j<list.length; j++) {
                if(list[j] == n) {
                    index = j;
                    break;
                }
            }
            
            answer[index] += 1;
        }
        
        return answer;
    }
}

너무 복잡하게 푼듯,,

 

 

qr code

https://school.programmers.co.kr/learn/courses/30/lessons/181903

 

프로그래머스

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

programmers.co.kr

class Solution {
    public String solution(int q, int r, String code) {
        String answer = "";
        
        for(int i=0; i<code.length(); i++) {
            if (i % q == r) {
                answer += code.charAt(i);
            }
        }
        
        return answer;
    }
}

 

'코딩테스트-알고리즘 > 도전' 카테고리의 다른 글

2024.01.11 - 4문제  (0) 2024.01.11
2024.01.10 - 1문제  (0) 2024.01.10
2024.01.08 - 18문제  (0) 2024.01.08
2024.01.07 - 10문제  (1) 2024.01.07
2024.01.06 - 4문제  (1) 2024.01.06