Created
May 12, 2015 07:39
-
-
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
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
//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