본문 바로가기
PS/프로그래머스

[프로그래머스] 테이블 해시 함수 - Java

by 진꿈청 2024. 7. 4.
import java.util.*;

class Solution {
    public int solution(int[][] data, int col, int row_begin, int row_end) {
        int answer = 0;
        
        Arrays.sort(data, (o1, o2) -> {
            if(o1[col - 1] == o2[col- 1]){
                return o2[0] - o1[0];
            }
            return o1[col - 1] - o2[col - 1];
        });
        
        List<Integer> modSum = new ArrayList<>();
        
        for(int i = row_begin - 1; i < row_end; i++){
            int sum = 0;
            for(int j = 0; j < data[i].length; j++){
                sum += data[i][j] % (i + 1);
            }
            // System.out.println(sum);
            modSum.add(sum);
        }
        
        answer = modSum.get(0);
        
        for(int i = 1; i < modSum.size(); i++){
            answer ^= modSum.get(i);
        }
        
        return answer;
    }
}