当前位置:网站首页>Leetcode/ and continuous shortest subarray greater than or equal to target
Leetcode/ and continuous shortest subarray greater than or equal to target
2022-07-29 02:03:00 【xcrj】
Code
package com.xcrj;
/** * The finger of the sword Offer II 008. And greater than or equal to target Continuous shortest subarray of * Given a containing n An array of positive integers and a positive integer target . * Find the sum of the array ≥ target The smallest length of Continuous subarray [numsl, numsl+1, ..., numsr-1, numsr] , And return its length . If there is no sub array that meets the conditions , return 0 . * Elements and >=target&& The subsequence of elements is continuous $$ The length of subsequence is the shortest */
public class Solution8 {
/** * The sliding window ( Double pointer ) * !!! Use different pointers to achieve different purposes * i j It all points to 0 * Move first j Seek sum>=target * Move again i Seek sum<target * Under the resultant force of the two phases, the subsequence with the smallest length satisfying the condition is obtained */
public int minSubArrayLen(int target, int[] nums) {
int i = 0;
int j = 0;
int sum = 0;
int minLen = Integer.MAX_VALUE;
//
while (j < nums.length) {
sum += nums[j];
while (sum >= target) {
minLen = Math.min(minLen, j - i + 1);
sum -= nums[i];
// Move i Seek sum<target
i++;
}
// Move j Seek sum>=target
j++;
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}
public static void main(String[] args) {
Solution8 solution8 = new Solution8();
System.out.println(solution8.minSubArrayLen(7, new int[]{
2, 3, 1, 2, 4, 3}));
}
}
Reference resources
author :LeetCode-Solution
link :https://leetcode.cn/problems/2VG8Kg/solution/he-da-yu-deng-yu-target-de-zui-duan-zi-s-ixef/
source : Power button (LeetCode)
边栏推荐
猜你喜欢

【流放之路-第四章】

MySQL high performance optimization notes (including 578 pages of notes PDF document), collected

StoneDB 邀请您参与开源社区月会!

Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!

【流放之路-第六章】

【GoLang】网络连接 net.Dial

Sigma-DSP-OUTPUT

Leetcode 112: path sum

Have you ever encountered the situation that the IP is blocked when crawling web pages?

【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索
随机推荐
Top network security prediction: nearly one-third of countries will regulate blackmail software response within three years
How companies make business decisions -- with the help of data-driven marketing
【7.27】代码源 - 【删数】【括号序列】【数字替换】【游戏】【画画】
数学建模——仓内拣货优化问题
leetcode/和大于等于target的连续最短子数组
LeetCode 113:路径总和 II
【流放之路-第八章】
In depth analysis of C language memory alignment
What is a proxy server? [2022 guide]
Promise解决异步
The basic concept of transaction and the implementation principle of MySQL transaction
[WesternCTF2018]shrine
What is the ISO assessment? How to do the waiting insurance scheme
知道创宇上榜CCSIP 2022全景图多个领域
As long as I run fast enough, it won't catch me. How does a high school student achieve a 70% salary increase under the epidemic?
Internship: tool class writing for type judgment
使用本地缓存+全局缓存实现小型系统用户权限管理
[7.27] code source - [deletion], [bracket sequence], [number replacement], [game], [painting]
【GoLang】网络连接 net.Dial
【MySQL】sql给表起别名