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还有数组对象也 ...
随机推荐
- HttpServletResponse设置下载文件
// path是指欲下载的文件的路径. File file = new File(path); // 取得文件名. String fi ...
- hashtable——散列表
2018-11-01 散列表---哈希表基于快速存取,时间换空间一种基于线性数组的线性表,不过元素之间并非紧密排列 散列函数--通过函数,有key关键码计算地址(相当于数组下标),函数尽可能使元素均匀 ...
- matplotlib报错_tkinter.TclError: no display name and no $DISPLAY environment variable
import matplotlib import matplotlib.pyplot as plt fig=plt.figure() #交互式测试,此时报错 解决办法,在引用后添加下面这一行 matp ...
- poj1562 Oil Deposits 深搜模板题
题目描述: Description The GeoSurvComp geologic survey company is responsible for detecting underground o ...
- centos7_ linux : Nginx安装手册
一: nginx安装环境 1: oracle vm虚拟机+Centos7系统的yum环境的安装 配置本地yum库(用root用户操作) 创建挂载目录 mkdir /mnt/cdrom 查看挂载目录 l ...
- Confluence 6 查看内容索引概要
内容索引,通常也被称为查找索引,这个索引被用来在 Confluence 中支持查找.这个索引同时也被其他的一些功能使用,例如在归档邮件中构建邮件主题,View Space Activity 的特性和将 ...
- Confluence 6 重新获得站点备份文件
Confluence 将会创建备份,同时压缩 XML 文件后存储熬你的 <home-directory>/backups> 目录中.你需要自己访问你安装的 Confluence 服务 ...
- python基础之迭代器与生成器
一.什么是迭代器: 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器是一个可以记住遍历的位置的对象. 迭代器的 ...
- 《剑指offer》用两个栈实现队列
本题来自<剑指offer> 用两个栈实现队列 题目: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路: 队列定义:先进先出 栈定义:先进后出 要 ...
- linux 基础知识(三)
抽空把Linux的一些基础的东西再补充一下,安全的东西真的很多都是要自己不断的学习,很多还是今天学习了一点时间过后不用就会忘记.所以学习的东西就是要不断地往复. 有时候感觉有时候快就是慢,慢就是快. ...