Day9力扣打卡

打卡记录

掷骰子等于目标和的方法数(动态规划)

链接

用 f[i][j] 表示投了 i 次投骰子得到点数总和,从而得到状态转移方程 f[i][j]=f[i−1][j]+f[i−1][j−1]+⋯+f[i−1][j−min(k−1,j)] 。

cpp 复制代码
class Solution {
public:
    int numRollsToTarget(int n, int k, int target) {
        const int MOD = 1e9 + 7;
        vector<int> f(target + 1, 0);
        f[0] = 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = target; j >= 0; --j) {
                f[j] = 0;
                for (int x = 1; x <= k && x <= j; ++x)
                    f[j] = (f[j] + f[j - x]) % MOD;
            }
        }
        return f[target];
    }
};

长度最小的子数组(双指针 滑动窗口)

链接

满足条件就排出前面的元素,保证满足条件的窗口,然后求解每回窗口坐标的最小值。

cpp 复制代码
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size(), sum = 0, ans = 0x3f3f3f3f;
        for (int i = 0, j = 0; i < n; ++i) {
            sum += nums[i];
            while (j < n && sum >= target) {
                ans = min(ans, i - j + 1);
                sum -= nums[j++];
            }
        }
        return ans == 0x3f3f3f3f ? 0 : ans;
    }
};
相关推荐
霍田煜熙2 分钟前
CBMS最新源码
算法
NAGNIP14 分钟前
主流的激活函数有哪些?
算法
NAGNIP16 分钟前
Self-Attention 为什么要做 QKV 的线性变换?又为什么要做 Softmax?
算法
希望_睿智25 分钟前
实战设计模式之中介者模式
c++·设计模式·架构
core51232 分钟前
PageRank 算法:互联网的“人气投票”
算法·pagerank
小白菜又菜35 分钟前
Leetcode 1523. Count Odd Numbers in an Interval Range
算法·leetcode
你们补药再卷啦1 小时前
人工智能算法概览
人工智能·算法
cnxy1881 小时前
围棋对弈Python程序开发完整指南:步骤3 - 气(Liberties)的计算算法设计
python·算法·深度优先
AndrewHZ1 小时前
【图像处理基石】什么是光栅化?
图像处理·人工智能·算法·计算机视觉·3d·图形渲染·光栅化
小白菜又菜1 小时前
Leetcode 944. Delete Columns to Make Sorted
算法·leetcode