Home [Algorithm] Hash2
Post
Cancel

[Algorithm] Hash2

Hash

HashMap<Key, Value>


Task

2. μœ„μž₯ in Programmers (Level 2)


πŸ“Œ Problem

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

πŸ“Œ The point

  • Use number of cases
    • ex. If {hat=3, shirt=2, pants=1}, it must be calculated like (3*2*1) + (3*2) + (3*1) + (1*2) + 3 + 2 + 1 If the formula, it’s (n + 1) * (m + 1) * (o + 1) - 1 (⭐️⭐️⭐️ Do remember)
  • Use HashMap

    • HashMap.containsKey(String)
    • HashMap.put(key, value)
    • HashMap.get(key)
    • HashMap.keySet() : iterator of keys

πŸ“Œ Answer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.HashMap;

class Solution {
    public int solution(String[][] clothes) {
        
        // for multiplication
        int answer = 1;
        
        // save <type, count>
        HashMap<String, Integer> map = new HashMap<>();
        
        for (int i = 0; i < clothes.length; i++) {
            String type = clothes[i][1];
            if (!map.containsKey(type)) 
                map.put(type, 1);
            else 
                map.put(type, map.get(type) + 1);
        }
        // result : ex. {eyewear=1, headgear=2}
        
        // calculate number of cases
        // ex. (eyewear1 + 1) * (headgear2 + 1) - 1 = 5
        for (String keys : map.keySet())
            answer *= (map.get(keys) + 1);
        answer -= 1;
        
        return answer;
    }
}
/**
[Condition]
- 맀일 λ‹€λ₯Έ μ˜·μ„ μ‘°ν•© 
- clothes[i] : ["name", "type"]
- clothes.length : 1 ~ 30
- clothes.[i].length : 1 ~ 20
- ν•˜λ£¨μ— μ΅œμ†Œ ν•œ 개의 μ˜μƒ
- return μ„œλ‘œ λ‹€λ₯Έ 옷의 μ‘°ν•©μ˜ 수
**/

The source : https://sas-study.tistory.com/215


πŸ“Œ Words

  • number of cases : 경우의 수
  • formula : 公式 곡식
  • factor : ε› ζ•°γ€€μΈμˆ˜
  • factorize : ε› ζ•°εˆ†θ§£ μΈμˆ˜λΆ„ν•΄
  • multiplication : κ³±μ…ˆ
  • plus / minus / time / divided by : + / - / * / /

Why I almost forgot math I learned when I was in high school? lol Of course it also is needed knowledge of the basic math in coding test. But it’s so interesting and funny because I like math and can feel I go back to young moment 🀭 I’ll be the genious of math!!! Keep doing hard!

This post is licensed under CC BY 4.0 by the author.

[Algorithm] Hash

[Algorithm] Heap

Comments powered by Disqus.