首页 > 其他分享 >leet Code [209. Minimum Size Subarray Sum]

leet Code [209. Minimum Size Subarray Sum]

时间:2022-10-11 15:34:25浏览次数:82  
标签:leet Code 窗口 nums 209 int result 滑动 total

[209. Minimum Size Subarray Sum]

[(https://leetcode.cn/problems/minimum-size-subarray-sum/)

image-20221011150522837

暴力解法

  • 两个for循环,不断寻找符合条件的子序列
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int result = INT32_MAX; // 最终的结果
        int total = 0; // 子序列的数值之和
        int count = 0; // 子序列的长度
        for (int i = 0; i < nums.size(); i++) { // 设置子序列起点为i
            total = 0;
            for (int j = i; j < nums.size(); j++) { // 设置子序列终止位置为j
               total += nums[j];
                if (total >= target) { // 一旦发现子序列和超过了s,更新result
                    count = j - i + 1; // 更新子序列的长度
                    result = result < subLength ? result : subLength;
                    break; // 因为是找符合条件最短的子序列,所以一旦符合条件就break
                }
            }
        }
        // 返回0,说明没有符合条件的子序列
        return result == INT32_MAX ? 0 : result;
    }
};
  • 时间复杂度:O(n^2)

    空间复杂度:O(1)

滑动窗口

  • 所谓滑动窗口:不断调节子序列起始位置和终止位置,从而得出想要的结果
  • 在暴力解法中,我们使用了两个for循环,若用滑动窗口如何以一个for循环得出结果呢?
  • 思路:
    • 首先,一个for循环是用来表示滑动窗口的起始位置,还是终止位置?若是表示起始位置,其实看着和暴力解法相差无几。因此for循环应该用来表示终止位置
    • 三个重点
      • 滑动窗口内表示的是什么?
        • 窗口其实就是一个满足条件的连续子数组
      • 滑动窗口如何移动起始位置?
        • 若窗口内总值sum大于target,则需要向前移动起始位置
      • 滑动窗口如何移动终止位置?
        • 若窗口内sum小于target,则需要向前移动终止位置
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int left = 0;	//滑动窗口起始位置
        int total = 0;	//滑动窗口终止位置
        int count = 0;	//滑动窗口长度
        int result = INT32_MAX;		//最终长度值
        for( int right = 0; right < nums.size(); )		//for循环控制终止位置
        {
            total += nums[right++];
            ++count;
            
            //滑动窗口精髓
            while(total >= target)
            {
                result = count < result ? count : result;
                --count;
                total -= nums[left++];
            }
        }
        return result == INT32_MAX ? 0 : result;
    }
};
  • 时间复杂度:O(n)

    空间复杂度:O(1)

标签:leet,Code,窗口,nums,209,int,result,滑动,total
From: https://www.cnblogs.com/chenglixue/p/16779394.html

相关文章

  • LeetCode 1195. Fizz Buzz Multithreaded
    原题链接在这里:https://leetcode.com/problems/fizz-buzz-multithreaded/题目:Youhavethefourfunctions:printFizz thatprintstheword "fizz" totheconsole......
  • leet Code 977. Squares of a Sorted Array_network
    [977.SquaresofaSortedArray][(https://leetcode.cn/problems/squares-of-a-sorted-array/)暴力解法对数组中每个元素平方后再排序代码如下:classSolution......
  • leetcode-394.字符串解码
    394.字符串解码publicStringdecodeString(Strings){Stack<Character>stack=newStack<>();for(charc:s.toCharArray()){if(c......
  • Codeforces Round #825 (Div. 2) A - C
    A模拟即可。voidsolve(){intn;cin>>n;vector<int>a(n),b(n);intcnt1=0,cnt2=0;for(inti=0;i<n;i++){cin>>a[i];......
  • leetcode 785. Is Graph Bipartite判断二分图 (中等)
    一、题目大意存在一个无向图,图中有n个节点。其中每个节点都有一个介于0到n-1之间的唯一编号。给你一个二维数组graph,其中graph[u]是一个节点数组,由节点u......
  • 在 macOS 中用 gcc 替换 clang 作为 VSCode 编译器
    众所周知,macOS下的默认C/C++编译器是clang/clang++而非gcc/g++,尽管在大部分情况下或许难以察觉其中的区别,但偶尔我们会需要用到gcc中的一些函数等等,因此改用VSCod......
  • VSCode 插件 vsix格式文件 离线安装
    场景 有些时候内网不能上网,则需要从共享目录直接安装下载好的vsix格式文件一、假设已经有了vsix离线文件(下载vsix暂不了解,后抽空补)二、文件放在vscode的安装目录......
  • Leetcode 33 -- 二分查找&&归约思想
    题目描述搜索旋转排序数组思路思路来源一个清晰的思路:这道题和平常二分法查找的不同就在于,把一个有序递增的数组分成了,两个递增的数组,我们需要做的就是判断这个......
  • vscode如何链接git远程仓库gitee或github
    vscode如何链接git远程仓库gitee或githubhttps://blog.csdn.net/G_C_H/article/details/1206732271.在GitHub上创建新的仓库2.生成SSH密钥开启GitBash命令行中输......
  • ESP32开发环境搭建 IDF3.3.5+VScode
    1、 软件准备:①ESP-IDF:包含ESP32API和用于操作工具链的脚本。②工具链msys32:用于编译ESP32应用程序。③编辑工具VisualStudioCode  注意:工具链和ESP-IDF需......