首页 > 其他分享 >[LeetCode] 1578. Minimum Time to Make Rope Colorful

[LeetCode] 1578. Minimum Time to Make Rope Colorful

时间:2023-12-28 09:04:02浏览次数:39  
标签:Colorful neededTime int Make rope colors 1578 Bob balloons

Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the ith balloon.

Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful. You are given a 0-indexed integer array neededTime where neededTime[i] is the time (in seconds) that Bob needs to remove the ith balloon from the rope.

Return the minimum time Bob needs to make the rope colorful.

Example 1:
Input: colors = "abaac", neededTime = [1,2,3,4,5]
Output: 3
Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
Bob can remove the blue balloon at index 2. This takes 3 seconds.
There are no longer two consecutive balloons of the same color. Total time = 3.

Example 2:
Input: colors = "abc", neededTime = [1,2,3]
Output: 0
Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.

Example 3:
Input: colors = "aabaa", neededTime = [1,2,3,4,1]
Output: 2
Explanation: Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.

Constraints:
n == colors.length == neededTime.length
1 <= n <= 105
1 <= neededTime[i] <= 104
colors contains only lowercase English letters.

使绳子变成彩色的最短时间。

Alice 把 n 个气球排列在一根绳子上。给你一个下标从 0 开始的字符串 colors ,其中 colors[i] 是第 i 个气球的颜色。
Alice 想要把绳子装扮成 彩色 ,且她不希望两个连续的气球涂着相同的颜色,所以她喊来 Bob 帮忙。Bob 可以从绳子上移除一些气球使绳子变成 彩色 。给你一个下标从 0 开始的整数数组 neededTime ,其中 neededTime[i] 是 Bob 从绳子上移除第 i 个气球需要的时间(以秒为单位)。
返回 Bob 使绳子变成 彩色 需要的 最少时间 。

思路

思路是贪心。对于任何两个连续的气球,如果他们的颜色相同,则需要去除 neededTime 更多的那个。
具体的实现上,对于任何两个连续的气球 i 和 i + 1,如果 i 的 neededTime 更多,则我们在累加 i + 1 的 neededTime 的同时,将 i 和 i + 1 的位置 swap 一下,这样就能使 i 可以跟后面的 i + 2 继续比较了。

复杂度

时间O(n)
空间O(1)

代码

Java实现

class Solution {
    public int minCost(String colors, int[] neededTime) {
        int n = colors.length();
        int res = 0;
        for (int i = 0; i < n - 1; i++) {
            if (colors.charAt(i) == colors.charAt(i + 1)) {
                res += Math.min(neededTime[i], neededTime[i + 1]);
                if (neededTime[i] > neededTime[i + 1]) {
                    swap(neededTime, i, i + 1);
                }
            }
        }
        return res;
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

标签:Colorful,neededTime,int,Make,rope,colors,1578,Bob,balloons
From: https://www.cnblogs.com/cnoodle/p/17931875.html

相关文章

  • cmake管理qt项目,设置windows和linux下生成的程序图标,以及任务栏显示设置的图标
    先代码设置MainWindow图标://主要用于在linux下运行程序时,在任务栏显示图标MainWindoww;w.setWindowIcon(QIcon(":/res/icon.png"));(*windows下设置生成的exe程序的ico图标后,默认也会对运行程序时任务栏的图标也设置成这个ico,但是同样的代码拿到linux下就无效,需要其他方......
  • B. Make Almost Equal With Mod
    原题链接题解,看完你对最大公约数,求余一定有更深的认识事实1.当序列中有奇数又有偶数时,2就是那个k事实2.当\(a[i]\mod\b=c,\foralli\in[1,n]\)时$a[i]\mod\2b=c\,if(a[i]//b)==$偶数$a[i]\mod\2b=c+b\,if(a[i]//b)==$奇数事实3.如上,对......
  • cargo-make rust 任务执行以及构建工具
    再学习nakago框架的时候发现其使用了cargo-make这个工具,但是很方便,类似make的构建模式包含的特性依赖管理,别名支持,支持workspace简单使用安装cargoinstall--forcecargo-make参考使用创建一个cargo项目 cargonewappdemoMakefile.toml文件cargonewappdemoMakefile.to......
  • Xmake v2.8.6 发布,新的打包插件:XPack
    Xmake是一个基于Lua的轻量级跨平台构建工具。它非常的轻量,没有任何依赖,因为它内置了Lua运行时。它使用xmake.lua维护项目构建,相比makefile/CMakeLists.txt,配置语法更加简洁直观,对新手非常友好,短时间内就能快速入门,能够让用户把更多的精力集中在实际的项目开发上。我们......
  • 初中英语优秀范文100篇-038Should Students Make Firiends Online?学生应该在线交友吗
    PDF格式公众号回复关键字:SHCZFW038记忆树1Nowadays,manyteenagersshowagreatinterestinmakingfriendsonline.翻译现如今,许多青少年对于在网上交朋友表现出很大的兴趣。简化记忆兴趣句子结构1"Nowadays"是一个副词,表示这个句子描述的是现在的情景。2"man......
  • Windows环境 CMake 配置C++调用Python
    #CMakeLists.txtadd_library(python3STATICIMPORTED)#这里是使用python的安装路径set_target_properties(python3PROPERTIESIMPORTED_LOCATION"D:/python/libs/python39.lib")#使用python的静态库target_link_libraries(TestDemo......
  • SAP-DB-服务器组-003-pacemaker集群-在AWS平台里-创建及配置-SAPHanaTopology资源及SA
    关于基础环境的安装,还是可以参考笔者另一篇文章,APP的部分《SAP-APP-服务器组-001-pacemaker集群的基础环境的安装部署》https://www.cnblogs.com/5201351/p/17899446.html 1、DB需要多安装  resource-agents-sap-hana[root@db01qq-5201351]#yuminstall-yresource-ag......
  • cmake应用:集成gtest进行单元测试
    编写代码有bug是很正常的,通过编写完备的单元测试,可以及时发现问题,并且在后续的代码改进中持续观测是否引入了新的bug。对于追求质量的程序员,为自己的代码编写全面的单元测试是必备的基础技能,在编写单元测试的时候也能复盘自己的代码设计,是提高代码质量极为有效的手段。在本系......
  • cpp环境搭建 - vs2017编译CMakeLists项目(Box2dLite)
    box2dlite地址:GitHub-erincatto/box2d-lite:Asmall2Dphysicsengine vs2017不支持utf-8withoutbom问题box2dlite的源码文件是utf-8withoutbom的,如果在里面写了中文注释,就会出现编译错误解决办法:将文件编码改成utf-8带bom的(这边没有在附加选项加/utf-8貌似也没问题......
  • cargo-make rust 任务执行以及构建工具
    再学习nakago框架的时候发现其使用了cargo-make这个工具,但是很方便,类似make的构建模式包含的特性依赖管理,别名支持,支持workspace简单使用安装cargoinstall--forcecargo-make参考使用创建一个cargo项目 cargonewappdemo......