Leetcode Weekly Contest 257(暴力、排序、动态递归)

题目链接: Leetcode Weekly Contest 257

1、1995. Count Special Quadruplets

难度:Easy

思路:

暴力。

代码:

class Solution {
    public int countQuadruplets(int[] nums) {
        int n=nums.length;
        int res=0;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                for(int k=j+1;k<n;k++){
                    for(int l=k+1;l<n;l++){
                        if(nums[i]+nums[j]+nums[k]==nums[l]){
                            res++;
                        }
                    }
                }
            }
        }
        return res;
    }
}

2、1996. The Number of Weak Characters in the Game

难度:Medium

思路:

排序,排序的方式比较巧妙。

class Solution {
    public int numberOfWeakCharacters(int[][] nums) {
        Arrays.sort(nums,(a,b)->a[0]!=b[0]?b[0]-a[0]:a[1]-b[1]);
        int res=0,max=0;
        for(int[] num:nums){
            if(num[1]<max){
                res++;
            }
            max=Math.max(max,num[1]);
        }
        return res;
    }
}

3、1997. First Day Where You Have Been in All the Rooms

难度:Medium

思路:

动态规划

代码

class Solution {
    public int firstDayBeenInAllRooms(int[] A) {
        int n=A.length;
        int mod=1000_000_007;
        long[] dp=new long[n];//dp[i]表示第一次到达房间i的时间
        //第一次到达房间i,前面的房间都访问了偶数次
        for(int i=1;i<n;i++){
            dp[i]=(2*dp[i-1]-dp[A[i-1]]+2+mod)%mod;
        }
        return (int)dp[n-1] ;
    }
}

4、1998. GCD Sort of an Array

难度:Hard

思路

需要用到并查集,暂时不会。

代码

 //
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值