ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 798. Smallest Rotation with Highest Score

2022-08-07 15:03:57  阅读:148  来源: 互联网

标签:index score point int nums Score 798 Smallest diff


原题链接在这里: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

题解:

For the example 1 in [2,3,1,4,0]. The index of 1 is 2. 1 <= index, it is counted as 1 point.

It is rotating 2 times to index 0, it is going to lose 1 point.

It is rotated from i to A[i] - 1, which means rotates i - (A[i] - 1) times.

And when it rotate from index 0 to n - 1, it is going to gain one point.

Thus we have diff to maintain the decreasing point for each step. 

The second for loop is to accumlate the change point. totoal[3] = total[2] + diff[3] + 1.

For 0 and n, it would never add or minus point, we could think it from 0 to n - 1, first minus 1 point and then add 1 point.

Time Complexity: O(n). n = nums.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int bestRotation(int[] nums) {
 3         int n = nums.length;
 4         int res = 0;
 5         int [] diff = new int[n];
 6         for(int i = 0; i < n; i++){
 7             diff[(i - nums[i] + 1 + n) % n]--;
 8         }
 9         
10         for(int k = 1; k < n; k++){
11             diff[k] += diff[k - 1] + 1;
12             if(diff[res] < diff[k]){
13                 res = k;
14             }
15         }
16         
17         return res;
18     }
19 }

 

标签:index,score,point,int,nums,Score,798,Smallest,diff
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16559049.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有