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 be
    almostIncreasingSequence(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 be
    almostIncreasingSequence(sequence) = true.

    You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 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 return false.

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的更多相关文章

  1. CodeSignal 刷题 —— matrixElementSum

    After they became famous, the CodeBots all decided to move to a new building and live together. The ...

  2. LeetCode刷题系列

    LeetCode 我们工作面试和提高自身数据结构和算法能力的时候往往需要刷刷题,我选择LeetCode是通过一个留学论坛了解的.专业,覆盖语种全面. 提前说说刷题的心得: 尽量手写代码,少使用IDE的 ...

  3. ife任务刷题总结(一)-css reset与清除浮动

    本文同时发布于本人的个人网站www.yaoxiaowen.com 百度创办的前端技术学院,是一个面向大学生的前端技术学习平台.虽然只有大学生才有资格报名,提交代码进行比赛排名.但是这并不妨碍我们这些初 ...

  4. 刷题ING...

    我用codeVS刷题.. 努力准备!!

  5. XidianOJ 1020 ACMer去刷题吧

    题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...

  6. 【BZOJ-4590】自动刷题机 二分 + 判定

    4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 156  Solved: 63[Submit][Status ...

  7. NOI题库分治算法刷题记录

    今天晚自习机房刷题,有一道题最终WA掉两组,极其不爽,晚上回家补完作业欣然搞定它,特意来写篇博文来记录下 (最想吐槽的是这个叫做分治的分类,里面的题目真的需要分治吗...) 先来说下分治法 分治法的设 ...

  8. NOI题库刷题日志 (贪心篇题解)

    这段时间在NOI题库上刷了刷题,来写点心得和题解 一.寻找平面上的极大点 2704:寻找平面上的极大点 总时间限制:  1000ms  内存限制:  65536kB 描述 在一个平面上,如果有两个点( ...

  9. 用js刷题的一些坑

    leecode可以用js刷题了,我大js越来越被认可了是吧.但是刷题中会因为忽略js的一些特性掉入坑里.我这里总结一下我掉过的坑. 坑1:js中数组对象是引用对象 js中除了object还有数组对象也 ...

随机推荐

  1. python之hashlib

    简介: 用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法.在python3中已经废弃了md5和sha模块,简单说明 ...

  2. Confluence 6 手动备份

    Confluence 的 Attachment Storage Configuration 可以配置 Confluence 将附件存储在  home directory,或者是存储在数据库中. Dat ...

  3. Confluence 6 自定义默认空间内容

    中文标题[自定义默认空间内容] Confluence 管理员 可以编辑用于创建主页和新站点的模板.默认的内容将会在新空间创建后的主页上显示出来.这个与站点空间,个人空间和空间蓝图的模板是不同的. 模板 ...

  4. 【python】统一转换日期格式dateutil.parser.parse

    背景: 我有很多很多的日志数据,每个日志里面都有日期字符串,我需要将其转换为datetime格式. 问题是,这些日志里的字符串格式五花八门,有2017-05-25T05:27:30.313292255 ...

  5. linux基础实操四

    实操一: 1)为新加的硬盘分区,一个主分区大小为10剩余空间给扩展分区,在扩展分区上划分2个逻辑分别为5G 2)式化主分区为ext3系统 #mkfs.ext3 /dev/sdb1 3 将逻辑分区设置为 ...

  6. Niagara 泵阀

    1.泵阀系统 2.Niagara 每次在启动workbench的时候安装一下daemon,后启动platform 启动station的时候,当station的status变成Running之后启动

  7. Win#password;;processon #clone;;disassemble;;source find

    1.密码学思维导图 源地址:https://www.processon.com/view/5a61d825e4b0c090524f5b8b 在这之前给大家分享 如何在 processon上搜索公开克隆 ...

  8. MVC开发中的常见错误-01未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。

    错误信息:未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或 ...

  9. Django实现注册页面_头像上传

    Django实现注册页面_头像上传 Django实现注册页面_头像上传 1.urls.py 配置路由 from django.conf.urls import url from django.cont ...

  10. PHP中self和this的用法区别

    PHP支持类和面向对象结构,PHP的类的静态函数和变量不与任何特定类的实例相关联(换句话说,一个对象).请看:类与对象的区别. 相反,静态函数和变量与类定义本身相关联.换言之,一个类的所有实例都共享相 ...