【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], 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].
结题思路:本题主要考察的是算法的时间复杂度,如果时间复杂度是O(n^2)的话,系统会判定超时,所以关键是优化算法。如下图,我们可以把题意理解成是每次把数组的第一个元素移到最后然后求出当前状态的point,最后得出最大的point出现的时机。point是根据元素的值减去元素所在下标得来的,只有元素的值小于或者等于下标才能得到point。接下来,我们再观察每次移动数组的第一个元素后的得失point情况,首先只有一个可能会得到point的,那就是移动的元素小于数组末尾的下标;对于失去point的情况,除了第一个元素外,其他的元素相当于是右移,而右移是一个下标减小的过程,元素的值不变,而下标不断减小,所以在平移过程中,原先能得到point的元素可能会变得得不到point,而原先就等不到的不管平移到哪里都还是得不到。发现这些规律后,结题的方法就呼之欲出了。首先,遍历数组,得到所有得到point的元素数量count,以及一个包括元素值和元素下标差值v以及v出现的次数的字典(这里我用的是数组,可能直接通过下标查找),显然所有v出现的次数是等于point的元素数量的。接下来就是关键了,再次遍历数组,每移动一个元素,用count减去失去point元素的数量,再加上移动到最后的元素是否能得到point的数量,即是这次移动后的point值,直到所有元素都移动到最后,即可求出最大的point。还有一点要注意的是,第一个元素移到数组最后时,如果能得到point,需要把这个元素加到字典中去,因为随着数组的移动,这个元素后面有可能又会失去point。
代码如下:
class Solution(object):
def bestRotation(self, A):
"""
:type A: List[int]
:rtype: int
"""
l = []
K = 0
highest = 0
val = [0 for x in range(len(A)*2)]
for i in range(len(A)):
if A[i] - i <= 0:
val[-1*(A[i] -i)] += 1
highest += 1
count = highest
for i in range(1,len(A)):
count -= val[i-1] #每移动一个元素,用count减去失去point元素的数量
val[i-1] = 0
t = A[i-1] - (len(A) -1)
if t <= 0:
count += 1 #再加上移动到最后的元素是否能得到point的数量
val[-1*t+i] += 1 #关键点:要把平移的元素重新加入字典,因为后面可能会失去point
if highest <count:
highest = count
K = i
return K
【leetcode】Smallest Rotation with Highest Score的更多相关文章
- [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 ...
- 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 ...
- 75th LeetCode Weekly Contest 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 ...
- [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 ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
随机推荐
- 阶段3 1.Mybatis_02.Mybatis入门案例_3.mybatis注解开发和编写dao实现类的方式
注解的用法 直接创建一个新的项目 下一步直接next 然后finish即可 把之前项目01里面的代码直接复制过来 复制到我们02的注解的工程中 把01项目导入的依赖也都粘贴过来 再把测试类复制过去 复 ...
- 系统分析与设计HW4
个人作业 用例建模 a. 阅读 Asg_RH 文档,绘制用例图. b. 选择你熟悉的定旅馆在线服务系统(或移动 APP),如绘制用例图.并满足以下要求: 对比 Asg_RH 用例图,请用色彩标注出创新 ...
- JMV监控工具之JConsole
一.简介 JConsole是一个基于JMX的GUI工具,用于连接正在运行的JVM,它是Java自带的简单性能监控工具.下面以对tomcat的监控为例,带领大家熟悉JConsole这个工具. 二.配置 ...
- Debian系统中当安装deb软件时出现:deb cdrom:[Debian GNU/Linux 9.3.0 _Stretch_ - Official amd64 DVD Binary-1 20171209-12:11]/ stretch contrib main
vi /etc/apt/sources.list // 注释掉下面这句话# deb cdrom:[Debian GNU/Linux 9.3.0 _Stretch_ - Official amd64 D ...
- Spring MVC 中RequestContextHolder获取request和response
1.最简单方式:处理方法入参 例如: @RequestMapping("/test") @ResponseBody public void saveTest(HttpServlet ...
- cocos2dx基础篇(21) 进度条CCProgressTimer
[3.x] (1)去掉 "CC" (2)CCProgressTimerType 改为强枚举 ProgressTimer::Type:: // RADIAL //扇形进度计时器 BA ...
- (转载)gcc编译选项总结
转载自:https://blog.csdn.net/gatieme/article/details/21389603 常用编译选项 gcc and g++分别是gnu的c & c++编译器 g ...
- vue防止 由于网速出现 闪现{{}}
防止闪现可能应为网速的原因{{msg}} 一直解析不了, 于是用户就看到它了,不友好, 于是 vue推出 与css配合 [v-cloak] {display:none}
- C语言第十一周作业
这个作业属于哪个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/ ...
- [转帖]APT34攻击全本分析
APT34攻击全本分析 https://zhuanlan.kanxue.com/article-8401.htm 高手太多 我等菜鸟目不暇接.. 一.事件综述 2019年4月18日,黑客/黑客组织使用 ...