๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JAVA] ์œ„์žฅ - ํ•ด์‹œ

by ynzu๐Ÿค 2022. 2. 18.
๋ฐ˜์‘ํ˜•

 

 

์ด ๋ฌธ์ œ๋Š” HashMap์— putํ•˜๋Š” ๋ถ€๋ถ„์ด ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜ ๋ฌธ์ œ์™€ ๋˜‘๊ฐ™๋‹ค. 

๋จผ์ € HashMap์— ์˜ท์˜ ์ข…๋ฅ˜๋ณ„๋กœ ๋ช‡ ๊ฐœ๊ฐ€ ์žˆ๋Š”์ง€ put ํ•˜๋„๋ก ํ•˜์ž.

import java.util.HashMap;

class Solutio {
    public int solution(String[][] clothes) {
        int answer = 1;
        
        HashMap<String,Integer> hm = new HashMap<String, Integer>();
        
        for (int i=0; i<clothes.length; i++) {
        	hm.put(clothes[i][1], hm.getOrDefault(clothes[i][1], 0)+1);
        }
        
	    	
        for (String key : hm.keySet()) {
        	answer *= (hm.get(key) + 1);
        
        }
	    
        return answer-1;
    }
}

 

์˜ˆ์ œ1์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๋ฉด HashMap์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ธด ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

eyewear:1
headgear:2

 

๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋กœ  eyewear๊ฐ€ 1๊ฐœ, headgear๊ฐ€ 2๊ฐœ ์กด์žฌํ•  ๋•Œ ์ด๋ฅผ ์กฐํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” eyewear x headgear๋กœ 2์ด๋‹ค.

ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ์—์„  eyewear๋งŒ ์ฐฉ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์™€ headgear๋งŒ ์ฐฉ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ํฌํ•จ์‹œ์ผœ์•ผ ํ•˜๋ฏ€๋กœ (eyewear+1) x (headgear+1)๋กœ 6์ด๋‹ค.

์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐํ•  ๊ฒฝ์šฐ ์•„๋ฌด๊ฒƒ๋„ ์ž…์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰์— -1๋ฅผ ํ•ด์ฃผ๋ฉด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€