Code Signal_练习题_almostIncreasingSequence
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 bealmostIncreasingSequence(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 bealmostIncreasingSequence(sequence) = true
.You can remove
3
from the array to get the strictly increasing sequence[1, 2]
. Alternately, you can remove2
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的更多相关文章
- 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) + ...
随机推荐
- RabbitMQ Java实例
引入RabbitMQ的jar包 <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amq ...
- CAN总线协议 学习笔记
1.CAN总线网络 CAN总线网络主要挂在CAN_H和CAN_L,各个节点通过这两条线实现信号的串行差分传输,为了避免信号的反射和干扰,还需要在CAN_H和CAN_L之间接上120欧姆的终端电阻,但是 ...
- django -orm操作总结
前言 Django框架功能齐全自带数据库操作功能,本文主要介绍Django的ORM框架 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MyS ...
- 多项式求逆元详解+模板 【洛谷P4238】多项式求逆
概述 多项式求逆元是一个非常重要的知识点,许多多项式操作都需要用到该算法,包括多项式取模,除法,开跟,求ln,求exp,快速幂.用快速傅里叶变换和倍增法可以在$O(n log n)$的时间复杂度下求出 ...
- selenium+java iframe定位
关于 driver.switchTo().frame(参数).这中间的参数表达有以下几种方式. driver.switchTo().frame(0):用<iframe>标签的位置数量来 ...
- iOS 获取设备的各种信息的方法
一.目录结构: 获取屏幕宽度与高度 获取设备版本号 获取iPhone名称 获取app版本号 获取电池电量 获取当前系统名称 获取当前系统版本号 获取通用的唯一识别码UUID 获取当前设备IP 获取总内 ...
- IIS web证书申请与安装
数字证书一般是由权威机构颁发的,操作系统会携带权威机构的根证书和中级证书.如果操作系统没有携带权威机构签发的根证书,浏览器会报警,如www.12306.cn,需要安装铁道部根证书后,才能正常访问. 证 ...
- vue、react、angular三大框架对比 && 与jQuery的对比
前端当前最火的三大框架当属vue.react以及angular了. 但是在做项目的时候,我们怎么去选择呢? 这里做一个比较,希望大家可以有一个比较清晰的认识. vue与react vue和react ...
- 四、CLR执行程序集中代码和IL代码简介
三.加载公共语言运行时中介绍了在安装了.Net Framework中加载公共语言运行时,公共语言运行时加载程序集的过程.以及通过vs stdio设置源码编译的目标平台的过程. 本问主要介绍公共语言加载 ...
- 极高效内存池实现 (cpu-cache)
视频请看 : http://edu.csdn.net/course/detail/627 1.内存池的目的 提高程序的效率 减少运行时间 避免内存碎片 2.原理 要解决上述两个问题,最好的方法就是 ...