Leetcode Weekly Contest 275(暴力、滑动窗口、位掩码、贪心)

题目链接: Leetcode Weekly Contest 275

1、2133. Check if Every Row and Column Contains All Numbers

难度:Easy

思路

暴力求解。

代码:

class Solution {
    public boolean checkValid(int[][] matrix) {
        int n=matrix.length;
        for(int i=0;i<matrix.length;i++){
            Set<Integer> set=new HashSet<>();
            for(int j=0;j<matrix[0].length;j++){
                if(matrix[i][j]>=1&&matrix[i][j]<=n&&!set.contains(matrix[i][j])){
                    set.add(matrix[i][j]);
                }
                else{
                    return false;
                }
            }
        }
        for(int j=0;j<n;j++){
            Set<Integer> set=new HashSet<>();
            for(int i=0;i<n;i++){
                if(matrix[i][j]>=1&&matrix[i][j]<=n&&!set.contains(matrix[i][j])){
                    set.add(matrix[i][j]);
                }
                else{
                    return false;
                }
            }
        }
        return true;
    }
}

2、2134. Minimum Swaps to Group All 1’s Together II

难度:Medium

思路:

因为数组时首尾相接的,所有将数组复制一份接到原数组的尾部。然后用滑动窗口进行求解。

代码:

class Solution {
    public int minSwaps(int[] A) {
        int oneNum=0;
        for(int n:A){
            if(n==1){
                oneNum++;
            }
        }
        int[] nums=new int[A.length*2];
        for(int i=0;i<A.length;i++){
            nums[i]=A[i];
            nums[i+A.length]=A[i];
        }
        //寻找长度为oneNum的滑动窗口中最多有几个1
        int max=0;//最长连续1的长度
        int curr=0;
        for(int i=0;i<oneNum;i++){
            if(nums[i]==1){
                curr++;
            }
        }
        max=curr;
        for(int i=oneNum;i<nums.length;i++){
            if(nums[i]==1){
                curr++;
            }
            if(nums[i-oneNum]==1){
                curr--;
            }
            max=Math.max(max,curr);
        }
        return oneNum-max;
    }
}

3、2135. Count Words Obtained After Adding a Letter

难度:Medium

思路:

将字符串转化成二进制数。

代码

class Solution {
    public int wordCount(String[] startWords, String[] targetWords) {
        Set<Integer> set=new HashSet<>();
        for(String s:startWords){
            int n=0;
            for(char c:s.toCharArray()){
                n=n+(1<<(c-'a'));
            }
            set.add(n);
        }
        int res=0;
        for(String s:targetWords){
            int n=0;
            for(char c:s.toCharArray()){
                n=n+(1<<(c-'a'));
            }
            for(int i=0;i<26;i++){
                if((n&(1<<i))>0){
                    int temp=n-(1<<i);
                    if(set.contains(temp)){
                        res++;
                        break;
                    }
                }
            }
        }
        return res;
    }
}

4、2136. Earliest Possible Day of Full Bloom

难度:Hard

思路

贪心,生长时间较久的种子先种。

代码

class Solution {
    public int earliestFullBloom(int[] plantTime, int[] growTime) {
        int n=plantTime.length;
        int[][] a=new int[n][2];
        for(int i=0;i<n;i++){
            a[i][0]=plantTime[i];
            a[i][1]=growTime[i];
        }
        Arrays.sort(a,(x,y)->y[1]-x[1]);//按growTime从大到小排序
        //贪心思想,growTime长的种子先种下
        int total=0;
        int curr=0;//记录planTime
        for(int i=0;i<n;i++){
            total=Math.max(total,curr+a[i][0]+a[i][1]);
            curr+=a[i][0];
        }
        return total;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值