Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
homework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ligang
homework
Commits
5ba32960
Commit
5ba32960
authored
Jan 21, 2019
by
ligang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
'第6次作业'
parent
4b1af0ab
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
0 deletions
+34
-0
06-homework-ligang/heaters.py
+34
-0
No files found.
06-homework-ligang/heaters.py
0 → 100644
View file @
5ba32960
#!/usr/bin/env python
# -*-coding:utf-8 -*-
#冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。
# 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径。
# 所以,你的输入将会是房屋和供暖器的位置。你将输出供暖器的最小加热半径。
# 说明:
# 给出的房屋和供暖器的数目是非负数且不会超过 25000。
# 给出的房屋和供暖器的位置均是非负数且不会超过10^9。
# 只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖。
# 所有供暖器都遵循你的半径标准,加热的半径也一样。
# 示例 1:输入: [1,2,3],[2]输出: 1解释: 仅在位置2上有一个供暖器。
# 如果我们将加热半径设为1,那么所有房屋就都能得到供暖。
# 示例 2:输入: [1,2,3,4],[1,4]输出: 1解释: 在位置1, 4上有两个供暖器。
# 我们需要将加热半径设为1,这样所有房屋就都能得到供暖
class
Heater
:
def
findRadius
(
self
,
houses
,
heaters
):
heaters
.
sort
()
houses
.
sort
()
radius
=
0
i
=
0
# 哨兵
heaters
=
[
-
1
]
+
heaters
+
[
float
(
'inf'
)]
for
house
in
houses
:
while
house
>
heaters
[
i
]:
i
=
i
+
1
current_radius
=
min
(
house
-
heaters
[
i
-
1
],
heaters
[
i
]
-
house
)
radius
=
max
(
radius
,
current_radius
)
return
radius
house
=
[
1
,
2
,
3
,
4
]
heater
=
[
1
,
4
]
h
=
Heater
()
s
=
h
.
findRadius
(
house
,
heater
)
print
(
s
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment