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. MIUI 7 会是小米的救命稻草吗?

    7 会是小米的救命稻草吗?" title="MIUI 7 会是小米的救命稻草吗?"> 花无百日红,人无千日好.再绚烂的曾经,或许一朝不慎,就会成为过去.在科技圈,诺 ...

  2. 码海拾遗:基于MySQL Connector/C++的MySQL操作(连接池)

    1.MySQL安装及简单设置 (1)安装:在OSX系统下,可以使用万能的“brew install”命令来进行安装:brew isntall mysql(默认安装最新版的MySQL) (2)启动:br ...

  3. TensorFlow学习笔记(一)

    [TensorFlow API](https://www.tensorflow.org/versions/r0.12/how_tos/variable_scope/index.html) Tensor ...

  4. Ftp Centos · GitBook

    これよくない pyftpdlibをつかおう sudo easy_install pyftpdlib nohup python -m pyftpdlib > pyftpdlib.log 2> ...

  5. MySQL学习之路 一 : MySQL 5.7.19 源码安装

    MySQL 5.7.19 源码安装 查看系统: # cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 安装依赖包 # yum - ...

  6. Python开发(二):列表、字典、元组与文件处理

    Python开发(二):列表.字典.元组与文件处理 一:列表二:元组三:字典四:文件处理 一:列表   为什么需要列表 可以通过列表可以对数据实现最方便的存储.修改等操作.字符串是不能修改的,所以无法 ...

  7. handlebar.js模板引擎(轻页面小工程可用)

    介绍 Handlebars 让你能够有能力高效地容易地创立语义化的模版.Handlebars兼容Mustache语法,在大多数情况下它可以读取Mustache的语法并在你当前模板中使用.具体点击这里 ...

  8. 前端每日实战:74# 视频演示如何用纯 CSS 创作一台 MacBook Pro

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MXNNyR 可交互视频 此视频是可 ...

  9. 前端每日实战:31# 视频演示如何利用 CSS 的动画原理,创作一个乒乓球对打动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/rvgLzK 可交互视频教程 此视频 ...

  10. ES6的Proxy

    最近在Javascript的设计编程中,用到的那个单例模式,感觉就类似一种代理的思想[其实就是缓存的一种机制],单例模式就是: function getSingle(fn){ var result; ...