Skip to content

Instantly share code, notes, and snippets.

@alireza-mpr
Created November 14, 2020 23:40
Show Gist options
  • Select an option

  • Save alireza-mpr/1fb8e4eb8b472ef515e0c02d730f2982 to your computer and use it in GitHub Desktop.

Select an option

Save alireza-mpr/1fb8e4eb8b472ef515e0c02d730f2982 to your computer and use it in GitHub Desktop.
// Solution 1:
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let wests=[];
wests[A.length-1] = A[A.length-1]==1 ? 1 : 0;
for(let i=A.length-2; i>-1; i--)
{
wests[i] = wests[i+1];
if(A[i]==1)
wests[i]++;
}
let sum=0;
for(let cntr=0; cntr< A.length; cntr++){
let east = A[cntr];
if(east != 0)
continue;
sum += wests[cntr];
if(sum > 1000000000)
return -1
}
return sum;
}
// Solution 2:
// Solution 1:
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let sum = 0;
let ones = 0;
for(let i=A.length-1; i>-1; i--)
{
if(A[i] == 0)
sum += ones;
else
ones++;
if(sum > 1000000000 )
return -1;
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment