ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 4. 위장
    코딩 테스트/Level 2 2019. 11. 11. 21:10
    반응형

    https://programmers.co.kr/learn/courses/30/lessons/42578

     

    코딩테스트 연습 - 위장

     

    programmers.co.kr

    딕셔너리 (맵) 자료형을 이용해서 카운트할 수 있습니다.  

    파이썬

    def solution(clothes):
        temp = {}
        for _, cat in clothes:
            if cat in temp:
                temp[cat] += 1
            else:
                temp[cat] = 1
        answer = 1
        for each in temp.values():
            answer *= each + 1
        return answer - 1

    딕셔너리의 setdefault를 사용하면 코드를 더 줄일 수 있습니다. 

    def solution(clothes):
        temp = {}
        for _, cat in clothes:
            temp.setdefault(cat, 0)
            temp[cat] += 1
        answer = 1
        for each in temp.values():
            answer *= each + 1
        return answer - 1

    딕셔너리의 get을 이용하면 한 줄 더 줄일 수 있습니다. 

    def solution(clothes):
        temp = {}
        for _, cat in clothes:
            temp[cat] = temp.get(cat, 0) + 1
        answer = 1
        for each in temp.values():
            answer *= each + 1
        return answer - 1

    함수형으로.. 

    from collections import Counter
    from functools import reduce
    
    
    def solution(clothes):
        return reduce(lambda x, y: x * (y + 1), Counter(cat for _, cat in clothes).values(), 1) - 1
    

     

    자바스크립트

    function solution(clothes) {
        let temp = {};
        for (let i = 0; i < clothes.length; i++){
            if (clothes[i][1] in temp) temp[clothes[i][1]]++
            else temp[clothes[i][1]] = 1
        }
        var answer = 1;
        for (let each in temp) answer *= (temp[each] + 1)
        return answer - 1;
    }

     

    자바

    import java.util.HashMap;
    
    class Solution {
        public int solution(String[][] clothes) {
            HashMap<String, Integer> clothesMap = new HashMap<>();
            for (String[] each : clothes) {
                if (!clothesMap.containsKey(each[1])) {
                    clothesMap.put(each[1], 1);
                }
                clothesMap.put(each[1], clothesMap.get(each[1]) + 1);
            }
            var total = 1;
            for (var key : clothesMap.keySet()) {
                total *= clothesMap.get(key);
            }
            return total - 1;
        }
    }

     

    코틀린

    import java.util.HashMap
    
    class Solution {
        fun solution(clothes: Array<Array<String>>): Int {
            val clothesMap = HashMap<String, Int>()
            for (each in clothes) {
                if (!clothesMap.containsKey(each[1])) {
                    clothesMap[each[1]] = 1
                }
                clothesMap[each[1]] = clothesMap[each[1]]!! + 1
            }
            var total = 1
            for (key in clothesMap.keys) {
                total *= clothesMap[key]!!
            }
            return total - 1
        }
    }

     

    func solution(clothes [][]string) int {
        temp := make(map[string]int)
        for _, cloth := range clothes{
            temp[cloth[1]]++
        }
        answer := 1
        for _, v := range temp{
            answer *= v + 1
        }
        return answer - 1
    }

     

    C#

    using System.Collections.Generic;
    
    public class Solution
    {
        public int solution(string[,] clothes)
        {
            Dictionary<string, int> counts = new Dictionary<string, int>();
            for (int i = 0; i < clothes.GetLength(0); i++)
            {
                if (!counts.ContainsKey(clothes[i, 1]))
                    counts[clothes[i, 1]] = 0;
                counts[clothes[i, 1]]++;
            }
            int answer = 1;
            foreach (var n in counts)
            {
                answer *= n.Value + 1;
            }
            return answer - 1;
        }
    }
    반응형
Designed by Tistory.