Skip to content

Commit 6779119

Browse files
committed
feat: add solutions for a week
1 parent 8578f7c commit 6779119

10 files changed

+584
-0
lines changed

Diff for: README.md

+8
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@
283283
| 847 | Shortest Path Visiting All Nodes | [Ruby](./algorithms/ruby/0847-shortest-path-visiting-all-nodes.rb) | Hard |
284284
| 852 | Peak Index in a Mountain Array | [Ruby](./algorithms/ruby/0852-peak-index-in-a-mountain-array.rb) | Medium |
285285
| 859 | Buddy Strings | [Ruby](./algorithms/ruby/0859-buddy-strings.rb) | Easy |
286+
| 862 | Shortest Subarray with Sum at Least K | [Ruby](./algorithms/ruby/0862-shortest-subarray-with-sum-at-least-k.rb) | Hard |
286287
| 863 | All Nodes Distance K in Binary Tree | [Ruby](./algorithms/ruby/0863-all-nodes-distance-k-in-binary-tree.rb) | Medium |
287288
| 864 | Backspace String Compare | [Ruby](./algorithms/ruby/0864-shortest-path-to-get-all-keys.rb) | Hard |
288289
| 867 | Transpose Matrix | [Ruby](./algorithms/ruby/0867-transpose-matrix.rb) | Easy |
@@ -400,6 +401,7 @@
400401
| 1561 | Maximum Number of Coins You Can Get | [Ruby](./algorithms/ruby/1561-maximum-number-of-coins-you-can-get.rb) | Medium |
401402
| 1569 | Number of Ways to Reorder Array to Get Same BST | [Ruby](./algorithms/ruby/1569-number-of-ways-to-reorder-array-to-get-same-bst.rb) | Hard |
402403
| 1572 | Matrix Diagonal Sum | [Ruby](./algorithms/ruby/1572-matrix-diagonal-sum.rb) | Easy |
404+
| 1574 | Shortest Subarray to be Removed to Make Array Sorted | [Ruby](./algorithms/ruby/1574-shortest-subarray-to-be-removed-to-make-array-sorted.rb) | Medium |
403405
| 1575 | Count All Possible Routes | [Ruby](./algorithms/ruby/1575-count-all-possible-routes.rb) | Hard |
404406
| 1579 | Remove Max Number of Edges to Keep Graph Fully Traversable | [Ruby](./algorithms/ruby/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.rb) | Hard |
405407
| 1584 | Min Cost to Connect All Points | [Ruby](./algorithms/ruby/1584-min-cost-to-connect-all-points.rb) | Medium |
@@ -456,6 +458,8 @@
456458
| 2024 | Maximize the Confusion of an Exam | [Ruby](./algorithms/ruby/2024-maximize-the-confusion-of-an-exam.rb) | Medium |
457459
| 2044 | Count Number of Maximum Bitwise-OR Subsets | [Ruby](./algorithms/ruby/2044-count-number-of-maximum-bitwise-or-subsets.rb) | Medium |
458460
| 2050 | Parallel Courses III | [Ruby](./algorithms/ruby/2050-parallel-courses-iii.rb) | Hard |
461+
| 2064 | Minimized Maximum of Products Distributed to Any Store | [Ruby](./algorithms/ruby/2064-minimized-maximum-of-products-distributed-to-any-store.rb) | Medium |
462+
| 2070 | Most Beautiful Item for Each Query | [Ruby](./algorithms/ruby/2070-most-beautiful-item-for-each-query.rb) | Medium |
459463
| 2090 | K Radius Subarray Averages | [Ruby](./algorithms/ruby/2090-k-radius-subarray-averages.rb) | Medium |
460464
| 2095 | Delete the Middle Node of a Linked List | [Ruby](./algorithms/ruby/2095-delete-the-middle-node-of-a-linked-list.rb) | Medium |
461465
| 2101 | Detonate the Maximum Bombs | [Ruby](./algorithms/ruby/2101-detonate-the-maximum-bombs.rb) | Medium |
@@ -503,8 +507,10 @@
503507
| 2501 | Longest Square Streak in an Array | [Ruby](./algorithms/ruby/2501-longest-square-streak-in-an-array.rb) | Medium |
504508
| 2542 | Maximum Subsequence Score | [Ruby](./algorithms/ruby/2542-maximum-subsequence-score.rb) | Medium |
505509
| 2551 | Put Marbles in Bags | [Ruby](./algorithms/ruby/2551-put-marbles-in-bags.rb) | Hard |
510+
| 2563 | Count the Number of Fair Pairs | [Ruby](./algorithms/ruby/2563-count-the-number-of-fair-pairs.rb) | Medium |
506511
| 2583 | Kth Largest Sum in a Binary Tree | [Ruby](./algorithms/ruby/2583-kth-largest-sum-in-a-binary-tree.rb) | Medium |
507512
| 2595 | Number of Even and Odd Bits | [Ruby](./algorithms/ruby/2595-number-of-even-and-odd-bits.rb) | Easy |
513+
| 2601 | Prime Subtraction Operation | [Ruby](./algorithms/ruby/2601-prime-subtraction-operation.rb) | Medium |
508514
| 2616 | Minimize the Maximum Difference of Pairs | [Ruby](./algorithms/ruby/2616-minimize-the-maximum-difference-of-pairs.rb) | Medium |
509515
| 2641 | Cousins in Binary Tree II | [Ruby](./algorithms/ruby/2641-cousins-in-binary-tree-ii.rb) | Medium |
510516
| 2642 | Design Graph With Shortest Path Calculator | [Ruby](./algorithms/ruby/2642-design-graph-with-shortest-path-calculator.rb) | Hard |
@@ -519,7 +525,9 @@
519525
| 2849 | Determine if a Cell Is Reachable at a Given Time | [Ruby](./algorithms/ruby/2849-determine-if-a-cell-is-reachable-at-a-given-time) | Medium |
520526
| 2966 | Divide Array Into Arrays With Max Difference | [Ruby](./algorithms/ruby/2966-divide-array-into-arrays-with-max-difference) | Medium |
521527
| 3011 | Find if Array Can Be Sorted | [Ruby](./algorithms/ruby/3011-find-if-array-can-be-sorted.rb) | Medium |
528+
| 3097 | Shortest Subarray With OR At Least K II | [Ruby](./algorithms/ruby/3097-shortest-subarray-with-or-at-least-k-ii.rb) | Medium |
522529
| 3163 | String Compression III | [Ruby](./algorithms/ruby/3163-string-compression-iii.rb) | Medium |
530+
| 3254 | Find the Power of K-Size Subarrays I | [Ruby](./algorithms/ruby/3254-find-the-power-of-k-size-subarrays-i.rb) | Medium |
523531

