原题链接:点击这里

一道很水很水的背包问题? 大概算不上背包吧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. jQuery(八)、ajax

    1.jQuery.ajax(url[, settings]) 通过HTTP请求加载远程数据. 注意:所有的settings选择都可以通过$.ajaxSetup()函数来全局指定. 回调函数 在实际开发 ...

  2. I/O输出流基础之FileOutputStream

    OutputStream:是所有字节输出流的父类,其作用是:用这个流把网络数据(getOutputStream()),或者内存中的字节数组数据写到文件系统中文件系统(FileOutputStream) ...

  3. Python二级-----------程序冲刺1

    1. 仅使用 Python 基本语法,即不使用任何模块,编写 Python 程序计算下列数学表达式的结果并输出,小数点后保留3位.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪ ...

  4. HTML/CSS快速入门

    Web概念 JavaWeb 使用java语言开发基于互联网的项目 软件架构 C/S架构:Client/Server 客户端/服务器 用户本地有一个客户端程序,在远程有一个服务端程序 如QQ,英雄联盟. ...

  5. Netty 核心内容之 编解码器

    原文链接 Netty 核心内容之 编解码器 代码仓库地址 编解码器 我认为Netty 最棒的一点就是Netty 设计的编解码链,这一优秀的设计,可以很方便的实现二进制流->ByteBuf-> ...

  6. qt生成二维码

    到官网下载qrencode http://fukuchi.org/works/qrencode/index.html.en qrenc.c不用,这个是测试用的,把config.h.in文件改为conf ...

  7. 阿里云服务器,Sql Server 本地连接服务器端问题记录

    1.如果你是阿里云服务器,配置参数都整好了并且排除了防火墙问题(关闭了防火墙),依然没有连接上,那就先考虑这个问题 问题:阿里云服务器的SQLServer不允许远程连接 原因:因为除了服务器上的防火墙 ...

  8. linux下oracle启动关闭

    1.以oracle身份登录数据库,命令:su – oracle 2.执行以下命令查看数据库监听器的状况: lsnrctl status 3.执行以下命令停止数据库监听器运行: lsnrctl stop ...

  9. scrapy安装失败:error:Microsoft Visual C++ 14.0 is reuired.及同类型安装问题解决办法

    今天在安装scrapy的时候(pip install Scrapy),出现了如下错误: building 'twisted.test.raiser' extensionerror: Microsoft ...

  10. redux 简介

    概述 Redux 本身是个极其简单的状态管理框架, 它的简单体现在概念少, 流程明确. 但是, 正是因为简单, 使用上没有强制的约束, 所以如果用不好, 反而会让状态管理更加混乱. 我觉得, 用好 R ...