五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

LeetCode 2134. Minimum Swaps to Group All 1's Together II

2023-04-23 17:00 作者:您是打尖兒還是住店呢  | 我要投稿

A?swap?is defined as taking two?distinct?positions in an array and swapping the values in them.

A?circular?array is defined as an array where we consider the?first?element and the?last?element to be?adjacent.

Given a?binary?circular?array?nums, return?the minimum number of swaps required to group all?1's present in the array together at?any location.

?

Example 1:

Input: nums = [0,1,0,1,1,0,0]

Output: 1

Explanation:?

Here are a few of the ways to group all the 1's together:?

[0,0,1,1,1,0,0] using 1 swap.?

[0,1,1,1,0,0,0] using 1 swap.?

[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).?

There is no way to group all 1's together with 0 swaps.?

Thus, the minimum number of swaps required is 1.

Example 2:

Input: nums = [0,1,1,1,0,0,1,1,0]

Output: 2

Explanation: Here are a few of the ways to group all the 1's together:?

[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).?

[1,1,1,1,1,0,0,0,0] using 2 swaps. There is no way to group all 1's together with 0 or 1 swaps. Thus, the minimum number of swaps required is 2.

Example 3:

Input: nums = [1,1,0,0,1]

Output: 0

Explanation: All the 1's are already grouped together due to the circular property of the array. Thus, the minimum number of swaps required is 0.

?

Constraints:

  • 1 <= nums.length <= 105

  • nums[i]?is either?0?or?1.

因?yàn)槭黔h(huán)形數(shù)組,所以只要判斷每個(gè)index開(kāi)始,內(nèi)部有多少個(gè)1就行,依次存儲(chǔ),最后返回sum-max即可;

題目不太難,但是跑出來(lái)時(shí)間太慢了,肯定還有更好的方法;

下面是代碼:

Runtime:?50 ms, faster than?5.12%?of?Java?online submissions for?Minimum Swaps to Group All 1's Together II.

Memory Usage:?61 MB, less than?5.12%?of?Java?online submissions for?Minimum Swaps to Group All 1's Together II.


LeetCode 2134. Minimum Swaps to Group All 1's Together II的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
祁阳县| 治县。| 铁岭市| 九寨沟县| 澄城县| 奈曼旗| 乃东县| 苍溪县| 秦安县| 崇义县| 普兰店市| 鲁甸县| 新邵县| 惠安县| 中江县| 博湖县| 永登县| 安岳县| 宝坻区| 胶南市| 冷水江市| 若羌县| 渭南市| 尉氏县| 旅游| 长寿区| 泸西县| 黔西| 子长县| 黑河市| 如东县| 上虞市| 贡嘎县| 迁安市| 光山县| 喀喇沁旗| 仲巴县| 客服| 峨眉山市| 盘山县| 扶风县|