LeetCode-0802

SQL50

1789

学习使用 union,union 有自动去重的功能,如果要保留重复,使用 union all

select employee_id,department_id
from Employee
group by employee_id
having count(*) = 1
union 
select employee_id,department_id
from Employee
where primary_flag = 'Y'

610

select x,y,z,
IF(x+y>z and x+z>y and y+z>x,'Yes','No') as triangle
from Triangle

180

select distinct l1.num as ConsecutiveNums 
from Logs l1,Logs l2, Logs l3
where l1.Num = l2.Num and l2.Num = l3.Num 
and l1.Id+1 = l2.Id and l2.Id + 1 = l3.Id

dp50基础

646. 最长数对链

class Solution {
    public int findLongestChain(int[][] pairs) {

        int n = pairs.length;
        int dp[] = new int[n];
        int max = 1;

        for(int i=0;i<n;i++)dp[i] = 1;
        Arrays.sort(pairs,(a,b)->{
            if(a[0]!=b[0])return a[0]-b[0];
            else  return a[1] - b[1];
        });

        for(int i=1;i<n;i++){
            for(int j=0;j<i;j++){
                if(pairs[j][1]<pairs[i][0]){
                    dp[i] = Math.max(dp[i],dp[j]+1);
                }
                max = Math.max(max,dp[i]);
            }
        }

        return max;
    }
}

1218.最长定差子序列

public int longestSubsequence(int[] arr, int difference) {

        int n = arr.length;
        int max = 1;
        Map<Integer,Integer> dp = new HashMap<>();

        for(int i=0;i<n;i++){
            int v = dp.getOrDefault(arr[i]-difference,0)+1;
            max = Math.max(max,v);
            dp.put(arr[i],v);
        }

        return max;
    }

1027. 最长等差数列

class Solution {
    public int longestArithSeqLength(int[] nums) {

        int max = 1;
        int n = nums.length;

        Map<Integer,Map<Integer,Integer>> dp = new HashMap<>();
        dp.put(0,new HashMap<>());

        for(int i=1;i<n;i++){
            for(int j=0;j<i;j++){
                
                int diff = nums[i] - nums[j];

                Map dpj = dp.get(j);
                Map dpi = dp.getOrDefault(i,new HashMap<>());
                int cnt = 1;
                
                if(dpj.get(diff)!=null){
                    cnt = Math.max((int) dpj.get(diff)+1, (int)dpi.getOrDefault(diff,1));
                }else{
                    cnt = Math.max(2,(int)dpi.getOrDefault(diff,1));
                }
                
                dpi.put(diff,cnt);
                max = Math.max(max,cnt);
                
                dp.put(i,dpi);
            }
        }

        return max;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值