原题链接:点击这里

一道很水很水的背包问题? 大概算不上背包吧QAQ 自己的dp 真的是太差劲啦,以后每天一道LeetCode 备战秋招!

package leetcode;

public class a689_Maximum_Sum_of_3_NonOverlapping_Subarrays {

    public static int[] maxSumOfThreeSubarrays(int[] nums, int k) {
        int [] ans = new int [3];
        int [][] dp = new int [3][nums.length+1];

        int [] sum = new int [nums.length+1];
        for(int j=1;j<=nums.length;j++) {
            if(j==1) {
                sum[j]=nums[j-1];
            }else {
                sum[j]+=sum[j-1]+nums[j-1];
            }
        }
        int mmx =0;
        for(int j=0;j<3;j++) {
            int max = 0;
            for(int i = k*j+1;i<=nums.length-k+1;i++) {
                if(j==0) {
                    dp[0][i] = sum[i+k-1]-sum[i-1];
                }else {
                    max = Math.max(max, dp[j-1][i-k]);
                    dp[j][i] = max+sum[i+k-1]-sum[i-1];
                    mmx = Math.max(mmx, dp[j][i]);
                }
            }
        }

        for(int j=2;j>=0;j--) {
            for(int i=1;i<=nums.length-k+1;i++) {
                if(dp[j][i]==mmx) {
                    ans[j]=i-1;
                    mmx -= sum[i+k-1]-sum[i-1];
                    break;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {

        int [] nums = {1,2,1,2,6,7,5,1};
        int k = 2;
        maxSumOfThreeSubarrays(nums,k);
    }

}
Runtime: 4 ms, faster than 82.08% of Java online submissions for Maximum Sum of 3 Non-Overlapping Subarrays.

Memory Usage: 42.6 MB, less than 34.91% of Java online submissions forMaximum Sum of 3 Non-Overlapping Subarrays.
 

LeetCode--689_Maximum_Sum_of_3_NonOverlapping_Subarrays的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. Myeclipse10.7添加本地插件方法

    -

  2. 位运算 leecode.389. 找不同

    //给定两个字符串 s 和 t,它们只包含小写字母. //字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. //请找出在 t 中被添加的字母 char findTheDifferenc ...

  3. java拦截器(interceptor)

    1.声明式 (1)注解,使用Aspect的@Aspect (2)实现HandlerInterceptor /** * 拦截请求 * * @author Administrator * */ @Comp ...

  4. centos7 最小安装初始化

    配置阿里yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup \&&cu ...

  5. centos 7 selinux开启关闭

    1 查看selinux状态 [root@localhost ~]# sestatus SELinux status: disabled 2 关闭 零时关闭 [root@localhost ~]# se ...

  6. 基于mysql的一些sql语法

    Sql: distinct: select distinct * from tea; Tea中每行的数据必有不同,若有两行相同的,则只输出一行 Limit: select * from stu lim ...

  7. springboot 应用中静态资源下载

    一. 场景介绍 Excel模板静态资源在,应用中的static文件夹中,文件名称包含中文; 需求:页面直接访问下载Excel模板. 二.目录结构 三.后台代码 @GetMapping("/d ...

  8. 《JAVA程序设计》_第七周学习总结

    一.学习内容 1.String类--8,1知识 Java专门提供了用来处理字符序列的String类.String类在java.lang包中,由于java.lang包中的类被默认引入,因此程序可以直接使 ...

  9. 分布式存储ceph——(4)ceph 添加/删除osd

    一.添加osd: 当前ceph集群中有如下osd,现在准备新添加osd:

  10. PHP实现部分字符隐藏

    /** * 隐藏部分字符串 * # 此方法多用于手机号码或身份证号.银行卡号的中间部分数字的隐藏 */ function func_substr_replace($str, $replacement ...