当前位置: 首页 > news >正文

【Leetcode 每日一题】2209. 用地毯覆盖后的最少白色砖块

问题背景

给你一个下标从 0 0 0 开始的 二进制 字符串 f l o o r floor floor,它表示地板上砖块的颜色。

  • f l o o r [ i ] floor[i] floor[i] 为 ‘0’ 表示地板上第 i i i 块砖块的颜色是 黑色
  • f l o o r [ i ] floor[i] floor[i] 为’1’ 表示地板上第 i i i 块砖块的颜色是 白色

同时给你 n u m C a r p e t s numCarpets numCarpets c a r p e t L e n carpetLen carpetLen。你有 n u m C a r p e t s numCarpets numCarpets黑色 的地毯,每一条 黑色 的地毯长度都为 c a r p e t L e n carpetLen carpetLen 块砖块。请你使用这些地毯去覆盖砖块,使得未被覆盖的剩余 白色 砖块的数目 最小 。地毯相互之间可以覆盖。
请你返回没被覆盖的白色砖块的 最少 数目。

数据约束

  • 1 ≤ c a r p e t L e n ≤ f l o o r . l e n g t h ≤ 1000 1 \le carpetLen \le floor.length \le 1000 1carpetLenfloor.length1000
  • f l o o r [ i ] floor[i] floor[i]要么是 ‘0’ ,要么是 ‘1’ 。
  • 1 ≤ n u m C a r p e t s ≤ 1000 1 \le numCarpets \le 1000 1numCarpets1000

解题过程

比较标准的动态规划模板题,关键是定义清楚状态,这里用 i i i表示剩余的地毯数量, j j j表示剩余的砖块数量。
空间优化的做法没完全理解,先不要求。

具体实现

递归

class Solution {
    public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
        int n = floor.length();
        int[][] memo = new int[numCarpets + 1][n];
        for (int[] row : memo) {
            Arrays.fill(row, -1);
        }
        return dfs(numCarpets, n - 1, floor.toCharArray(), memo, carpetLen);
    }

    private int dfs(int i, int j, char[] floor, int[][] memo, int carpetLen) {
        if (j < carpetLen * i) {
            return 0;
        }
        if (memo[i][j] != -1) {
            return memo[i][j];
        }
        int res = dfs(i, j - 1, floor, memo, carpetLen) + floor[j] - '0';
        if (i > 0) {
            res = Math.min(res, dfs(i - 1, j - carpetLen, floor, memo, carpetLen));
        }
        return memo[i][j] = res;
    }
}

递推

class Solution {
    public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
        char[] chF = floor.toCharArray();
        int n = chF.length;
        int[][] dp = new int[numCarpets + 1][n];
        dp[0][0] = chF[0] - '0';
        for (int j = 1; j < n; j++) {
            dp[0][j] = dp[0][j - 1] + chF[j] - '0';
        }
        for (int i = 1; i <= numCarpets; i++) {
            for (int j = carpetLen * i; j < n; j++) {
                dp[i][j] = Math.min(dp[i][j - 1] + chF[j] - '0', dp[i - 1][j - carpetLen]);
            }
        }
        return dp[numCarpets][n - 1];
    }
}

相关文章:

  • 多模态情感分析
  • ArcGIS Pro制作人口三维地图教程
  • 深度学习(2)-深度学习关键网络架构
  • 《Keras 3 :使用 Vision Transformers 进行物体检测》:此文为AI自动翻译
  • GitCode 助力至善云学:构建智慧教育平台
  • 053 性能压测 单机锁 setnx
  • buu-[OGeek2019]babyrop-好久不见41
  • C++ 设计模式-状态模式
  • 在s32ds for platform平台debug编译能正常编译,但是切换到release编译时报错
  • DeepSeek vs ChatGPT:AI 领域的华山论剑,谁主沉浮?
  • Uniapp判断设备是安卓还是 iOS,并调用不同的方法
  • 了解大数据
  • 虚拟机的创建及配置
  • Lineageos 22.1(Android 15)Launcer简单调整初始化配置
  • Qt学习(六) 软件启动界面 ,注册表使用 ,QT绘图, 视图和窗口绘图,Graphics View绘图框架:简易CAD
  • 数据库索引:缺点与类型全解析
  • CSS 布局技术深度解析:从传统到现代的核心布局方案
  • Arm64架构CentOS7服务器搭建Fabric环境
  • RPC:分布式系统的通信桥梁
  • 毕业项目推荐:基于yolov8/yolov5/yolo11的番茄成熟度检测识别系统(python+卷积神经网络)
  • 持续降雨存在落石风险,贵州黄果树景区水帘洞将封闭至6月初
  • 上海高院与上海妇联签协议,建立反家暴常态化联动协作机制
  • 魔都眼|锦江乐园摩天轮“换代”开拆,新摩天轮暂定118米
  • 92岁上海交大退休教师捐赠百万元给学校,其父也曾设奖学金
  • 中国结算澄清“严查场外配资”传闻:账户核查为多年惯例,无特殊安排
  • 四部门:强化汛期农业防灾减灾,奋力夺取粮食和农业丰收