leetcode 动态规划+滑动空间

为了能过笔试,为了能有offer,真的是拼了.感觉leetcode上题目的类型划分不是很精确呢,这个动态规划系列是A了一道上台阶的题目,按照下面联想的相关题目写的呢,可是感觉好像有的题目不是很动态规划呢。大概是我对动态规划理解不清晰吧。

523. Continuous Subarray Sum

Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
题目大意:给出的非负整数数组,至少有2个元素的和是k的倍数
解题思路:一开始非常蠢的开了个二维数组,用来存储数组元素的和,结果一直RE,10000*10000当然会RE了,还考虑什么压缩矩阵,因为数组只有上三角部分能用到,下三角部分全是0。后来猛然醒悟,直接用sum加就好了呀,于是就过了,注意考虑k = 0的情形和题目要求至少有2个元素

class Solution {
public:
    bool checkSubarraySum(vector<int>& nums, int k) {
        int n = nums.size();
        int sum  = 0;
        for(int i = 0; i < n; i++){
            for(int j = i; j < n; j++){
                sum += nums[j];
                if(k != 0 && sum % k == 0 && j-i >= 1)
                    return true;
                else if(k == 0 && sum == k && j-i >= 1)
                    return true;
      //      cout<<"sum = "<<sum<<endl;
            } 
            sum = 0;
        }
        return false;
    }
};

560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

Example 1:
Input:nums = [1,1,1], k = 2
Output: 2
Note:
The length of the array is in range [1, 20,000].
The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
题目大意:给一个整数数组和一个整数k,求出所有连续的子串使得子串的和等于k。
解题思路:这题套路和上题一样,也是sum求和判断sum是否等于k,如果等于k,计数值count++,最后返回计数值count即为所求。
AC代码:

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        int sum  =0, count = 0;
        for(int i = 0 ; i < nums.size(); ++i){
            for(int j = i; j < nums.size(); ++j){
                sum += nums[j];
                if(sum == k)
                    count++;
            }
            sum = 0;
        }
        return count;
    }
};

713. Subarray Product Less Than K

Your are given an array of positive integers nums.

Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.

Example 1:
Input: nums = [10, 5, 2, 6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6].
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
Note:

0 < nums.length <= 50000.
0 < nums[i] < 1000.
0 <= k < 10^6.
题目大意:给一个正整数的数组,计算并打印子阵列中所有元素的乘积小于k的(连续)子阵列的数量。
一拿到题发现和前面两个题目很类似啊,就是把加法换成了乘法,前两个题的思路太顺了,这个题想当然也按照上面的思路,一通求乘积,结果,开始RE后来TLE。然后把sum换long long类型解决了RE问题,TLE始终解决不了,后来百度了一下,发现用滑动窗口,顿时捶胸顿足啊,昨天晚上才看算法教程讲过这个方法,怎么就想不到应用呢?恨自己!
题目思路:
万万没想到,这道题还是没改出来,今天又去做了笔试,人家又考后缀树了,要死。

为了再理解一下滑动窗口的用法,又拐来做几道滑动窗口相关的题,哎,为什么自己的代码能力这么弱!啥!也!不!会!

209. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.

Example:

Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
题目大意:给定n个正整数和正整数s的数组,找到连续子阵列的最小长度,其总和≥s。 如果没有,则返回0。
解题思路:滑动空间,要考虑一下数组为空的情况
AC代码:

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int low = 0, high = -1;
        int n = nums.size();
        int sum = 0, res = n+1;
        while(low < n){
            if(high + 1 < n && sum < s){
                sum += nums[++high];
            }
            else
                sum -= nums[low++];
            if(sum >= s)
                res = min(res, high-low+1);
        }
        if(res == n+1)
            return 0;
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值