当前位置:网站首页>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); } }
边栏推荐
- Xctf mobile--app2 problem solving
- 社交社区论坛APP超高颜值UI界面
- Export the entire Oracle Database
- Togaf certification self-study classic v2.0
- The future of cloud computing cloud native
- Attack and defense world mobile--ph0en1x-100
- context. Getexternalfilesdir() is compared with the returned path
- [ManageEngine] the role of IP address scanning
- 【数据挖掘复习题】
- Tianyi ty1208-z brush machine detailed tutorial (free to remove)
猜你喜欢
![[comprehensive question] [Database Principle]](/img/d7/8c51306bb390e0383a017d9097e1e5.png)
[comprehensive question] [Database Principle]

Xctf mobile--app1 problem solving
![Sword finger offer04 Search in two-dimensional array [medium]](/img/c4/002c951f8d914aaea4f4133685ebd1.png)
Sword finger offer04 Search in two-dimensional array [medium]

Swift bit operation exercise

Node. Js: use of express + MySQL

Method overloading and rewriting

Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board

ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍

GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈

2021 autumn Information Security Experiment 1 (password and hiding technology)
随机推荐
剑指Offer05. 替换空格
Xctf mobile--app2 problem solving
Method overloading and rewriting
雲計算未來 — 雲原生
强大的头像制作神器微信小程序
2021 autumn Information Security Experiment 1 (password and hiding technology)
Eureka自我保护
Grid connection - Analysis of low voltage ride through and island coexistence
Applet wxss introduction
OpenStack节点地址改变
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
基于Linu开发的项目视频
【判断题】【简答题】【数据库原理】
[exercice 7] [principe de la base de données]
Kung Fu pays off, and learning is done
十条职场规则
Openstack node address change
The upward and downward transformation of polymorphism
temp