update
This commit is contained in:
parent
fdaa94a0fa
commit
5e6299439c
25
leetcode_py/1944.py
Normal file
25
leetcode_py/1944.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
# use monotonic stack
|
||||||
|
class Solution:
|
||||||
|
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
|
||||||
|
n = len(heights)
|
||||||
|
result = [0] * n
|
||||||
|
stack = []
|
||||||
|
# Process from right to left
|
||||||
|
for i in range(n - 1, -1, -1):
|
||||||
|
count = 0
|
||||||
|
# Pop elements shorter than current height
|
||||||
|
while stack and heights[stack[-1]] < heights[i]:
|
||||||
|
count += 1
|
||||||
|
stack.pop()
|
||||||
|
|
||||||
|
# If stack not empty, can see one more person
|
||||||
|
if stack:
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
result[i] = count
|
||||||
|
stack.append(i)
|
||||||
|
|
||||||
|
return result
|
43
leetcode_py/1998.py
Normal file
43
leetcode_py/1998.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def gcdSort(self, nums: List[int]) -> bool:
|
||||||
|
# 并查集
|
||||||
|
def find(x):
|
||||||
|
if parent[x] != x:
|
||||||
|
parent[x] = find(parent[x])
|
||||||
|
return parent[x]
|
||||||
|
|
||||||
|
def union(x, y):
|
||||||
|
parent[find(x)] = find(y)
|
||||||
|
|
||||||
|
# 获取一个数的所有质因数
|
||||||
|
def get_prime_factors(n):
|
||||||
|
factors = set()
|
||||||
|
i = 2
|
||||||
|
while i * i <= n:
|
||||||
|
while n % i == 0:
|
||||||
|
factors.add(i)
|
||||||
|
n //= i
|
||||||
|
i += 1
|
||||||
|
if n > 1:
|
||||||
|
factors.add(n)
|
||||||
|
return factors
|
||||||
|
|
||||||
|
# 初始化并查集
|
||||||
|
max_num = max(nums)
|
||||||
|
parent = list(range(max_num + 1))
|
||||||
|
|
||||||
|
# 对每个数字,将其与其质因数连接
|
||||||
|
for num in nums:
|
||||||
|
for prime in get_prime_factors(num):
|
||||||
|
union(num, prime)
|
||||||
|
|
||||||
|
# 检查排序后的数组是否可以通过交换得到
|
||||||
|
sorted_nums = sorted(nums)
|
||||||
|
for i in range(len(nums)):
|
||||||
|
if find(nums[i]) != find(sorted_nums[i]):
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
11
leetcode_py/2000.py
Normal file
11
leetcode_py/2000.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def reversePrefix(self, word: str, ch: str) -> str:
|
||||||
|
match = re.search(rf"{ch}", word)
|
||||||
|
if match:
|
||||||
|
position = match.start()
|
||||||
|
return word[: position + 1][::-1] + word[position + 1 :]
|
||||||
|
else:
|
||||||
|
return word
|
11
leetcode_py/2006.py
Normal file
11
leetcode_py/2006.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def countKDifference(self, nums: List[int], k: int) -> int:
|
||||||
|
count = 0 # number of pairs
|
||||||
|
for i in range(len(nums)):
|
||||||
|
for j in range(i + 1, len(nums)):
|
||||||
|
if abs(nums[i] - nums[j]) == k:
|
||||||
|
count += 1
|
||||||
|
return count
|
@ -1,27 +1,39 @@
|
|||||||
# Unfinish
|
|
||||||
from typing import Tuple, List
|
from typing import Tuple, List
|
||||||
import math
|
import math
|
||||||
|
|
||||||
n, m, r = input().split(" ")
|
n, m, r = map(int, input().split())
|
||||||
locations: List[Tuple[str, str]] = []
|
locations: List[Tuple[int, int]] = []
|
||||||
for i in range(int(m)):
|
|
||||||
location: Tuple[str, str] = tuple(input().split(" "))
|
|
||||||
locations.append(location)
|
|
||||||
|
|
||||||
map = [[0 for _ in range(int(n))] for _ in range(int(n))]
|
for i in range(m):
|
||||||
|
x, y = map(int, input().split())
|
||||||
|
# 将输入的1-based坐标转换为0-based坐标
|
||||||
|
locations.append((x - 1, y - 1))
|
||||||
|
|
||||||
|
grid = [[0 for _ in range(n)] for _ in range(n)]
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
for i in range(int(n)):
|
for location in locations:
|
||||||
for j in range(int(n)):
|
x, y = location
|
||||||
if map[i][j] == 0:
|
if grid[x][y] != 1:
|
||||||
for k in range(int(m)):
|
grid[x][y] = 1
|
||||||
distance = math.sqrt(
|
count += 1
|
||||||
(int(locations[k][0]) - i) ** 2 + (int(locations[k][1]) - j) ** 2
|
|
||||||
)
|
# 检查半径r内的所有点
|
||||||
if distance <= float(r):
|
left = max(0, x - r)
|
||||||
map[i][j] = 1
|
right = min(n, x + r + 1)
|
||||||
count = count + 1
|
top = max(0, y - r)
|
||||||
break
|
bottom = min(n, y + r + 1)
|
||||||
|
|
||||||
|
for i in range(left, right):
|
||||||
|
for j in range(top, bottom):
|
||||||
|
# 计算点(i,j)到探测器(x,y)的距离
|
||||||
|
distance = math.sqrt((i - x) ** 2 + (j - y) ** 2)
|
||||||
|
if distance <= r: # 如果在探测范围内
|
||||||
|
if grid[i][j] != 1:
|
||||||
|
grid[i][j] = 1
|
||||||
|
count += 1
|
||||||
|
|
||||||
print(count)
|
print(count)
|
||||||
print(map)
|
for row in grid:
|
||||||
|
print(row)
|
||||||
|
|
||||||
|
61
luogu_py/p4530.py
Normal file
61
luogu_py/p4530.py
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
# TODO: 未完成
|
||||||
|
def solve(n, m, balls):
|
||||||
|
# 将球按正负数和零分类并排序
|
||||||
|
pos = sorted([x for x in balls if x > 0], reverse=True) # 正数降序
|
||||||
|
neg = sorted([x for x in balls if x < 0]) # 负数升序
|
||||||
|
zeros = [x for x in balls if x == 0]
|
||||||
|
|
||||||
|
# 如果正数不够分配给每个篮筐,需要特殊处理
|
||||||
|
if len(pos) < m:
|
||||||
|
# 需要用负数或零来确保每个篮筐都有球
|
||||||
|
return None # 这里需要根据具体情况处理
|
||||||
|
|
||||||
|
# 初始化篮筐
|
||||||
|
baskets = [1] * m
|
||||||
|
|
||||||
|
# 优先处理负数对
|
||||||
|
neg_pairs = []
|
||||||
|
for i in range(0, len(neg) - 1, 2):
|
||||||
|
neg_pairs.append(neg[i] * neg[i + 1])
|
||||||
|
|
||||||
|
# 如果有单独的负数,与最小的正数配对或单独放在一个篮筐
|
||||||
|
if len(neg) % 2 == 1:
|
||||||
|
last_neg = neg[-1]
|
||||||
|
if pos and last_neg * pos[-1] > last_neg:
|
||||||
|
neg_pairs.append(last_neg * pos.pop())
|
||||||
|
else:
|
||||||
|
# 把单独的负数放在一个新的篮筐
|
||||||
|
smallest_basket_idx = 0
|
||||||
|
baskets[smallest_basket_idx] *= last_neg
|
||||||
|
|
||||||
|
# 将负数对的结果按从大到小排序
|
||||||
|
neg_pairs.sort(reverse=True)
|
||||||
|
|
||||||
|
# 分配负数对结果
|
||||||
|
for i, pair_product in enumerate(neg_pairs):
|
||||||
|
baskets[i % m] *= pair_product
|
||||||
|
|
||||||
|
# 分配零
|
||||||
|
zero_idx = 0
|
||||||
|
while zeros and zero_idx < m:
|
||||||
|
if baskets[zero_idx] == 1: # 只在篮筐未使用时放零
|
||||||
|
baskets[zero_idx] *= zeros.pop()
|
||||||
|
zero_idx += 1
|
||||||
|
|
||||||
|
# 分配剩余的正数
|
||||||
|
pos_idx = 0
|
||||||
|
for num in pos:
|
||||||
|
baskets[pos_idx % m] *= num
|
||||||
|
pos_idx += 1
|
||||||
|
|
||||||
|
return sum(baskets)
|
||||||
|
|
||||||
|
|
||||||
|
# 读取输入并处理
|
||||||
|
while True:
|
||||||
|
n, m = map(int, input().split())
|
||||||
|
if n == 0 and m == 0:
|
||||||
|
break
|
||||||
|
balls = list(map(int, input().split()))
|
||||||
|
result = solve(n, m, balls)
|
||||||
|
print(result)
|
Loading…
x
Reference in New Issue
Block a user