当前位置:网站首页>Sword finger offer14 the easiest way to cut rope
Sword finger offer14 the easiest way to cut rope
2022-07-03 12:50:00 【Mcc_ mingchao】

Analyzing the topic, we found , stay n<4 When , The maximum product is n-1( You have to cut ), be equal to 4 Cut into 2 Equal division , The product is 2, But more than 4 When , We can find out , Cut into several pieces with length of three , remainder <=4 Multiple line segments of , The bigger the product ( Because as long as it is greater than 4, Just cut it into pieces with one 3 The length must be larger than the product of other tangents , such as 5 Cut into pieces 2 3,6 Cut into pieces 3 3) such as 10 We are divided into 3 3 4, The product is 36,17 Cut into pieces 3 3 3 3 3 2 The product of 3^5*2. According to this principle We can easily make a while Cycle to achieve
class Solution {
public int cuttingRope(int n) {
if(n < 4){
return n - 1;
}
int res = 1;
while(n > 4){
res *= 3;
n -= 3;
//
}
return res * n;
}
}meanwhile 15 Cutting rope II of the question is to add a remainder operation on this basis
class Solution { public int cuttingRope(int n) { if(n<4){ return n-1; }long total=1; while(n>4){ total*=3; total=total%1000000007; n-=3; } return (int )(total*n%1000000007); } }
边栏推荐
- [problem exploration and solution of one or more filters or listeners failing to start]
- 阿里 & 蚂蚁自研 IDE
- (latest version) WiFi distribution multi format + installation framework
- Eureka自我保护
- Sword finger offer06 Print linked list from end to end
- The future of cloud computing cloud native
- 【習題七】【數據庫原理】
- 剑指Offer04. 二维数组中的查找【中等】
- Togaf certification self-study classic v2.0
- 记录自己vulnhub闯关记录
猜你喜欢

Xctf mobile--app2 problem solving

4. 无线体内纳米网:电磁传播模型和传感器部署要点

Use bloc to build a page instance of shutter

Sword finger offer09 Implementing queues with two stacks

idea将web项目打包成war包并部署到服务器上运行

Eureka自我保护

【数据挖掘复习题】

【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)

Public and private account sending prompt information (user microservice -- message microservice)

Record your vulnhub breakthrough record
随机推荐
What is more elegant for flutter to log out and confirm again?
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
Xctf mobile--rememberother problem solving
The best shortcut is no shortcut
Glide 4.6.1 API initial
Eureka self protection
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
公纵号发送提示信息(用户微服务--消息微服务)
Swift5.7 extend some to generic parameters
剑指Offer07. 重建二叉树
Xctf mobile--app2 problem solving
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
The future of cloud computing cloud native
[problem exploration and solution of one or more filters or listeners failing to start]
如何在微信小程序中获取用户位置?
GCN thinking - word2vec directly calculates text classification
Tensorflow binary installation & Failure
Using swift language features, write a pseudo-random number generator casually
RedHat5 安装Socket5代理服务器
剑指Offer06. 从尾到头打印链表