LeetCode 343

Integer 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.

Hint:

There is a simple O(n) solution to this problem.
You may check the breaking results of n ranging from 7 to 10 to discover the regularities.

 1 /*************************************************************************
 2     > File Name: LeetCode343.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: Tue 10 May 2016 04:17:35 PM CST
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Integer Break
11
12     Given a positive integer n, break it into the sum of at least two positive integers
13     and maximize the product of those integers. Return the maximum product you can get.
14
15     For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
16
17     Note: you may assume that n is not less than 2.
18
19     Hint:
20
21     There is a simple O(n) solution to this problem.
22     You may check the breaking results of n ranging from 7 to 10 to discover the regularities.
23
24  ************************************************************************/
25
26 #include<stdio.h>
27
28 int integerBreak( int n )
29 {
30     if( n <= 2 )
31     {
32         return 1;
33     }
34     if( n == 3 )
35     {
36         return 2;
37     }
38     int ret = 1;
39     while( n > 4 )
40     {
41         ret *= 3;
42         n -= 3;
43     }
44     return ret*n;
45 }
46
47 int main()
48 {
49     int n = 7;
50     int ret = integerBreak(n);
51     printf("%d\n",ret);
52
53     return 0;
54 }
时间: 2024-11-05 14:43:46

LeetCode 343的相关文章

LeetCode 343.整数拆分 - JavaScript

题目描述:给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 题目分析 题目中"n 至少可以拆分为两个正整数的和",这个条件说明了 n 是大于 1 的整数. 对 7 来说,可以拆成 3+4,最大乘积是 12. 对 8 来说,可以拆成 3+3+2,最大乘积是 18. 解法 1: 动态规划 状态数组dp[i]表示:数字 i 拆分为至少两个正整数之和的最大乘积.为了方便计算,dp 的长度是 n + 1,值初始化为 1. 显然dp[2]等于

leetcode 343. 整数拆分:动态规划(c++)

leetcode 343. 整数拆分 分析 状态表示: · dp[i] 表示整数 i 拆分乘积的最大值. 转移方程: · 对于每个数字 i 都进行一遍循环,计算 (i - j) * j,(j <= i - 1),并求其与 dp[i],dp[i - j] * j 的最大值,即:dp[i] = max(dp[i],(i - j) * j,dp[i - j] * j) · 与 dp[i - j] * j 比较是因为 i - j 可能小于 i - j 拆分的乘积. 边界: · 输入的整数 n 大于等于

LeetCode 343. Integer Break

https://leetcode.com/problems/integer-break/ 数学题. 1 #include <iostream> 2 using namespace std; 3 4 class Solution { 5 public: 6 int integerBreak(int n) { 7 if (n == 2) return 1; 8 if (n == 3) return 2; 9 10 int product = 1; 11 while (n > 4) 12 {

leetcode 343. Integer Break(dp或数学推导)

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 +

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 =

动态规划学习之LeetCode分割整数相关题目(第343、279、91题)

题目:给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 示例: 输入: 10 输出: 36 解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36. 说明: 你可以假设 n 不小于 2 且不大于 58. 分析: 1.定义一个状态转移数组dp,dp[i]表示数字i拆分为至少两项之和后,拆分后的数字之间的最大乘积. 2.寻找关系式,那么如何确定dp[i]的最大值呢?首先我们要确定它有哪些取值的可能,对于拆分之和的数字的乘积,这个乘

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro