Created
November 19, 2013 01:18
-
-
Save josephbleau/7538524 to your computer and use it in GitHub Desktop.
A comprehensive solution to: http://codingbat.com/prob/p189576
This file contains 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
public int maxSpan(int[] nums) { | |
HashMap<Integer, int[]> spanMap = new HashMap(); | |
int max = 0; | |
for(int i = 0; i < nums.length; ++i) { | |
if(!spanMap.containsKey(nums[i])) { | |
spanMap.put(nums[i], new int[]{i,i}); | |
} else { | |
spanMap.get(nums[i])[1] = i; | |
} | |
int span = spanMap.get(nums[i])[1] - | |
spanMap.get(nums[i])[0] + 1; | |
max = (span > max) ? span : max; | |
} | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment