查看: 3891| 回复: 10
跳转到指定楼层
上一主题 下一主题
收起左侧

Minimum Size Subarray Sum 如果array中有negative number怎么办?

🔗
bcc | 只看该作者 |倒序浏览
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.

上一篇:leetcode Course Schedule II的测试数据貌似有个bug
下一篇:哥大刷题求组队
推荐
celestial 2016-1-7 14:34:05 | 只看该作者
全局:
我用的是 treemap 做的,做的时候没有用 positive number这个条件,可能是漏看了 = =

import java.util.Map.Entry;

public class Solution {
    /**
     * @param nums: an array of integers
     * @param s: an integer
     * @return: an integer representing the minimum size of subarray
     */
    public int minimumSize(int[] nums, int s) {
        if (nums == null || nums.length == 0) return -1;
        
        int minLen = Integer.MAX_VALUE;
        TreeMap<Long, Integer> map = new TreeMap<>();
        map.put(0L, -1);
        long sum = 0L;
        for (int i = 0; i < nums.length; ++i) {
            sum += nums[i];
            Entry<Long, Integer> floorEntry = map.floorEntry(sum - s);
            if (floorEntry != null && floorEntry.getKey() >= 0) {
                minLen = Math.min(minLen, i - floorEntry.getValue());
            }
            map.put(sum, i);
        }
        return minLen == Integer.MAX_VALUE ? -1 : minLen;
    }
}

思想就是用 sum 统计数组中加到第 i 个元素的和,然后从 treemap 里面找有没有之前加到某个元素(第 j 个)之和小于 sum - s,因为从j+1到 i之和肯定大于等于s
回复

使用道具 举报

🔗
wxr.dal 2015-12-22 07:35:26 | 只看该作者
全局:
dp做?字数字数字数字数
回复

使用道具 举报

🔗
163 2016-1-7 22:04:36 | 只看该作者
全局:
celestial 发表于 2016-1-7 14:34
我用的是 treemap 做的,做的时候没有用 positive number这个条件,可能是漏看了 = =

import java.util. ...

感觉有bug,你作为对比的floorEntry的value(也就是index)不一定是所有满足条件的index里面最大的那个,这个在数组元素非负的时候确实成立,但是有负数的时候是不对的,考虑例子: array: [2,-1,-1,7], target: 5

你的code应该会输出3
回复

使用道具 举报

🔗
celestial 2016-1-11 11:34:27 | 只看该作者
全局:
163 发表于 2016-1-7 22:04
感觉有bug,你作为对比的floorEntry的value(也就是index)不一定是所有满足条件的index里面最大的那个, ...

You are right. Do you have any idea?
回复

使用道具 举报

🔗
UCLA_andy 2018-6-26 14:21:39 | 只看该作者
全局:
163 发表于 2016-1-7 22:04
感觉有bug,你作为对比的floorEntry的value(也就是index)不一定是所有满足条件的index里面最大的那个, ...

难道答案不就是3嘛?{-1,-1,7} ?
回复

使用道具 举报

🔗
cai_lw 2018-6-26 18:44:58 | 只看该作者
全局:
celestial 发表于 2016-1-11 11:34
You are right. Do you have any idea?

改成线段树就行了,复杂度也是O(nlogn)
(暂时想不到更简单的数据结构可以达到相同目的)

补充内容 (2018-6-26 18:51):
单调队列好像也行,代码短很多,但是复杂度仍然是O(nlogn)
回复

使用道具 举报

🔗
cai_lw 2018-7-1 10:15:01 | 只看该作者
全局:
神了,今天leetcode周赛最后一题就是这题,完全一模一样……
回复

使用道具 举报

全局:
cai_lw 发表于 2018-7-1 10:15
神了,今天leetcode周赛最后一题就是这题,完全一模一样……

对的. 确实是这道.. 聪明的你!

这题我看了一眼答案, O[n]能做出来的. 其实想法也不能说很难, 确实很巧..

用的是deque, 只把比当前sum小的数字放在里面了..
回复

使用道具 举报

🔗
cai_lw 2018-7-4 14:55:27 | 只看该作者
全局:
肥宅快乐水 发表于 2018-7-4 04:20
对的. 确实是这道.. 聪明的你!

这题我看了一眼答案, O[n]能做出来的. 其实想法也不能说很难, ...

这个就是单调队列啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表