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.
思路:
从左到右扫描一遍数组,用一个数组记录索引i左边出现的最大值。
然后,从右到左再扫描一遍,用另外一个数组记录索引i右边出现的最小值。
最后,比较两个数组中同一个索引i处左边的最大值与右边的最小值的情况。
int partitionDisjoint(vector<int>& A)
{
map<int,int>mpMax,mpMin;//对应的索引
int t = A[];
for(int i = ; i < A.size(); i++)
{
t = max(t,A[i]);
mpMax[i] = t;
}
t = A[A.size()-];
for(int i = A.size()-; i >= ; i--)
{
t = min(t,A[i]);
mpMin[i] = t;
} for(int i = ; i < A.size(); i++)
{
if(mpMax[i-] <= mpMin[i])return i;
} }
参考:

[leetcode-915-Partition Array into Disjoint Intervals]的更多相关文章

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

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

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

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

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

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

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

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

  5. Partition Array into Disjoint Intervals LT915

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

  6. Partition Array into Disjoint Intervals

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

  7. leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...

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

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

  9. LeetCode 1043. Partition Array for Maximum Sum

    原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...

随机推荐

  1. Oracle与MySQL使用区别

    与MySQL通过创建不同的数据库来存储表 Oracle提出表空间(tablespace)的概念作为逻辑上的存储区域来存储表, 而不同的表空间由不同的用户来管理 用户可以授予权限或角色 举例: 使用PL ...

  2. SQL循环插入批量数据

    declare @i intdeclare @qid int set @i=1set @qid=100 while @i<50000begininsert into Order(orderid, ...

  3. debug?用对拍!

    很多人考noip之类的比赛永远会发生一些奇怪的问题 比如说下面这两位(来自我的两位学长) sliver n:spli,考得怎样啊? spli:就那样啦,day1T1没推出来规律,别的还好 silver ...

  4. java Clob类型 转String

    1.我的数据库是oracle11g 遇到取出来的字段是clob类型,但是所需要的是string类型,写一个转换函数就可以解决问题了. // Clob类型 转String public String C ...

  5. docker 入门 (二)基本操作

    这一篇讲docker的基本操作. 请自行安装docker到自己的电脑上. 镜像的获取 要获取镜像,最简单的当然是从仓库去获取,docker的官方网站很不好练,其他的可选项有docker中国官网,阿里镜 ...

  6. 大数据 : Hadoop reduce阶段

    Mapreduce中由于sort的存在,MapTask和ReduceTask直接是工作流的架构.而不是数据流的架构.在MapTask尚未结束,其输出结果尚未排序及合并前,ReduceTask是又有数据 ...

  7. 话说文件系统——VFS简介(二)

    linux可以与很多文件系统完美的结合,可以很容易地把Windows.其他Unix系统.甚至在市场上很小众的文件系统轻松地移植到linux中. 这对于linux今天的成功是功不可没的,那为什么这么厉害 ...

  8. MySQL优化#参数配置优化

    ( 1).查看mysql里的线程,观察是否有长期运行或阻塞的sql: show full processlist 经查看,没有发现相关线程,可排除该原因 (2).疑似mysql连接使用完成后没有真正释 ...

  9. (数据科学学习手札55)利用ggthemr来美化ggplot2图像

    一.简介 R中的ggplot2是一个非常强大灵活的数据可视化包,熟悉其绘图规则后便可以自由地生成各种可视化图像,但其默认的色彩和样式在很多时候难免有些过于朴素,本文将要介绍的ggthemr包专门针对原 ...

  10. POJ3006-Dirichlet's Theorem on Arithmetic Progressions

    题意: 设一个等差数列,首元素为a,公差为d 现在要求输入a,d,n ,要求找出属于该等差数列中的第n个素数并输出 思路:空间换时间是个主旋律.素数表的生成用素数筛选法.方法是从2开始,对每个目前还标 ...