码迷,mamicode.com
首页 > 其他好文 > 详细

1013. Pairs of Songs With Total Durations Divisible by 60

时间:2019-03-21 00:51:00      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:++i   java   real   visible   ber   ...   targe   eal   val   

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;
        }

1013. Pairs of Songs With Total Durations Divisible by 60

标签:++i   java   real   visible   ber   ...   targe   eal   val   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!