[Swift Weekly Contest 128]LeetCode1013. 总持续时间可被 60 整除的歌曲 | Pairs of Songs With Total Durations Divisible by 60

In a list of songs, 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 with (time[i] + time[j]) % 60 == 0.

Example 1:

Input: [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: [60,60,60]
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.

Note:

  1. 1 <= time.length <= 60000
  2. 1 <= time[i] <= 500


在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒。

返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量。形式上,我们希望索引的数字  i < j 且有 (time[i] + time[j]) % 60 == 0

示例 1:

输入:[30,20,150,100,40]
输出:3
解释:这三对的总持续时间可被 60 整数:
(time[0] = 30, time[2] = 150): 总持续时间 180
(time[1] = 20, time[3] = 100): 总持续时间 120
(time[1] = 20, time[4] = 40): 总持续时间 60

示例 2:

输入:[60,60,60]
输出:3
解释:所有三对的总持续时间都是 120,可以被 60 整数。 

提示:

  1. 1 <= time.length <= 60000
  2. 1 <= time[i] <= 500


Runtime: 232 ms

Memory Usage: 19.4 MB

 1 class Solution {
 2     func numPairsDivisibleBy60(_ time: [Int]) -> Int {
 3         var n:Int = time.count
 4         var cnt:[Int] = [Int](repeating:0,count:60)
 5         var ans:Int = 0
 6         for i in 0..<n
 7         {
 8             var t:Int = time[i] % 60
 9             ans += cnt[(60 - t) % 60]
10             cnt[time[i] % 60] += 1
11         }
12         return ans
13
14     }
15 }

原文地址:https://www.cnblogs.com/strengthen/p/10546217.html

时间: 2024-10-05 06:22:02

[Swift Weekly Contest 128]LeetCode1013. 总持续时间可被 60 整除的歌曲 | Pairs of Songs With Total Durations Divisible by 60的相关文章

Leetcode-1013 Pairs of Songs With Total Durations Divisible by 60(总持续时间可被 60 整除的歌曲)

class Solution { public: int numPairsDivisibleBy60(vector<int>& time) { int hash[1501] {0}; for(int i = 0;i < time.size();i ++) { hash[time[i]] ++; } int rnt = 0; for(int i = 1;i < 501;i ++) { int left = 60-i; while(left<0) left += 60;

[Swift Weekly Contest 128]LeetCode1015. 至少有 1 位重复的数字 | Numbers With 1 Repeated Digit

Given a positive integer N, return the number of positive integers less than or equal to N that have at least 1 repeated digit. Example 1: Input: 20 Output: 1 Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11. Example

力扣(LeetCode) 1010. 总持续时间可被 60 整除的歌曲

在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒. 返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量.形式上,我们希望索引的数字 i < j 且有 (time[i] + time[j]) % 60 == 0. 示例 1: 输入:[30,20,150,100,40] 输出:3 解释:这三对的总持续时间可被 60 整数: (time[0] = 30, time[2] = 150): 总持续时间 180 (time[1] = 20, time[3] = 100): 总持续时间

[Swift Weekly Contest 120]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree

Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or

[Swift Weekly Contest 127]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal

Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that: For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j]. Given N, return any beautiful array A.  (It is guaranteed th

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t