내일배움캠프 44일차 TIL _ 10주 1일차

2024. 6. 17. 21:54TIL Java

 

  • 오늘 있었던 일
    • 팀과제
    • 알고리즘

SPRIONG

  • 스프링 정리

 

알고리즘 문제 풀기

 

     ● 의상

더보기

● 의상 //링크

import java.util.HashMap;
import java.util.Map;

class Solution {
    public int solution(String[][] clothes) {
       Map<String,Integer> map = new HashMap<>();
        for (String[] row : clothes) {
            map.put(row[1], map.getOrDefault(row[1], 0) + 1);}
        int[] n = map.values().stream().mapToInt(i -> i).toArray();
        int answer = 1;
        for( int a : n){
            answer *= a+1;
        }
        answer -= 1;
        return answer;
    }
}

 

  • 문제 지문에
    • face에 해당하는 의상이 crow_mask, blue_sunglasses, smoky_makeup이므로 아래와 같이 3개의 조합이 가능합니다.
  •  
    • (a + 1)(b + 1)(c + 1)...(해당 종류 옷의 수 + 1) -1 을 계산하면 된다는 글을 보고 맞춰서 해봤더니 됐다.
  • 밑은 다른 사람의 풀이
  • import java.util.*;
    import static java.util.stream.Collectors.*;
    
    class Solution {
        public int solution(String[][] clothes) {
            return Arrays.stream(clothes)
                    .collect(groupingBy(p -> p[1], mapping(p -> p[0], counting())))
                    .values()
                    .stream()
                    .collect(reducing(1L, (x, y) -> x * (y + 1))).intValue() - 1;
        }
    }
  • 에?

 


당일 회고

  • 깃 어려워... 태스트 어려워....