Advertisement

Pairs of Songs With Total Durations Divisible by 60 - LeetCode 1010 Solution

Pairs of Songs With Total Durations Divisible by 60 - Complete Solution Guide

Pairs of Songs With Total Durations Divisible by 60 is LeetCode problem 1010, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a list of songs where the i th song has a duration of time[i] seconds. Return the number of pairs of songs for which their total duration in seconds is divisible by 60 . Formally, we want the number of indices i , j such that i < j with (time[i] + time[j]) % 60 == 0 . Example 1: Input: time = [30,20,150,100,40] Output: 3 Explanation: Three pairs have a total duration divisible by 60: (time[0] = 30, time[2] = 150): total duration 180 (time[1] = 20, time[3] = 100): total duration 120

Detailed Explanation

The problem requires us to find the number of pairs of songs from a given list `time` whose total duration is divisible by 60. We need to iterate through all possible pairs of songs (i, j) where i < j and check if `(time[i] + time[j]) % 60 == 0`. The goal is to count how many such pairs exist.

Solution Approach

The provided solution uses an array `remainders` of size 60 to store the count of each remainder. It iterates through the input array `time`, calculates the remainder of each song's duration when divided by 60, and updates the count. While iterating, for each song, it checks how many songs already exist that have a remainder that complements the current song's remainder (i.e., the sum of the remainders is a multiple of 60).

Step-by-Step Algorithm

  1. Step 1: Initialize an array `remainders` of size 60 with all elements set to 0. This array will store the counts of each remainder when the song durations are divided by 60.
  2. Step 2: Initialize a variable `count` to 0. This variable will store the total number of pairs that satisfy the condition.
  3. Step 3: Iterate through the input array `time`.
  4. Step 4: For each song duration `t` in `time`, calculate its remainder `remainder = t % 60`.
  5. Step 5: If the `remainder` is 0, increment `count` by the value of `remainders[0]`. Otherwise, increment `count` by the value of `remainders[60 - remainder]`. This step is finding pairs that sum up to a multiple of 60.
  6. Step 6: Increment `remainders[remainder]` by 1. This updates the count of the current remainder.
  7. Step 7: After iterating through all the song durations, return the final `count`.

Key Insights

  • Insight 1: The crucial insight is that we only need to consider the remainder of each song's duration when divided by 60. If `(time[i] + time[j]) % 60 == 0`, then `(time[i] % 60 + time[j] % 60) % 60 == 0`. This simplifies the problem.
  • Insight 2: Instead of checking every pair, we can use a hash table (or in this case, an array of size 60) to store the frequency of each remainder. This allows us to efficiently find pairs that sum to a multiple of 60.
  • Insight 3: We need to handle the case where both remainders are 0 separately. For a remainder r, we are looking for a remainder of 60-r. However, when r is 0, we are looking for another remainder of 0.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table, Counting.

Companies

Asked at: Akamai, Atlassian, BlackRock, Citrix, Docusign, PayPal, Salesforce.