문제 조건에 주어진대로 sort한 뒤 비트 연산자(^)를 이용하여 연산을 시행하자.
function solution(data, col, row_begin, row_end) {
let sorted = data.sort((a,b)=>{
if(a[col-1] === b[col-1]){
return b[0] > a[0] ? 1 : -1
}else{
return a[col-1] > b[col-1] ? 1 : -1
}
})
let result = [];
sorted.forEach((d,i)=>{
let val = d
if(i+1 >= row_begin && i+1 <= row_end){
let sum = 0;
d.forEach((data,idx)=>{
sum += data%(i+1)
})
result.push(sum);
}
})
let a = result[0];
for(var i =1; i<result.length; i++){
a = a^ result[i];
}
return a;
}
js 내에는 비트연산자(^)가 존재하므로, 주어진 조건에 맞게 sort만 잘하면 비교적 매우 쉽게 풀수있었던 문제였다.