原题链接:点击这里

一道很水很水的背包问题? 大概算不上背包吧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. 使用curl制作简易百度搜索

    这几天研究了一下php中的curl类库,做了一个简单的百度搜索,先上代码 <div style="width:200px;height:100px;"> <div ...

  2. Django学习之十: staticfile 静态文件

    目录 Django学习之十: staticfile 静态文件 理解阐述 静态文件 Django对静态文件的处理 其它方面 总结 Django学习之十: staticfile 静态文件 理解阐述     ...

  3. 小程序中使用ECharts 异步加载数据

    官网例子都是同步的,怎么引入及同步demo请移步官网 <view class="container"> <ec-canvas id="mychart-d ...

  4. 微信小程序开发基础

    前言: 微信小程序开入入门,如果你有html+css+javascript的基础,那么你就很快地上手掌握的.下面提供微信小程序官方地址:https://developers.weixin.qq.com ...

  5. HDU 6152 - Friend-Graph

    Friend-Graph Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. Linux ssh登陆慢的两种原因分析

    Linux ssh登陆慢的两种原因分析 如果做运维就一定会遇到ssh登陆Linux服务器慢的问题,问题比较好解决,一般Google之后有很多文章都告诉你解决方法,但是很少有文章分析为什么会慢,这篇文章 ...

  7. Postgres 优雅存储树形数据

    碰到一个树形数据需要存储再数据控制,碰到以下两个问题: 在PG数据库中如何表达树形数据 如何有效率的查询以任意节点为Root的子树 测试数据 为了更加简单一些,我们将使用一下数据 Section A ...

  8. Win10 Ubuntu子系统运行32bit Linux原生程序

    本文主要描述的是:解决  Win10 Ubuntu子系统中运行  32bit Linux原生程序 报错  Exec format error . 问题来源于  在 Win10 Ubuntu子系统中运行 ...

  9. selenium之表格的定位

    浏览器网页常常会包含各类表格,自动化测试工程师可能会经常操作表格中的行,列以及某些特定的单元格,因此熟练掌握表格的定位方法是自动化测试实施过程中必要的技能. 被测试网页的HTML代码 <!DOC ...

  10. windows最简单的局部截图工具

    大家写博客的时候应该经常要用截图吧!不知道大家用的都是什么截图工具. 1.最开始我只会键盘的printscreen截图,然后去电脑,附件,画图....总之很多步骤,贼麻烦. 2.然后用电脑玩qq的时候 ...