返回介绍

solution / 0700-0799 / 0798.Smallest Rotation with Highest Score / README_EN

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

798. Smallest Rotation with Highest Score

中文文档

Description

You are given an array nums. You can rotate it by a non-negative integer k so that the array becomes [nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]. Afterward, any entries that are less than or equal to their index are worth one point.

  • For example, if we have nums = [2,4,1,3,0], and we rotate by k = 2, it becomes [1,3,0,2,4]. This is worth 3 points because 1 > 0 [no points], 3 > 1 [no points], 0 <= 2 [one point], 2 <= 3 [one point], 4 <= 4 [one point].

Return _the rotation index _k_ that corresponds to the highest score we can achieve if we rotated _nums_ by it_. If there are multiple answers, return the smallest such index k.

 

Example 1:

Input: nums = [2,3,1,4,0]
Output: 3
Explanation: Scores for each k are listed below: 
k = 0,  nums = [2,3,1,4,0],  score 2
k = 1,  nums = [3,1,4,0,2],  score 3
k = 2,  nums = [1,4,0,2,3],  score 3
k = 3,  nums = [4,0,2,3,1],  score 4
k = 4,  nums = [0,2,3,1,4],  score 3
So we should choose k = 3, which has the highest score.

Example 2:

Input: nums = [1,3,0,2,4]
Output: 0
Explanation: nums will always have 3 points no matter how it shifts.
So we will choose the smallest k, which is 0.

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] < nums.length

Solutions

Solution 1

class Solution:
  def bestRotation(self, nums: List[int]) -> int:
    n = len(nums)
    mx, ans = -1, n
    d = [0] * n
    for i, v in enumerate(nums):
      l, r = (i + 1) % n, (n + i + 1 - v) % n
      d[l] += 1
      d[r] -= 1
    s = 0
    for k, t in enumerate(d):
      s += t
      if s > mx:
        mx = s
        ans = k
    return ans
class Solution {
  public int bestRotation(int[] nums) {
    int n = nums.length;
    int[] d = new int[n];
    for (int i = 0; i < n; ++i) {
      int l = (i + 1) % n;
      int r = (n + i + 1 - nums[i]) % n;
      ++d[l];
      --d[r];
    }
    int mx = -1;
    int s = 0;
    int ans = n;
    for (int k = 0; k < n; ++k) {
      s += d[k];
      if (s > mx) {
        mx = s;
        ans = k;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int bestRotation(vector<int>& nums) {
    int n = nums.size();
    int mx = -1, ans = n;
    vector<int> d(n);
    for (int i = 0; i < n; ++i) {
      int l = (i + 1) % n;
      int r = (n + i + 1 - nums[i]) % n;
      ++d[l];
      --d[r];
    }
    int s = 0;
    for (int k = 0; k < n; ++k) {
      s += d[k];
      if (s > mx) {
        mx = s;
        ans = k;
      }
    }
    return ans;
  }
};
func bestRotation(nums []int) int {
  n := len(nums)
  d := make([]int, n)
  for i, v := range nums {
    l, r := (i+1)%n, (n+i+1-v)%n
    d[l]++
    d[r]--
  }
  mx, ans, s := -1, n, 0
  for k, t := range d {
    s += t
    if s > mx {
      mx = s
      ans = k
    }
  }
  return ans
}

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

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

发布评论

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