Algorithm


Problem Name: 798. Smallest Rotation with Highest Score

Problem Link: https://leetcode.com/problems/smallest-rotation-with-highest-score/

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

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int bestRotation(vector<int>& A) {
        int n = A.size();
        vector<int>k(n);
        for(int i = 0; i  <  n; i++){
            for(int j = 0; j  < = i - A[i]; j++) k[j]++;
            for(int j = i + 1; j  < = i + n - A[i] && j < n; j++) k[j]++;
        }
        return max_element(k.begin(), k.end()) - k.begin();
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [2,3,1,4,0]

Output

x
+
cmd
3

#2 Code Example with Javascript Programming

Code - Javascript Programming


const bestRotation = function(A) {
  const N = A.length
  const bad = new Array(N).fill(0)
  for (let i = 0; i  <  N; ++i) {
    let left = (i - A[i] + 1 + N) % N
    let right = (i + 1) % N
    bad[left]--
    bad[right]++
    if (left > right) bad[0]--
  }

  let best = -N
  let ans = 0,
    cur = 0
  for (let i = 0; i  <  N; ++i) {
    cur += bad[i]
    if (cur > best) {
      best = cur
      ans = i
    }
  }
  return ans
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [2,3,1,4,0]

Output

x
+
cmd
3

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def bestRotation(self, A):
        N = len(A)
        change = [1] * N
        for i in range(N): change[(i - A[i] + 1) % N] -= 1
        for i in range(1, N): change[i] += change[i - 1]
        return change.index(max(change))
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,3,0,2,4]
Advertisements

Demonstration


Previous
#797 Leetcode All Paths From Source to Target Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#799 Leetcode Champagne Tower Solution in C, C++, Java, JavaScript, Python, C# Leetcode