Dynamic Programming and State Compression (LeetCode #2209 & LeetCode #2212)

本文介绍了如何利用动态规划解决LeetCode中的两道题目,分别是关于最小化白色瓷砖数量和最大化射箭比赛得分的问题。通过定义二维数组dp,并结合状态压缩技巧,有效地解决了这两个问题。对于2209题,通过覆盖地毯减少白色瓷砖;对于2212题,通过优化射击策略最大化Bob的得分。
摘要由CSDN通过智能技术生成

LeetCode 2209

You are given a 0-indexed binary string floor, which represents the colors of tiles on a floor:

  • floor[i] = '0' denotes that the ith tile of the floor is colored black.
  • On the other hand, floor[i] = '1' denotes that the ith tile of the floor is colored white.

You are also given numCarpets and carpetLen. You have numCarpets black carpets, each of length carpetLen tiles. Cover the tiles with the given carpets such that the number of white tiles still visible is minimum. Carpets may overlap one another.

Return the minimum number of white tiles still visible.

At first sight, there are an infinitely huge amount of combinations available and it seems impossible to test and find out the best one. But yet again, dynamic programming comes and saves the day:

We first define a 2-D integer array: int dp[i][j], storing the minimum number of white tiles from 0-th to the i-th tile after covering j carpet. When we increment i by 1, i.e. extending the current tiles by another one, we will be finding out the minimum of:

1) Cover: Covering the current tile with a carpet, meaning no white tile from the (i - length of carpet +1) to the current one, plus the minimum

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值