当前位置:网站首页>Zuo Chengyun - Dachang question brushing class - the minimum number of exchanges of one character on the left and another character on the right
Zuo Chengyun - Dachang question brushing class - the minimum number of exchanges of one character on the left and another character on the right
2022-04-22 04:12:00 【AttackingRookie】
subject

answer
Time complexity O(n)
Ideas
Greedy Algorithm , hypothesis G On the left B On the right , The first one. G Put it in the first place , How many steps does it take , Put the second G Put it in the second position , How many steps does it take , And so on .

Code
public class Solution {
public static int minSteps(String s) {
if (null == s || s.length() == 0) {
return 0;
}
char[] chars = s.toCharArray();
int gLast = 0;
int gRes = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == 'G') {
gRes += i - gLast;
gLast++;
}
}
int bLast = 0;
int bRes = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == 'B') {
bRes += i - bLast;
bLast++;
}
}
return Math.min(gRes, bRes);
}
}
版权声明
本文为[AttackingRookie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211053430032.html
边栏推荐
- Rsync remote synchronization
- See how the project manager brings a project to ruin
- rsync远程同步
- LeetCode_矩形_困难_391.完美矩形
- 02-SparkSQL
- Pytorch uses profiler to analyze the performance of the model
- What level have you learned from your pointer? Eight pointer questions let you deepen your understanding of pointer (Part 2) - O -
- DOM事件流和事件委托
- sumo教程——公共交通教程
- 教程——sumolympics
猜你喜欢

Data mining series (2)_ The data mining plug-in of Excel connects to SQL server

Autodesk genuine Service 2020 Supprimer

交通行业提升数据利用效率的核心是做好数据交换与共享

英语 | Day11、12 x 句句真研每日一句(意思群)

How to check whether the version of oraclejdk is charged on the official website

01-Read&Write

Open source culture is still shining - in the openeuler community, with technology and idea, you are the protagonist

Exploring Presto SQL Engine (2) - Analysis of join

探究Presto SQL引擎(2)-浅析Join

Principle of average bilateral locking strategy
随机推荐
L'échange et le partage des données sont au cœur de l'amélioration de l'efficacité de l'utilisation des données dans l'industrie des transports.
iptables使用
Ronglian Qimo empowers enterprises with intelligent services and redefines the value of customer service
What level have you learned from your pointer? Eight pointer questions let you deepen your understanding of pointer (Part 2) - O -
How to solve the problem that the table association is not displayed when importing SQL from powerdesipowerdesigner
Shell programming
SQL statements used occasionally
均线双边对锁策略原理
Gaussian distribution -- Derivation in error measurement
探究Presto SQL引擎(2)-浅析Join
JS dynamically generates a table and adds a scroll bar
搜索内容入库
容联七陌赋能企业智能化服务,重新定义客服价值
Cursor iterator mode
Mysql中的Decimal类型是什么?
如何在官网查看OracleJDK那个版本是否收费
DOM事件流和事件委托
Determination of bipartite graph by coloring method
L3-022 地铁一日游 (30 分)【floyd+dfs】
Implement joint type verification of parameters in nest