일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 315. Count of Smaller Numbers After Self
- 운영체제
- Python Code
- 시바견
- Generator
- iterator
- 파이썬
- Python
- concurrency
- Convert Sorted List to Binary Search Tree
- 프로그래머스
- Class
- Regular Expression
- 43. Multiply Strings
- Decorator
- 30. Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- DWG
- LeetCode
- t1
- attribute
- 109. Convert Sorted List to Binary Search Tree
- Protocol
- kaggle
- shiba
- 컴퓨터의 구조
- 715. Range Module
- 밴픽
- Python Implementation
- data science
- Today
- Total
Scribbling
Range Checking Algorithm (check if there's a point in a given range) 본문
Range Checking Algorithm (check if there's a point in a given range)
focalpoint 2022. 8. 16. 00:58Question: There is a stream of two different operations. One is placing a point on a single axis. The other is to check if there's a point in a given range. (see the image below)
1> Brute Force
1-1> For every range check, we can just go over all the points it includes. Time complexity for that would be O(R * N), where R is range and N is the number of range checks.
1-2> Or, we can just go over all the points and check the point is included in the range. Time complexity would be O(M * N), where M is the number of points.
2> Binary Search
If we put points in ascending order, then we can use binary search to solve the problem. Putting points in order would take O(M*logM).
Then, binary search to find out where both start and end of the range should be placed --> O(N*logM).
from bisect import bisect_left, bisect_right
points = [-1, 2, 7, 9, 13]
ranges = [(-2, 1), (2, 2), (3, 5), (10, 14)]
for s, e in ranges:
i, j = bisect_left(points, s), bisect_right(points, e)
if i != j:
print('There\'s at least one point in the given range.')
else:
print('There\'s no point in the given range.')
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
Python Sorted Containers (0) | 2022.10.26 |
---|---|
LeetCode: 1996. The Number of Weak Characters in the Game (0) | 2022.10.21 |
Monotonic Stack (0) | 2022.08.08 |
LeetCode: 839. Similar String Groups (0) | 2022.05.25 |
Shuffle Algorithm: Knuth Shuffle (0) | 2022.01.15 |