leetcode刷题,总结,记录,备忘 343

leetcode343Integer Break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.

For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

Note: you may assume that n is not less than 2.

Show Hint

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

使用动态规划,通过观察我们可以发现一个规律,n的最大值要么是在n-1的基础上再加上一个1,要么是在n-2的基础上加上2.首先如果是以n-1的结果为基础,如果单纯加一个1在n-1的数组中,结果是不变的,所以需要在n-1的数组中的某个元素上加1,这个1最好加在n-1的数组中的最小的数上,这样得到的乘积才是最大的。如果以n-2为基础的话,直接在n-2的数组中加个2即可,然后在这2个数组中计算结果最大的那个就是n的结果。假设dp这个数组是保存每个数字的各个数字的数组,比如dp[10]
= {3, 3, 4}。在递归公式之前需要做的工作,dp[n - 2].push_back(2); sort(dp[n - 2].begin(), dp[n - 2].end()); dp[n  - 1][0]++ ;  sort(dp[n - 1].begin(),
dp[n - 1].end());  递归公式: dp[n] = func(dp[n - 2]) > func(dp[n - 1]) ? dp[n - 2] : dp[n - 1];  func是一个计算数组结果的辅助函数,对数组做排序是为了服务于之后的计算。具体可以查看代码。

class Solution {
public:
    int function(vector<int> t)
    {
        int sum = 1;
        for (int i = 0; i < t.size(); ++i)
        {
            sum *= t[i];
        }

        return sum;
    }

    int integerBreak(int n) {
        if (n <= 0)
        {
            return 0;
        }

        if (n == 1)
        {
            return 1;
        }

        vector<vector<int> > dp(n + 1, vector<int>());

        dp[1].push_back(1);
        dp[2].push_back(1);
        dp[2].push_back(1);

        for (int i = 3; i < dp.size(); ++i)
        {
            vector<int> temp1 = dp[i - 2];
            temp1.push_back(2);
            vector<int> temp2 = dp[i - 1];
            temp2[0]++;
            sort(temp1.begin(), temp1.end());
            sort(temp2.begin(), temp2.end());

            if (function(temp1) > function(temp2))
            {
                dp[i] = temp1;
            }
            else
            {
                dp[i] = temp2;
            }
        }

        return function(dp[n]);

    }
};
时间: 2024-08-30 01:52:24

leetcode刷题,总结,记录,备忘 343的相关文章

【leetcode刷题笔记】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题解:因为题目要求原地算法,所以我们只能利用矩阵第一行和第一列存放置零信息. 首先遍历第一行和第一列,看他们是否需要全部置零,用两个变量first_column_zero和first_row_zero来记录: 遍历矩阵,如果某个位置matrix[i][j]出现了0,就把matrix[i][0]和Matrix[0

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9], insert and merge [

【leetcode刷题笔记】Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

【leetcode刷题笔记】N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of

【leetcode刷题笔记】Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

【leetcode刷题笔记】Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

【leetcode刷题笔记】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题解:基本的罗马字符和数字对应如下表所示: 罗马字符 数字 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 每隔一个字符又可以与相邻的两个字符组成一个新的数,例如I可以和V和X组成IV和IX,这样又可以生成6个数字,如下表: 罗马字符 数字 IV 4 IX

leetcode 刷题之路 66 Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和数字sum,输出二叉树中从根节点到

【leetcode刷题笔记】Merge Intervals

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题解:首先对所有的区间按照start大小排序,然后遍历排序后的数组,用last记录前一个区间,如果遍历的当前区间可以和last合并,就把它合并到last里面:否则就把last放到answer list中,并且更新last