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还有数组对象也 ...
随机推荐
- 使用第三方组件(django-redis)创建连接池
settings里面: ##redis配置CACHES={ 'default':{ 'BACKEND':'django_redis.cache.RedisCache', 'LOCATION':'red ...
- Poco::Crypto--加解密(AES)
Poco::Crypto--加解密(AES) 1 int main(const std::vector<std::string>& args) 2 { 3 /*TO DO*/ 4 ...
- ios中input获取焦点时的问题
1.获取焦点时,input会变大 解决办法是:font-size设置为32px以上 还有就是要在header里面加这一行代码:<meta name="viewport" co ...
- PID控制器开发笔记之十一:专家PID控制器的实现
前面我们讨论了经典的数字PID控制算法及其常见的改进与补偿算法,基本已经覆盖了无模型和简单模型PID控制经典算法的大部.再接下来的我们将讨论智能PID控制,智能PID控制不同于常规意义下的智能控制,是 ...
- RianbowDesign
换了很多个名字,最后换成这个. 主要考虑把自己所学的.所经历的都结合到一起,用这种方式来保持热情. 算是一个设计网站,管理咨询,游戏设计,平面logo之类,想最多地攘括到里边.就像自己不确定的主业一样 ...
- Confluence 6 从关闭的连接中恢复
当数据库服务器进行重启或者因为网络问题导致连接中断.所有在数据库连接池中的连接都会被中断.希望处理这个问题,通常需要 Confluence 进行重启. 但是,数据库连接池中的连接可以通过运行 SQL ...
- ionic3 点击输入 框弹出白色遮罩 并把 界面顶到上面
这个蛋疼 问题 ,在android 上面遇到这种情况 ,键盘弹出的时候 总有一个白色的遮罩在后面出现,网上查了很久都没发现原因. 偶然发现一个方法 ,试下了下 问题解决了. 代码如下: IonicMo ...
- LeetCode(79): 单词搜索
Medium! 题目描述: 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元 ...
- java 接口实现的概念整理
1.在java语言中接口由类实现,以便使用接口中的方法,重写接口中的方法,实现接口的类必须重写接口中的所有类,由于接口中的方法一定是 public abstract方法,所以类重写接口中的方法不仅要去 ...
- lightoj 1282 取对数的操作
/* 前三位 len=log10n^k(乘积的长度) len=klog10n n^k=x*10^(len-1) x=n^k/10^(len-1) log10x = k*log10n - (len-1) ...