鱼C论坛

 找回密码
 立即注册
查看: 1774|回复: 0

[学习笔记] Leetcode 343. Integer Break

[复制链接]
发表于 2020-7-31 04:09:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Seawolf 于 2020-7-31 04:22 编辑
  1. 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.

  2. Example 1:

  3. Input: 2
  4. Output: 1
  5. Explanation: 2 = 1 + 1, 1 × 1 = 1.
  6. Example 2:

  7. Input: 10
  8. Output: 36
  9. Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
  10. Note: You may assume that n is not less than 2 and not larger than 58.
复制代码

  1. 给定一个正整数n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。

  2. 示例 1:

  3. 输入: 2
  4. 输出: 1
  5. 解释: 2 = 1 + 1, 1 × 1 = 1。
  6. 示例2:

  7. 输入: 10
  8. 输出: 36
  9. 解释: 10 = 3 + 3 + 4, 3 ×3 ×4 = 36。
  10. 说明: 你可以假设n不小于 2 且不大于 58。
复制代码

  1. class Solution:
  2.     def integerBreak(self, n: int) -> int:
  3.         if n == 2 or n == 3:
  4.             return n - 1
  5.         res = 1
  6.         while n > 4:
  7.             n = n - 3
  8.             res = res * 3
  9.         return res * n
复制代码


Why factor 2 or 3? The math behind this problem.

Screenshot from 2020-07-30 16-11-14.png

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 01:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表