{"id":7294,"date":"2020-08-23T10:51:03","date_gmt":"2020-08-23T17:51:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7294"},"modified":"2020-08-23T10:51:19","modified_gmt":"2020-08-23T17:51:19","slug":"leetcode-1560-most-visited-sector-in-a-circular-track","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1560-most-visited-sector-in-a-circular-track\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1560. Most Visited Sector in a Circular Track"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>&nbsp;and an integer array&nbsp;<code>rounds<\/code>.&nbsp;We&nbsp;have a circular track which consists of&nbsp;<code>n<\/code>&nbsp;sectors labeled from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>. A marathon will be held on this track, the marathon consists of&nbsp;<code>m<\/code>&nbsp;rounds. The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;round starts at sector&nbsp;<code>rounds[i - 1]<\/code>&nbsp;and ends at sector&nbsp;<code>rounds[i]<\/code>. For example, round 1 starts at sector&nbsp;<code>rounds[0]<\/code>&nbsp;and ends at sector&nbsp;<code>rounds[1]<\/code><\/p>\n\n\n\n<p>Return&nbsp;<em>an array of the most visited sectors<\/em>&nbsp;sorted in&nbsp;<strong>ascending<\/strong>&nbsp;order.<\/p>\n\n\n\n<p>Notice that you&nbsp;circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/14\/tmp.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, rounds = [1,3,1,2]\n<strong>Output:<\/strong> [1,2]\n<strong>Explanation:<\/strong> The marathon starts at sector 1. The order of the visited sectors is as follows:\n1 --&gt; 2 --&gt; 3 (end of round 1) --&gt; 4 --&gt; 1 (end of round 2) --&gt; 2 (end of round 3 and the marathon)\nWe 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.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2, rounds = [2,1,2,1,2,1,2,1,2]\n<strong>Output:<\/strong> [2]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7, rounds = [1,3,5,7]\n<strong>Output:<\/strong> [1,2,3,4,5,6,7]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= n &lt;= 100<\/code><\/li><li><code>1 &lt;= m &lt;= 100<\/code><\/li><li><code>rounds.length == m + 1<\/code><\/li><li><code>1 &lt;= rounds[i] &lt;= n<\/code><\/li><li><code>rounds[i] != rounds[i + 1]<\/code>&nbsp;for&nbsp;<code>0 &lt;= i &lt; m<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  vector<int> mostVisited(int n, vector<int>& rounds) {\n    vector<int> counts(n);\n    counts[rounds[0] - 1] = 1;\n    for (int i = 1; i < rounds.size(); ++i)\n      for (int s = rounds[i - 1]; ; ++s) {\n        ++counts[s %= n];\n        if (s == rounds[i] - 1) break;\n      }\n    const int max_count = *max_element(begin(counts), end(counts));\n    vector<int> ans;\n    for (int i = 0; i < n; ++i)      \n      if (counts[i] == max_count) ans.push_back(i + 1);    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n&nbsp;and an integer array&nbsp;rounds.&nbsp;We&nbsp;have a circular track which consists of&nbsp;n&nbsp;sectors labeled from&nbsp;1&nbsp;to&nbsp;n. A marathon will be held on this track, the marathon consists&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,158,643,179],"class_list":["post-7294","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-mod","tag-round","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7294"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7294\/revisions"}],"predecessor-version":[{"id":7296,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7294\/revisions\/7296"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}