Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
newcaoguo
AlgorithmEmmiter
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmEmmiter
/
IntegerBreak.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
47 lines (46 loc) · 1.92 KB
master
Breadcrumbs
AlgorithmEmmiter
/
IntegerBreak.java
Copy path
Top
File metadata and controls
Code
Blame
47 lines (46 loc) · 1.92 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.company;
public class IntegerBreak {
/**
* 这道题给了我们一个正整数n,让我们拆分成至少两个正整数之和,使其乘积最大,题目提示中让我们用O(n)来解题,而且告诉我们找7到10之间的规律,那么我们一点一点的来分析:
* <p>
* 正整数从1开始,但是1不能拆分成两个正整数之和,所以不能当输出。
* <p>
* 那么2只能拆成1+1,所以乘积也为1。
* <p>
* 数字3可以拆分成2+1或1+1+1,显然第一种拆分方法乘积大为2。
* <p>
* 数字4拆成2+2,乘积最大,为4。
* <p>
* 数字5拆成3+2,乘积最大,为6。
* <p>
* 数字6拆成3+3,乘积最大,为9。
* <p>
* 数字7拆为3+4,乘积最大,为12。
* <p>
* 数字8拆为3+3+2,乘积最大,为18。
* <p>
* 数字9拆为3+3+3,乘积最大,为27。
* <p>
* 数字10拆为3+3+4,乘积最大,为36。
* <p>
* ....
* <p>
* 那么通过观察上面的规律,我们可以看出从5开始,
* 数字都需要先拆出所有的 3,一直拆到剩下一个数为 2 或者 4,
* 因为剩 4 就不用再拆了,拆成两个 2 和不拆没有意义,
* 而且 4 不能拆出一个 3 剩一个 1,这样会比拆成 2 + 2 的乘积小。
* 那么这样我们就可以写代码了,先预处理 n 为 2 和 3 的情况,
* 然后先将结果res初始化为 1,然后当 n 大于 4 开始循环,
* 我们结果自乘 3,n 自减 3,根据之前的分析,
* 当跳出循环时,n只能是 2 或者 4,再乘以res返回即可:
*/
public int integerBreak(int n) {
if (n == 2 || n == 3) return n - 1;
int res = 1;
while (n > 4) {
res *= 3;
n -= 3;
}
return res * n;
}
}
You can’t perform that action at this time.