Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Example

    • For sequence = [1, 3, 2, 1], the output should be
      almostIncreasingSequence(sequence) = false.

      There is no one element in this array that can be removed in order to get a strictly increasing sequence.

    • For sequence = [1, 3, 2], the output should be
      almostIncreasingSequence(sequence) = true.

      You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

我的解答: 

 想半天想不出来,通过查阅各个大佬的做法,发现用这种方法的挺多
def almostIncreasingSequence(sequence):
count = 0
for i in range(len(sequence)-1):
if i+1 < len(sequence) and sequence[i] >= sequence[i+1]:
count += 1
if i+2 < len(sequence) and sequence[i] >= sequence[i+2]:
count += 1
return count < 3

膜拜大佬:

上个代码还能看懂,这个来自捷克共和国的一位大佬的代码真懵了..啥题都能一行代码解决...
def almostIncreasingSequence(s):
return 3> sum((i >= j) + (i >= k) for i, j, k in zip(s, s[1:], s[2:] + [10**6]))

Code Signal_练习题_almostIncreasingSequence的更多相关文章

  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. 原来css也可以计算-calc()使用

    在浏览其他人的源代码时,看到了一个陌生的属性:width:calc(100% - 10px -10px); 出于好奇心,百度了一下,看到了以下这篇文章,http://www.w3cplus.com/c ...

  2. 怎么在Win7系统清除DNS缓存和刷新DHCP列表

    如何清除DNS缓存?开始-运行,如下图所示: 2 在谈出的对话框中输入“cmd”,如下图所示: 3 在出现的DOS命令窗口输入“ipconfig /flushdns”,然后就清除DNS缓存了,在我们遇 ...

  3. ReentrantLock总体概括

    一.锁的实现原理: JAVA concurrent包下面的锁是通过AbstractQueuedSynchronizer内的Node类下面的state属性来实现的,并且锁的可重入属性也是通过state实 ...

  4. 网络请求 爬虫学习笔记 一 requsets 模块的使用 get请求和post请求初识别,代理,session 和ssl证书

    前情提要: 为了养家糊口,为了爱与正义,为了世界和平, 从新学习一个爬虫技术,做一个爬虫学习博客记录 学习内容来自各大网站,网课,博客. 如果觉得食用不良,你来打我啊 requsets 个人觉得系统自 ...

  5. 【UOJ#435】【集训队作业2018】Simple Tree 分块+树链剖分

    题目大意: 有一棵有根树,根为 1 ,点有点权.现在有 m 次操作,操作有 3 种:1 x y w ,将 x 到 y 的路径上的点点权加上 w (其中 w=±1w=±1 ):2 x y ,询问在 x ...

  6. QQ聊天框测试用例设计

    QQ.微信聊天框的主要功能就是发送消息和接收别人发过来的消息. 消息内容类型: 纯文字 纯图片 纯表情 文字+表情 文件 发送键: 点击“发送”发送 使用快捷键发送(针对电脑端) 用户在线状态: 在线 ...

  7. 【学习笔记】linux bash script

    1. sed sed 是一种流编辑器,它是文本处理中非常常用的工具,能够完美的配合正则表达式使用,功能非常强大. mkdir playground touch test.txt echo " ...

  8. Mac下的paths.d目录神奇用法

    首先,这个方法是通过PG的做法学到的,且这个方法只能在Mac下用,在Linux下还真没有这个方法. 这个paths.d的作用很简单,就是在里面创建一个文件,然后写上需要在全局命令行下用到的命令,直接配 ...

  9. shell脚本实现无密码交互的SSH自动登陆

    ssh连接远程主机时候询问密码,跟su.sudo命令的默认行为一样,是不从stdin读入数据的,据称是为安全考虑,但是有时候在脚本当中确实需要无人守值的登陆. 搜索一下不难找到类似的例子,使用expe ...

  10. cygwin 安装.

    在线安装, http://www.cygwin.com/  64位的,下载安装. 先装的低配的,只有几个组件装了,不然全部装太大,下次需要再装... binutils gcc gdb windows ...