본문 바로가기

Algorithm/Programmers

[프로그래머스_JAVA_알고리즘] 주사위의 개수 #해시

HashMap을 이용해서 풀어봤습니다.

import java.util.*;

class Solution {
    public int solution(int[] box, int n) {
        // box 가로, 세로, 높이
        // n 모서리의 길이
        int answer = 1;
        
        HashMap<Integer, Integer> hashmap = new HashMap<>();
        
        for(int i = 0; i < box.length; i++)
            hashmap.put(i, box[i]/n);
        
        for(int i = 0; i < box.length; i++)
            answer *= hashmap.get(i);
        
        return answer;
    }
}

 

key 값으로는 i 값, value 값으로는 길이 당 주사위의 최대 개수가 됩니다.

hashmap에 put 해서 값을 넣고,

hashmap을 get 해서 key 값에 대한 value의 곱을 리턴하면 됩니다.

 

.

.

.

 

문제는 아래에서 확인할 수 있습니다.

https://school.programmers.co.kr/learn/courses/30/lessons/120845

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr