Most Visited Sector in a Circular Track - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Sunday 23 August 2020

Most Visited Sector in a Circular Track


Most Visited Sector in a Circular Track


 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

// class Solution {

// public:

//     vector<int> mostVisited(int n, vector<int>& rounds) {

        

//         vector<int> a(n,0);

          

//         for(int i=0;i<rounds.size()-1;++i)

//         {

        

//             int k=rounds[i]-1;

//             while(k!=(rounds[i+1]-1))

//             {

//                 a[k]=a[k]+1;;

//                 k=(k+1)%n;

//             }

           

//         }

//         a[rounds[rounds.size()-1]-1]++;

    

//         vector<int> v;

//         vector<pair<int,int> > res;

//         for(int i=0;i<n;++i)

//         {

//             res.push_back({a[i],i+1});

//         }

//         sort(res.begin(),res.end(),[](const pair<int,int> &a,const pair<int,int> &b){

//             return a.first>b.first;

//         });

//         v.push_back(res.begin()->second);

        

//         for(int i=0;i<n-1;++i)

//         {

//             if(res[i].first!=res[i+1].first)

//                 break;

//             else

//                 v.push_back(res[i+1].second);

//         }

//             sort(v.begin(),v.end());

//             return v;

        

//     }

// };


-----------------------------------------------------------------------------------------------------------------------------

class Solution {

public:

    int nn;

    int nxt(int x)

    {

        return x % nn + 1;        

    }

    

    vector<int> mostVisited(int n, vector<int>& rounds) 

    {

        nn = n;

        vector<int> ret;

        for(int j = rounds[0]; j != rounds.back(); j = nxt(j))

            ret.push_back(j);

        ret.push_back(rounds.back());

        sort(ret.begin(),ret.end());

        return ret;

    }

};