enoch012 / JavaBasicStudy

Java 기초 스터디 (2023.09 ~ 10)
0 stars 1 forks source link

10월 15일 / 코딩테스트 연습 (chanmi) #19

Open enoch012 opened 11 months ago

enoch012 commented 11 months ago

옹알이 (1)

0단계 문제 중에 가장 정답률이 가장 낮길래 풀어보았다.

문제

제한사항

입출력 예

babbling | result -- | -- ["aya", "yee", "u", "maa", "wyeoo"] | 1 ["ayaye", "uuuma", "ye", "yemawoo", "ayaa"] | 3

유의사항

문제풀이

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

class Solution {
    public int solution(String[] babbling) {
        int count = 0;

        // 검사목록을 담은 babblingList
        List<String> babblingList = new ArrayList<>(Arrays.asList("aya", "ye", "woo", "ma"));

        // 주어진 babbling 길이만큼 향상된 for문 실행
        for(String targetStr : babbling){
            count = searchBabblingList(targetStr, babblingList) ? 
                    count + 1 : count;
        }

        // count 값을 리턴
        return count;
    }

    // 문자열 하나에 babbling List과 일치하는 문자열이 있는지 검사하는 함수
    boolean searchBabblingList(String targetStr, List<String> babblingList){
        // 최종 targetStr을 검사할 정규 표현식
        String regExp = "[0-9]+";

        for(String searchStr : babblingList){
            // 대상 문자열에 i번째 단어가 포함되어 있다면
            if(targetStr.contains(searchStr)){
                // 숫자 1로 해당 문자열을 대체
                targetStr = targetStr.replace(searchStr, "1");
            }
        }

        // 발음할 수 있는 문자열이라면 정규표현식에 위반되지 않으므로
        // true를 리턴한다. 숫자 외에 다른 문자열이 하나라도 포함되어있다면 false
        return targetStr.matches(regExp);
    }
}

회고

enoch012 commented 11 months ago

주사위 게임 3

문제

제한사항

입출력 예

a | b | c | d | result -- | -- | -- | -- | -- 2 | 2 | 2 | 2 | 2222 4 | 1 | 4 | 4 | 1681 6 | 3 | 3 | 6 | 27 2 | 5 | 2 | 6 | 30 6 | 4 | 2 | 5 | 2

유의사항

문제풀이

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.HashSet;

class Solution {
    public int solution(int a, int b, int c, int d) {
        int answer = 0;

        List<Integer> diceList = new ArrayList<>(Arrays.asList(a, b, c, d));

        // Set 으로 변환, 크기가 4가 아니라면 중복이 있다는 뜻
        Set<Integer> diceSet = new HashSet<>(diceList);
        int diceSetSize = diceSet.size();

        switch(diceSetSize){
            case 4 : // 숫자가 모두 다름
            answer = case4(diceList); break;

            case 3 : // 2개 숫자가 중복, 나머지 두개는 서로 다름 
            answer = case3(diceList, diceSet); break;

            case 2 : // 숫자가 각 2개씩 중복, 중복이 2:2인 경우와 3:1인 경우 분리
            answer = case2(diceList, diceSet); break;

            case 1 : // 숫자가 모두 동일함
            answer = diceList.get(0) * 1111; break;
        }

        return answer;
    }

    int case4(List<Integer> diceList){
        Collections.sort(diceList); // 오름차순으로 정렬
        return diceList.get(0); // 최소값 리턴
    }

    int case3(List<Integer> diceList, Set<Integer> diceSet){

        int dupliNum = 0;
        for(int num : diceSet){
            if(Collections.frequency(diceList, num) == 2){
                dupliNum = num; // 중복 값 알아내기
            }
        }

        // 중복 값이 없을 때까지 삭제 (실제론 2번)
        while(diceList.contains(dupliNum)) diceList.remove(new Integer(dupliNum));

        return diceList.get(0) * diceList.get(1); // 나머지 두 수 곱하기
    }

    int case2(List<Integer> diceList, Set<Integer> diceSet){

        int answer = 0;
        int numFirst = diceList.get(0); // 리스트의 첫번째 숫자
        int dupliNum = Collections.frequency(diceList, numFirst); // 첫 숫자의 중복 횟수

        List<Integer> diceList2 = new ArrayList<>(diceSet); // 중복 제거한 set을 다시 list로

        int num1 = diceList2.get(0);
        int num2 = diceList2.get(1);

        switch(dupliNum){
            case 1 :
                if(num1 == numFirst){
                    // 같다면 나머지 3개가 중복인 것
                    answer = (int) Math.pow(((10 * num2) + num1), 2);
                } else {
                    // 아니라면 중복수인 것
                    answer = (int) Math.pow(((10 * num1) + num2), 2);
                } break;

            case 2 : // 중복이 2:2인 경우
                answer = (num1 + num2) * Math.abs(num1 - num2); break;

            case 3 :
                if(num1 == numFirst){
                    // 같다면 중복수인 것
                    answer = (int) Math.pow(((10 * num1) + num2), 2);
                } else {
                    // 아니라면 나머지 다른 하나의 수
                    answer = (int) Math.pow(((10 * num2) + num1), 2);
                } break;
        }

        return answer;
    }

}

회고

enoch012 commented 11 months ago

OX퀴즈

문제

제한사항

입출력 예

["3 - 4 = -3", "5 + 6 = 11"] | ["X", "O"] -- | -- ["19 - 6 = 13", "5 + 66 = 71", "5 - 15 = 63", "3 - 1 = 2"] | ["O", "O", "X", "O"]

문제풀이

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

class Solution {
    public String[] solution(String[] quizList) {
        List<String> answerList = new ArrayList<>(); // "O", "X"를 담을 리스트

        for(String quiz : quizList){
            String[] quizSplit = quiz.split(" "); // 공백 기준으로 문자열을 잘라서 배열로 만들기

            int num1 = Integer.parseInt(quizSplit[0]);
            int num2 = Integer.parseInt(quizSplit[2]);
            int quizAnswer = Integer.parseInt(quizSplit[4]);

            int trueAnswer = quizSplit[1].equals("+") ? num1 + num2 : num1 - num2;

            if(quizAnswer == trueAnswer) answerList.add("O");
            else answerList.add("X");
        }

        String[] answer = answerList.toArray(new String[0]);

        return answer;
    }
}

image

회고