Code Signal_练习题_arrayChange
You are given an array of integers. On each move you are allowed to increase exactly one of its element by one. Find the minimal number of moves required to obtain a strictly increasing sequence from the input.
Example
For inputArray = [1, 1, 1], the output should bearrayChange(inputArray) = 3.
我的解答:
def arrayChange(inputArray):
count = 0
for i in range(len(inputArray)-1):
if inputArray[i] >= inputArray[i+1]:
count += inputArray[i] + 1 - inputArray[i + 1]
inputArray[i+1] = inputArray[i] + 1
return count
膜拜大佬:
def arrayChange(inputArray):
a = 0
for i in range(1, len(inputArray)):
if inputArray[i] <= inputArray[i - 1]:
f = (inputArray[i - 1] - inputArray[i]) + 1
inputArray[i] = inputArray[i - 1] + 1
a += f
return a
Code Signal_练习题_arrayChange的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- Code Signal_练习题_absoluteValuesSumMinimization
Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...
随机推荐
- canvas绘制简单的霓虹灯效果
canvas简单动画分为三个步骤: 1.清除画布区域的内容: 2.重绘: 3.执行requestAnimationFrame(); 这个霓虹灯效果的demo,我没有用requestAnimationF ...
- 2016级算法第六次上机-B.ModricWang's FFT : EASY VERSION
1114 ModricWang's FFT EASY VERSION 思路 利用FFT做大整数乘法,实际上是把大整数变成多项式,然后做多项式乘法. 例如,对于\(1234\),改写成\(f(x)=1* ...
- 皕杰报表 javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
今天做报表的时候,跳转到显示报表页面的时候不出来数据,报错说数据集未产生. 后台报错 javax.naming.NameNotFoundException: Name jdbc is not boun ...
- SPOJ Lexicographical Substring Search 求字典序第k大子串 后缀自动机
题目传送门 思路:按字典序,小的字符优先选取.对于一个字符,如果以这个字符开头的子串大于等于k个,那说明这个字符是应该选的,并且选完之后,可能还要继续选.如果以这个字符开头的子串小于k个,说明这个字符 ...
- 转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- OpenERP 中的on_change方法总结
1.xml中应为on_change=""的形式 2.py文件中 self,cr,uid,ids为必备参数,后面的参数根据xml文件中的参数的数量而定 3.return的是一个字典, ...
- (转)drbd详解
原文:http://blog.csdn.net/u014421556/article/details/52925442
- Android动画原理
1 Frame Animation:大体意思就是将UI设计的多张图片组成的动画,然后在将他们组合起来连贯进行播放,类似于早期电影的工作原理. 2 Tween Animation:是对某个View进行一 ...
- 对于SQL Server 2008删除或压缩数据库日志的方法
由于数据库日志增长被设置为“无限制”,所以时间一长日志文件必然会很大,一个400M的数据库居然有4G的LOG文件,严重占用了磁盘空间.由于主要是做OLAP,所以数据库本身不会有大变动,所以日志也就没有 ...
- Hibernate 查询语句基本用法
转发: http://459104018-qq-com.iteye.com/blog/720538