๐ป Coding Problems Solving/Permutations | Combinations
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์์ฅ
Kim_dev
2022. 4. 16. 15:44
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์์ฅ
1. ๋ฌธ์ : Link
๊ฒฝ์ฐ์ ์ ์กฐํฉ์ ๋ฌป๋ ๋ฌธ์
2. ํ์ด
ํด์ฌ๋ฅผ ์ด์ฉํด์ ํ์ด
์ท์ ์์ ๋ ๊ฒฝ์ฐ๋ฅผ + 1ํด์ค์ผํ๊ณ
๋ง์ง๋ง์ ์ ๋ถ ์์ ๋ ๊ฒฝ์ฐ๋ ์๊ธฐ ๋๋ฌธ์ -1
3. ์ฝ๋
def solution(clothes):
c = {}
for clo in clothes:
cl = clo[1]
if cl not in c:
c[cl] = 1
else:
c[cl] += 1
answer = 1
for type in c:
answer *= (c[type] + 1)
return answer-1
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
class Solution {
public int solution(String[][] clothes) {
int answer = 1; //๊ณฑ์
์ ์ํด 1๋ก ์ ์ธ
HashMap<String, Integer> clothesMap = new HashMap<String, Integer>();
//map ๊ตฌํ๊ธฐ
for(int i =0; i<clothes.length; i++){
//์์์ข
๋ฅ, ๊ฐฏ์
clothesMap.put(clothes[i][1], clothesMap.getOrDefault(clothes[i][1], 0)+1);
}
//์กฐํฉ
Set<String> keySet = clothesMap.keySet(); //์์์ข
๋ฅ.
for(String key : keySet) {
answer *= clothesMap.get(key)+1;
}
return answer-1; //์๋ฌด๊ฒ๋ ์์
๋ ๊ฒฝ์ฐ์ ์ ์ ๊ฑฐ
}
}