1013. Pairs of Songs With Total Durations Divisible by 60

time >=1 and <= 500, so sum >= 2 and sum <= 1000
there are 16 numbers in this interval divisible by 60

from 60 to 960, so the problem becomes find the number of pairs with
sum of 60...960

    class Solution {
        public int numPairsDivisibleBy60(int[] time) {
            int ret = 0;
            int[] counter = new int[501];
            for (int i = 0; i < time.length; ++i) {
                for (int j = 1; j <= 16; ++j) {
                    int target = j * 60 - time[i];
                    if (target <= 0 || target > 500) continue;
                    if (counter[target] != 0) ret += counter[target];
                }
                counter[time[i]] += 1;
            }
            return ret;
        }
    }

And after ac, I saw a better solution in discussion area.

It‘s really great.

            public int numPairsDivisibleBy60(int[] time) {
            int c[]  = new int[60], res = 0;
            for (int t : time) {
                res += c[(60 - t % 60) % 60];
                c[t % 60] += 1;
            }
            return res;
        }

原文地址:https://www.cnblogs.com/exhausttolive/p/10568880.html

时间: 2024-08-29 21:07:36

1013. Pairs of Songs With Total Durations Divisible by 60的相关文章

[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.

Pairs of Songs With Total Durations Divisible by 60 LT1010

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.

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;

Leetcode 373. Find K Pairs with Smallest Sums

373. Find K Pairs with Smallest Sums Total Accepted: 1453 Total Submissions: 5789 Difficulty: Medium You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define a pair (u,v) which consists of one element from t

.net core中加载lua脚本的类库: MoonSharp

前言 MoonSharp是一个支持C#调用lua脚本的类库,支持.net, .net core, mono, unity,因此在.net core中也能够使用,而且加载和调用lua也很方便简单: 官网:http://www.moonsharp.org/ 源码:https://github.com/xanathar/moonsharp nuget:PM> Install-Package MoonSharp 使用 加载脚本 1 string scriptCode = @" 2 sum = 0

cf701E Connecting Universities

Treeland is a country in which there are n towns connected by n?-?1 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2k universities which are located in different towns. Recently, the president signe

Bubble Cup X - Finals [Online Mirror] B. Neural Network country 矩阵快速幂加速转移

B. Neural Network country time limit per test 2 seconds memory limit per test 256 megabytes Due to the recent popularity of the Deep learning new countries are starting to look like Neural Networks. That is, the countries are being built deep with ma

监控 -- vmstat

小Q:躲在某一时间,想念一段时光的掌纹:躲在某一地点,想念一个站在来路也站在去路的,让我牵挂的人. 简单的描述,希望可以帮到大家,有更多关于vmstat的用法,希望可以互相讨论! ====================================================== vmstat命令:  用来获得有关进程.虚存.页面交换空间及 CPU活动的信息.这些信息反映了系统的负载情况 系统中运行的每个进程都需要使用内存,但不是每个进程都需要每时每刻使用系统分 配的内存空间.当系统运行

Connecting Universities

Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2k universities which are located in different towns. Recently, the president signe