问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3760 访问。

如果数组是单调递增或单调递减的,那么它是单调的。

如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的。 如果对于所有 i <= j,A[i]> = A[j],那么数组 A 是单调递减的。

当给定的数组 A 是单调数组时返回 true,否则返回 false。

输入:[1,2,2,3]

输出:true

输入:[6,5,4,4]

输出:true

输入:[1,3,2]

输出:false

输入:[1,2,4,5]

输出:true

输入:[1,1,1]

输出:true

提示:

1 <= A.length <= 50000

-100000 <= A[i] <= 100000


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.

Input: [1,2,2,3]

Output: true

Input: [6,5,4,4]

Output: true

Input: [1,3,2]

Output: false

Input: [1,2,4,5]

Output: true

Input: [1,1,1]

Output: true

Note:

1 <= A.length <= 50000

-100000 <= A[i] <= 100000


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3760 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = { 1, 2, 5 }; var res = IsMonotonic(nums);
Console.WriteLine(res); Console.ReadKey();
} private static bool IsMonotonic(int[] A) {
return isMonotonicIncrease(A) || isMonotonicReduce(A);
} private static bool isMonotonicIncrease(int[] A) {
for(var i = 0; i < A.Length - 1; i++) {
if(A[i + 1] < A[i]) {
return false;
}
}
return true;
} private static bool isMonotonicReduce(int[] A) {
for(var i = 0; i < A.Length - 1; i++) {
if(A[i + 1] > A[i]) {
return false;
}
}
return true;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3760 访问。

True

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#896-单调数列(Monotonic Array)的更多相关文章

  1. LeetCode 896. 单调数列(Monotonic Array)

    896. 单调数列 896. Monotonic Array 题目描述 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调 ...

  2. C#LeetCode刷题之#665-非递减数列( Non-decreasing Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3732 访问. 给定一个长度为 n 的整数数组,你的任务是判断在最 ...

  3. [Swift]LeetCode896. 单调数列 | Monotonic Array

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

  4. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  5. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  6. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配   17.8% 困难 45 跳跃游戏 II   25.5% 困难 55 跳跃游戏   30.6% 中等 122 买卖股票的最佳时机 II C ...

  7. LeetCode刷题模板(1):《我要打10个》之二分法

    Author       :  叨陪鲤 Email         : vip_13031075266@163.com Date          : 2021.01.23 Copyright : 未 ...

  8. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  9. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

随机推荐

  1. Spring Boot 2.x基础教程:EhCache缓存的使用

    上一篇我们学会了如何使用Spring Boot使用进程内缓存在加速数据访问.可能大家会问,那我们在Spring Boot中到底使用了什么缓存呢? 在Spring Boot中通过@EnableCachi ...

  2. 第二章: IPC机制

    这章关于进程的概念,没有深入太多,只做了解跟学习 IPC: Inter-Process Communication,进程间通信或者跨进程通信,两个进程之间进行数据交换的过程 2.1介绍 线程:CPU调 ...

  3. 使用SQL语句进行特定值排序

    使用SQL语句进行查询时,对数据进行排序,排序要求为排序的一个字段中特定值为顶部呈现: select * from TableName order by case TableFieldName whe ...

  4. Fastjson到了说再见的时候了

    生命太短暂,不要去做一些根本没有人想要的东西.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习 ...

  5. C++语法小记---标准库

    C++标准库 C++标准库包含如下内容: C++标准编译工具链 C++扩展编译工具链(各种C++编译器独有) C++标准库 C++库 C库 C兼容库(为了兼容能够用C编译器编译的项目,直接使用C++也 ...

  6. 在VS Code中编写IAR项目

    在VS Code中编写IAR项目 首先按照网上的教程,下载C/C++插件,以及IAR Eebedded Workbench插件,安装完成重启VS Code. 项目目录下新建.vscode文件夹,并新建 ...

  7. element-ui的el-progress组件增加修改status状态

    需求:实现进度条增长中呈现百分比,达到100%后将el-progress的status设置为“success” 想法:element对于status只给出了'success', 'exception' ...

  8. springboot(4)Druid作为项目数据源(添加监控)

    参考博客:恒宇少年:https://www.jianshu.com/p/e84e2709f383 Druid简介 Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JD ...

  9. webserver 返回json 如何去掉 <string xmlns="http://tempuri.org/">

    [WebMethod]  public void GetJson(String sJson)  {    Context.Response.Charset = "UTF-8"; / ...

  10. ken桑带你读源码 之scrapy scrapy\extensions

    logstats.py 爬虫启动时 打印抓取网页数   item数 memdebug.py 爬虫结束 统计还被引用的内存 也就是说gc 回收不了的内存   memusage.py 监控爬虫 内存占用  ...