Skip to content

Commit 8578f7c

Browse files
committed
feat: add 1829 and 2275
1 parent 6c7c029 commit 8578f7c

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@
436436
| 1802 | Maximum Value at a Given Index in a Bounded Array | [Ruby](./algorithms/ruby/1802-maximum-value-at-a-given-index-in-a-bounded-array.rb) | Medium |
437437
| 1814 | Count Nice Pairs in an Array | [Ruby](./algorithms/ruby/1814-count-nice-pairs-in-an-array.rb) | Medium |
438438
| 1822 | Sign of the Product of an Array | [Ruby](./algorithms/ruby/1822-sign-of-the-product-of-an-array.rb) | Easy |
439+
| 1829 | Maximum XOR for Each Query | [Ruby](./algorithms/ruby/1829-maximum-xor-for-each-query.rb) | Medium |
439440
| 1838 | Frequency of the Most Frequent Element | [Ruby](./algorithms/ruby/1838-frequency-of-the-most-frequent-element.rb) | Medium |
440441
| 1845 | Seat Reservation Manager | [Ruby](./algorithms/ruby/1845-seat-reservation-manager.rb) | Medium |
441442
| 1846 | Maximum Element After Decreasing and Rearranging | [Ruby](./algorithms/ruby/1846-maximum-element-after-decreasing-and-rearranging.rb) | Medium |
@@ -470,6 +471,7 @@
470471
| 2264 | Largest 3-Same-Digit Number in String | [Ruby](./algorithms/ruby/2264-largest-3-same-digit-number-in-string.rb) | Easy |
471472
| 2265 | Count Nodes Equal to Average of Subtree | [Ruby](./algorithms/ruby/2265-count-nodes-equal-to-average-of-subtree.rb) | Medium |
472473
| 2272 | Substring With Largest Variance | [Ruby](./algorithms/ruby/2272-substring-with-largest-variance.rb) | Hard |
474+
| 2275 | Largest Combination With Bitwise AND Greater Than Zero | [Ruby](./algorithms/ruby/2275-largest-combination-with-bitwise-and-greater-than-zero.rb) | Medium |
473475
| 2300 | Successful Pairs of Spells and Potions | [Ruby](./algorithms/ruby/2300-successful-pairs-of-spells-and-potions.rb) | Medium |
474476
| 2305 | Fair Distribution of Cookies | [Ruby](./algorithms/ruby/2305-fair-distribution-of-cookies.rb) | Medium |
475477
| 2316 | Count Unreachable Pairs of Nodes in an Undirected Graph | [Ruby](./algorithms/ruby/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph.rb) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
# 1829. Maximum XOR for Each Query
4+
# Medium
5+
# https://leetcode.com/problems/maximum-xor-for-each-query
6+
7+
=begin
8+
You are given a sorted array nums of n non-negative integers and an integer maximumBit. You want to perform the following query n times:
9+
1. Find a non-negative integer k < 2maximumBit such that nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k is maximized. k is the answer to the ith query.
10+
2. Remove the last element from the current array nums.
11+
Return an array answer, where answer[i] is the answer to the ith query.
12+
13+
Example 1:
14+
Input: nums = [0,1,1,3], maximumBit = 2
15+
Output: [0,3,2,3]
16+
Explanation: The queries are answered as follows:
17+
1st query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.
18+
2nd query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.
19+
3rd query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.
20+
4th query: nums = [0], k = 3 since 0 XOR 3 = 3.
21+
Example 2:
22+
Input: nums = [2,3,4,7], maximumBit = 3
23+
Output: [5,2,6,5]
24+
Explanation: The queries are answered as follows:
25+
1st query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.
26+
2nd query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.
27+
3rd query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.
28+
4th query: nums = [2], k = 5 since 2 XOR 5 = 7.
29+
Example 3:
30+
Input: nums = [0,1,2,2,5,7], maximumBit = 3
31+
Output: [4,3,6,4,6,7]
32+
33+
Constraints:
34+
* nums.length == n
35+
* 1 <= n <= 105
36+
* 1 <= maximumBit <= 20
37+
* 0 <= nums[i] < 2maximumBit
38+
* nums​​​ is sorted in ascending order.
39+
=end
40+
41+
# @param {Integer[]} nums
42+
# @param {Integer} maximum_bit
43+
# @return {Integer[]}
44+
def get_maximum_xor(nums, maximum_bit)
45+
mask = (1 << maximum_bit) - 1
46+
curr = 0
47+
nums.map do |num|
48+
curr ^= num
49+
curr ^ mask
50+
end.reverse
51+
end
52+
53+
# **************** #
54+
# TEST #
55+
# **************** #
56+
57+
require "test/unit"
58+
class Test_get_maximum_xor < Test::Unit::TestCase
59+
def test_
60+
assert_equal [0, 3, 2, 3], get_maximum_xor([0, 1, 1, 3], 2)
61+
assert_equal [5, 2, 6, 5], get_maximum_xor([2, 3, 4, 7], 3)
62+
assert_equal [4, 3, 6, 4, 6, 7], get_maximum_xor([0, 1, 2, 2, 5, 7], 3)
63+
end
64+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
# 2275. Largest Combination With Bitwise AND Greater Than Zero
4+
# Medium
5+
# https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero
6+
7+
=begin
8+
The bitwise AND of an array nums is the bitwise AND of all integers in nums.
9+
* For example, for nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1.
10+
* Also, for nums = [7], the bitwise AND is 7.
11+
You are given an array of positive integers candidates. Evaluate the bitwise AND of every combination of numbers of candidates. Each number in candidates may only be used once in each combination.
12+
Return the size of the largest combination of candidates with a bitwise AND greater than 0.
13+
14+
Example 1:
15+
Input: candidates = [16,17,71,62,12,24,14]
16+
Output: 4
17+
Explanation: The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0.
18+
The size of the combination is 4.
19+
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
20+
Note that more than one combination may have the largest size.
21+
For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.
22+
Example 2:
23+
Input: candidates = [8,8]
24+
Output: 2
25+
Explanation: The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0.
26+
The size of the combination is 2, so we return 2.
27+
28+
Constraints:
29+
* 1 <= candidates.length <= 105
30+
* 1 <= candidates[i] <= 107
31+
=end
32+
33+
# @param {Integer[]} candidates
34+
# @return {Integer}
35+
def largest_combination(candidates)
36+
max_count = 0
37+
count = 0
38+
39+
for i in 0..31 do
40+
count = 0
41+
42+
candidates.each do |num|
43+
if num & (1 << i) != 0
44+
count += 1
45+
end
46+
end
47+
48+
max_count = [count, max_count].max
49+
end
50+
51+
max_count
52+
end
53+
54+
# **************** #
55+
# TEST #
56+
# **************** #
57+
58+
require "test/unit"
59+
class Test_largest_combination < Test::Unit::TestCase
60+
def test_
61+
assert_equal 4, largest_combination([16, 17, 71, 62, 12, 24, 14])
62+
assert_equal 2, largest_combination([8, 8])
63+
end
64+
end

0 commit comments

Comments
 (0)