程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Non leetcode Title brushing record [engineering implementation title] - Python code + detailed notes

編輯:Python

Engineering realization problem

# class Solution:
# def minimumTimeRequired(self, jobs: List[int], k: int) -> int:
# self.record_dict = collections.defaultdict(list)
# self.count = 0
class SkiRankingSystem:
def __init__(self):
self.record_dict = collections.defaultdict(list)
self.count = 0
# addRecord(int userId, int duration)
# Add a skiing record , The same userId You can add multiple skiing records
def add_record(self, user_id: int, duration: int) -> None:
self.count += 1
self.record_dict[user_id].append((duration, self.count))
self.record_dict[user_id].sort()
# int[] getTopAthletes(int num)
# Return to the front of the fastest skiing num An athlete , Return with array , Row from low to high according to the skiing time , If the time is the same, it will return in the order of records
def get_top_athletes(self, num: int) -> List[int]:
athletes = sorted(self.record_dict.items(), key=lambda item: (item[1][0][0], item[1][0][1]))
# item[1] Is to take value, because item[0] yes key
# item[1][0] Is to take the smallest of each athlete duration, Because the smallest one was recorded before duration Top row
# item[1][0][0] Is the minimum length of time recorded duration,item[1][0][1] Is the recording sequence of the minimum duration record , In the corresponding title “ If the time is the same, it will return in the order of records ” The requirements of
result = []
for i in athletes[:num]:
result.append(i[0])
print("get_top_athletes:", result)
print("athletes:",athletes)
return result
# int[] queryTop3Record(int userId)
# Return to the fastest skater 3 Time record ,( Note that the data here can be repeated , The same athlete can ski many times for the same time , If it is the same, it also needs to return )
def query_top3_record(self, user_id: int) -> List[int]:
record = self.record_dict[user_id][:3]
print("query_top3_record:", record)
return [i[0] for i in record]
# Use cases
add_record(self, 1, 5)
add_record(self, 1, 33)
add_record(self, 1, 22)
add_record(self, 1, 44)
add_record(self, 2, 8)
add_record(self, 3, 44)
add_record(self, 4, 11)
add_record(self, 4, 5)
print("list1:", self.record_dict)
get_top_athletes(self, 4)
add_record(self, 3, 8)
query_top3_record(self, 1)
print("list2:", self.record_dict)
# stdout
# list1: defaultdict(<class 'list'>, {1: [(5, 1), (22, 3), (33, 2), (44, 4)], 2: [(8, 5)], 3: [(44, 6)], 4: [(5, 8), (11, 7)]})
# get_top_athletes: [1, 4, 2, 3]
# athletes: [(1, [(5, 1), (22, 3), (33, 2), (44, 4)]), (4, [(5, 8), (11, 7)]), (2, [(8, 5)]), (3, [(44, 6)])]
# query_top3_record: [(5, 1), (22, 3), (33, 2)]
# list2: defaultdict(<class 'list'>, {1: [(5, 1), (22, 3), (33, 2), (44, 4)], 2: [(8, 5)], 3: [(8, 9), (44, 6)], 4: [(5, 8), (11, 7)]})
# Input example 1:
# SkiRankingSystem sys = new SkiRankingSystem();
# sys.addRecord(1, 10);
# sys.addRecord(2, 8);
# sys.getTopAthletes(3); return [2,1]
# sys.queryTop3Record(1); return [10]
# Input example 2:
# SkiRankingSystem sys = new SkiRankingSystem();
# sys.addRecord(20, 8);
# sys.addRecord(22, 6);
# sys.addRecord(20, 6);
# sys.getTopAthletes(4);# return [22, 20];
# sys.addRecord(33, 5);
# sys.addRecord(22, 9);
# sys.addRecord(31, 4);
# sys.getTopAthletes(4);# return [31, 33, 22, 20];
# sys.addRecord(20, 8);
# sys.queryTop3Record(20);# return [6, 8, 8]
# sys.addRecord(20, 6);
# sys.queryTop3Record(20);# return [6, 6, 8]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved