Commit 002805fe by 牛家玺

添加供暖器和矩阵问题

parent d31a52d2
class Heater:
def findRadius(self, houses, heaters):
heaters.sort()
heater_len = len(heaters)
radius = 0
for house in houses:
low, height = 0, heater_len - 1
while low <= height:
mid = (low + height) // 2
if heaters[mid] > house:
height = mid - 1
else:
low = mid + 1
low_distance = abs(house - heaters[height])
height_distance = abs(house - heaters[height + 1]) if height + 1 < heater_len else 922337203685477580
min_radius = min(low_distance, height_distance)
if radius < min_radius:
radius = min_radius
print(radius)
return radius
houses = [1, 2, 3]
heaters = [2]
Heater().findRadius(houses, heaters)
houses = [1, 2, 3, 4]
heaters = [1, 4]
Heater().findRadius(houses, heaters)
houses = [1]
heaters = [1, 2, 3, 4]
Heater().findRadius(houses, heaters)
houses = [282475249, 622650073, 984943658, 144108930, 470211272, 101027544, 457850878, 458777923]
heaters = [823564440, 115438165, 784484492, 74243042, 114807987, 137522503, 441282327, 16531729, 823378840, 143542612]
Heater().findRadius(houses, heaters)
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)
print(radius)
houses = [1, 2, 3]
heaters = [2]
Heater().findRadius(houses, heaters)
houses = [1, 2, 3, 4]
heaters = [1, 4]
Heater().findRadius(houses, heaters)
houses = [1]
heaters = [1, 2, 3, 4]
Heater().findRadius(houses, heaters)
houses = [282475249, 622650073, 984943658, 144108930, 470211272, 101027544, 457850878, 458777923]
heaters = [823564440, 115438165, 784484492, 74243042, 114807987, 137522503, 441282327, 16531729, 823378840, 143542612]
Heater().findRadius(houses, heaters)
# def quicksort1(array):
# doquicksort1(array, 0, len(array)-1)
#
#
# def doquicksort1(array, low, height):
# if low < height:
# mid = (low + height) // 2
# partition1(mid, array)
# doquicksort1(array, low, mid - 1)
# doquicksort1(array, mid + 1, height)
#
#
# def partition1(mid, array):
#
#
# def quicksort2(array):
# print(array)
#
#
# quicksort1([1, 5, 7, 8, 2, 34, 6, 0, 3])
# quicksort2([1, 5, 7, 8, 2, 34, 6, 0, 3])
def isToeplitzMatri(matrix):
line = len(matrix) - 1
row = len(matrix[0]) - 1
for i in range(line):
for r in range(row):
if matrix[i][r] != matrix[i + 1][r + 1]:
return False
return True
"""
[1,2,3,4],
[5,1,2,3],
[9,5,1,2]
"""
matrix = [[1, 2, 3, 4],
[5, 1, 2, 3],
[9, 5, 1, 2]]
print(isToeplitzMatri(matrix))
def quick_sort(array, l, r):
if l >= r:
return
stack = []
stack.append(l)
stack.append(r)
while stack:
low = stack.pop(0)
high = stack.pop(0)
if high - low <= 0:
continue
x = array[high]
i = low - 1
for j in range(low, high):
if array[j] <= x:
i += 1
array[i], array[j] = array[j], array[i]
array[i + 1], array[high] = array[high], array[i + 1]
stack.extend([low, i, i + 2, high])
print(array)
array = [1, 7, 8, 9, 30, 5, 6]
quick_sort(array, 0, len(array)-1)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment