[TIL] 20231226

Xtraa·2023년 12월 26일
0

TIL

목록 보기
32/99

공부한 내용

프로그래머스 코딩테스트

의상

import java.util.*;

class Solution {
    public int solution(String[][] clothes) {
        int answer = 0;
        Map<String, Integer> map = new HashMap<>();
        
        for (int i = 0; i < clothes.length; i++) {
            map.put(clothes[i][1], map.getOrDefault(clothes[i][1], 0) + 1);
        }
        
        int count = 1;
        
        for (String str : map.keySet()) {
            count *= map.get(str) + 1;
        }
        
        answer = count - 1;
        
        return answer;
    }
}

문제 해결

이제는 익숙해진 풀이 유형. 하지만 경우의 수를 구하는 건 찾아봤다. 곱의 법칙 기억!

할인 행사

class Solution {
    public int solution(String[] want, int[] number, String[] discount) {
        int answer = 0;
        
        // want[i], number[i] : map(want[i], number[i])와 같다.
        
        for (int i = 0; i < discount.length - 9; i++) { // 시작 날짜, 할인 품목
            int j = 0;
            for (; j < want.length; j++) { // want 품목
                int count = 0;
                for (int k = i; k <= i + 9; k++) {
                    if (want[j].equals(discount[k])) count++;
                }
                if (count != number[j]) break;
            }
            if (j == want.length) answer++;
        }
        
        return answer;
    }
}

문제 해결

처음에 map으로 접근하려고 했는데 다시 보니 want와 number은 그 자체로 key, map의 형태로 이루어져 있어서 그대로 풀면 되겠다는 생각으로 접근했다.

for (int i = 0; i < discount.length - 9; i++) { // 시작 날짜, 할인 품목
처음에 조건을 discount.length로 했는데 그렇게 할 경우 i가 증가 했을때, i부터 +9 까지 조회해야 하는데 discount의 범위를 벗어나는 경우가 발생하기 때문에 discount.length - 9로 지정

그리고 다시 이중 for문으로 want 품목과 discount품목을 비교해서 count값을 구하고, count와 number[i]값이 다르면 다음 날로 넘어가서 다시 처음부터 시작하면 된다.


팀프로젝트

https://teamsparta.notion.site/24c89dadc751460cb159b064fb2c9d84


profile
https://xtraa.notion.site/Xtraa-ed48ac432d354d01b5bf5b0da5ec94a9?pvs=4

0개의 댓글