일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- LeetCode
- 43. Multiply Strings
- attribute
- kaggle
- concurrency
- 파이썬
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- Protocol
- 30. Substring with Concatenation of All Words
- Python Implementation
- shiba
- 315. Count of Smaller Numbers After Self
- Regular Expression
- Python
- data science
- Python Code
- Substring with Concatenation of All Words
- t1
- Generator
- 프로그래머스
- 컴퓨터의 구조
- 시바견
- 밴픽
- 715. Range Module
- Class
- DWG
- Decorator
- 운영체제
- iterator
- Today
- Total
Scribbling
Number of pairs that satisfies the range condition (lower <= p[i] - p[j] <= upper) 본문
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/
Count of Range Sum - LeetCode
Can you solve this real interview question? Count of Range Sum - Given an integer array nums and two integers lower and upper, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in
leetcode.com
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/
Count of Smaller Numbers After Self - LeetCode
Can you solve this real interview question? Count of Smaller Numbers After Self - Given an integer array nums, return an integer array counts where counts[i] is the number of smaller elements to the right of nums[i]. Example 1: Input: nums = [5,2,6,1] O
leetcode.com
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 |