leetcode刷题记录_DP_II

1143. Longest Common Subsequence

// https://leetcode.com/problems/longest-common-subsequence/

最长公共子序列
状态转移方程:

if (s[i] == t[j]){
    dp[i][j] = dp[i-1][j-1] + 1;
}
else {
    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
494. Target Sum

//给定一个数组nums和一个数字target, 对数组中的数字可进行+、-操作
//有多少种组合方式可使得最终结果为target

// dfs <--!https://leetcode.com/problems/target-sum/discuss/97335/Short-Java-DP-Solution-with-Explanation-->
// dp的solution待续。。。

//递归方式

static int count = 0;
public static int findTargetSumWays(int[] nums, int S) {
    if (nums.length == 0){
        return 0;
    }
    dfs(nums, S, 0, 0);


    return count;
}

public static void dfs(int[] nums, int target, int tmpSum, int pos){
    if (pos == nums.length) {
        if (tmpSum == target) {
            count += 1;
        }
        return;
    }
    dfs(nums, target, tmpSum+nums[pos], pos+1);
    dfs(nums, target, tmpSum-nums[pos], pos+1);
}
978. Longest Turbulent Subarray

//用两个数组inc, dec记录到当前元素的波动增长、减小的最大子串长度
//ref
public static int maxTurbulenceSize(int[] A) {
    int[] inc = new int[A.length];
    int[] dec = new int[A.length];
    int result=1;
    inc[0] = dec[0] = 1;
    for(int i=1;i<A.length;i++){
        if (A[i]<A[i-1]){
            dec[i] = inc[i-1] + 1;
            inc[i] = 1;
        }
        else if (A[i]>A[i-1]){
            inc[i] = dec[i-1] + 1;
            dec[i] = 1;
        }
        else {
            inc[i] = 1;
            dec[i] = 1;
        }
    }

    for(int i=0;i<A.length;i++){
        if (inc[i]>result){
            result = inc[i];
        }
    }

    for(int i=0;i<A.length;i++){
        if (dec[i]>result){
            result = dec[i];
        }
    }
    return result;
}
16. 3Sum Closest

//类似题目: leetcode-3Sum, 找出数组中相加为0的数据对

public int threeSumClosest(int[] nums, int target) {
    Arrays.sort(nums);
    int i = 0, left = 0, right = 0;
    if (nums.length < 3) {
        return 0;
    }
    int res = 9999;
    int result = 0;
    while(i < nums.length-2){
        left = i + 1;
        right = nums.length - 1;
        while (left < right){
            if ( Math.abs(nums[i] + nums[left] + nums[right] - target) == 0){
                return target;
            }
            if (Math.abs(nums[i] + nums[left] + nums[right] - target) < res){
                res = Math.abs(nums[i] + nums[left] + nums[right] - target);
                result = nums[i] + nums[left] + nums[right];
            }
            if (nums[i] + nums[left] + nums[right] < target){
                left += 1;
            }
            else if(nums[i] + nums[left] + nums[right] > target){
                right -= 1;
            }
        }
        i += 1;
    }
    return result;
}
665. Non-decreasing Array

//leetcode中easy级别,其实不easy...

public static boolean checkPossibility(int[] nums){
    int count = 0;
    int l = nums.length;
    if (l < 1 || nums == null){
        return false;
    }
    if (l == 1){
        return true;
    }
    int i = 0;
    while(i < l-1) {
        if (nums[i+1] < nums[i]){
            count += 1;
            if (count > 1) {
                return false;
            }
            if (i == 0 || nums[i-1] <= nums[i+1]) {
                nums[i] = nums[i+1];
            }
            else {
                nums[i+1] = nums[i];
            }
        }
        i += 1;
    }
    return true;
}

public static void main(String[] args) {
    int[] nums = new int[]{4,2,1};
    boolean d = checkPossibility(nums);
    System.out.println(d);
}

a > b c d, a是第一个元素,为了保持序列非递减,需要将b的值赋给a

a b < c > d, b < d, 此时a b d是满足非递减的要求的,c的存在破坏了规则,因此改变c, c=d

a b < c > d, b > d, 此时a b c满足非递减规则,改变d, d=c

ref: https://www.cnblogs.com/grandyang/p/7565424.html

''我们通过分析上面三个例子可以发现,当我们发现后面的数字小于前面的数字产生冲突后,有时候需要修改前面较大的数字(比如前两个例子需要修改4),有时候却要修改后面较小的那个数字(比如前第三个例子需要修改2),那么有什么内在规律吗?是有的,判断修改那个数字其实跟再前面一个数的大小有关系,首先如果再前面的数不存在,比如例子1,4前面没有数字了,我们直接修改前面的数字为当前的数字2即可。而当再前面的数字存在,并且小于当前数时,比如例子2,-1小于2,我们还是需要修改前面的数字4为当前数字2;如果再前面的数大于当前数,比如例子3,3大于2,我们需要修改当前数2为前面的数3。''

674. Longest Continuous Increasing Subsequence

最长连续递增子序列 VS 最长递增子序列(LIS)

e.g.:
Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. 
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4. 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值