首页 > 其他分享 >LeetCode 1151. 最少交换次数来组合所有的 1

LeetCode 1151. 最少交换次数来组合所有的 1

时间:2024-06-02 23:57:36浏览次数:15  
标签:swaps int 交换 1151 number 最少 ones data LeetCode

1151. 最少交换次数来组合所有的 1

给出一个二进制数组 data,你需要通过交换位置,将数组中 任何位置 上的 1 组合到一起,并返回所有可能中所需 最少的交换次数

示例 1:

输入: data = [1,0,1,0,1]
输出: 1
解释: 
有三种可能的方法可以把所有的 1 组合在一起:
[1,1,1,0,0],交换 1 次;
[0,1,1,1,0],交换 2 次;
[0,0,1,1,1],交换 1 次。
所以最少的交换次数为 1。

示例  2:

输入:data = [0,0,0,1,0]
输出:0
解释: 
由于数组中只有一个 1,所以不需要交换。

示例 3:

输入:data = [1,0,1,0,1,0,0,1,1,0,1]
输出:3
解释:
交换 3 次,一种可行的只用 3 次交换的解决方案是 [0,0,0,0,0,1,1,1,1,1,1]。

示例 4:

输入: data = [1,0,1,0,1,0,1,1,1,0,1,0,0,1,1,1,0,0,1,1,1,0,1,0,1,1,0,0,0,1,1,1,1,0,0,1]
输出: 8

提示:

  • 1 <= data.length <= 10^5
  • data[i] == 0 or 1.

提示 1

How many 1's should be grouped together ? Is not a fixed number?


提示 2

Yeah it's just the number of 1's the whole array has. Let's name this number as ones


提示 3

Every subarray of size of ones, needs some number of swaps to reach, Can you find the number of swaps needed to group all 1's in this subarray?


提示 4

It's the number of zeros in that subarray.


提示 5

Do you need to count the number of zeros all over again for every position ?


提示 6

Use Sliding Window technique.

解法:定长滑动窗口

class Solution {
    public int minSwaps(int[] data) {
        int n = data.length;
        // ones表示数组中1的数目
        int ones = 0;
        int ans = Integer.MAX_VALUE;
        // swaps表示窗口内0的数目
        int swaps = 0;
        for (int i = 0; i < n; i++) {
            ones += data[i];
        }
        if (ones <= 1) {
            return 0;
        }
        // 定长窗口,起始窗口为data[0,..,ones - 1]
        int left = 0;
        int right = 0;
        for (; right < ones - 1; right++) {
            swaps += data[right] == 0 ? 1 : 0;
        }
        while (right < n) {
            swaps += data[right] == 0 ? 1 : 0;
            ans = Math.min(ans, swaps);
            swaps -= data[left] == 0 ? 1 : 0;
            left++;
            right++;
        }
        return ans;
    }
}

复杂度分析 

  • 时间复杂度:O(n),n 是 数组data 的长度。
  • 空间复杂度:O(1)。

另一种写法:

class Solution {
    public int minSwaps(int[] data) {
        int n = data.length;
        // ones表示数组中1的数目
        int ones = 0;
        int ans = Integer.MAX_VALUE;
        // swaps表示窗口内0的数目
        int swaps = 0;
        for (int i = 0; i < n; i++) {
            ones += data[i];
        }
        int left = 0;
        int right = 0;
        while (right < n) {
            swaps += data[right] == 0 ? 1 : 0;
            if (right - left + 1 == ones) {
                ans = Math.min(ans, swaps);
                swaps -= data[left] == 0 ? 1 : 0;
                left++;
            }
            right++;
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }
}

复杂度分析 

  • 时间复杂度:O(n),n 是 数组data 的长度。
  • 空间复杂度:O(1)。

标签:swaps,int,交换,1151,number,最少,ones,data,LeetCode
From: https://blog.csdn.net/m0_56090828/article/details/139400028

相关文章

  • LeetCode 1411. Number of Ways to Paint N × 3 Grid
    原题链接在这里:https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/description/题目:Youhavea grid ofsize nx3 andyouwanttopainteachcellofthegridwithexactlyoneofthethreecolors: Red, Yellow, or Green whilemakingsuretha......
  • leetcode 60 排列序列
    排列序列已解答困难相关标签相关企业给出集合[1,2,3,...,n],其所有元素共有n!种排列。按大小顺序列出所有排列情况,并一一标记,当n=3时,所有排列如下:"123""132""213""231""312""321"给定n和k,返回第k个排列。示例1:输入:n=3,k=3输出:"213"示......
  • Leetcode 3171. Find Subarray With Bitwise AND Closest to K
    Leetcode3171.FindSubarrayWithBitwiseANDClosesttoK1.解题思路2.代码实现题目链接:3171.FindSubarrayWithBitwiseANDClosesttoK1.解题思路这道题坦率地说让我感觉很挫败,又一次没有自力搞定,是看了大佬们的答案才搞定的……知道比没有搞定更难受的......
  • leetcode-280. 摆动数组
    给你一个的整数数组nums,将该数组重新排序后使nums[0]<=nums[1]>=nums[2]<=nums[3]...简单想法排序双指针,一前一后插入贪心?猜的假定前i个已经摆动,i+1存在奇、偶两种情况奇数——若nums[i+1]>=nums[i+2]则符合条件,若nums[i+1]<nums[i+2],尝试交换......
  • LeetCode //C - 147. Insertion Sort List
    147.InsertionSortListGiventheheadofasinglylinkedlist,sortthelistusinginsertionsort,andreturnthesortedlist’shead.Thestepsoftheinsertionsortalgorithm:Insertionsortiterates,consumingoneinputelementeachrepetitionand......
  • leetcode-624.数组列表中的最大距离
    数组列表中的最大距离给定m个数组,每个数组都已经按照升序排好序了。现在你需要从两个不同的数组中选择两个整数(每个数组选一个)并且计算它们的距离。两个整数a和b之间的距离定义为它们差的绝对值|a-b|。你的任务就是去找到最大距离目标题意中的绝对值|a-b|等价于选取......
  • Leetcode 3161. 物块放置查询
    https://leetcode.cn/problems/block-placement-queries/description/有一条无限长的数轴,原点在0处,沿着x轴正方向无限延伸。给你一个二维数组queries,它包含两种操作:操作类型1:queries[i]=[1,x]。在距离原点x处建一个障碍物。数据保证当操作执行的时候,位置x处......
  • 【leetcode】——第 400 场周赛,2题选手签个到
    第一题:100307.候诊室中的最少椅子数给你一个字符串 s,模拟每秒钟的事件 i:如果 s[i]=='E',表示有一位顾客进入候诊室并占用一把椅子。如果 s[i]=='L',表示有一位顾客离开候诊室,从而释放一把椅子。返回保证每位进入候诊室的顾客都能有椅子坐的 最少 椅子数,假设候诊室......
  • leetCode.89. 格雷编码
    leetCode.89.格雷编码题目思路代码classSolution{public:vector<int>grayCode(intn){vector<int>res(1,0);//n=0时,之后一位0while(n--){//想要实现对象超下来,就从末尾开始,让vector里面加元素for(......
  • leetCode.90. 子集 II
    leetCode.90.子集II题目思路代码classSolution{public:vector<vector<int>>res;vector<int>path;vector<vector<int>>subsetsWithDup(vector<int>&nums){//先排序,让有相同元素的都放到一起sort(nums.be......