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 in right.
  • left and right 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:

  1. 2 <= A.length <= 30000
  2. 0 <= A[i] <= 10^6
  3. 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的更多相关文章

  1. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

  2. [Swift]LeetCode915.将分区数组分成不相交的间隔 | Partition Array into Disjoint Intervals

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  3. [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  4. 【leetcode】915. Partition Array into Disjoint Intervals

    题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...

  5. Partition Array into Disjoint Intervals

    2020-02-10 22:16:50 问题描述: 问题求解: 解法一:MultiSet O(nlog) 看了下数据规模,第一个想到的是multiset,肯定可以ac的,就直接敲了出来. public ...

  6. [leetcode-915-Partition Array into Disjoint Intervals]

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  7. 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 ...

  8. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  9. 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 ...

随机推荐

  1. ssh 使用 aws

    使用 PuTTY 从 Windows 连接到 Linux 实例 启动您的实例之后,您可以连接到该实例,然后像使用您面前的计算机一样来使用它. 注意 启动实例后,需要几分钟准备好实例,以便您能连接到实例 ...

  2. 安装scrapy框架

    前提安装好python.setuptools. 1.安装Python 安装完了记得配置环境,将python目录和python目录下的Scripts目录添加到系统环境变量的Path里.在cmd中输入py ...

  3. dedecms list 添加自定义字段方法

    在内容模型管理中,添加字段时需这样:

  4. 原生js实现ajax与jquery的ajax库,及json

    这是一篇笔记博客, Ajax: 和服务器进行数据交换(异步) 用js实现复杂的原理:用于发送请求的对象在不同的浏览器中是不同的 同源策略:ajax发送请求的url地址与服务器地址必须是同一域名,协议, ...

  5. Openstack 集群,及常用服务的 高可用 haproxy配置

    一.介绍 配置文件位置(yum 安装):/etc/haproxy/haproxy.cfg 全局配置 #------------------------------------------------- ...

  6. Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等

    Selenium  如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...

  7. Shell教程 之字符串

    1.Shell字符串 字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号. 1.1 单引号 str='I am ...

  8. xadmin系列之单实例模式

    先看下单实例的定义 python的模块实现单例模式是python语言特有的,python的模块天然就是单例的,因为python有个pyc文件,导入一次后,第二次导入直接从pyc中取数据了 这里我们主要 ...

  9. Efficiency optimizing

    *low efficiency l_it_alv_stpox[] = g_it_alv_stpox[]. SORT l_it_alv_stpox BY zz_matnr idnrk. LOOP AT ...

  10. AnguarJS——第10章 路由

    第10章 路由 一个应用是由若个视图组合而成的,根据不同的业务逻辑展示给用户不同的视图,路由则是实现这一功能的关键. 10.1 SPA SPA(Single Page Application)指的是通 ...