当前位置:网站首页>Likou 209 - String with the Minimum Length - Sliding Window Method
Likou 209 - String with the Minimum Length - Sliding Window Method
2022-08-02 11:45:00 【Zhang Ran Ran √】
Title description
Given an array of n positive integers and a positive integer target .
Find the contiguous subarray [numsl, numsl+1, ..., numsr-1, numsr] with the smallest length in the array that satisfies its sum ≥ target, and return its length.Returns 0 if no matching subarray exists.
Solution ideas

- Cage's vivid sliding window solution;
- First create two pointers first and last to point to the head of nums;
- Move the right pointer, and consider whether the value between first------last meets the condition of >= target every time it moves;
- If not satisfied, last continues to move to the right;
- If it is satisfied, the first pointer moves to the right;
- Count the number of elements between first-----last that satisfy the condition each time, record the minimum value, and return the minimum value.
- When returning, you should add a judgment condition to see if the sum condition is met. If the sum condition is not met, and the minimum value is not updated, return 0.
Input and output example

Code
class Solution {public int minSubArrayLen(int target, int[] nums) {int len = nums.length;int first = 0;int sum = 0;int min = Integer.MAX_VALUE;for(int last = 0; last < len; last++){sum += nums[last];while(sum >= target){min = Math.min(min,last-first+1);sum -= nums[first++];}}return min == Integer.MAX_VALUE ? 0 : min;}}边栏推荐
- How to connect TDengine through DBeaver?
- 暑期总结3
- 【kali-信息收集】(1.9)Metasploit+搜索引擎工具Shodan
- ssm web page access database data error
- openresty 性能优化
- Coroutines and Lifecycle in Kotlin
- ASP.NET Core 6框架揭秘实例演示[31]:路由&ldquo;高阶&rdquo;用法
- What is the future of smartwatches?
- Mysql transaction isolation level and MVCC (multi-version concurrency control)
- excel 批量翻译-excel 批量函数公司翻译大全免费
猜你喜欢

AQS-AbstractQueuedSynchronizer

Camera Hal OEM模块 ---- cmr_snapshot.c

使用kubesphere图形界面创建一个应用操作流程

Create an application operation process using the kubesphere GUI
![[kali-information collection] (1.9) Metasploit + search engine tool Shodan](/img/d2/6fae03d7597daa908a6816abc34e04.png)
[kali-information collection] (1.9) Metasploit + search engine tool Shodan

Multithreading (Basic) - 40,000 word summary

爆款视频怎么做?这里或许有答案!

DTG-SSOD:最新半监督检测框架,Dense Teacher(附论文下载)

打破千篇一律,DIY属于自己独一无二的商城

AQS-AbstractQueuedSynchronizer
随机推荐
List排序 ,取最大值最小值
SQL 经典50题(题目+解答)(1)
jacoco的学习以及理解
划分训练集,验证集,测试集
Create your own app applet ecosystem with applet containers
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
Getting Started with Three.JS Programmatic Modeling
5G网络切片技术
21 Days Learning Challenge - Day 1 Punch (Screen Density)
QT笔记——Q_PROPERTY了解
pyqt5连接MYSQL数据库问题
npm install报错npm ERR Could not resolve dependency npm ERR peer
【Acunetix-Forgot your password】
ASP.NET Core 6框架揭秘实例演示[31]:路由&ldquo;高阶&rdquo;用法
注意力机制
npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
8大软件供应链攻击事件概述
Thymeleaf
SQL 数据更新
元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存