fix(posts): fetch daily from files
This commit is contained in:
parent
3d2ee97afe
commit
2bcc3f4675
9 changed files with 691 additions and 0 deletions
|
|
@ -0,0 +1 @@
|
|||
|
||||
28
public/code/algorithms/leetcode-daily/beauty.cpp
Normal file
28
public/code/algorithms/leetcode-daily/beauty.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
std::vector<int> maximumBeauty(std::vector<std::vector<int>>& items, std::vector<int>& queries) {
|
||||
std::sort(items.begin(), items.end());
|
||||
std::vector<std::pair<int, int>> sorted_queries;
|
||||
sorted_queries.reserve(queries.size());
|
||||
// couple queries with their indices
|
||||
for (size_t i = 0; i < queries.size(); ++i) {
|
||||
sorted_queries.emplace_back(queries[i], i);
|
||||
}
|
||||
std::sort(sorted_queries.begin(), sorted_queries.end());
|
||||
|
||||
int beauty = items[0][1];
|
||||
size_t i = 0;
|
||||
std::vector<int> ans(queries.size());
|
||||
|
||||
for (const auto [query, index] : sorted_queries) {
|
||||
while (i < items.size() && items[i][0] <= query) {
|
||||
beauty = std::max(beauty, items[i][1]);
|
||||
++i;
|
||||
}
|
||||
// invariant: items[i - 1] is the rightmost considerable item
|
||||
ans[index] = i > 0 && items[i - 1][0] <= query ? beauty : 0;
|
||||
}
|
||||
|
||||
return std::move(ans);
|
||||
}
|
||||
11
public/code/algorithms/leetcode-daily/cfps-naive.py
Normal file
11
public/code/algorithms/leetcode-daily/cfps-naive.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def countFairPairs(self, nums, lower, upper):
|
||||
nums.sort()
|
||||
ans = 0
|
||||
|
||||
for i, num in enumerate(nums):
|
||||
k = bisect_left(nums, lower - num, 0, i)
|
||||
j = bisect_right(nums, upper - num, 0, i)
|
||||
|
||||
ans += k - j
|
||||
|
||||
return ans
|
||||
16
public/code/algorithms/leetcode-daily/cfps-twoptr.py
Normal file
16
public/code/algorithms/leetcode-daily/cfps-twoptr.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
def countFairPairs(self, nums, lower, upper):
|
||||
nums.sort()
|
||||
ans = 0
|
||||
|
||||
def pairs_leq(x: int) -> int:
|
||||
pairs = 0
|
||||
l, r = 0, len(nums) - 1
|
||||
while l < r:
|
||||
if nums[l] + nums[r] <= x:
|
||||
pairs += r - l
|
||||
l += 1
|
||||
else:
|
||||
r -= 1
|
||||
return pairs
|
||||
|
||||
return pairs_leq(upper) - pairs_leq(lower - 1)
|
||||
16
public/code/algorithms/leetcode-daily/minend.cpp
Normal file
16
public/code/algorithms/leetcode-daily/minend.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
long long minEnd(int n, long long x) {
|
||||
int bits_to_distribute = n - 1;
|
||||
long long mask = 1;
|
||||
|
||||
while (bits_to_distribute > 0) {
|
||||
if ((x & mask) == 0) {
|
||||
// if the bit should be set, set it-otherwise, leave it alone
|
||||
if ((bits_to_distribute & 1) == 1)
|
||||
x |= mask;
|
||||
bits_to_distribute >>= 1;
|
||||
}
|
||||
mask <<= 1;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
30
public/code/algorithms/leetcode-daily/msl-bitwise.py
Normal file
30
public/code/algorithms/leetcode-daily/msl-bitwise.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
def minimumSubarrayLength(self, nums, k):
|
||||
ans = sys.maxsize
|
||||
|
||||
largest = max(*nums, k)
|
||||
num_digits = floor((log(max(largest, 1))) / log(2)) + 1
|
||||
|
||||
counts = [0] * num_digits
|
||||
l = 0
|
||||
|
||||
def update(x, delta):
|
||||
for i in range(len(counts)):
|
||||
if x & 1:
|
||||
counts[i] += delta
|
||||
x >>= 1
|
||||
|
||||
def bitwise_or():
|
||||
return reduce(
|
||||
operator.or_,
|
||||
(1 << i if count else 0 for i, count in enumerate(counts)),
|
||||
0
|
||||
)
|
||||
|
||||
for r, num in enumerate(nums):
|
||||
update(num, 1)
|
||||
while l <= r and bitwise_or() >= k:
|
||||
ans = min(ans, r - l + 1)
|
||||
update(nums[l], -1)
|
||||
l += 1
|
||||
|
||||
return -1 if ans == sys.maxsize else ans
|
||||
18
public/code/algorithms/leetcode-daily/msl-naive.py
Normal file
18
public/code/algorithms/leetcode-daily/msl-naive.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
def minimumSubarrayLength(self, nums, k):
|
||||
# provide a sentinel for "no window found"
|
||||
ans = sys.maxsize
|
||||
window = deque()
|
||||
l = 0
|
||||
|
||||
# expand the window by default
|
||||
for r in range(len(nums)):
|
||||
# consider `nums[r]`
|
||||
window.append(nums[r])
|
||||
# shrink window while valid
|
||||
while l <= r and reduce(operator.or_, window) >= k:
|
||||
ans = min(ans, r - l + 1)
|
||||
window.popleft()
|
||||
l += 1
|
||||
|
||||
# normalize to -1 as requested
|
||||
return -1 if ans == sys.maxsize else ans
|
||||
Loading…
Add table
Add a link
Reference in a new issue