524532
### Database
525533
| # | Title | Solution | Difficulty |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# frozen_string_literal: true
2+
3+
# 862. Shortest Subarray with Sum at Least K
4+
# https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k
5+
# Hard
6+
7+
=begin
8+
Given an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1.
9+
A subarray is a contiguous part of an array.
10+
11+
Example 1:
12+
Input: nums = [1], k = 1
13+
Output: 1
14+
Example 2:
15+
Input: nums = [1,2], k = 4
16+
Output: -1
17+
Example 3:
18+
Input: nums = [2,-1,2], k = 3
19+
Output: 3
20+
21+
Constraints:
22+
* 1 <= nums.length <= 105
23+
* -105 <= nums[i] <= 105
24+
* 1 <= k <= 109
25+
=end
26+
27+
# @param {Integer[]} nums
28+
# @param {Integer} k
29+
# @return {Integer}
30+
def shortest_subarray(nums, k)
31+
n = nums.length
32+
sum = Array.new(n + 1, 0)
33+
34+
n.times do |i|
35+
sum[i + 1] = sum[i] + nums[i]
36+
end
37+
38+
q = Array.new(n + 1)
39+
l = 0
40+
r = 0
41+
min_length = n + 1
42+
43+
sum.length.times do |i|
44+
while r > l && sum[i] >= sum[q[l]] + k
45+
min_length = [min_length, i - q[l]].min
46+
l += 1
47+
end
48+
49+
while r > l && sum[i] <= sum[q[r - 1]]
50+
r -= 1
51+
end
52+
53+
q[r] = i
54+
r += 1
55+
end
56+
57+
min_length <= n ? min_length : -1
58+
end
59+
60+
# **************** #
61+
# TEST #
62+
# **************** #
63+
64+
require "test/unit"
65+
class Test_shortest_subarray < Test::Unit::TestCase
66+
def test_
67+
assert_equal 1, shortest_subarray([1], 1)
68+
assert_equal(-1, shortest_subarray([1, 2], 4))
69+
assert_equal 3, shortest_subarray([2, -1, 2], 3)
70+
end
71+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
# 1574. Shortest Subarray to be Removed to Make Array Sorted
4+
# https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted
5+
# Medium
6+
7+
=begin
8+
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing.
9+
Return the length of the shortest subarray to remove.
10+
A subarray is a contiguous subsequence of the array.
11+
12+
Example 1:
13+
Input: arr = [1,2,3,10,4,2,3,5]
14+
Output: 3
15+
Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted.
16+
Another correct solution is to remove the subarray [3,10,4].
17+
Example 2:
18+
Input: arr = [5,4,3,2,1]
19+
Output: 4
20+
Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1].
21+
Example 3:
22+
Input: arr = [1,2,3]
23+
Output: 0
24+
Explanation: The array is already non-decreasing. We do not need to remove any elements.
25+
26+
Constraints:
27+
* 1 <= arr.length <= 105
28+
* 0 <= arr[i] <= 109
29+
=end
30+
31+
# @param {Integer[]} arr
32+
# @return {Integer}
33+
def find_length_of_shortest_subarray(arr)
34+
n = arr.size
35+
left = 0
36+
left += 1 while left + 1 < n && arr[left] <= arr[left + 1]
37+
return 0 if left == n - 1
38+
39+
right = n - 1
40+
right -= 1 while right - 1 >= 0 && arr[right - 1] <= arr[right]
41+
42+
min_length = [n - left - 1, right].min
43+
44+
i = 0
45+
j = right
46+
while i <= left && j < n
47+
if arr[i] <= arr[j]
48+
min_length = [min_length, j - i - 1].min
49+
i += 1
50+
else
51+
j += 1
52+
end
53+
end
54+
55+
min_length
56+
end
57+
58+
# ********************#
59+
# TEST #
60+
# ********************#
61+
62+
require "test/unit"
63+
class Test_find_length_of_shortest_subarray < Test::Unit::TestCase
64+
def test_
65+
assert_equal 3, find_length_of_shortest_subarray([1, 2, 3, 10, 4, 2, 3, 5])
66+
assert_equal 4, find_length_of_shortest_subarray([5, 4, 3, 2, 1])
67+
assert_equal 0, find_length_of_shortest_subarray([1, 2, 3])
68+
end
69+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
# 2064. Minimized Maximum of Products Distributed to Any Store
4+
# https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store
5+
# Medium
6+
7+
=begin
8+
You are given an integer n indicating there are n specialty retail stores. There are m product types of varying amounts, which are given as a 0-indexed integer array quantities, where quantities[i] represents the number of products of the ith product type.
9+
You need to distribute all products to the retail stores following these rules:
10+
* A store can only be given at most one product type but can be given any amount of it.
11+
* After distribution, each store will have been given some number of products (possibly 0). Let x represent the maximum number of products given to any store. You want x to be as small as possible, i.e., you want to minimize the maximum number of products that are given to any store.
12+
Return the minimum possible x.
13+
14+
Example 1:
15+
Input: n = 6, quantities = [11,6]
16+
Output: 3
17+
Explanation: One optimal way is:
18+
- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
19+
- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
20+
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.
21+
22+
Example 2:
23+
Input: n = 7, quantities = [15,10,10]
24+
Output: 5
25+
Explanation: One optimal way is:
26+
- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
27+
- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
28+
- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
29+
The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.
30+
31+
Example 3:
32+
Input: n = 1, quantities = [100000]
33+
Output: 100000
34+
Explanation: The only optimal way is:
35+
- The 100000 products of type 0 are distributed to the only store.
36+
The maximum number of products given to any store is max(100000) = 100000.
37+
38+
Constraints:
39+
* m == quantities.length
40+
* 1 <= m <= n <= 105
41+
* 1 <= quantities[i] <= 105
42+
=end
43+
44+
# @param {Integer} n
45+
# @param {Integer[]} quantities
46+
# @return {Integer}
47+
def minimized_maximum(n, quantities)
48+
ok = quantities.max
49+
ng = 0
50+
while (ok - ng).abs > 1
51+
mid = (ok + ng) / 2
52+
if quantities.sum { |q| (q + mid - 1) / mid } <= n
53+
ok = mid
54+
else
55+
ng = mid
56+
end
57+
end
58+
ok
59+
end
60+
61+
# **************** #
62+
# TEST #
63+
# **************** #
64+
65+
require "test/unit"
66+
class Test_minimized_maximum < Test::Unit::TestCase
67+
def test_
68+
assert_equal 3, minimized_maximum(6, [11, 6])
69+
assert_equal 5, minimized_maximum(7, [15, 10, 10])
70+
assert_equal 100000, minimized_maximum(1, [100000])
71+
end
72+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
# 2070. Most Beautiful Item for Each Query
4+
# Medium
5+
# https://leetcode.com/problems/most-beautiful-item-for-each-query
6+
7+
=begin
8+
You are given a 2D integer array items where items[i] = [pricei, beautyi] denotes the price and beauty of an item respectively.
9+
You are also given a 0-indexed integer array queries. For each queries[j], you want to determine the maximum beauty of an item whose price is less than or equal to queries[j]. If no such item exists, then the answer to this query is 0.
10+
Return an array answer of the same length as queries where answer[j] is the answer to the jth query.
11+
12+
Example 1:
13+
Input: items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6]
14+
Output: [2,4,5,5,6,6]
15+
Explanation:
16+
- For queries[0]=1, [1,2] is the only item which has price <= 1. Hence, the answer for this query is 2.
17+
- For queries[1]=2, the items which can be considered are [1,2] and [2,4].
18+
The maximum beauty among them is 4.
19+
- For queries[2]=3 and queries[3]=4, the items which can be considered are [1,2], [3,2], [2,4], and [3,5].
20+
The maximum beauty among them is 5.
21+
- For queries[4]=5 and queries[5]=6, all items can be considered.
22+
Hence, the answer for them is the maximum beauty of all items, i.e., 6.
23+
Example 2:
24+
Input: items = [[1,2],[1,2],[1,3],[1,4]], queries = [1]
25+
Output: [4]
26+
Explanation:
27+
The price of every item is equal to 1, so we choose the item with the maximum beauty 4.
28+
Note that multiple items can have the same price and/or beauty.
29+
Example 3:
30+
Input: items = [[10,1000]], queries = [5]
31+
Output: [0]
32+
Explanation:
33+
No item has a price less than or equal to 5, so no item can be chosen.
34+
Hence, the answer to the query is 0.
35+
36+
Constraints:
37+
* 1 <= items.length, queries.length <= 105
38+
* items[i].length == 2
39+
* 1 <= pricei, beautyi, queries[j] <= 109
40+
=end
41+
42+
# @param {Integer[][]} items
43+
# @param {Integer[]} queries
44+
# @return {Integer[]}
45+
def maximum_beauty(items, queries)
46+
items.sort_by!(&:last)
47+
queries.collect { |q| items.reverse_each.inject(nil) { |res, (p, b)| break b if p <= q } || 0 }
48+
end
49+
50+
# **************** #
51+
# TEST #
52+
# **************** #
53+
54+
require "test/unit"
55+
class Test_maximum_beauty < Test::Unit::TestCase
56+
def test_
57+
assert_equal [2, 4, 5, 5, 6, 6], maximum_beauty([[1, 2], [3, 2], [2, 4], [5, 6], [3, 5]], [1, 2, 3, 4, 5, 6])
58+
assert_equal [4], maximum_beauty([[1, 2], [1, 2], [1, 3], [1, 4]], [1])
59+
assert_equal [0], maximum_beauty([[10, 1000]], [5])
60+
end
61+
end
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
# 2563. Count the Number of Fair Pairs
4+
# https://leetcode.com/problems/count-the-number-of-fair-pairs
5+
# Medium
6+
7+
=begin
8+
Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs.
9+
A pair (i, j) is fair if:
10+
* 0 <= i < j < n, and
11+
* lower <= nums[i] + nums[j] <= upper
12+
13+
Example 1:
14+
Input: nums = [0,1,7,4,4,5], lower = 3, upper = 6
15+
Output: 6
16+
Explanation: There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5).
17+
Example 2:
18+
Input: nums = [1,7,9,2,5], lower = 11, upper = 11
19+
Output: 1
20+
Explanation: There is a single fair pair: (2,3).
21+
22+
Constraints:
23+
* 1 <= nums.length <= 105
24+
* nums.length == n
25+
* -109 <= nums[i] <= 109
26+
* -109 <= lower <= upper <= 109
27+
=end
28+
29+
# @param {Integer[]} nums
30+
# @param {Integer} lower
31+
# @param {Integer} upper
32+
# @return {Integer}
33+
def count_fair_pairs(nums, lower, upper)
34+
nums.sort!
35+
36+
nums.each_with_index.sum do |num, i|
37+
lower_idx = nums.bsearch_index { |x| x >= lower - num } || nums.size
38+
lower_idx = i + 1 if lower_idx <= i
39+
40+
upper_idx = nums.bsearch_index { |x| x > upper - num } || nums.size
41+
next 0 if upper_idx < lower_idx
42+
43+
upper_idx - lower_idx
44+
end
45+
end
46+
47+
# **************** #
48+
# TEST #
49+
# **************** #
50+
51+
require "test/unit"
52+
class Test_count_fair_pairs < Test::Unit::TestCase
53+
def test_
54+
assert_equal 6, count_fair_pairs([0, 1, 7, 4, 4, 5], 3, 6)
55+
assert_equal 1, count_fair_pairs([1, 7, 9, 2, 5], 11, 11)
56+
end
57+
end

0 commit comments

Comments
 (0)