2020-02-10 22:16:50

问题描述:

问题求解:

解法一:MultiSet O(nlog)

看了下数据规模,第一个想到的是multiset,肯定可以ac的,就直接敲了出来。

    public int partitionDisjoint(int[] A) {
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int num : A) map.put(num, map.getOrDefault(num, 0) + 1);
int n = A.length;
int curr = -1;
for (int i = 0; i < n - 1; i++) {
curr = Math.max(curr, A[i]);
map.put(A[i], map.get(A[i]) - 1);
if (map.get(A[i]) == 0) map.remove(A[i]);
int key = map.firstKey();
if (key >= curr) return i + 1;
}
return -1;
}

解法二:left & right O(n)

这个方法就是先做一次预处理,使用一个数组去从右遍历到当前数字的最小值,之后再从左遍历得到当前的最大值并和后面的最大值比较即可。

解法三:O(n)

解法三就比较巧妙了。

max :记录到目前为止的最小值。

localMax :当前最左的localMax

如果我们遍历到一个数字比localMax要小的话,那么其必定是在left的,此时更新idx和localMax即可。

    public int partitionDisjoint(int[] a) {
int localMax = a[0], partitionIdx = 0, max = localMax;
for (int i = 1; i < a.length; i++) {
max = Math.max(max, a[i]);
if (localMax > a[i]) {
localMax = max;
partitionIdx = i;
}
}
return partitionIdx + 1;
}

  

  

Partition Array into Disjoint Intervals的更多相关文章

  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. Partition Array into Disjoint Intervals LT915

    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 分割数组为不相交的区间

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

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

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

  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. YCSB项目学习

    主要总结Yahoo的数据库测试项目YCSB的使用(针对redis). github网址:https://github.com/brianfrankcooper/YCSB 需要安装 java maven ...

  2. Jenkins+Git+Fastlane+Fir CI集成

    上一篇有讲关于fastlane自动化部署,本篇将会着重讲关于fastlane的实际应用. 目标: 利用自动化jenkins打包工具,自动拉取git仓库代码 不需要通过手动检查修改xcode中项目配置修 ...

  3. 改进"尽最大努力交付"的服务

    改进"尽最大努力交付"的服务 网络层的作用就是负责在不同的网段尽力转发数据包,但是负责中专数据包的路由器并不关心数据包的内容和优先顺序.而是先到达的数据包先处理,后到达的数据包排队 ...

  4. Nginx 配置GeoIP2 禁止访问,并允许添加白名单过滤访问设置

    配置环境:Centos 7.6 + Tengine 2.3.2 GeoIP2 下载地址:https://dev.maxmind.com/geoip/geoip2/geolite2/ 1. Nginx  ...

  5. Docker实战之Zookeeper集群

    1. 概述 这里是 Docker 实战系列第四篇.主要介绍分布式系统中的元老级组件 Zookeeper. ZooKeeper 是一个开源的分布式协调服务,是 Hadoop,HBase 和其他分布式框架 ...

  6. VUE实现Studio管理后台(三):支持多语言国际化(vue-i18n)

    RXEditor的第一版本是英文版,有些朋友看起来觉得不习惯,后来因为惰性,不愿意再修改旧代码加入中文版,这次提前就把这个问题解决了,克服惰性最好的方式,就是想到就尽快去做,避免拖延. 本来计划在界面 ...

  7. Django报Warning错误 RuntimeWarning: DateTimeField Goods.create_at received a naive datetime (2019-07-31 23:05:58) while time zone support is active

    报错和UTC(世界标准时间)有关,在settings.py 文件中设置 USE_TZ = False 警告错误不再报

  8. Cisco模拟器的基本使用

    获取帮助查找命令 只需输入一个'?'便可得到详细的帮助信息,如果想获取c开头的命名,那么直接输入'c?'即可. 在各个模式下切换的方法 给如图所示路由器接口配置IP地址 第一步:安装HWIC-2T(串 ...

  9. MySQL数据库无完整备份删库,除了跑路还能怎么办?

    1.背景 前段时间,由于运维同事的一次误操作,清空了内网核心数据库,导致了公司内部管理系统长时间不可用,大量知识库内容由于没有备份险些丢失. 结合这两天微盟的删库跑路事件,我们可以看到,数据库的备份与 ...

  10. DirectX11--深入理解Effects11、使用着色器反射机制(Shader Reflection)实现一个复杂Effects框架

    前言 如果之前你是跟随本教程系列学习的话,应该能够初步了解Effects11(现FX11)的实现机制,并且可以编写一个简易的特效管理框架,但是随着特效种类的增多,要管理的着色器.资源等也随之变多.如果 ...