第一題
題目:分發糖果
每個人想要的糖果數目數組為arr[],問第K個糖果會給到誰?
class Solution():
def __init__(self) -> None:
pass
def solve(self,n,k,arr):
# 暴力模拟
nums = [(arr[i],i) for i in range(len(arr))]
nums.sort(reverse=True) # 從大到小排序
tot = 0 # 已經安排的個數
idx = len(nums)-1
cnt = 0 # 目前這個數已經發過的糖果
while(tot < k ):
num , cur_i = nums[idx]
if tot + (num-cnt) * len(nums) < k:
tot = tot + (num-cnt) * (idx+1) # 更新
cnt = num
idx -= 1
nums.pop()
else: # 如果不能發一圈,那麼就得逐個安排
remain = k-tot # 剩餘的糖果
nums = sorted(nums,key=lambda x:x[1])
new_id = remain % len(nums)
print(nums[new_id-1][1]+1)
return
# print(idx)
# N,K = 5,10
# nums = [2,1,5,2,4]
#6 3333333333
#1 1000000000 1000000000 1 1000000000 1000000000
N,K = list(map(int,input().split()))
nums = list(map(int,input().split()))
s = Solution()
s.solve(N,K,nums)
第二題
一個地圖具有上下左右,問,最多能走幾個坐标不出界。
第三題
輸入:
12
pypypoonyony
輸出:
2