Created
April 18, 2015 16:05
-
-
Save stephen-maina/486da3cc092b7ffc6361 to your computer and use it in GitHub Desktop.
from research online
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
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