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 be
arrayChange(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的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  3. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  4. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  5. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  6. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  7. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. 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. RunningCassandraInEclipse(转载)

    转载自:http://wiki.apache.org/cassandra/RunningCassandraInEclipse Eclipse is open source. Download Ecli ...

  2. Mysql备份之Innobakcupex&Xtrabackup

                       一.innobackupex备份工具 基本选项 --compress:该选项表示压缩innodb数据文件的备份. --compress-threads:该选项表示 ...

  3. cool kickass

    I can stay like this alllllllllll daaaaaaaaayyyyyy.

  4. Python对象引用和del删除引用

    1.首先介绍下python的对象引用 1)Python中不存在传值调用,一切传递的都是对象引用,也可以认为是传址调用.即Python不允许程序员选择采用传值或传引用.Python参数传递采用的是“传对 ...

  5. php-redis 模块 文档

    直接从这位朋友转载过来. 地址 Redis::__construct构造函数$redis = new Redis(); connect, open 链接redis服务参数host: string,服务 ...

  6. 21.Decorator修饰器

    1.类的修饰 2.方法的修饰 3.为什么修饰器不能用于函数? 4.core-decorators.js 5.使用修饰器实现自动发布事件 6.Mixin 7.Trait 8.Babel转码器的支持

  7. Java Servlet 缺点

    1.web.xml配置比较多 2.servlet具有容器依赖性(tomcat没有启动,就没有用)

  8. C 扩展库 - sqlite3 API CRUD

    CRUD struct student typedef struct STUDENT { unsigned int id; unsigned char name[16]; unsigned int a ...

  9. springboot-22-自定义starter

    先说下springboot的运行原理 springboot最主要的配置 是 @SpringBootApplication 然后这里面 @EnableAutoCOnfiguration 最为重要, 继续 ...

  10. 使用C++11 开发一个半同步半异步线程池

    摘自:<深入应用C++11>第九章 实际中,主要有两种方法处理大量的并发任务,一种是一个请求由系统产生一个相应的处理请求的线程(一对一) 另外一种是系统预先生成一些用于处理请求的进程,当请 ...