2018-07-08 13:24:31

一、525. Contiguous Array

问题描述:

问题求解:

我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已经给出是50000量级,显然只能是O(n),至多O(nlogn)的复杂度。

本题使用DP和滑动数组都比较棘手,这才是最麻烦的地方,我们知道一般来说对于subarray的问题,dp和滑动数组是两大利器,但是本题这两个最实用的解法都不再适用,那么该如何解决呢?

其实想通了就是一个很简单的问题,本题是longest target sum subarray的变种题,核心的思路就是进行一步转化,如果把0变成-1,那么原题就变成了求target sum == 0的最长子数组。

这里给出的方案是preSum + HashMap的策略来进行解决,可以说方法是比较巧妙的。

    public int findMaxLength(int[] nums) {
int res = 0;
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) if (nums[i] == 0) nums[i] = -1;
int sum = 0;
map.put(0, -1);
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (map.containsKey(sum)) res = Math.max(res, i - map.get(sum));
else map.put(sum, i);
}
return res;
}

二、1124. Longest Well-Performing Interval

问题求解:

问题求解:

无独有偶,leetcode最近的contest中出了一题非常类似的问题,本质上依然是最长target sum的子数组,当时的想法也是陷入了范式中,错误的认为子数组的问题都可以使用dp或者滑动数组进行求解,导致走入了死胡同。

本题也是一条变种题,可见如何直接出subarray of target sum 是没有多大的价值的,这个题目直接问的话,就非常简单直白,很容易就会想到使用hashmap去求解,但是如果进行一下包装,就未必能想到了。

总之,碰到subarray的问题,不仅要能联想到dp/sliding window,还要有意识去想想其他的解法。

    public int longestWPI(int[] hours) {
if (hours.length == 0) return 0;
int n = hours.length;
int res = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
if (hours[i] > 8) hours[i] = 1;
else hours[i] = -1;
}
int curSum = 0;
for (int i = 0; i < n; i++) {
curSum += hours[i];
if (curSum >= 1) res = Math.max(res, i + 1);
if (map.containsKey(curSum - 1)) res = Math.max(res, i - map.get(curSum - 1));
map.put(curSum, map.getOrDefault(curSum, i));
}
return res;
}

  

Contiguous Array with Equal Number of 0 & 1的更多相关文章

  1. [LeetCode] Contiguous Array 邻近数组

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  2. [Swift]LeetCode525. 连续数组 | Contiguous Array

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  3. 994.Contiguous Array 邻近数组

    描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...

  4. LeetCode 525. Contiguous Array

    525. Contiguous Array Add to List Description Submission Solutions Total Accepted: 2476 Total Submis ...

  5. 525. Contiguous Array两位求和为1的对数

    [抄题]: Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 ...

  6. 525. Contiguous Array

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  7. [LeetCode] 525. Contiguous Array 相连的数组

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  8. 【LeetCode】525. Contiguous Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...

  9. IE下Array.prototype.slice.call(params,0)

    i8 不支持 Array.prototype.slice.call(params,0) params可以是 HTMLCollection.类数组.string字符串

随机推荐

  1. XMLHelper类 源码(XML文档帮助类,静态方法,实现对XML文档的创建,及节点和属性的增、删、改、查)

    以下是代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...

  2. DIV CSS 绘制风车

    我得说,CSS和DIV是个有趣的东西. 由于脑袋一无聊,突然想,画个DIV风车怎么样,于是就画了一个. border的风格可以自主选择. 上代码: <style> *{ margin:0p ...

  3. 概率检索模型:BIM+BM25+BM25F

    1. 概率排序原理 以往的向量空间模型是将query和文档使用向量表示然后计算其内容相似性来进行相关性估计的,而概率检索模型是一种直接对用户需求进行相关性的建模方法,一个query进来,将所有的文档分 ...

  4. 搭建Linux-java web运行环境之一:安装jdk+tomcat

    环境 OS:Red Hat Enterprise Linux Server release 7.3 (Maipo) JDK:jdk-7u80-linux-x64.tar.gz Tomcat:apach ...

  5. angularjs 路由参数

    .state('classrooms',{ url: '/classrooms/:id' }) .state('classrooms',{ url: '/classrooms/{id}' }) .st ...

  6. 最新 mysql登录报错“Access denied for user 'root'@'localhost' (using password: NO”的处理方法

    1.关闭正在运行的MySQL.2.打开DOS窗口,转到mysql\bin目录.3.输入mysqld --skip-grant-tables回车.如果没有出现提示信息,那就对了.(正常的情况是光标闪烁没 ...

  7. Socket和ServletSocket的交互

    ServerSocket(int port) 是服务端绑定port端口,调accept()监听等待客户端连接,它返回一个连接队列中的一个socket. Socket(InetAddress addre ...

  8. http协议/获得请求/中文参数处理/访问数据库

    # 1. http协议(了解)## (1)什么是http协议?一种网络应用层协议,规定了浏览器与web服务器之间如何通信以及相应的的数据包的结构.注:tcp/ip协议:保证数据可靠的传递.(UDP不可 ...

  9. python的os模块和sys模块

    os模块 os.getcwd()  获取当前的工作目录 os.chdir('绝对路径/相对于当前工作目录的路径')  改变工作目录,相当于shell的cd命令,例如Windows平台下os.chdir ...

  10. 09:Linux 中各个文件夹的作用

    参考博客 /  根目录 包含了几乎所的文件目录.相当于中央系统.进入的最简单方法是:cd /. /boot  引导程序,内核等存放的目录 这个目录,包括了在引导过程中所必需的文件.在最开始的启动阶段, ...