当前位置:网站首页>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)
边栏推荐
- Data platform data access practice
- LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
- 把逻辑做在Sigma-DSP中的优化实例-数据分配器
- [understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
- Introduction to shared data center agent
- druid. IO custom real-time task scheduling policy
- golang run时报undefined错误【已解决】
- 数学建模——公交调度优化
- [netding cup 2020 rosefinch group]nmap
- Sigma-DSP-OUTPUT
猜你喜欢
![[golang] use select {}](/img/30/fa593ec682a40c47689c1fd88f9b83.png)
[golang] use select {}
![Golang run times undefined error [resolved]](/img/9b/3379aeeff59b47531fe277f7422ce7.png)
Golang run times undefined error [resolved]

Large scale web crawling of e-commerce websites (Ultimate Guide)

【流放之路-第三章】

Analyze OP based on autoware_ global_ Planner global path planning module re planning

Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system

数学建模——红酒品质分类

StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
![[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"

Sigma-DSP-OUTPUT
随机推荐
What is browser fingerprint recognition
Know that Chuangyu is listed in many fields of ccsip 2022 panorama
秘术冬潮烙技能搭配
Lua third-party byte stream serialization and deserialization module --lpack
正则过滤数据学习笔记(①)
【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索
[网鼎杯 2020 朱雀组]Nmap
How companies make business decisions -- with the help of data-driven marketing
Analyze OP based on autoware_ global_ Planner global path planning module re planning
golang run时报undefined错误【已解决】
分布式开发漫谈
[golang] synchronization lock mutex
为什么 BI 软件都搞不定关联分析
Golang run times undefined error [resolved]
【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
Talk about possible problems when using transactions (@transactional)
Stonedb invites you to participate in the open source community monthly meeting!
[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
[the road of Exile - Chapter III]
The solution of reducing the sharpness of pictures after inserting into word documents