2024/10/14 灵茶题单 滑动窗口
This commit is contained in:
parent
266e67f8be
commit
f0d53cc2be
|
@ -0,0 +1,37 @@
|
|||
package com.cool.ling_cha_mount.sliding_windows;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/10/14/11:10
|
||||
* @Description: 2134. 最少交换次数来组合所有的 1 II
|
||||
* Level 4
|
||||
* Hard 2
|
||||
* Score 1748
|
||||
*/
|
||||
public class Num2134 {
|
||||
|
||||
public int minSwaps(int[] nums) {
|
||||
int count = 0;
|
||||
for (int num : nums) {
|
||||
count += num;
|
||||
}
|
||||
int num = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
num += nums[i];
|
||||
}
|
||||
int res = num;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
num -= nums[i];
|
||||
int right = i + count;
|
||||
if (right >= nums.length) {
|
||||
right -= nums.length;
|
||||
}
|
||||
num += nums[right];
|
||||
res = Math.max(res, num);
|
||||
}
|
||||
return count - res;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue