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
3from the array to get the strictly increasing sequence[1, 2]. Alternately, you can remove2to 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
trueif 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还有数组对象也 ...
随机推荐
- idea Unable to open debugger port (127.0.0.1:58006) Address already in use: JVM_Bind 的解决办法
报错说端口58006 被占用了,于是去修改端口 重新dubug 发现换个端口号还是不行,同样的错误.有时候你把idea关闭重新打开依旧不起作用.最暴力的办法就是重启电脑... 问题解决: 查看使用中的 ...
- OCP 相关课程列表
OCP 相关课程列表 第一天:Linux基础 和 Oracle 11 R2 数据库安装教程图解 1:< VM 安装 linux Enterprise_R5_U4_Server_I386_DVD教 ...
- Confluence 6 代理和 HTTPS 设置连接器
很多用户选择将 Confluence 运行在反向代理的后面,同时还启用了 HTTPS.将你的的 Confluence 反向代理配置正确就显得非常必要了,并且能够避免后期在使用 Confluence 遇 ...
- django模板导入外部js和css等文件
1.新建文件夹templates(存放模板文件),新建文件夹media(存放js.css.images文件夹),并把两个文件夹放到了项目的根目录下 2.设定模板路径 设置模板路径比较简单,只要在set ...
- linux学习笔记:第三单元 Linux命令及获取帮助
第三单元 Linux命令及获取帮助 11) 了解Linux命令的语法格式:命令 [选项] [参数]2) 掌握命令格式中命令.选项.参数的具体含义a) 命令:告诉Linux(UNIX)操作系统做(执行) ...
- poj2441状态压缩dp基础
/* 给定n头牛,m个谷仓,每头牛只能在一些特定的谷仓,一个谷仓只能有一头牛 问可行的安排方式 dp[i][j]表示前i头牛组成状态j的方案数,状态0表示无牛,1表示有牛 使用滚动数组即可 枚举到第i ...
- poj3107树的重心
/*树的重心求法:两次dfs,第一次dfs处理出每个结点的size,以此求每个结点大儿子的size,第二次dfs将每个结点大儿子的size和余下结点数进行比较,所有结点里两个值之间差值最小的那个点就是 ...
- WebStorm中常用的快捷键及使用技巧
------------------------------------- 近期整理了如下个人觉得比较常用的快捷键,也请前辈给予补充.多多指教. --------------------------- ...
- AI-视图组件-五个接口的最终简化版
五个接口最终版 #url.py # 序列化最贱版本 url(r'^customer/$', views.CustomerView.as_view({"get":"list ...
- 目标检测算法之YOLOv1与v2
YOLO:You Only Look Once(只需看一眼) 基于深度学习方法的一个特点就是实现端到端的检测,相对于其他目标检测与识别方法(如Fast R-CNN)将目标识别任务分成目标区域预测和类别 ...