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)
- ex. If {hat=3, shirt=2, pants=1}, it must be calculated like
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!
Comments powered by Disqus.