avatarjb stevenard

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2366

Abstract

n class="hljs-literal">true</span> } <span class="hljs-comment">// 5.</span> durations.insert(movieDuration) } <span class="hljs-comment">// 6.</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> }</pre></div><h2 id="890e">Explanations:</h2><p id="6a83">1. If the trip duration is not greater than 0 or if the movie duration is empty, there aren’t two trips that can match, return false.</p><div id="4d73"><pre><span class="hljs-keyword">guard</span> tripDuration <span class="hljs-operator">></span> <span class="hljs-number">0</span> <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> } <span class="hljs-keyword">guard</span> <span class="hljs-operator">!</span>movieDurations.isEmpty <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> }</pre></div><p id="0460">2. Let’s create a set to save durations we saw, and let’s iterate over the movie durations (only if the movie duration is lesser than the trip duration).</p><div id="80d5"><pre><span class="hljs-keyword">var</span> durations <span class="hljs-operator">=</span> <span class="hljs-type">Set</span><<span class="hljs-type">Int</span>>() <span class="hljs-keyword">for</span> movieDuration <span class="hljs-keyword">in</span> movieDurations <span class="hljs-keyword">where</span> movieDuration <span class="hljs-operator"><</span> tripDuration {</pre></div><p id="f585">3. Let’s find the remaining time from the current movie and the total duration of the trip.</p><div id="054f"><pre><span class="hljs-keyword">let</span> remainingTime <span class="hljs-operator">=</span> tripDuration — movieDuration</pre></div><p id="3b8e">4. If the remaining time exists in the saved durations, we have a matching pair of movies that equal the trip duration, then return true.</p><div id="f724"><pre><span class="hljs-keyword">if</span> durations.contains(remainingTime) { <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span> }</pre></div><p id="3ec5">5. Insert the current movie duration into the saved durations set.</p><div id="ad08"><pre>durations.insert(movieDuration)</pre></div><p id="26b2">6. We didn’t find any pair; return false.</p><div id="2efb"><pre><span class="hl

Options

js-keyword">return</span> <span class="hljs-literal">false</span></pre></div><h2 id="4411">Complexity:</h2><p id="1a60">- Time Complexity: As we browse all movies, the time complexity is linear O(n),</p><p id="21b7">- Space Complexity: As we might save all movies, the space complexity is linear O(n).</p><h2 id="73b9">Test-cases:</h2><div id="9d62"><pre><span class="hljs-keyword">public</span> <span class="hljs-keyword">func</span> <span class="hljs-title function_">testIsThereAny2Movies</span>() { <span class="hljs-keyword">let</span> movieDurations <span class="hljs-operator">=</span> [<span class="hljs-number">3</span>, <span class="hljs-number">7</span>, <span class="hljs-number">9</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>] <span class="hljs-built_in">assert</span>(isThereAny2Movies(tripDuration: <span class="hljs-number">12</span>, movieDurations: movieDurations) <span class="hljs-operator">==</span> <span class="hljs-literal">true</span>, <span class="hljs-string">"isThereAny2Movies 12 not equal"</span>) <span class="hljs-built_in">assert</span>(isThereAny2Movies(tripDuration: <span class="hljs-number">15</span>, movieDurations: movieDurations) <span class="hljs-operator">==</span> <span class="hljs-literal">false</span>, <span class="hljs-string">"isThereAny2Movies 15 not equal"</span>) }</pre></div><h2 id="6337">Follow-up:</h2><p id="1f86">No follow-up question.</p><p id="4404"><a href="https://readmedium.com/algorithms-questions-429c4d84a98f"><< More Interview Questions</a> | <a href="https://readmedium.com/learn-data-structures-and-algorithms-with-swift-5-6-d9f36a4027dd">Data Structures and Algorythms >></a></p><div id="c879" class="link-block"> <a href="https://medium.com/@jbstevenard/membership"> <div> <div> <h2>Join Medium with my referral link — jb stevenard</h2> <div><h3>Read every story from jb stevenard (and thousands of other writers on Medium). Your membership fee directly supports jb…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*TEb_Qg9xmrjfkxhH)"></div> </div> </div> </a> </div></article></body>

23. Trip Duration’s Movies

Question: Given a list of movie length and trip duration, find if there are two movie durations where their sum equals the trip duration.

For example, given [3, 7, 9, 2, 1] and 12, you should return true (3 + 9 == 12),

For example, given [3, 7, 9, 2, 1] and 15, you should return false.

Hints:

- You need a data structure to remember what you already saw,

- It’s a two-sum problem.

Solution:

func isThereAny2Movies(tripDuration: Int, movieDurations: [Int]) -> Bool {
  // 1.
  guard tripDuration > 0 else { return false }
  guard !movieDurations.isEmpty else { return false }
  // 2.
  var durations = Set<Int>()
  for movieDuration in movieDurations where movieDuration < tripDuration {
    // 3.
    let remainingTime = tripDuration - movieDuration
    // 4.
    if durations.contains(remainingTime) {
      return true
    }
    // 5.
    durations.insert(movieDuration)
  }
  // 6.
  return false
}

Explanations:

1. If the trip duration is not greater than 0 or if the movie duration is empty, there aren’t two trips that can match, return false.

guard tripDuration > 0 else { return false }
guard !movieDurations.isEmpty else { return false }

2. Let’s create a set to save durations we saw, and let’s iterate over the movie durations (only if the movie duration is lesser than the trip duration).

var durations = Set<Int>()
for movieDuration in movieDurations where movieDuration < tripDuration {

3. Let’s find the remaining time from the current movie and the total duration of the trip.

let remainingTime = tripDuration — movieDuration

4. If the remaining time exists in the saved durations, we have a matching pair of movies that equal the trip duration, then return true.

if durations.contains(remainingTime) {
  return true
}

5. Insert the current movie duration into the saved durations set.

durations.insert(movieDuration)

6. We didn’t find any pair; return false.

return false

Complexity:

- Time Complexity: As we browse all movies, the time complexity is linear O(n),

- Space Complexity: As we might save all movies, the space complexity is linear O(n).

Test-cases:

public func testIsThereAny2Movies() {
  let movieDurations = [3, 7, 9, 2, 1]
  assert(isThereAny2Movies(tripDuration: 12, movieDurations: movieDurations) == true, "isThereAny2Movies 12 not equal")
  assert(isThereAny2Movies(tripDuration: 15, movieDurations: movieDurations) == false, "isThereAny2Movies 15 not equal")
}

Follow-up:

No follow-up question.

<< More Interview Questions | Data Structures and Algorythms >>

Data Structures
Algorithms
Coding
Programming
Interview Questions
Recommended from ReadMedium