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) + ...
随机推荐
- 1.HTML练习(二)
一.表格练习: 1.<table>标签:声明一个表格,它的常用属性如下: border属性 定义表格的边框,设置值是数值 cellpadding属性 定义单 ...
- 利用python 学习数据分析 (学习二)
内容学习自: Python for Data Analysis, 2nd Edition 就是这本 纯英文学的很累,对不对取决于百度翻译了 前情提要: 各种方法贴: https://w ...
- L03-Linux RHEL6.5系统中配置本地yum源
1.将iso镜像文件上传到linux系统.注意要将文件放在合适的目录下,因为后面机器重启时还要自动挂载,所以此次挂载成功之后该文件也不要删除. 2.将iso光盘挂载到/mnt/iso目录下. (1)先 ...
- Hadoop 3.1.2 下载安装和分布式搭建的准备
官方有详细文档:https://hadoop.apache.org/docs/r3.1.2/hadoop-project-dist/hadoop-common/SingleCluster.html 我 ...
- php 前台生成多维数组 后台批量添加
同一个地方绊倒两次,记录一下哈 1)前台表单,看 name <div class="tab-pane row " id="tab-1" > < ...
- Dijkstra+优先队列 堆优化
关于堆优化 传统\(Dijkstra\),在选取中转站时,是遍历取当前最小距离节点,但是我们其实可以利用小根堆(STL的priority_queue)优化这个过程,从而大大降低复杂(\(O(V2+E) ...
- jedis 整合 Spring
一, 单机版整合spring 1. 使用默认配置文件进行配置 <bean id="redisClient" class="redis.clients.jedis ...
- 怎么搭建一个5T的私有云盘
视频 点击打开视频 下载地址 免费域名网址: https://www.freewebhostingarea.com/ ftp工具: http://t.cn/EXWxYUI oneindex: http ...
- Mac无法将自定义图标添加到Launchpad的替代方案(桌面双击Shell运行)
截止在几天之前的Mac OS版本都无法实现将自定义图标添加到Launchpad.我使用的是10.12. 替代的思路就是在桌面新建一个Shell文件,然后使软件在后台运行,最后就是双击Shell文件能自 ...
- redux设计到源码 --- 美团点评技术团队(转)
https://tech.meituan.com/redux-design-code.html