注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
两道题地里前几天都有人发布过,可惜我没认真准备答案
1.
There are n chapters to be studied where the ith chapter has pages[i] pages. In one day, the student decides to read up to p of the remaining pages, each from some k consecutive chapters. Thus, the number of pages remaining to be read is reduced by p from each of these chapters. If a chapter has less than p pages remaining, the student reads the remaining pages, and the remaining page count is set to 0 for subsequent days. Find the minimum number of days the student needs to read all the chapters completely, i.e., the remaining page count of all chapters is 0.
Note: The chapters read must be contiguous. Example: The number of chapters is n = 3, the number of chapters read in a day is k = 2, the number of pages read from each chapter is p = 2, and the page counts of each chapter are pages = [3, 1, 4]. The chapters can be read as follows: Choose chapters 1 and 2, remaining pages to be read = [1, 0, 4]. Choose chapters 1 and 2, remaining pages to be read = [0, 0, 4]. Choose chapters 2 and 3, remaining pages to be read = [0, 0, 2]. Finally, choose chapters 2 and 3, remaining pages to be read = [0, 0, 0]. The chapters cannot be read in less than 4 days.
Function Description: Complete the function findMinimumDays in the editor below. findMinimumDays has the following parameters: int pages[n]: the number of pages in each chapter int k: the number of chapters chosen each day int p: the maximum number of pages read from each chapter in a day Returns long_int: the minimum number of days to read all pages of all chapters Constraints: 1 ≤ n ≤ 10^5 1 ≤ pages[i] ≤ 10^9 1 ≤ k ≤ n 1 ≤ p ≤ 10^9 Example 1: pages = [3, 4] k = 1 p = 2 output: 4 read the first chapter for the first two days, and the second chapter for the next two days.
下面解法是双子座给出的:import math
def findMinimumDays(pages: list[int], k: int, p: int) -> int:
"""
Finds the minimum number of days required to read all chapters.
Args:
pages: A list of integers where pages[i] is the number of pages in the i-th chapter.
k: The number of consecutive cway such that abs(candidate[i] - candidate[i+1])<=1 for 0<=i<m-1 and abs(candidate[m-1] - candidate[0]) <= 1. Find the maximum number of servers the client can buy from the n available servers
下面解法是ChatGPT给出的from collections import Counter
def getMaxServers(powers):
count = Counter(powers)
uniq = sorted(count.keys())
max_len = 0
left = 0
# 滑动窗口右边界
for right in range(len(uniq)):
# 如果当前窗口不连续,重置左边界
if right > 0 and uniq[right] != uniq[right - 1] + 1:
left = right
# 检查当前窗口是否满足构造条件
valid = True
if right - left + 1 >= 3:
for mid in range(left + 1, right): # 中间值必须出现 >=2 次
if count[uniq[mid]] < 2:
valid = False
break
if valid:
# 计算当前窗口长度(所有值的总数)
total = sum(count[uniq[i]] for i in range(left, right + 1))
max_len = max(max_len, total)
# 单独考虑一个值的情况(例如很多个2)
for val in count.values():
max_len = max(max_len, val)
return max_len
# 示例测试
powers = [7,2,2,3,2,1,2,2]
print(getMaxServers(powers)) # 输出应为 7
|