Skip to content

Instantly share code, notes, and snippets.

@yevgnenll
Created October 7, 2016 10:43
Show Gist options
  • Save yevgnenll/f0cf128c569d1a0359955ae0dced9b22 to your computer and use it in GitHub Desktop.
Save yevgnenll/f0cf128c569d1a0359955ae0dced9b22 to your computer and use it in GitHub Desktop.
function solution(A){
n = A.length;
if(n === 1)
return Math.abs(A[0]);
if(n === 2)
return Math.abs(A[0]-A[1]);
var total_array = arr_sum(A);
var min_abs = false;
var left = 0, right = 0;
for(var i=1; i<n-1; i++){
left += A[i-1];
right = total_array - left;
current_abs = Math.abs(left - right);
min_abs = min_abs !== false ? (min_abs < current_abs ? min_abs : current_abs): current_abs;
}
return min_abs;
}
function arr_sum(arr){
var total = 0;
for(var i in arr){
total += arr[i];
}
return total;
}
console.log(solution([-10, -5, -3, -4, -5]));
@yevgnenll
Copy link
Author

yevgnenll commented Oct 7, 2016

left의 수를 구하면서 right의 수를 동시에 구했고, 이것을 하나하나 비교하는 방법

  • 내가 처음 생각한건 절대로 답이 아니다

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment