일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Regular Expression
- 밴픽
- 파이썬
- Python Implementation
- Python
- 715. Range Module
- data science
- Substring with Concatenation of All Words
- 프로그래머스
- Convert Sorted List to Binary Search Tree
- DWG
- attribute
- 315. Count of Smaller Numbers After Self
- 운영체제
- shiba
- Protocol
- kaggle
- LeetCode
- concurrency
- 30. Substring with Concatenation of All Words
- t1
- 시바견
- Class
- 109. Convert Sorted List to Binary Search Tree
- iterator
- 컴퓨터의 구조
- Generator
- 43. Multiply Strings
- Decorator
- Python Code
Archives
- Today
- Total
Scribbling
Number of pairs that satisfies the range condition (lower <= p[i] - p[j] <= upper) 본문
Computer Science/Algorithms & Data Structures
Number of pairs that satisfies the range condition (lower <= p[i] - p[j] <= upper)
focalpoint 2023. 5. 15. 17:26
How does one can find the number of pairs in an array that satisfies the below condition?
: lower <= p[i] - p[j] <= upper
The simplest and most intuitive approach would be iterating over all the pairs of the array, which would be O(N**2).
The mergesort approach, on the other hand, can solve the problem in O(NlogN).
The below question should be addressed with the same method.
327. Count of Range Sum
https://leetcode.com/problems/count-of-range-sum/description/
class Solution:
def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
pSum = [0]
for num in nums:
pSum.append(pSum[-1] + num)
ret = 0
def mergeSort(pSum, left, right):
if left == right:
return [pSum[left]]
nonlocal lower, upper, ret
mid = (left + right) // 2
lSum = mergeSort(pSum, left, mid)
rSum = mergeSort(pSum, mid+1, right)
l, r = 0, 0
for lVal in lSum:
while r < len(rSum) and rSum[r] - lVal <= upper:
r += 1
while l < len(rSum) and rSum[l] - lVal < lower:
l += 1
ret += r - l
pSum[left:right+1] = sorted(pSum[left:right+1])
return pSum[left:right+1]
mergeSort(pSum, 0, len(pSum)-1)
return ret
315. Count of Smaller Numbers After Self
https://leetcode.com/problems/count-of-smaller-numbers-after-self/description/
class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
N = len(nums)
ret = [0] * N
def mergesort(iarr, left, right):
if left == right:
return [iarr[left]]
mid = (left + right) // 2
larr = mergesort(iarr, left, mid)
rarr = mergesort(iarr, mid+1, right)
j = 0
for i in larr:
n1 = nums[i]
while j < len(rarr) and nums[rarr[j]] < n1:
j += 1
ret[i] += j
iarr[left:right+1] = sorted(iarr[left:right+1], key=lambda i: nums[i])
return iarr[left:right+1]
mergesort([i for i in range(N)], 0, N-1)
return ret
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
LeetCode: 173. Binary Search Tree Iterator (0) | 2023.09.30 |
---|---|
Dynamic Programming: Finding The Recurrence Relation (0) | 2023.08.22 |
LeetCode: 1639. Number of Ways to Form a Target String Given a Dictionary (0) | 2023.04.07 |
LeetCode: 358. Rearrange String k Distance Apart (0) | 2023.03.10 |
Fenwick Tree (or Binary Indexed Tree) Python Implementation, 308. Range Sum Query 2D - Mutable (0) | 2023.03.02 |