题目描述
在美丽的尧山,有一个大广场,50周年校庆的时候Solo就在大广场上见证了史上最壮观的焰火。
在广场上有一排方砖是有颜色的,被涂上红色或者绿色,从左到右排列。现在校方要求重新喷涂颜色,但不一定要每一块方砖都重新喷涂,因为校方的目的是:每一块红色的方砖都至少在绿色方砖的左边(也就是每一个红的左边不能有绿的),并且尽量喷涂最少的次数。
要么是全部是R,要么是左边是R右边是G
let str = 'RGGGR';
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === 'G') count++
}
for (let i = 0; i < str.length; i++) {
let leftCount = 0,
rightCount = 0;
for (j = 0; j < i; j++) {
if (str[j] === 'G') leftCount++
}
for (j = i; j < str.length; j++) {
if (str[j] === 'R') rightCount++
}
if (leftCount + rightCount < count) count = leftCount + rightCount
}
console.log(count)
PS: 这个至少真的是误导人, 还有喷涂最少的次数,应该是喷涂最少的方砖