[codesignal] almostIncreasingSequence

Descriptions

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.

Note: sequence a0, a1, …, an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

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.

Solution

#include <iostream>
#include <vector>
#include <algorithm>

bool almostIncreasingSequence(std::vector<int> sequence) {
    // 1. 检查哪个位置,数组变得无序,即 it 之前(不包括)的元素有序
    auto it = std::is_sorted_until(sequence.begin(), sequence.end(), [](int x, int y) { return x <= y; });
    // 2. 检查 it 后面(包括)的元素是否有序,如果无序,直接返回 false
    if (!std::is_sorted(it, sequence.end(), [](int x, int y) { return x <= y; })) {
        return false;
    };
    // 3. 如果无序的位置在 0, 1 和 end - 1 处,则返回 true,位置为 0 表示只有一个元素
    if (!(sequence.begin() + 2 <= it && it <= sequence.end() - 2)) {
        return true;
    }
    // 4. 其余情况  (it - 2), (it - 1), (it), (it + 1)
    // 因为在 it 处不满足条件,因此需要删除 (it) 或者 (it - 1)
    // 若删除 (it),则因满足 (it - 1) < (it + 1)
    // 若删除 (it - 1),则因满足 (it - 2) < (it)
    if (*(it - 1) < *(it + 1) || *(it - 2) < *it) {
        return true;
    }

    return false;
}


int main() {
    std::vector<int> num {3, 5, 67, 98, 3};
    std::cout << std::boolalpha << almostIncreasingSequence(num) << std::endl;

    return 0;
}
引用\[1\]: def matrixElementsSum(matrix): sum = 0 for i in range(len(matrix\[0\])): # 先处理第一行 if matrix\[0\]\[i\] != 0: sum += matrix\[0\]\[i\] for i in range(1, len(matrix)): for j in range(len(matrix\[0\])): if matrix\[i-1\]\[j\] == 0: # 同一列中,如果上一行的元素为0,则把此处的元素也置为0 matrix\[i\]\[j\] =0 sum += matrix\[i\]\[j\] return sum 引用\[2\]: def variableName(name): dict = {'word':'qwertyuiopasdfghjklzxcvbnm', 'digit':'0123456789', 'underline':'_'} if name\[0\].lower() in dict\['word'\] or name\[0\] in dict\['underline'\]: for i in name: if i.lower() in dict\['word'\] or i in dict\['underline'\] or i in dict\['digit'\]: pass else: return False return True else: return False 引用\[3\]: 当他们成名后,所有的代码机器人都决定搬到新的建筑里住在一起。建筑是用房间的矩形矩阵表示的。矩阵中的每个单元格都包含一个表示房间价格的整数。有些房间是免费的(费用为0),但这可能是因为它们是闹鬼的,所以所有的机器人都害怕它们。这就是为什么任何空闲的房间或者位于同一列的空闲房间下面的任何地方都不适合机器人居住的原因。 问题: codesignal答案 回答: 由于没有提供codesignal的具体问题,我无法给出具体的答案。但是根据引用\[1\]和引用\[2\]的代码,可以看出这是两个函数的实现。第一个函数是用来计算一个矩阵中所有非零元素的和,而第二个函数是用来判断一个变量名是否符合命名规范。至于引用\[3\]中的内容,它描述了一个故事情景,但与codesignal的答案无关。如果你能提供更具体的问题,我将能够给出更准确的答案。 #### 引用[.reference_title] - *1* *3* [CodeSignal 刷题 —— matrixElementSum](https://blog.csdn.net/weixin_34320159/article/details/93242308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Code Signal_练习题_variableName](https://blog.csdn.net/weixin_33835103/article/details/94533161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值