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.

  1. Example 1:
  2. Input: [2, 3, 1, 4, 0]
  3. Output: 3
  4. Explanation:
  5. Scores for each K are listed below:
  6. K = 0, A = [2,3,1,4,0], score 2
  7. K = 1, A = [3,1,4,0,2], score 3
  8. K = 2, A = [1,4,0,2,3], score 3
  9. K = 3, A = [4,0,2,3,1], score 4
  10. K = 4, A = [0,2,3,1,4], score 3

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

  1. Example 2:
  2. Input: [1, 3, 0, 2, 4]
  3. Output: 0
  4. Explanation: A will always have 3 points no matter how it shifts.
  5. 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]放在最后一位,我们又要计算一次移动范围。

  1. class Solution:
  2. def bestRotation(self, A):
  3. """
  4. :type A: List[int]
  5. :rtype: int
  6. """
  7. List= [0 for x in range(len(A)*3)]
  8. sum = 0
  9. Len = len(A)
  10. for i in range(Len):
  11. if i>=A[i]:
  12. List[i-A[i]]=List[i-A[i]]+1 #i-ans表示可以移动的范围
  13. sum=sum+1
  14. Max = sum
  15. tag = 0
  16. for i in range(1,Len):
  17. sum=sum-List[i-1]#每次移动i个元素,那么i-1范围的将无法符合要求
  18. List[i-1] = 0
  19. ans=A[i-1]-(Len-1)
  20. if ans<=0:
  21. sum=sum+1
  22. List[i-ans]=List[i-ans]+1
  23. if Max<sum:
  24. Max=sum
  25. tag=i
  26.  
  27. return tag
  1. int bestRotation(int* A, int ASize) {
  2. int* rotationPoint = calloc(ASize, sizeof(int));
  3. for (int i = ; i < ASize; ++i) {
  4. int target = A[i];
  5. for (int k = ; k < ASize; ++k) {
  6. if ((i >= k && target <= i - k) || (i < k && target <= ASize - k + i))
  7. rotationPoint[k]++;
  8. }
  9. }
  10. int max = -, k;
  11. for (int i = ; i < ASize; ++i) {
  12. if (rotationPoint[i] > max) {
  13. max = rotationPoint[i];
  14. k = i;
  15. }
  16. }
  17. free(rotationPoint);
  18. return k;
  19. }

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. oracle --(一)数据块(data Block)

    基本关系:数据库---表空间---数据段---分区---数据块 数据块(data Block)一.数据块Block是Oracle存储数据信息的最小单位.这里说的是Oracle环境下的最小单位.Orac ...

  2. spirng boot web配置开发

    spring-booter-starter-web是spring-boot web发开的核心,自动配置信息存储在spring-boot-autoconfigure.jar 下面的web目录里面,包含了 ...

  3. Opengl使用模型视图变换移动光源

    光源绕一个物体旋转,按下鼠标左键时,光源位置旋转. #include <GL/glut.h> static int spin = 0;static GLdouble x_1 = 0.0;s ...

  4. 提取a标签的链接文字

    在seg上看到一个问题 <a href="http://www.abc.com/thread-4131866-1-1.html" class="s xst" ...

  5. 红帽企业版RHEL7.1在研域工控板上,开机没有登陆窗口 -- 编写xorg.conf 简单三行解决Ubuntu分辩率不可调的问题

    红帽企业版RHEL7.1在研域工控板上,开机没有登陆窗口 没有登陆窗口 的原因分析: 没有登陆窗口的原因是因为有多个屏幕在工作,其中一个就是build-in 屏幕(内置的虚拟屏幕)和外接的显示器,并且 ...

  6. (转)Linux操作系统下VMware的多网卡桥接转换

    VMware,鼎鼎大名的虚拟机软件,没有人不知道吧?当然,在Linux下使用虚拟机软件,并不一定需要使用VMWare,Xen也是非常不错的选择,有很多评测就认为XEN的表现优于VMware.可惜的是X ...

  7. SDUT 3379 数据结构实验之查找七:线性之哈希表

    数据结构实验之查找七:线性之哈希表 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定 ...

  8. 事件Event 介绍总结

    最近在总结一些基础的东西,主要是学起来很难懂,但是在日常又有可能会经常用到的东西.前面介绍了 C# 的 AutoResetEvent的使用介绍, 这次介绍事件(event). 事件(event),对于 ...

  9. Algorithms - Quick Sort

    印象 图2 快速排序过程 思想 通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序的目的. 分析 稳定: ...

  10. centoOS下安装python3 和 pip: command not found

    在更新python3的时候会自动安装pip3,但是安装完成后,pip -V发现出错:command not found,找了好久,发现在建立软连接的时候路径写错了. 总结一下安装python3和发现p ...