返回介绍

solution / 1000-1099 / 1010.Pairs of Songs With Total Durations Divisible by 60 / README_EN

发布于 2024-06-17 01:03:32 字数 5072 浏览 0 评论 0 收藏 0

1010. Pairs of Songs With Total Durations Divisible by 60

中文文档

Description

You are given a list of songs where the ith 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
(time[1] = 20, time[4] = 40): total duration 60

Example 2:

Input: time = [60,60,60]
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.

 

Constraints:

  • 1 <= time.length <= 6 * 104
  • 1 <= time[i] <= 500

Solutions

Solution 1

class Solution:
  def numPairsDivisibleBy60(self, time: List[int]) -> int:
    cnt = Counter(t % 60 for t in time)
    ans = sum(cnt[x] * cnt[60 - x] for x in range(1, 30))
    ans += cnt[0] * (cnt[0] - 1) // 2
    ans += cnt[30] * (cnt[30] - 1) // 2
    return ans
class Solution {
  public int numPairsDivisibleBy60(int[] time) {
    int[] cnt = new int[60];
    for (int t : time) {
      ++cnt[t % 60];
    }
    int ans = 0;
    for (int x = 1; x < 30; ++x) {
      ans += cnt[x] * cnt[60 - x];
    }
    ans += (long) cnt[0] * (cnt[0] - 1) / 2;
    ans += (long) cnt[30] * (cnt[30] - 1) / 2;
    return ans;
  }
}
class Solution {
public:
  int numPairsDivisibleBy60(vector<int>& time) {
    int cnt[60]{};
    for (int& t : time) {
      ++cnt[t % 60];
    }
    int ans = 0;
    for (int x = 1; x < 30; ++x) {
      ans += cnt[x] * cnt[60 - x];
    }
    ans += 1LL * cnt[0] * (cnt[0] - 1) / 2;
    ans += 1LL * cnt[30] * (cnt[30] - 1) / 2;
    return ans;
  }
};
func numPairsDivisibleBy60(time []int) (ans int) {
  cnt := [60]int{}
  for _, t := range time {
    cnt[t%60]++
  }
  for x := 1; x < 30; x++ {
    ans += cnt[x] * cnt[60-x]
  }
  ans += cnt[0] * (cnt[0] - 1) / 2
  ans += cnt[30] * (cnt[30] - 1) / 2
  return
}
function numPairsDivisibleBy60(time: number[]): number {
  const cnt: number[] = new Array(60).fill(0);
  for (const t of time) {
    ++cnt[t % 60];
  }
  let ans = 0;
  for (let x = 1; x < 30; ++x) {
    ans += cnt[x] * cnt[60 - x];
  }
  ans += (cnt[0] * (cnt[0] - 1)) / 2;
  ans += (cnt[30] * (cnt[30] - 1)) / 2;
  return ans;
}

Solution 2

class Solution:
  def numPairsDivisibleBy60(self, time: List[int]) -> int:
    cnt = Counter()
    ans = 0
    for x in time:
      x %= 60
      y = (60 - x) % 60
      ans += cnt[y]
      cnt[x] += 1
    return ans
class Solution {
  public int numPairsDivisibleBy60(int[] time) {
    int[] cnt = new int[60];
    int ans = 0;
    for (int x : time) {
      x %= 60;
      int y = (60 - x) % 60;
      ans += cnt[y];
      ++cnt[x];
    }
    return ans;
  }
}
class Solution {
public:
  int numPairsDivisibleBy60(vector<int>& time) {
    int cnt[60]{};
    int ans = 0;
    for (int x : time) {
      x %= 60;
      int y = (60 - x) % 60;
      ans += cnt[y];
      ++cnt[x];
    }
    return ans;
  }
};
func numPairsDivisibleBy60(time []int) (ans int) {
  cnt := [60]int{}
  for _, x := range time {
    x %= 60
    y := (60 - x) % 60
    ans += cnt[y]
    cnt[x]++
  }
  return
}
function numPairsDivisibleBy60(time: number[]): number {
  const cnt: number[] = new Array(60).fill(0);
  let ans: number = 0;
  for (let x of time) {
    x %= 60;
    const y = (60 - x) % 60;
    ans += cnt[y];
    ++cnt[x];
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文