Skip to content

Instantly share code, notes, and snippets.

@josephbleau
Created November 19, 2013 01:18
Show Gist options
  • Save josephbleau/7538524 to your computer and use it in GitHub Desktop.
Save josephbleau/7538524 to your computer and use it in GitHub Desktop.
A comprehensive solution to: http://codingbat.com/prob/p189576
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