返回介绍

solution / 1500-1599 / 1560.Most Visited Sector in a Circular Track / README_EN

发布于 2024-06-17 01:03:18 字数 4290 浏览 0 评论 0 收藏 0

1560. Most Visited Sector in a Circular Track

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def mostVisited(self, n: int, rounds: List[int]) -> List[int]:
    if rounds[0] <= rounds[-1]:
      return list(range(rounds[0], rounds[-1] + 1))
    return list(range(1, rounds[-1] + 1)) + list(range(rounds[0], n + 1))
class Solution {
  public List<Integer> mostVisited(int n, int[] rounds) {
    int m = rounds.length - 1;
    List<Integer> ans = new ArrayList<>();
    if (rounds[0] <= rounds[m]) {
      for (int i = rounds[0]; i <= rounds[m]; ++i) {
        ans.add(i);
      }
    } else {
      for (int i = 1; i <= rounds[m]; ++i) {
        ans.add(i);
      }
      for (int i = rounds[0]; i <= n; ++i) {
        ans.add(i);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> mostVisited(int n, vector<int>& rounds) {
    int m = rounds.size() - 1;
    vector<int> ans;
    if (rounds[0] <= rounds[m]) {
      for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
    } else {
      for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
      for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
    }
    return ans;
  }
};
func mostVisited(n int, rounds []int) []int {
  m := len(rounds) - 1
  var ans []int
  if rounds[0] <= rounds[m] {
    for i := rounds[0]; i <= rounds[m]; i++ {
      ans = append(ans, i)
    }
  } else {
    for i := 1; i <= rounds[m]; i++ {
      ans = append(ans, i)
    }
    for i := rounds[0]; i <= n; i++ {
      ans = append(ans, i)
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文