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. 201671010127 2016—2017-2 java学习新征程

    通过大一整个学年对Python和C语言的学习,我对编程的感受有了更进一步的认识.随着时代的进步,编程语言也在实时更新,面对越来越多的编程语言,对于在编程方面的初学者,选择一门适合自己的编程语言就显得十 ...

  2. HDU 5242 树链剖分思想的贪心

    题意及博客 树链剖分分为2步,第一次求出深度,重儿子,第二次求出重链,用到了启发式的思想,即对于比较重的儿子,尽量去完整的维护它.类似于我们去合并两个堆,明显把小的堆逐个插入大的堆中会比大的往小的插更 ...

  3. nodejs读文件

    1.异步读取文件:var fs= require('fs'); // 从文件系统中读取请求的文件内容 fs.readFile(pathname.substr(1), function (err, da ...

  4. Mysql date, time, timestamp日期时间相关

    date: 格式:YYYY-MM-DD,时间范围:[0000-00-00, 9999-12-31],存储空间:3bytes time: 格式:HH:MM:SS,时间范围:[00:00:00, 23:5 ...

  5. Verilog 语言 001 --- 入门级 --- 编写一个半加器电路模块

    Verilog 语言编写一个 半加器 电路模块 半加器 的电路结构: S = A 异或 B C = A 与 B 1. 程序代码 module h_adder (A, B, SO, CO); input ...

  6. boost::thread 库的使用

    转载自:http://blog.csdn.net/yockie/article/details/9181939 概要 通过实例介绍boost thread的使用方式,本文主要由线程启动.Interru ...

  7. 算法Sedgewick第四版-第1章基础-2.3 Quicksort-001快速排序

    一. 1.特点 (1)The quicksort algorithm’s desirable features are that it is in-place (uses only a small a ...

  8. p3584 [POI2015]LAS

    传送门 分析 f[i][S](S∈[0,4])表示第iii个食物没有被选/左边选/右边选/同时选的状态是由哪一个状态转移来的 我们需要满足两个条件: 每个人只能选择一个  改变选择之后不会比当前获得热 ...

  9. ZROI2018普转提day6t3

    传送门 分析 居然卡哈希数,万恶的出题人...... 感觉我这个方法似乎比较呆,我的代码成功成为了全网最慢的代码qwq 应该是可以直接哈希的 但由于我哈希学的不好又想练练线段树维护哈希,于是就写了个线 ...

  10. SDUT 3343 数据结构实验之二叉树四:还原二叉树

    数据结构实验之二叉树四:还原二叉树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定一棵 ...