博客
关于我
整数拆分
阅读量:203 次
发布时间:2019-02-28

本文共 531 字,大约阅读时间需要 1 分钟。

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

示例 1:

输入: 2输出: 1解释: 2 = 1 + 1, 1 × 1 = 1。

示例 2:

输入: 10输出: 36解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。

说明: 你可以假设 n 不小于 2 且不大于 58。

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/integer-break

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

分析:

尽可能将n分解成3和2,并且最多只能有两个2,此时乘积最大

class Solution {   public:    int integerBreak(int n) {           if(n<=3) return 1*(n-1);        int p=1;        while(n>=5)        {               n-=3;            p*=3;        }        return p*n;    }};

如有问题,欢迎在评论区提问

你可能感兴趣的文章
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>