LeetCode-0801

SQL

596

select class 
from Courses
group by class
having count(*) >= 5

1729

select user_id,count(*) as followers_count
from Followers
group by user_id
order by user_id

619

select Max(num) as num
from (select num
    from MyNumbers
    group by num
    having count(*) = 1) as t1
    

1045

select customer_id
from Customer
group by customer_id
having count(distinct product_key) = (select count(*) from Product)

dp50

115 不同的子序列

class Solution {
    public int numDistinct(String s, String t) {

        int n = s.length();
        int m = t.length();

        int dp[][] = new int[n+1][m+1];

        dp[0][0] = 1;
        for(int i=1;i<=n;i++)dp[i][0]=1;

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(s.charAt(i-1)==t.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1] + dp[i-1][j] ; 
                }else{
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[n][m];
    }
}

300 最长上升子序列

class Solution {
    public int lengthOfLIS(int[] nums) {
        
        int n = nums.length;
        int dp[] = new int[n];
        int max = 1;
        for(int i=0;i<n;i++)dp[i] = 1;

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

}
class Solution {
    public int lengthOfLIS(int[] nums) {

        int n = nums.length;
        int arr[] = new int[n];
        int it = 0;

        for(int num:nums){
            int pos = lowerBound(arr,num,it);
            arr[pos] = num;
            if(pos==it)it++;
        }
        return it;
    }


    public int lowerBound(int[] arr,int v,int it){
        int start = 0, end = it;
        while(start<end){
            int mid = start + (end - start)/2;
            if(arr[mid] < v){
                start = mid+1;
            }else{
                end = mid;
            }
        }
        return end;
    }

}

673.最长递增子序列个数

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

        int n = nums.length;
        int dp[][] = new int[n][2];
        int maxLen=1;

        for(int i=0;i<n;i++)dp[i][0] = dp[i][1] = 1;

        for(int i=1;i<n;i++){
            for(int j=i-1;j>=0;j--){
                if(nums[j]<nums[i]){
                    if(dp[j][0]+1>dp[i][0]){
                        dp[i][0] = dp[j][0]+1;
                        dp[i][1] = dp[j][1];
                    }else if(dp[j][0]+1==dp[i][0]){
                        dp[i][1] += dp[j][1];
                    }
                }
            }
            maxLen = Math.max(maxLen,dp[i][0]);
        }

        int sum = 0;
        for(int i=0;i<n;i++)
            if(dp[i][0] == maxLen)sum+=dp[i][1];

        return sum;
    }
}

你可能感兴趣的:(LeetCode,leetcode,算法,职场和发展)