Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.07.03 - [PG] 위장 #109

Closed
suhyunsim opened this issue Jul 5, 2021 · 0 comments
Closed

21.07.03 - [PG] 위장 #109

suhyunsim opened this issue Jul 5, 2021 · 0 comments
Assignees
Labels
lv.2 프로그래머스 - level 2 성공 맞은 문제 해시 해시

Comments

@suhyunsim
Copy link
Owner

문제

핵심 아이디어

  • Map 활용해서 풀이
  • 스트림으로 푸는 풀이가 있어서 추가로 정리했다.

어려운 점, 실수

풀이

package main.java.com.poogle.PG.Q42578;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static java.util.stream.Collectors.*;

public class Solution {

    public static int solution(String[][] clothes) {
        int answer = 1;
        Map<String, Integer> clothMap = new HashMap<>();

        for (int i = 0; i < clothes.length; i++) {
            clothMap.put(clothes[i][1], clothMap.getOrDefault(clothes[i][1], 0) + 1);
        }

        Set<String> keySet = clothMap.keySet();
        for (String key : keySet) {
            answer *= clothMap.get(key) + 1;
        }
        return answer - 1;
    }

    public static int solutionStream(String[][] clothes) {
        return Arrays.stream(clothes)
                .collect(groupingBy(p -> p[1], mapping(p -> p[0], counting())))
                .values()
                .stream().reduce(1L, (x, y) -> x * (y + 1)).intValue() - 1;
    }

    public static void main(String[] args) {
        System.out.println(solution(new String[][]{{"yellowhat", "headgear"}, {"bluesunglasses", "eyewear"}, {"green_turban", "headgear"}}));
        System.out.println(solutionStream(new String[][]{{"yellowhat", "headgear"}, {"bluesunglasses", "eyewear"}, {"green_turban", "headgear"}}));
    }
}
@suhyunsim suhyunsim added 성공 맞은 문제 해시 해시 lv.2 프로그래머스 - level 2 labels Jul 5, 2021
@suhyunsim suhyunsim added this to the 7월 1주 차 milestone Jul 5, 2021
@suhyunsim suhyunsim self-assigned this Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lv.2 프로그래머스 - level 2 성공 맞은 문제 해시 해시
Projects
None yet
Development

No branches or pull requests

1 participant