diff --git a/leetcode_py/1944.py b/leetcode_py/1944.py new file mode 100644 index 0000000..8217e3d --- /dev/null +++ b/leetcode_py/1944.py @@ -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 diff --git a/leetcode_py/1998.py b/leetcode_py/1998.py new file mode 100644 index 0000000..cee6c0e --- /dev/null +++ b/leetcode_py/1998.py @@ -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 diff --git a/leetcode_py/2000.py b/leetcode_py/2000.py new file mode 100644 index 0000000..edfe30a --- /dev/null +++ b/leetcode_py/2000.py @@ -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 diff --git a/leetcode_py/2006.py b/leetcode_py/2006.py new file mode 100644 index 0000000..64957e7 --- /dev/null +++ b/leetcode_py/2006.py @@ -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 diff --git a/luogu_py/p3717.py b/luogu_py/p3717.py index ff2823d..fdd9710 100644 --- a/luogu_py/p3717.py +++ b/luogu_py/p3717.py @@ -1,27 +1,39 @@ -# Unfinish from typing import Tuple, List import math -n, m, r = input().split(" ") -locations: List[Tuple[str, str]] = [] -for i in range(int(m)): - location: Tuple[str, str] = tuple(input().split(" ")) - locations.append(location) +n, m, r = map(int, input().split()) +locations: List[Tuple[int, int]] = [] -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 -for i in range(int(n)): - for j in range(int(n)): - if map[i][j] == 0: - for k in range(int(m)): - distance = math.sqrt( - (int(locations[k][0]) - i) ** 2 + (int(locations[k][1]) - j) ** 2 - ) - if distance <= float(r): - map[i][j] = 1 - count = count + 1 - break +for location in locations: + x, y = location + if grid[x][y] != 1: + grid[x][y] = 1 + count += 1 + + # 检查半径r内的所有点 + left = max(0, x - r) + right = min(n, x + r + 1) + top = max(0, y - r) + 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(map) \ No newline at end of file +for row in grid: + print(row) + diff --git a/luogu_py/p4530.py b/luogu_py/p4530.py new file mode 100644 index 0000000..70af060 --- /dev/null +++ b/luogu_py/p4530.py @@ -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)