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. LINQ的左连接、右连接、内连接和Lamda表达式实现Left join

    1.左连接: var LeftJoin = from t1 in l1join t2 in l2on t1.ID equals t2.ID into Joinedt12from t3 in Joine ...

  2. Integer类之成员变量

    一.一共11个成员变量. 二.详情介绍. 1.value值.这个是Integer类的唯一标志.最重要的实例属性. 2.最小值和最大值常量.注意,计算机里面是以补码形式保存的,因此用十六进制时,给的数据 ...

  3. 5Lambda表达式

    C++11中的Lambda表达式用于定义并创建匿名的函数对象,以简化编程工作.首先看一下Lambda表达式的基本构成: [函数对象参数](操作符重载函数参数)mutable或exception -&g ...

  4. c# 模拟get请求例子,演示Session会话状态。

    创建一个控制台 程序: using System; using System.Collections.Generic; using System.IO; using System.IO.Compres ...

  5. Keepalived 安装

    Keepalived 安装安装环境 keepalived-1.2.18.tar.gz VM虚拟机redhat6.5-x64:192.168.1.201 Xshell4 部署方案 201部署 安装步骤  ...

  6. Python: re.IGNORECASE 标志参数字符串忽略大小写的搜索替换

    为了在文本操作时忽略大小写,需要在使用re 模块的时候给这些操作提供re.IGNORECASE 标志参数.比如 >>> text = 'UPPER PYTHON, lower pyt ...

  7. Linux基础命令---cpio

    cpio 从归档中复制文件,或者复制文件到归档中.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. Cpio命令有三种工作模式: 1)c ...

  8. 原 用Tomcat服务器配置https双向认证过程实战

    什么是https? 百度百科足够解释它:http://baike.baidu.com/view/14121.htm 工具:keytool (Windows下路径:%JAVA_HOME%/bin/key ...

  9. 获取Json字符串中的key和value

    获取Json字符串中的key和value 在web项目中经常会用到json数据(如:struts2处理请求返回json数据给jsp解析),因此,JSONObject对象是必备的,这时就需要引入相关的j ...

  10. zabbix-server新增zabbix-agent

    zabbix监控系统搭建好了之后,就需要为各种角色host加入进来,现在新增一台zabbix-agent: 1.在172.16.23.128上安装zabbix-agent,zabbix-server: ...