CodeSignal 刷题 —— 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]
.
Input/Output
[execution time limit] 0.5 seconds (cpp)
[input] array.integer sequence
Guaranteed constraints:
2 ≤ sequence.length ≤ 105
,-105 ≤ sequence[i] ≤ 105
.[output] boolean
- Return
true
if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise returnfalse
.
- Return
Python3解法:
import copy def isSequence(lst):
for i in range(len(lst)):
if lst.count(lst[i]) > 1: #如果列表中有重复的数字,返回False
return False
sort_lst = copy.copy(lst)
sort_lst.sort()
if lst == sort_lst:
return True
else:
return False def almostIncreasingSequence(sequence):
if isSequence(sequence):
return True
i = 0
while i < len(sequence):
sequence_copy = copy.copy(sequence)
del sequence_copy[i] # 循环,一个一个删除,查看剩余序列是否严格递增有序
if isSequence(sequence_copy):
return True
i += 1 return False print(almostIncreasingSequence([1,2,1,2]))
C++解法:
bool isSequence(vector<int> sequence)
{
set<int>iset;
for(auto e: sequence)
iset.insert(e);
if(iset.size() != sequence.size()) //如果sequence中有重复元素,返回false
return false; vector<int>v = sequence; //如果sequence与排序后的v相等,则说明sequence是递增有序的
sort(v.begin(), v.end());
if(sequence == v)
return true;
else
return false; } bool almostIncreasingSequence(std::vector<int> sequence)
{
if (isSequence(sequence))
return true;
int i = ;
while (i < sequence.size())
{
vector<int> v = sequence;
v.erase(v.begin() + i);
if(isSequence(v))
return true;
++i;
}
return false;
}
CodeSignal 刷题 —— almostIncreasingSequence的更多相关文章
- CodeSignal 刷题 —— matrixElementSum
After they became famous, the CodeBots all decided to move to a new building and live together. The ...
- LeetCode刷题系列
LeetCode 我们工作面试和提高自身数据结构和算法能力的时候往往需要刷刷题,我选择LeetCode是通过一个留学论坛了解的.专业,覆盖语种全面. 提前说说刷题的心得: 尽量手写代码,少使用IDE的 ...
- ife任务刷题总结(一)-css reset与清除浮动
本文同时发布于本人的个人网站www.yaoxiaowen.com 百度创办的前端技术学院,是一个面向大学生的前端技术学习平台.虽然只有大学生才有资格报名,提交代码进行比赛排名.但是这并不妨碍我们这些初 ...
- 刷题ING...
我用codeVS刷题.. 努力准备!!
- XidianOJ 1020 ACMer去刷题吧
题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...
- 【BZOJ-4590】自动刷题机 二分 + 判定
4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 156 Solved: 63[Submit][Status ...
- NOI题库分治算法刷题记录
今天晚自习机房刷题,有一道题最终WA掉两组,极其不爽,晚上回家补完作业欣然搞定它,特意来写篇博文来记录下 (最想吐槽的是这个叫做分治的分类,里面的题目真的需要分治吗...) 先来说下分治法 分治法的设 ...
- NOI题库刷题日志 (贪心篇题解)
这段时间在NOI题库上刷了刷题,来写点心得和题解 一.寻找平面上的极大点 2704:寻找平面上的极大点 总时间限制: 1000ms 内存限制: 65536kB 描述 在一个平面上,如果有两个点( ...
- 用js刷题的一些坑
leecode可以用js刷题了,我大js越来越被认可了是吧.但是刷题中会因为忽略js的一些特性掉入坑里.我这里总结一下我掉过的坑. 坑1:js中数组对象是引用对象 js中除了object还有数组对象也 ...
随机推荐
- 玩转EhCache之最简单的缓存框架
二.主要特性 快速: 简单: 多种缓存策略: 缓存数据有两级:内存和磁盘,因此无需担心容量问题: 缓存数据会在虚拟机重启的过程中写入磁盘: 可以通过 RMI.可插入 API 等方式进行分布式缓存: 具 ...
- python基础--压缩文件
1)怎么压缩备份多个文件 使用zipfile 创建压缩文件 查看信息 解压缩 # 创建 import zipfile # os.chdir('test') my_zip = zipfile.ZipFi ...
- CDH运维
1.单个节点宕机后,想可能存在的问题: 时间同步是否正常运行 hbase对时间是否同步很敏感 2.zookeeper报警 ZooKeeper 服务 canary 因未知原因失败. 该警报是在重启CM的 ...
- Confluence 6 安全概述和建议概述
这个文档是针对 Confluence 的系统管理员希望对 Confluence Web应用程序安全性进行评估而设计的.这个页面将对系统的安全进行大致的描述,同时也会对 Confluence 的安全配置 ...
- Confluence 6 关于统一插件管理器
所有的组件通过 统一插件管理器(Universal Plugin Manager)进行管理,这个也被称为 UPM.UPM 可以在几乎所有的 Atlassian 应用中找到,能够提供完整同意的插件安装管 ...
- axis 数据流
产生数据流的代码 模板 重新修改了下 :]axis_data_cnt='d0; :]axis_data_frame_cnt='d0; :]delay_cnt='d0; initial begin ...
- day02 编程语言和变量
编程语言 编程语言分为三种 机器语言:用计算机能听得懂的二进位制语言来写程序 汇编语言:用英文字母来代替二进位制来写的程序 高级语言(两种) 编译型语言(C语言):相当于谷歌翻译整个程序写好一次性写好 ...
- string标准C++中的的用法总结(转)
转自:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻 ...
- 监控CPU使用率并发送报警邮件
#!/bin/bash DATE=$(date +%F" "%H:%M) #只支持centos6 IP=$(ifconfig eth0 | awk -F '[ :]+' '/ine ...
- mac 显示/不显示"任何来源"_ mac打开安装文件显示文件破损解决办法
系统: macOS_10.12 导致文件破损原因: 软件有经过了汉化或者破解,所以可能被Mac认为「已损坏」 解决问题办法: 系统偏好设置 -> 安全性与隐私 -> 通用 -> 选择 ...