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

分类讨论 3800

3800. Minimum Cost to Make Two Binary Strings Equal

You are given two binary strings s and t, both of length n, and three positive integers flipCostswapCost, and crossCost.

You are allowed to apply the following operations any number of times (in any order) to the strings s and t:

  • Choose any index i and flip s[i] or t[i] (change '0' to '1' or '1' to '0'). The cost of this operation is flipCost.
  • Choose two distinct indices i and j, and swap either s[i] and s[j] or t[i] and t[j]. The cost of this operation is swapCost.
  • Choose an index i and swap s[i] with t[i]. The cost of this operation is crossCost.

Return an integer denoting the minimum total cost needed to make the strings s and t equal.

Example 1:

Input: s = "01000", t = "10111", flipCost = 10, swapCost = 2, crossCost = 2

Output: 16

Explanation:

We can perform the following operations:

  • Swap s[0] and s[1] (swapCost = 2). After this operation, s = "10000" and t = "10111".
  • Cross swap s[2] and t[2] (crossCost = 2). After this operation, s = "10100" and t = "10011".
  • Swap s[2] and s[3] (swapCost = 2). After this operation, s = "10010" and t = "10011".
  • Flip s[4] (flipCost = 10). After this operation, s = t = "10011".

The total cost is 2 + 2 + 2 + 10 = 16.

Example 2:

Input: s = "001", t = "110", flipCost = 2, swapCost = 100, crossCost = 100

Output: 6

Explanation:

Flipping all the bits of s makes the strings equal, and the total cost is 3 * flipCost = 3 * 2 = 6.

Example 3:

Input: s = "1010", t = "1010", flipCost = 5, swapCost = 5, crossCost = 5

Output: 0

Explanation:

The strings are already equal, so no operations are required. 

Constraints:

  • n == s.length == t.length
  • 1 <= n <= 105​​​​​​​
  • 1 <= flipCost, swapCost, crossCost <= 109
  • s and t consist only of the characters '0' and '1'.
class Solution:def minimumCost(self, s: str, t: str, flipCost: int, swapCost: int, crossCost: int) -> int:# scenario1 全部用flipdiff = sum([s[i] != t[i] for i in range(len(s))])flip = diff * flipCost# scenario2 尽量用swap,然后用flips_zero = sum([s[i] != t[i] and s[i] == '0' for i in range(len(s))])t_zero = diff - s_zerosmaller, bigger = min(s_zero, t_zero), max(s_zero, t_zero)swap = smaller * swapCost + (diff - smaller * 2) * flipCost# scenario3 先用crossswap,再用swap,然后用flipcross_count = (bigger - smaller) // 2smaller += cross_countbigger -= cross_countcross = cross_count * crossCost + smaller * swapCost + (diff - smaller * 2) * flipCostreturn min(flip, swap, cross)'''
3种情况:
1. 都用flip
2. swap + flip 
3. crossswap + flip100000
001111s:01000
t:01111
diff = 5
s_0 = 1
t_0 = 410000
01111
diff = 5
s_0 = 1
t_0 = 4smaller = 1
bigger = 5
cross = (bigger - smaller) // 211000
00111'''

 

http://www.jsqmd.com/news/197229/

相关文章:

  • 企业级语音转写解决方案:基于Fun-ASR构建私有化系统
  • 如何通过去耦电容提升PLC抗干扰能力:项目应用详解
  • 2025年12月江苏徐州智慧电力系统服务商选型指南 - 2025年品牌推荐榜
  • 高效语音转文字:Fun-ASR + GPU加速完整实践教程
  • 热词功能显著提升专业术语识别率——Fun-ASR应用案例分享
  • C# Avalonia 19- DataBinding- CheckBoxList
  • 网盘直链下载助手搭配Fun-ASR:批量处理云端音频文件
  • Zoho Projects全生命周期:覆盖从构思到交付
  • 爆火!7款AI写论文神器半天生成3万字,真实参考文献!
  • 开源语音识别新星Fun-ASR:中文转写准确率提升50%
  • Yesware销售赋能:跟踪客户打开情况
  • Drip电商营销:促进GPU算力复购
  • SalesLoft客户旅程编排:提高成交率
  • Pspice安装一步到位:电力电子模型库加载指南
  • 深度剖析CCS软件中的断点与变量监控功能
  • 钉钉通义联合推出Fun-ASR,支持31种语言语音识别
  • AUTOSAR网络管理唤醒原理通俗解释
  • NVIDIA驱动版本要求:CUDA 11.8+才能启用GPU加速
  • 电子玩具发声秘籍:51单片机驱动蜂鸣器演奏歌曲
  • Pure Chat免安装:JS代码直接嵌入
  • Baidu AI Cloud文心大模型:对比竞品优势
  • MindMaster思维导图:梳理Fun-ASR功能结构
  • 从零实现Packet Tracer汉化(Windows环境)
  • JavaScript——文件处理工具函数
  • JavaScript——防抖节流工具函数
  • ActiveCampaign个性化旅程:根据行为触发动作
  • Wrike任务依赖分析:确保关键路径顺畅
  • Windows下解决未知usb设备(设备描述)的深度剖析
  • 如何在工业网关中集成RS485和RS232通信协议:项目应用
  • Sendinblue短信补充:重要通知不遗漏