Skip to content

Instantly share code, notes, and snippets.

@SH4DY
Created May 12, 2015 07:39
Show Gist options
  • Save SH4DY/6dd962ba1e4f260fafbd to your computer and use it in GitHub Desktop.
Save SH4DY/6dd962ba1e4f260fafbd to your computer and use it in GitHub Desktop.
In-Flight Entertainment. Knapsack with only 2 integer values. https://www.interviewcake.com/question/inflight-entertainment
//We can do this with only one loop
//thanks to the fact that we are looking for
//EXACTLY 2 movies which match the length.
//Duplicates are not being counted
//as we check for a hit FIRST and then put the
//value of the first movie into the map.
public static boolean isTwoMovies(int fl, int[] ml){
HashMap<Integer, Boolean> hm = new HashMap<Integer, Boolean >();
for(int i = 0; i < ml.length; i++){
int restTime = fl - ml[i];
if(hm.get(restTime) != null) return true;
hm.put(ml[i], true);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment