zoukankan      html  css  js  c++  java
  • [LeetCode 1560] Most Visited Sector in a Circular Track

    Given an integer n and an integer array rounds. We have a circular track which consists of n sectors labeled from 1 to n. A marathon will be held on this track, the marathon consists of m rounds. The ith round starts at sector rounds[i - 1] and ends at sector rounds[i]. For example, round 1 starts at sector rounds[0] and ends at sector rounds[1]

    Return an array of the most visited sectors sorted in ascending order.

    Notice that you circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).

     

    Example 1:

    Input: n = 4, rounds = [1,3,1,2]
    Output: [1,2]
    Explanation: The marathon starts at sector 1. The order of the visited sectors is as follows:
    1 --> 2 --> 3 (end of round 1) --> 4 --> 1 (end of round 2) --> 2 (end of round 3 and the marathon)
    We can see that both sectors 1 and 2 are visited twice and they are the most visited sectors. Sectors 3 and 4 are visited only once.

    Example 2:

    Input: n = 2, rounds = [2,1,2,1,2,1,2,1,2]
    Output: [2]
    

    Example 3:

    Input: n = 7, rounds = [1,3,5,7]
    Output: [1,2,3,4,5,6,7]
    

     

    Constraints:

    • 2 <= n <= 100
    • 1 <= m <= 100
    • rounds.length == m + 1
    • 1 <= rounds[i] <= n
    • rounds[i] != rounds[i + 1] for 0 <= i < m

    This simplest solution of this problem needs this key observation: since the entire marathon has no track sector gaps, so if we go through the entire track sectors once(360 degree), then the visit time to each sector gets cancelled out. This means we will only have 2 cases: (1) if the start sector <= end sector, the most visited sector range is [start, end]. (2) if the start sector > end sector, the most visited sector range is [1, end] + [start, n].  Whether going less than 1 lap or more than 1 laps does not matter as a full lap has no impact on the relative visit times.

    The runtime is O(N) and space is O(1). 

    class Solution {
        public List<Integer> mostVisited(int n, int[] rounds) {
            List<Integer> ans = new ArrayList<>();
            if(rounds[0] <= rounds[rounds.length - 1]) {
                for(int i = rounds[0]; i <= rounds[rounds.length - 1]; i++) {
                    ans.add(i);
                }
            }
            else {
                for(int i = 1; i <= rounds[rounds.length - 1]; i++) {
                    ans.add(i);
                }
                for(int i = rounds[0]; i <= n; i++) {
                    ans.add(i);
                }
            }
            return ans;
        }
    }

    For practice, let's try another O(N) approach: For 2 consecutive round inputs, we can treat it as an interval, then we can convert the original problem to the following problem:

    Give N - 1 intervals, with each interval's start and end index in [1, N], calcuate the frequency of each index and return the most frequent indices in asecending order. 

    We can apply the prefix sum techique to calculate all the frequencies in O(N) time with O(N) extra space. This is one modification we need to make though: the intervals can have bigger start index than end index, as mentioned in case 2 of the above solution. For this kind of intervals, we need to convert it to 2 separate intervals: [1, end], [start, n]. Lastly, do not forget that a previous interval's end index is off by the next interval's start index by 1, otherwise, we'd visit the same sector twice! 

    class Solution {
        public List<Integer> mostVisited(int n, int[] rounds) {
            List<Integer> ans = new ArrayList<>();
            int[] cnt = new int[n + 2];
            for(int i = 1; i < rounds.length; i++) {
                int start = rounds[i - 1];
                int end = rounds[i];
                if(i > 1) {
                    start++;
                    if(start > n) {
                        start = 1;
                    }
                }
                if(start <= end) {
                    cnt[start]++;
                    cnt[end + 1]--;
                }
                else {
                    cnt[start]++;
                    cnt[n + 1]--;
                    cnt[1]++;
                    cnt[end + 1]--;
                }
            }
            int maxCnt = 0;
            for(int i = 1; i < cnt.length; i++) {
                cnt[i] += cnt[i - 1];
                maxCnt = Math.max(maxCnt, cnt[i]);
            }
            for(int i = 1; i <= n; i++) {
                if(cnt[i] == maxCnt) {
                    ans.add(i);
                }
            }
            return ans;
        }
    }

    Related Problems

    [Codeforces] MultiSet

  • 相关阅读:
    Vue.directive()方法创建全局自定义指令
    vue中通过ref属性来获取dom的引用
    v-cloak指令
    v-if和v-show
    vue中的v-on事件监听机制
    vue指令v-model
    vue中v-for系统指令的使用
    从零开始在虚拟机中搭建一个4个节点的CentOS集群(一)-----下载及配置CentOS
    MySQL-数据库创建与删除
    MySQL-PREPARE语句
  • 原文地址:https://www.cnblogs.com/lz87/p/13551752.html
Copyright © 2011-2022 走看看