当前位置:网站首页>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); } }
边栏推荐
- 2020-11_ Technical experience set
- ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
- Ali & ant self developed IDE
- Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
- CNN MNIST handwriting recognition
- Use bloc to build a page instance of shutter
- 初入职场,如何快速脱颖而出?
- Kung Fu pays off, and learning is done
- [review questions of database principles]
- Approve iPad, which wants to use your icloud account
猜你喜欢

并网-低电压穿越与孤岛并存分析

Eureka自我保护

雲計算未來 — 雲原生
![[review questions of database principles]](/img/c3/81d192a40bcc4f5d72fcbe76c708bb.png)
[review questions of database principles]

4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment

T430 toss and install OS majave 10.14

基于同步坐标变换的谐波电流检测

Attack and defense world mobile--ph0en1x-100

Application of ncnn neural network computing framework in orange school orangepi 3 lts development board

Detailed explanation of the most complete constraintlayout in history
随机推荐
剑指Offer03. 数组中重复的数字【简单】
Swift bit operation exercise
[judgment question] [short answer question] [Database Principle]
基于同步坐标变换的谐波电流检测
剑指Offer09. 用两个栈实现队列
双链笔记·思源笔记综合评测:优点、缺点、评价
Airflow installation jump pit
如何在微信小程序中获取用户位置?
自抗扰控制器七-二阶 LADRC-PLL 结构设计
Swift Error Handling
4. 无线体内纳米网:电磁传播模型和传感器部署要点
Sword finger offer09 Implementing queues with two stacks
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
JVM memory model
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
[exercice 7] [principe de la base de données]
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
Node.js: express + MySQL的使用
Approve iPad, which wants to use your icloud account
写一个简单的nodejs脚本