算法设计与应用基础:第十四周

343. Integer Break

  • Total Accepted: 40852
  • Total Submissions: 89682
  • Difficulty: Medium
  • Contributor: LeetCode

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 and not larger than 58.


题解:首先考虑当一个正整数分成两个正整数时如何做到最大,易得偶数时是两个n/2,奇数时是(n-1)/2 和 (n+1)/2,而当这两个分出来的数相乘大于原来的n时,那么n就应该分解,由此得出n>=4 和n>=5。就是说最终得到的分解的因子不能大于4,1是没有意义的,所以分解的因子只能为2和3,并且2 * 2 * 2 < 3 * 3,所以2的个数不能超过2个。根据这些限制,代码如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值