Monotonic Array LT896
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 1:
Input: [1,2,2,3]
Output: true
Example 2:
Input: [6,5,4,4]
Output: true
Example 3:
Input: [1,3,2]
Output: false
Example 4:
Input: [1,2,4,5]
Output: true
Example 5:
Input: [1,1,1]
Output: true
Note:
1 <= A.length <= 50000
-100000 <= A[i] <= 100000
Idea 1. Check if it is monotonic increasing or decreasing, 需要2个pass, 自己曾想把这2个合二为一个pass, 才意识到有==, 序列开始可以是递增或递减
Time complexity: O(n), 2 scan
Space complexity: O(1)
class Solution {
private boolean isMonotonicIncreasing(int[] A) {
int i = 1;
while(i < A.length && A[i-1] <= A[i]) {
++i;
}
return i == A.length;
} private boolean isMonotonicDecreasing(int[] A) {
int i = 1;
while(i < A.length && A[i-1] >= A[i]) {
++i;
}
return i == A.length;
} public boolean isMonotonic(int[] A) {
return isMonotonicIncreasing(A) || isMonotonicDecreasing(A);
}
}
反着想,有递增pair就不是递减
class Solution {
private boolean isMonotonicIncreasing(int[] A) {
for(int i = 1; i < A.length; ++i) {
if(A[i-1] > A[i]) {
return false;
}
}
return true;
} private boolean isMonotonicDecreasing(int[] A) {
for(int i = 1; i < A.length; ++i) {
if(A[i-1] < A[i]) {
return false;
}
}
return true;
} public boolean isMonotonic(int[] A) {
return isMonotonicIncreasing(A) || isMonotonicDecreasing(A);
}
}
Idea 1.b. Similar to Longest Turbulent Subarray LT978, store the sign and return false if current sign is opposite to previous sign.
Time complexity: O(n)
Space complexity: O(1)
class Solution { public boolean isMonotonic(int[] A) {
int flag = 0;
for(int i = 1; i < A.length; ++i) {
int c = Integer.compare(A[i-1], A[i]);
if(c == 0) {
continue;
} if(flag != 0 && c != flag) {
return false;
}
flag = c;
} return true;
}
}
Idea 1.c 如果同时有decreasing and increasing pair, 肯定不是monotonic
class Solution {
public boolean isMonotonic(int[] A) {
boolean increasing = false;
boolean decreasing = false;
for(int i = 1; i < A.length; ++i) {
if(A[i-1] > A[i]) {
increasing = true;
}
else if(A[i-1] < A[i]) {
decreasing = true;
}
} if(increasing && decreasing) {
return false;
} return true;
}
}
官方的妙法,反着来, 注意上面的解法直接return increasing^decreasing不行,考虑数组全是==, 官方的这个考虑到了,就是想法有些绕
class Solution {
public boolean isMonotonic(int[] A) {
boolean increasing = true;
boolean decreasing = true;
for(int i = 1; i < A.length; ++i) {
if(A[i-1] < A[i]) {
increasing = false;
}
else if(A[i-1] > A[i]) {
decreasing = false;
}
} return increasing || decreasing;
}
}
Monotonic Array LT896的更多相关文章
- 896. Monotonic Array@python
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- 【Leetcode_easy】896. Monotonic Array
problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...
- LeetCode 896. 单调数列(Monotonic Array)
896. 单调数列 896. Monotonic Array 题目描述 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调 ...
- [Swift]LeetCode896. 单调数列 | Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- LeetCode 896 Monotonic Array 解题报告
题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...
- [LeetCode&Python] Problem 896. Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- 896. Monotonic Array单调数组
[抄题]: An array is monotonic if it is either monotone increasing or monotone decreasing. An array A i ...
- 896. Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- [LeetCode] 896. Monotonic Array 单调数组
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
随机推荐
- Python 输出
普通的输出 生活中的“输出”: 软件中的“输出”: python中变量的输出: print('hello world') 格式化输出 占位符% print('Hello,%s' % 'Python') ...
- vagrant up报错 Warning: Authentication failure. Retrying...解决方案
参照链接 https://www.cnblogs.com/zqifa/p/vagrant-1.html 可以解决问题.
- CentOS7.5 安装ssh
yum -y install openssh-clients 如果出现 Permissions 0644 for ‘/root/.ssh/id_rsa’ are too open. 等错误显示了,原来 ...
- JS购物车编辑
实现了:第一件商品的加减实现了:全选/全不选(使用prop而不是attr)实现了:删除(遮罩层) 未实现:第二件商品的删除未实现:小计及应付款额的初始化(写死的) 计算小数乘法时,要先乘100 < ...
- 在consul上注册web服务
1. 创建web服务 IDEA->File->New->Project->Empty Project,project name取名provider,点击finish 2. 创建 ...
- 代码:CSS——reset.css
http://www.cnblogs.com/qq21270/p/5577856.html 图片列表 A链接标签: /* 链接样式.文字颜色 */ a{color:#666;text-decorati ...
- Signals的使用(通知)
https://docs.djangoproject.com/en/2.1/topics/signals/
- python中的多进程与多线程(二)
1.使用多线程可以有效利用CPU资源,线程享有相同的地址空间和内存,这些线程如果同时读写变量,导致互相干扰,就会产生并发问题,为了避免并发问题,绝不能让多个线程读取或写入相同的变量,因此python中 ...
- spring okhttp3
准备工作 在pom.xml文件中增加以下依赖 <dependency> <groupId>com.squareup.okhttp3</groupId> <ar ...
- gridView 删除一行后自动定位到指定行
/// <summary> /// 删除后定位到某一行 /// </summary> /// <param name="aCode"></ ...