Partition Array into Disjoint Intervals LT915
Given an array A
, partition it into two (contiguous) subarrays left
and right
so that:
- Every element in
left
is less than or equal to every element inright
. left
andright
are non-empty.left
has the smallest possible size.
Return the length of left
after such a partitioning. It is guaranteed that such a partitioning exists.
Example 1:
Input: [5,0,3,8,6]
Output: 3
Explanation: left = [5,0,3], right = [8,6]
Example 2:
Input: [1,1,1,0,6,12]
Output: 4
Explanation: left = [1,1,1,0], right = [6,12]
Note:
2 <= A.length <= 30000
0 <= A[i] <= 10^6
- It is guaranteed there is at least one way to partition
A
as described.
Idea 1. max(nums[0]... nums[i]) <= min(nums[i+1],..nums[n-1]), build maxArray from left, minArray from right
Time comlexity: T(n)
Space complexity: T(n)
using index:
class Solution {
public int partitionDisjoint(int[] A) {
int[] maxIndex = new int[A.length];
for(int i = 1; i < A.length; ++i) {
if(A[i] > A[maxIndex[i-1]]) {
maxIndex[i] = i;
}
else {
maxIndex[i] = maxIndex[i-1];
}
} int[] minIndex = new int[A.length];
minIndex[A.length-1] = A.length-1;
for(int i = A.length-2; i >= 0; --i) {
if(A[i] < A[minIndex[i+1]]) {
minIndex[i] = i;
}
else {
minIndex[i] = minIndex[i+1];
}
} for(int i = 0; i < A.length-1; ++i) {
if(A[maxIndex[i]] <= A[minIndex[i+1]]) {
return i + 1;
}
} return 0;
}
}
No need to use index
class Solution {
public int partitionDisjoint(int[] A) {
int[] maxFromLeft = new int[A.length];
maxFromLeft[0] = A[0];
for(int i = 1; i < A.length; ++i) {
maxFromLeft[i] = Math.max(maxFromLeft[i-1], A[i]);
} int[] minFromRight = new int[A.length];
minFromRight[A.length-1] = A[A.length-1];
for(int i = A.length-2; i >= 0; --i) {
minFromRight[i] = Math.min(minFromRight[i+1], A[i]);
} for(int i = 0; i < A.length-1; ++i) {
if(maxFromLeft[i] <= minFromRight[i+1]) {
return i + 1;
}
} return 0;
}
}
Idea 2. 从讨论里看到的妙解,只需要保持2个变量,localMax记录有效partition里的最大值, maxSoFar记录遍历至今的最大值,nums[0]...nums[paritionIndex] (localMax) | [nums[partitonIndex]...nums[i-1]| (maxSoFar), 遍历到一个数nums[i],
paritionIndex 不变 if nums[i] >= localMax
paritionIndex = i, 需要包括nums[i], localMax也需要更新至maxSoFar
Time complexity: O(n)
Space complexity: O(1)
class Solution {
public int partitionDisjoint(int[] A) {
int localMax = A[0];
int maxSoFar = A[0];
int partitionIndex = 0;
for(int i = 1; i < A.length; ++i) {
if(A[i] < localMax) {
partitionIndex = i;
localMax = maxSoFar;
}
maxSoFar = Math.max(maxSoFar, A[i]);
} return partitionIndex + 1;
}
}
Partition Array into Disjoint Intervals LT915的更多相关文章
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
- [Swift]LeetCode915.将分区数组分成不相交的间隔 | Partition Array into Disjoint Intervals
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- 【leetcode】915. Partition Array into Disjoint Intervals
题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...
- Partition Array into Disjoint Intervals
2020-02-10 22:16:50 问题描述: 问题求解: 解法一:MultiSet O(nlog) 看了下数据规模,第一个想到的是multiset,肯定可以ac的,就直接敲了出来. public ...
- [leetcode-915-Partition Array into Disjoint Intervals]
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- Max coverage disjoint intervals
Assume you have k<=10^5 intervals [a_i, b_i] \in [1,10^18] (some of them may overlap), and you ne ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
随机推荐
- Light Probe
[Light Probe] Light Probes provide a way to capture and use information about light that is passing ...
- 关于OPEN_MAX宏undeclared的问题
最近在看unp时,I/O复用-poll一章的代码使用到了OPEN_MAX.据书中描述,这一宏定义在limits.h头文件中,指代一个进程在任意时刻能打开的最大描述符数目.但在代码编译时遇到了错误,提示 ...
- jQuery 替换元素
参考https://www.cnblogs.com/halai/p/6868027.html http://www.w3school.com.cn/jquery/manipulation_replac ...
- javac编译带包的java文件需要在命令处加参数
不带包:javac aaa.java 带包:javac -d . aaa.java 带包就是 java文件中含有 package com.aaa;
- 如何将div中的内容设置为空同时还要保留div本身
将div的innerHTML置为空即可,下面有2类方法可以实现: 假设有如下的html片段: <div id="test">这是要删除的内容,还要保留test本身< ...
- php71
yum -y install php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd jpegsrc libmcrypt libpng li ...
- 81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- TZOJ 3659 神奇的探险之旅(有向无环每个点只能经过一次最长路dij)
描述 我们正在设计这样的一款儿童探险游戏:游戏由很多故事场景组成,每个场景中都有一个问题,游戏根据玩家的回答将进入下一场景,经过巧妙的设计,我们保证在一次“探险旅行”中,不会重复的进入任何相同的场景, ...
- spring cloud ribbon和feign的区别
spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign. Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器 它可以在客户端配置 ribb ...
- ABAP开发需要养成的习惯—程序修改数据库表
①此外将内表数据写入数据库,推荐用Modify而不是insert,因为会有些key一样的报dump loop at it_record. * 报错 * insert ...