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. iOS获取设备ip地址(OC版)

    #import <SystemConfiguration/CaptiveNetwork.h> #import <ifaddrs.h> #import <arpa/inet ...

  2. 百度地图中找不到BMap的解决

    一般情况下是引用的问题,产生的原因大概有两种 1.不同架构的引用方式不同,引用js的方式不同导致 2.自身调用顺序有误 官方的引用方式是使用标签引入,示例 <script type=" ...

  3. Java并发编程(六)原子性与易变性

    原子性 原子是最小单元.不可再分的意思.原子性是指某个操作在获取CPU时间时,要么就给它足够时间,让这个操作执行完,要么就不执行这个操作,执行时不能出现上下文切换(把CPU时间从一个线程分配到另一个线 ...

  4. 传递的值是this,在js里就不用再写$(this)

    <input class="editinput" value="${detail.earlymoneyrmb}" name="earlymone ...

  5. vue项目获取当前地址栏参数(非路由传参)

    项目中遇到一个需求,就是另一个管理系统带参直接单纯的跳转跳转到vue pc项目中的某个页面,后再初始化查询数据,参数以地址栏的形式传入 管理系统:打开新地址地址 let obj = { id: 21, ...

  6. linux 学习第十一天

    一.配置服务说明 1.1.linux系统中的一切都是文件 1.2.配置一个服务就是在修改去配置文件 1.3.要想让新的配置文件立即生效,需要重启对应的服务 二.配置网卡 2.1.编辑配置文件 vim ...

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

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

  8. Angular4 自制华容道拼图(可以升级难度、关卡、更换图片)

    前端工程师新手一枚,之前一直做些小设计,以及静态页面的编写工作.刚刚接触 Angular 没有多久,四个月前对于 js 也只是会写 alert 之流,现在进步算是很大,下面是自制的华容道拼图(可以升级 ...

  9. Redis笔记 -- make编译安装报错记录2则(一)

    1.Redis的获取与安装,目前最新稳定版本为4.0.10 Redis:  https://redis.io/download GitHub:  https://github.com/antirez/ ...

  10. php wamp基础环境搭建

    一.apache 安装配置: 1.安装apache 1.1 下载地址:https://www.apachelounge.com/download/ 1.2 将下载的文件解压到你想安装的目录 D:\WA ...