0
import java.util.HashSet;
import java.util.Set;

public static boolean canTwoMoviesFillFlight(int[] movieLengths, int flightLength) {

    // movie lengths we've seen so far
    Set<Integer> movieLengthsSeen = new HashSet<>();

    for (int firstMovieLength : movieLengths) {

        int matchingSecondMovieLength = flightLength - firstMovieLength;
        if (movieLengthsSeen.contains(matchingSecondMovieLength)) {
            return true;
        }

        movieLengthsSeen.add(firstMovieLength);
    }

    // we never found a match, so return false
    return false;
}

How does the HashSet already have all the values from movieLengths?

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
  • 1
    This code would make more sense if you take a small set of example values, go through the code line by line and draw on a board or paper what's happening – denvercoder9 May 25 '20 at 22:48
  • `canTwoMoviesFillFlight([2, 1, 3, 4], 6)` Use this to debug and see why the code is correct and how it works. The answer would be true for that. The answer would be false for this `canTwoMoviesFillFlight([2, 1, 3, 7], 6)` – denvercoder9 May 25 '20 at 22:49

3 Answers3

0

This problem asks about finding two movies that sum up to a given duration.

You may make use of the hash set by storing the complement of this particular movie. Assume that you have a movie (60 minutes) in your first iteration, and you calculate its complement to fill the whole flight duration (100 minutes). At first, the set is empty, so you can't find this value and you'll insert the duration to the set. In the second iteration, you'll have a movie with 40 minutes, so you'll search the set if it contains a movie with (100 - 40 = 60) minutes and you'll find that you inserted the first one. So, you will return true.

Mohammed Deifallah
  • 1,290
  • 1
  • 10
  • 25
0

Your HashSet is empty when it's created:

Set<Integer> movieLengthsSeen = new HashSet<>();

However, the next step is to loop through a set of values passed into the method. I added comments for clarification:

//movieLengths is passed in
for (int firstMovieLength : movieLengths) {
        //create value from 2 passed in params
        int matchingSecondMovieLength = flightLength - firstMovieLength;

        //Here it checks to see if the value has been added to the hash,
        //if so, return true (won't happen on the first pass because
        //the hash set is empty).
        //Otherwise continue with the algorithm.
        if (movieLengthsSeen.contains(matchingSecondMovieLength)) {
            return true;
        }

        //If the hash doesn't have the value, which it won't on the first pass
        //and possible subsequent passes, it will add the value and repeat
        movieLengthsSeen.add(firstMovieLength);
    }

TLDR; The HashSet is empty. It becomes populated as the for-loop runs.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

As you iterate through the enhanced for loop, each firstMovieLength is added to the hashset at the end. So when you enter the second iteration, the previous firstMovieLength is present, and in the third iteration the previous two are elements of the hashset, and so forth.