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

ops/2025/2/22 6:11:40/

问题背景

给你一个下标从 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];}
}

http://www.ppmy.cn/ops/160440.html

相关文章

AI基础:数据可视化简易入门(Matplotlib和Seaborn)

Matplotlib是一个Python的2D绘图库&#xff0c;它以各种硬拷贝和跨平台的交互式环境生成出版质量级别的图形。 Seaborn是基于Python且非常受欢迎的图形可视化库&#xff0c;在Matplotlib的基础上进行了更高级别的封装&#xff0c;使作图更加方便快捷。 1 Matplotlib 1.1 通过…

2024年国赛高教杯数学建模A题板凳龙闹元宵解题全过程文档及程序

2024年国赛高教杯数学建模 A题 板凳龙闹元宵 原题再现 “板凳龙”&#xff0c;又称“盘龙”&#xff0c;是浙闽地区的传统地方民俗文化活动。人们将少则几十条&#xff0c;多则上百条的板凳首尾相连&#xff0c;形成蜿蜒曲折的板凳龙。盘龙时&#xff0c;龙头在前领头&#x…

web安全:跨站请求伪造 (CSRF)

跨站请求伪造 (CSRF) ​ 跨站请求伪造&#xff08;CSRF&#xff0c;Cross-Site Request Forgery&#xff09; 是一种网络攻击方式&#xff0c;攻击者诱使受害者在未经其授权的情况下执行特定操作。CSRF 利用受害者已登录的身份和浏览器自动发送的认证信息&#xff08;如 Cooki…

TTL和CMOS的区别【数电速通】

CMOS电平&#xff1a;电压范围在3&#xff5e;15V&#xff1b;常见电压在12V。 TTL电平&#xff1a;电压范围在0&#xff5e;5V&#xff0c;常见都是5V CMOS的特点&#xff1a;电平由电源VDD​ 决定&#xff0c;而不是外部电源电平。 COMS电路的使用注意事项 我们在使用CMOS…

AI赋能传统系统:Spring AI Alibaba如何用大模型重构机票预订系统?

零、效果 一、启动项目 1.0、前置条件 Java 17Node.js 18获取 API key 具体文档&#xff1a;获取APP ID和Workspace ID_大模型服务平台百炼(Model Studio)-阿里云帮助中心 1.1、下载代码 Github地址&#xff1a;https://github.com/springaialibaba/spring-ai-alibaba-exa…

深度探索:DeepSeek与鸿蒙HarmonyOS应用开发的深度融合

文章目录 一、概述1.1 什么是DeepSeek&#xff1f;1.2 鸿蒙HarmonyOS的特点 二、技术优势与应用场景2.1 技术优势2.2 应用场景 三、开发指南3.1 环境搭建3.2 集成AI模型3.3 分布式任务调度 四、实际案例分析4.1 智能家居控制4.2 智能健康监测 五、未来展望《AI智能化办公&#…

Memcached(主主复制与keepalive高可用)

案例环境 cache1&#xff1a;192.168.180.144 cache2&#xff1a;192.168.180.145 cache-api&#xff1a;192.168.180.143 案例过程 前置准备 关闭所有设备防火墙 systemctl stop firewalld && setenforce 0 更改主机名 hostnamectl set-hostname cache1 &…

linux 搭建nfs服务(共享文件夹)

linux 搭建nfs服务&#xff08;共享文件夹&#xff09; 要在Linux服务器上创建一个共享目录&#xff0c;并让macOS可以访问和复制拷贝文件&#xff0c;你可以按照以下步骤操作&#xff1a; 这个以ip为192.168.3.200的ubuntu 2022.04的服务器为示例 nfs 服务安装 在Linux服务…