原题链接:点击这里

一道很水很水的背包问题? 大概算不上背包吧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. 学JAVA第十六 天,JAVA多态

    今天老师讲了多态的使用 多态是同一个行为具有多个不同表现形式或形态的能力. 多态的优点: 1. 消除类型之间的耦合关系 2. 可替换性  3. 可扩充性 4. 接口性 5. 灵活性 6. 简化性 我个 ...

  2. vue项目中获取cdn域名插件

    import axios from 'axios' let CdnPath = {} CdnPath.install = function (Vue, options) { Vue.prototype ...

  3. 一、Snapman多人协作电子表格之——Snapman自我介绍

    一.Snapman系统介绍 Snapman是一个真正现代化的电子表格系统:QQ是即时通讯软件,那Snapman就是一个即时工作系统. 微软CEO纳德拉说:Excel才是微软最伟大的产品,Excel将所 ...

  4. SQLServer 日期函数及日期转换数据类型

    一.统计语句 1.--统计当前[>当天00点以后的数据] SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT( ...

  5. SQL Server数据库漏洞评估了解一下

    SQL Server Management Studio 17.4或更高版本的SSMS中提供了SQL Server漏洞侦测(VA)功能,此功能允许SQL Server扫描您的数据库以查找潜在的安全漏洞 ...

  6. Failure sending mail: The user or group name 'xxx\xxxx' is not recognized.Mail will not be resent

      一批Reporting Service报表的订阅突然报如下错误: Failure sending mail: The user or group name 'xxx\xxxx' is not re ...

  7. c/c++ 模板 类型推断

    模板类型的推断 下面的函数f是个模板函数,typename T.下表是,根据调用测的实参,推断出来的T的类型. 请注意下表的红字部分, f(T&& t)看起来是右值引用,但其实它会根据 ...

  8. Linux学习历程——SUID、SGID、SBIT简介

    一.SUID.SGID.SBIT简介 SUID:对一个可执行文件,不是以发起者身份来获取资源,而是以可执行文件的属主身份来执行.SGID:对一个可执行文件,不是以发起者身份来获取资源,而是以可执行文件 ...

  9. android申请多个权限的正确姿势

    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permi ...

  10. day 21 垃圾回收机制、标记删除及分代回收

    垃圾回收机制 # 不能被程序访问到的数据,就称之为垃圾 引用计数 # 引用计数是用来记录值的内存地址被记录的次数的​# 每一次对值地址的引用都可以使该值的引用计数 +1# 每一次对值地址的释放都可以使 ...