LintCode 1745: Monotonic Array

本文介绍了一种用于判断数组是否为单调递增或单调递减的算法。通过一次遍历,该算法可以确定数组A是否满足单调性条件,即对于所有i<=j,A[i]<=A[j]或A[i]>=A[j]。文章提供了具体的实现代码,并附带了示例输入输出。
  1. Monotonic Array

An array is monotonic if it is either monotone increasing or monotone decreasing.

An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j].

Return true if and only if the given array A is monotonic.

Example
Example 1:

Input: [1,2,2,3]
Output: true
Example 2:

Input: [1,3,2]
Output: false
Notice
1 \leq A.length \leq 500001≤A.length≤50000
-100000 \leq A[i] \leq 100000−100000≤A[i]≤100000

解法1:

class Solution {
public:
    /**
     * @param A: a array
     * @return: is it monotonous
     */
    bool isMonotonic(vector<int> &A) {
        int n = A.size();
        if (n <= 1) return true;
        bool increasing, decreasing;
        
        if (A[1] > A[0]) {
            increasing = true;
            decreasing = false;
        } else if (A[1] < A[0]) {
            increasing = false;
            decreasing = true;
        } else {
            increasing = true;
            decreasing = true;
        }
 
        for (int i = 2; i < n; ++i) {
            if (increasing && A[i] >= A[i - 1]) continue;
            if (decreasing && A[i] <= A[i - 1]) continue;
            return false;
        }
        return true;
    }
};
home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Stereo-Inertial/stereo_inertial_tum_vi.cc: In function ‘int main(int, char**)’: /home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Stereo-Inertial/stereo_inertial_tum_vi.cc:207:26: error: ‘std::chrono::monotonic_clock’ has not been declared 207 | std::chrono::monotonic_clock::time_point t1 = std::chrono::monotonic_clock::now(); | ^~~~~~~~~~~~~~~ /home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Stereo-Inertial/stereo_inertial_tum_vi.cc:216:26: error: ‘std::chrono::monotonic_clock’ has not been declared 216 | std::chrono::monotonic_clock::time_point t2 = std::chrono::monotonic_clock::now(); | ^~~~~~~~~~~~~~~ /home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Stereo-Inertial/stereo_inertial_tum_vi.cc:224:87: error: ‘t2’ was not declared in this scope; did you mean ‘tm’? 224 | double ttrack= std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count(); | ^~ | tm /home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Stereo-Inertial/stereo_inertial_tum_vi.cc:224:92: error: ‘t1’ was not declared in this scope; did you mean ‘y1’? 224 | double ttrack= std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count(); | ^~ | y1 /home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Stereo-Inertial/stereo_inertial_tum_vi.cc:125:12: warning: unused variable ‘t_resize’ [-Wunused-variable] 125 | double t_resize = 0.f; | ^~~~~~~~ /home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Stereo-Inertial/stereo_inertial_tum_vi.cc:126:12: warning: unused variable ‘t_track’ [-Wunused-variable] 126 | double t_track = 0.f; | ^~~~~~~ /home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Monocular-Inertial/mono_inertial_tum_vi.cc: In function ‘int main(int, char**)’: /home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Monocular-Inertial/mono_inertial_tum_vi.cc:201:26: error: ‘std::chrono::monotonic_clock’ has not been declared 201 | std::chrono::monotonic_clock::time_point t1 = std::chrono::monotonic_clock::now(); | ^~~~~~~~~~~~~~~ /home/huahua/vslam_ws/src/ORB_SLAM3/Examples_old/Monocular-Inertial/mono_inertial_tum_vi.cc:211:26: error: ‘std::chrono::monotonic_clock’ has not been declared 211 | std::chrono::monotonic_clock::time_point t2 = std::chrono::monotonic_clock::now();
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值