Created
October 7, 2016 10:43
-
-
Save yevgnenll/f0cf128c569d1a0359955ae0dced9b22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
left의 수를 구하면서 right의 수를 동시에 구했고, 이것을 하나하나 비교하는 방법