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 }}
chenwenhang
AlgorithmPractice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmPractice
/
src
/
array_and_matrix
/
Heater.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
59 lines (44 loc) · 1.8 KB
master
Breadcrumbs
AlgorithmPractice
/
src
/
array_and_matrix
/
Heater.java
Copy path
Top
File metadata and controls
Code
Blame
59 lines (44 loc) · 1.8 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
package array_and_matrix;
/**
* @Author: Wenhang Chen
* @Description:冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径。
* <p>
* 所以,你的输入将会是房屋和供暖器的位置。你将输出供暖器的最小加热半径。
* <p>
* 说明:
* <p>
* 给出的房屋和供暖器的数目是非负数且不会超过 25000。
* 给出的房屋和供暖器的位置均是非负数且不会超过10^9。
* 只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖。
* 所有供暖器都遵循你的半径标准,加热的半径也一样。
* 示例 1:
* <p>
* 输入: [1,2,3],[2]
* 输出: 1
* 解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。
* 示例 2:
* <p>
* 输入: [1,2,3,4],[1,4]
* 输出: 1
* 解释: 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。
* @Date: Created in 8:28 6/4/2020
* @Modified by:
*/
public class Heater {
public int findNearest(int n, int[] heaters) {//找到离n最近的供暖器
int ans = 1500000000;
for (int i = 0; i < heaters.length; i++) {
ans = Math.min(ans, Math.abs(heaters[i] - n));
}
return ans;
}
public int findRadius(int[] houses, int[] heaters) {
int rad = -1;
for (int i = 0; i < houses.length; i++) {
//供暖器取离得最近的那个的半径
//最小加热半径要取所有半径中最大的
rad = Math.max(rad, findNearest(houses[i], heaters));
}
return rad;
}
}
You can’t perform that action at this time.