Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created April 18, 2015 16:05
Show Gist options
  • Save stephen-maina/486da3cc092b7ffc6361 to your computer and use it in GitHub Desktop.
Save stephen-maina/486da3cc092b7ffc6361 to your computer and use it in GitHub Desktop.
from research online
class Solution {
public int solution(int[] a) {
int result = 0;
int[] dps = new int[a.length];
int[] dpe = new int[a.length];
for (int i = 0; i < a.length; i++)
{
dps[Math.max(0, i - a[i])]++;
dpe[Math.min(a.length - 1, i + a[i])]++;
}
int t = 0;
for (int i = 0; i < a.length; i++)
{
if (dps[i] > 0)
{
result += t * dps[i];
result += dps[i] * (dps[i] - 1) / 2;
t += dps[i];
}
t -= dpe[i];
}
if(result>10000000) return -1;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment