首页 > 其他分享 >2712. Minimum Cost to Make All Characters Equal (Medium)

2712. Minimum Cost to Make All Characters Equal (Medium)

时间:2023-06-19 10:25:01浏览次数:56  
标签:2712 index Medium Make obtain cost characters Apply operation

Description

2712. Minimum Cost to Make All Characters Equal (Medium) You are given a 0-indexed binary string s of length n on which you can apply two types of operations:

  • Choose an index i and invert all characters from index 0 to index i (both inclusive), with a cost of i + 1
  • Choose an index i and invert all characters from index i to index n - 1 (both inclusive), with a cost of n - i

Return the minimum cost to make all characters of the string equal.

Invert a character means if its value is '0' it becomes '1' and vice-versa.

Example 1:

Input: s = "0011"
Output: 2
Explanation: Apply the second operation with i = 2 to obtain s = "0000" for a cost of 2. It can be
shown that 2 is the minimum cost to make all characters equal.

Example 2:

Input: s = "010101"
Output: 9
Explanation: Apply the first operation with i = 2 to obtain s = "101101" for a cost of 3.
Apply the first operation with i = 1 to obtain s = "011101" for a cost of 2.
Apply the first operation with i = 0 to obtain s = "111101" for a cost of 1.
Apply the second operation with i = 4 to obtain s = "111110" for a cost of 2.
Apply the second operation with i = 5 to obtain s = "111111" for a cost of 1.
The total cost to make all characters equal is 9. It can be shown that 9 is the minimum cost to make
all characters equal.

Constraints:

  • 1 <= s.length == n <= 10⁵
  • s[i] is either '0' or '1'

Solution

While traversing the string, when encountering a situation where s[i] is not equal to s[i + 1], we need to reverse the string. We can choose to execute either the first or second approach, opting for the one with the lower cost.

By following this procedure, the characters to the left of s[i], including s[i] itself, will all be identical.

Code

class Solution {
public:
    long long minimumCost(string s) {
    	long long res = 0;
    	int n = s.size();
    	for (int i = 0; i < n - 1; ++i) {
    		if (s[i] != s[i + 1]) {
    			res += min(i + 1, n - i - 1);
    		}
    	}
        return res;
    }
};

标签:2712,index,Medium,Make,obtain,cost,characters,Apply,operation
From: https://www.cnblogs.com/zwyyy456/p/17490449.html

相关文章

  • CMake官网教程学习
    简介本文档是根据CMake的官方教程学习的笔记,同时将教程中C++实现的代码更改为C语言实现。当前还未学习完。教程官网:CMakeTutorial—CMake3.27.0-rc1Documentation中文教程:教程—CMake3.26.4Documentation(cmake-doc.readthedocs.io)官方教程源码下载:https://cmake.o......
  • 2712.minimum Cost to Make All Characters Equal
    Description2712.MinimumCosttoMakeAllCharactersEqual(Medium)Youaregivena0-indexedbinarystringsoflengthnonwhichyoucanapplytwotypesofoperations:Chooseanindexiandinvertallcharactersfromindex0toindexi(bothinclusive......
  • cmake 常用操作
    #打印变量出来看execute_process(COMMAND${CMAKE_COMMAND}-Eecho"hbbdebuginfoPROJECT_VERSION=${PROJECT_VERSION}PROJECT_SOURCES=${PROJECT_SOURCES}MACOSX_BUNDLE_BUNDLE_VERSION=${PROJECT_VERSION}MACOSX_BUNDLE_......
  • Makefile编写模板 & 学习笔记
    一、模板#伪命令.PHONY:cleancompileSocompileExerun:compileExe@./maincompileExe:compileSo@g++main.cpp-Llib-lsoowCapture-lcamapi-lpthread=lImageProc-ljpeg-lhv_static-omaincompileSo:@g++fPIC-sharedsoowCapture.cpp-Iinclu......
  • CMake个人理解和使用
    前言CMake是一个构建工具,通过它可以很容易创建跨平台的项目。通常使用它构建项目要分两步,通过源代码生成工程文件,通过工程文件构建目标产物(可能是动态库,静态库,也可能是可执行程序)。使用CMake的一个主要优势是在多平台或者多人协作的项目中,开发人员可以根据自己的喜好来使选择IDE,......
  • CMake
    转载说明:以下内容来自从零开始详细介绍CMakeCMake说明cmake的定义是什么?-----高级编译配置工具当多个人用不同的语言或者编译器开发一个项目,最终要输出一个可执行文件或者共享库(dll,so等等)这时候神器就出现了-----CMake!所有操作都是通过编译CMakeLists.txt来完成的—简单......
  • CMakeLists --- 指定安装目录 CMAKE_INSTALL_PREFIX
    cmake指定makeinstall时的安装目录:通过设置CMAKE_INSTALL_PREFIX的值来控制。有两种方法:1.在执行cmake时,指定安装目录:cmake-DCMAKE_INSTALL_PREFIX=/xxx/x..2.直接在CMakeLists.txt中设置set(CMAKE_INSTALL_PREFIX/xxx/x) 编译完成后,执行makeinstall即可。......
  • CMakeLists --- 设置rpath_link方法 编译报错try using -rpath or -rpath-link)
    指令:add_link_options("LINKER:-rpath-link,${THIRD_LIBS_DIR}")THIRD_LIBS_DIR:需要链接的库的目录作用:编译生成一个可执行文件时,依赖一个动态库A,动态库A同时又依赖动态库B.如果我们没有显示集成动态库B时,链接器会去-rpath-link设置的目录中寻找依赖项。 例子:1.库A,依赖库B......
  • vscode+cmake c++ hello world!
    1.新建一个测试目录hello及一些必要文件D:\HELLO\HELLOCPP│CMakeLists.txt└─main.cppCMakeLists.txt#工程名project(Hello)#生成目标add_executable(Hellomain.cpp)hello.cpp#include<iostream>usingnamespacestd;intmain(){cout<<"hellowo......
  • CMake命令行添加编译参数
    CMake命令行添加编译参数学习自coroserver例程:https://github.com/windoze/coroservercoroserver是一个应用Boost.Asio和Boost.Coroutine的多线程TCP服务器。README中有编译命令行示例:`CXXFLAGS="-std=c++11-stdlib=libc++"LDFLAGS="-stdlib=libc++"cmake[options]pa......