Partition Array into Disjoint Intervals
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的更多相关文章
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
- [Swift]LeetCode915.将分区数组分成不相交的间隔 | Partition Array into Disjoint Intervals
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- Partition Array into Disjoint Intervals LT915
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- 【leetcode】915. Partition Array into Disjoint Intervals
题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...
- [leetcode-915-Partition Array into Disjoint Intervals]
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- 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 ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- 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 ...
随机推荐
- 【i3】manjaro配置
介绍 这是一期关于manjaro的安装到开发环境配置i3wm的教程.我即将在manjaro_gnome里面演示 主要的软件或者工具 i3wm tmux nvim polybar compton ran ...
- C++与引用2
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- Implementing 5G NR Features in FPGA
目录 论文来源 论文简介 基本原理 论文创新点 借鉴之处 论文来源 2018 European Conference on Networks and Communications (EuCNC),Ja ...
- phpstudy渗透到服务器
0x00 目标站点www.test.ichunqiu 0x01 尝试登陆系统 -尝试弱密码登陆 结果:forbidden!!! -尝试万能账号密码登陆 1‘ or 1=1--+ 和 1‘ or 1=1 ...
- [LeetCode] 994. Rotting Oranges 腐烂的橘子
题目: 思路: 每个腐烂的橘子都能将自己上下左右的新鲜橘子传染,像极了现在的肺炎... 如果格子中只有一个腐烂的橘子,那么这便是一个典型的层次遍历,第一个传染多个,称为第二层,第二层传染第三层 但这里 ...
- centos7搭建ceph集群
一.服务器规划 主机名 主机IP 磁盘配比 角色 node1 public-ip:10.0.0.130cluster-ip:192.168.2.130 sda,sdb,sdcsda是系统盘,另外两块数 ...
- 2020ubuntu1804server编译安装redis5笔记(二)配置redis
前一篇笔记记录了ubuntu1804server编译安装redis5,接下来要配置redis5了 网址:https://www.cnblogs.com/qumogu/p/12435694.html 第 ...
- Django之Model相关操作
一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 pr ...
- 龙叔拿了20几个offer,原因竟有些泪目...
我是龙叔,一个分享互联网技术和心路历程的大叔. 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles ...
- 五分钟用Docker快速搭建Go开发环境
挺早以前在我写过一篇用 `Docker`搭建LNMP开发环境的文章:[用Docker搭建Laravel开发环境](http://mp.weixin.qq.com/s?__biz=MzUzNTY5MzU ...