Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1], A{K+2], ... A[A.length - 1], A[0], A[1], ..., A[K-1].  Afterward, any entries that are less than or equal to their index are worth 1 point.

For example, if we have [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].

Over all possible rotations, return the rotation index K that corresponds to the highest score we could receive.  If there are multiple answers, return the smallest such index K.

Example 1:
Input: [2, 3, 1, 4, 0]
Output: 3
Explanation:
Scores for each K are listed below:
K = 0, A = [2,3,1,4,0], score 2
K = 1, A = [3,1,4,0,2], score 3
K = 2, A = [1,4,0,2,3], score 3
K = 3, A = [4,0,2,3,1], score 4
K = 4, A = [0,2,3,1,4], score 3

So we should choose K = 3, which has the highest score.

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

Note:

  • A will have length at most 20000.
  • A[i] will be in the range [0, A.length].

题意很简单,如果简单想的话,其实有个n^2的解法,就是每个数字都算它能够加分的部分,然后存起来把分数最大的那个拿出来就好了

但对于初始位置,我们也可以知道对于这个数字的移动范围,比如a[5]=2,那么这位置上的2,可以移动5-2=3个位置不会减分。。

这里知道的是,如果移动i个位置,那么范围在i-1的数字将失去得分,变为0。

那么把a[4]放到最后一个位置呢?我们需要判断一下就好了,处理完毕之后,毕竟把a[4]放在最后一位,我们又要计算一次移动范围。

class Solution:
def bestRotation(self, A):
"""
:type A: List[int]
:rtype: int
"""
List= [0 for x in range(len(A)*3)]
sum = 0
Len = len(A)
for i in range(Len):
if i>=A[i]:
List[i-A[i]]=List[i-A[i]]+1 #i-ans表示可以移动的范围
sum=sum+1
Max = sum
tag = 0
for i in range(1,Len):
sum=sum-List[i-1]#每次移动i个元素,那么i-1范围的将无法符合要求
List[i-1] = 0
ans=A[i-1]-(Len-1)
if ans<=0:
sum=sum+1
List[i-ans]=List[i-ans]+1
if Max<sum:
Max=sum
tag=i return tag
int bestRotation(int* A, int ASize) {
int* rotationPoint = calloc(ASize, sizeof(int));
for (int i = ; i < ASize; ++i) {
int target = A[i];
for (int k = ; k < ASize; ++k) {
if ((i >= k && target <= i - k) || (i < k && target <= ASize - k + i))
rotationPoint[k]++;
}
}
int max = -, k;
for (int i = ; i < ASize; ++i) {
if (rotationPoint[i] > max) {
max = rotationPoint[i];
k = i;
}
}
free(rotationPoint);
return k;
}

75th LeetCode Weekly Contest Smallest Rotation with Highest Score的更多相关文章

  1. [LeetCode] Smallest Rotation with Highest Score 得到最高分的最小旋转

    Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1 ...

  2. LeetCode – Smallest Rotation with Highest Score

    Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1 ...

  3. 【leetcode】Smallest Rotation with Highest Score

    题目如下: Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], ...

  4. [Swift]LeetCode798. 得分最高的最小轮调 | Smallest Rotation with Highest Score

    Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1 ...

  5. 75th LeetCode Weekly Contest Champagne Tower

    We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...

  6. 75th LeetCode Weekly Contest All Paths From Source to Target

    Given a directed, acyclic graph of N nodes.  Find all possible paths from node 0 to node N-1, and re ...

  7. 75th LeetCode Weekly Contest Rotate String

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  8. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  9. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

随机推荐

  1. mysql show status 参数解析

    状态名 作用域 详细解释 Aborted_clients Global 由于客户端没有正确关闭连接导致客户端终止而中断的连接数 Aborted_connects Global 试图连接到MySQL服务 ...

  2. C++——override

    override关键字作用: 如果派生类在虚函数声明时使用了override描述符,那么该函数必须重载其基类中的同名函数,否则代码将无法通过编译.举例子说明 struct Base { virtual ...

  3. go语言的第一个helloworld

    1.新建一个hello.go文件 添加如下代码: package main  // 代码包声明语句. import "fmt" //系统包用来输出的 func main() { / ...

  4. Switch/Case 的穿透性

    /*键盘录入1到12 ,对应输出该月份对应的季节 .如果输入的不是1到12,输出提示信息:您输入的数据有误. PS: 春季:3,4,5月份 夏季: 6,7,8月份 秋季: 9,10,11月份 冬季:1 ...

  5. Git 之 配置文件与用户凭证

    配置文件 Git的配置文件有三个: 系统配置: /private/etc/gitconfig 用户配置: ~/.gitconfig 项目配置:.git/config 用户凭证 由于Git和Github ...

  6. Linux问题FAQ1

    1.使用vi编辑器时候,按方向键会产生A,B,C之类的 解决办法:ubuntu server 8.04, vim版本为 7.1.138,客户端使用pietty.vim 在插入模式下, 方向键被转为A ...

  7. 关于 windows mobile 进程操作的方法

    #region Process class /// <summary> /// Summary description for Process. /// </summary> ...

  8. Charles常见问题汇总

    Charles是一款很好用的抓包修改工具,但是如果你不是很熟悉这个工具的话,肯定会遇到各种感觉很莫名其妙的状况,这里就来帮你一一解答下面再说说charles的一些其他常用的功能. 选择请求后,右键可以 ...

  9. Linux下性能监控工具介绍

    本章解释如何使用适用于Linux的大量性能工具及每个工具中信息的意义.即使已经使用top或者sar,也可能从本章学到相关知识. 应该养成使用这些工具的习惯.当然要知道如何诊断性能问题,但也应该定期寻找 ...

  10. Visual Studio 代码格式化插件(等号自动对齐、注释自动对齐等)

    1.下载地址 插件:Code alignment  下载地址 2.介绍 Based on principles borrowed from mathematics and other discipli ...