CODEKATA 61 ~ 64

Tak Jeon·2025년 1월 10일

알고리즘

목록 보기
70/101

61번 로또의 최고 순위와 최저 순위

import java.util.*;

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(6,1);
        map.put(5,2);
        map.put(4,3);
        map.put(3,4);
        map.put(2,5);
        map.put(1,6);
        map.put(0,6);
        
        int[] answer = new int[2];
        int correct = 0;
        int zero = 0;
        for(int i = 0 ; i < 6 ; i++){
            if(lottos[i] == 0){
                zero++;
                continue;
            }
            for(int j = 0 ; j < 6 ; j++){
                if(lottos[i] == win_nums[j]){
                    correct++;
                    break;
                }
            }
        }
        answer[0] = map.get(correct + zero);
        answer[1] = map.get(correct);
        return answer;
    }
}

62번 옹알이 (2)

class Solution {
    public int solution(String[] babbling) {
        String[] canSay = {"aya","ye","woo","ma"};
        int answer = 0;
        
        for(String word : babbling){
            boolean check = true;
            
            for(String cur : canSay){
                if(word.contains(cur + cur)){
                    check = false;
                    break;
                }
                
                word = word.replace(cur, " ");
            }
            
            if(check && word.trim().isEmpty()){
                answer++;
            }
        }
      
        return answer;
    }
}

63번 숫자 짝꿍

class Solution {
    public String solution(String X, String Y) {
        int[] countX = new int[10];
        int[] countY = new int[10];
        
        for(char c : X.toCharArray()){
            countX[c - '0']++;
        }
        
        for(char c : Y.toCharArray()){
            countY[c - '0']++;
        }
        
        StringBuilder sb = new StringBuilder();
        for(int i = 9 ; i >= 0 ; i--){
            int canPut = Math.min(countX[i], countY[i]);
            sb.append(String.valueOf(i).repeat(canPut));
        }
        if(sb.length() == 0) return "-1";
        
        if(sb.charAt(0) == '0') return "0";
        
        return sb.toString();
    }
}

64번 체육복

import java.util.*;

class Solution {
    public int solution(int n, int[] lost, int[] reserve) {
        Arrays.sort(lost);
        int lostP = lost.length;
        int[] rS = new int[n + 2];
        for(int cur : reserve){
            rS[cur]++;
        }
        
        for(int i = 0 ; i < lost.length ; i++){
            if(rS[lost[i]] > 0){
                rS[lost[i]]--;
                lost[i] = -1;
                lostP--;
            }
        }
        
        for(int i = 0 ; i < lost.length ; i++){
            if(lost[i] == -1)continue;
            
            if(rS[lost[i] - 1] > 0){
                rS[lost[i] - 1]--;
                lostP--;
            }else if(rS[lost[i] + 1] > 0){
                rS[lost[i] + 1]--;
                lostP--;
            }
        }
        return n - lostP;
    }
}
profile
문제 해결을 좋아하는 개발자 입니다 :)

0개의 댓글