일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Python
- 시바견
- Generator
- 715. Range Module
- 프로그래머스
- 컴퓨터의 구조
- Class
- Protocol
- Regular Expression
- iterator
- 운영체제
- Decorator
- kaggle
- shiba
- 315. Count of Smaller Numbers After Self
- data science
- 30. Substring with Concatenation of All Words
- t1
- concurrency
- 밴픽
- 43. Multiply Strings
- Python Code
- LeetCode
- Python Implementation
- DWG
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- Substring with Concatenation of All Words
- 파이썬
- attribute
- Today
- Total
Scribbling
Python Sorted Containers 본문
Python Sorted Containers
focalpoint 2022. 10. 26. 07:24
Python does not provide standard library for ordered set or ordered dictionary.
However, there's an Apache2 licensed sorted collections library "SortedContainer".
You can install the library with the following cmd.
pip install sortedcontainers
The library provides three different containers, which are SortedList, SortedSet, SortedDict.
SortedList
.add(value): add a value to the list within O(logN) time.
.update(iterable): update the list with the given iterable in O(k*logN) time.
.remove(value) / .discard(value): removes the value in the list in O(logN) time.
SortedList can be very useful when you need to remove elements in the list in an efficient manner.
LeetCode Prob 1606. Find Servers That Handled Most Number of Requests (https://leetcode.com/problems/find-servers-that-handled-most-number-of-requests/) is a good example.
In this problem setting, we need to remove elements in idleServers when a server starts working on a request. With default list in Python, removing element would take O(N) time causing inefficiency. A convenient way to deal with such situation would be exploiting the advantage of "SortedList".
from sortedcontainers import SortedList
from bisect import bisect_left
import heapq
class Solution:
def busiestServers(self, k: int, arrival: List[int], load: List[int]) -> List[int]:
# (finishTime, serverID)
schedules = []
# serverID
idleServers = SortedList(list(range(k)))
# current time
curTime = 0
# workLoads
workLoads = [0] * k
for i, (t, l) in enumerate(zip(arrival, load)):
curTime = t
while schedules and schedules[0][0] <= curTime:
_, serverID = heapq.heappop(schedules)
idleServers.add(serverID)
if not idleServers:
continue
num = i % k
idx = bisect_left(idleServers, num)
if idx < len(idleServers):
serverID = idleServers[idx]
else:
serverID = idleServers[0]
heapq.heappush(schedules, (curTime+l, serverID))
idleServers.discard(serverID)
workLoads[serverID] += 1
maxLoad = max(workLoads)
return [i for i in range(k) if workLoads[i] == maxLoad]
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
Recursive Descent Parsing - Python Implementation (0) | 2022.11.12 |
---|---|
Rabin-Karp Algorithm (2) | 2022.11.09 |
LeetCode: 1996. The Number of Weak Characters in the Game (0) | 2022.10.21 |
Range Checking Algorithm (check if there's a point in a given range) (0) | 2022.08.16 |
Monotonic Stack (0) | 2022.08.08 |