【leetcode】Array—— Minimum Size Subarray Sum(209)

来源:互联网 发布:单兵数字侦察软件 编辑:程序博客网 时间:2024/05/29 03:16

题目:

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.

More practice:

If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).

思路 O(n):维护两个指针,即一个窗口,统计两指针之间数字的累积和。

leetcode解释:Since the given array contains only positive integers, the subarray sum can only increase by including more elements. Therefore, you don't have to include more elements once the current subarray already has a sum large enough. This gives the linear time complexity solution by maintaining a minimum window with a two indices.

代码:

    private int solveN(int s, int[] nums) {        int start = 0, end = 0, sum = 0, minLen = Integer.MAX_VALUE;        while (end < nums.length) {            while (end < nums.length && sum < s) sum += nums[end++];            if (sum < s) break;            while (start < end && sum >= s) sum -= nums[start++];            if (end - start + 1 < minLen) minLen = end - start + 1;        }        return minLen == Integer.MAX_VALUE ? 0 : minLen;    }

思路 N logN:用一个长度为nums.length+1的数组统计nums数组的累计和,该求和数组为递增的,可以用binary search进行查找

leetcode解释:As to NLogN solution, logN immediately reminds you of binary search. In this case, you cannot sort as the current order actually matters. How does one get an ordered array then? Since all elements are positive, the cumulative sum must be strictly increasing. Then, a subarray sum can expressed as the difference between two cumulative sum. Hence, given a start index for the cumulative sum array, the other end index can be searched using binary search.

代码:

    private int solveNLogN(int s, int[] nums) {        int[] sums = new int[nums.length + 1];        for (int i = 1; i < sums.length; i++) sums[i] = sums[i - 1] + nums[i - 1];        int minLen = Integer.MAX_VALUE;        for (int i = 0; i < sums.length; i++) {            int end = binarySearch(i + 1, sums.length - 1, sums[i] + s, sums);            if (end == sums.length) break;            if (end - i < minLen) minLen = end - i;        }        return minLen == Integer.MAX_VALUE ? 0 : minLen;    }    private int binarySearch(int lo, int hi, int key, int[] sums) {        while (lo <= hi) {           int mid = (lo + hi) / 2;           if (sums[mid] >= key){               hi = mid - 1;           } else {               lo = mid + 1;           }        }        return lo;    }

1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 工伤公司垫付医疗费没法报销怎么办 司法考试照片耳朵露不出来怎么办 新华社毕业证照片用光了怎么办 农村父母投靠落户社保怎么办 退休后投靠父母户口怎么办 要离婚想儿子了怎么办 怀孕期间离婚了孩子户口怎么办 常州武进区怎么办居住证明 跟老公离婚了户口怎么办 离婚了不给户口怎么办 父母不给户口本迁户口怎么办 产能置换的煤矿职工怎么办 如果是单位集体户小孩读书怎么办 异地防疫不给打怎么办? 青岛市办理大龄就业困难补贴怎么办 就业登记证掉了怎么办 就业信息填错了怎么办 小孩入学父母无单位怎么办 和公婆住一起很压抑怎么办 不想和公婆一起住怎么办 在家啃老三年了怎么办 新时代卫计工作怎么办 被公司辞退不发工资怎么办 被公司辞退后不发工资怎么办 领导分配的工作太多怎么办 领导故意不给活怎么办 户口迁移后医疗社保怎么办 有了c证考b证怎么办 顶替姐姐上班已到退休年龄怎么办 年龄过60岁厂里拖欠工资怎么办 领导找人顶替我怎么办 宁夏超生了没钱交罚款怎么办? 户口年龄上大了怎么办 孩子年龄报小了怎么办 招工档案年龄有涂改怎么办 退伍军被别人顶替上班怎么办 二孩政策前生的怎么办 孩子晕车怎么办最有效方法得当 事业单位编外人员改革工伤怎么办 工伤仲裁后法院一审判决后怎么办 我媳妇删了我该怎么办