当前位置:网站首页>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); } }
边栏推荐
- Public and private account sending prompt information (user microservice -- message microservice)
- Project video based on Linu development
- 剑指Offer05. 替换空格
- [judgment question] [short answer question] [Database Principle]
- 启用MemCached的SASL认证
- 阿里 & 蚂蚁自研 IDE
- [ArcGIS user defined script tool] vector file generates expanded rectangular face elements
- Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
- The upward and downward transformation of polymorphism
- 剑指Offer03. 数组中重复的数字【简单】
猜你喜欢
Togaf certification self-study classic v2.0
Glide question you cannot start a load for a destroyed activity
Sword finger offer06 Print linked list from end to end
最新版抽奖盲盒运营版
[download attached] password acquisition tool lazagne installation and use
社交社区论坛APP超高颜值UI界面
Xctf mobile--app3 problem solving
强大的头像制作神器微信小程序
Day 1 of kotlin learning: simple built-in types of kotlin
Sword finger offer09 Implementing queues with two stacks
随机推荐
【综合题】【数据库原理】
Glide question you cannot start a load for a destroyed activity
studio All flavors must now belong to a named flavor dimension. Learn more
[download attached] password acquisition tool lazagne installation and use
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
Swift5.7 扩展 some 到泛型参数
The foreground uses RSA asymmetric security to encrypt user information
Exploration of sqoop1.4.4 native incremental import feature
【ManageEngine】IP地址扫描的作用
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
【習題七】【數據庫原理】
Swift Error Handling
Quickly learn member inner classes and local inner classes
Integer case study of packaging
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
[comprehensive question] [Database Principle]
Lambda expression
Tianyi ty1208-z brush machine detailed tutorial (free to remove)
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
Eureka自我保护