Skip to content

Commit 6713df7

Browse files
committed
2172. Maximum AND Sum of Array: RETRY
1 parent 350b123 commit 6713df7

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,3 +1642,4 @@ mod s2167_minimum_time_to_remove_all_cars_containing_illegal_goods;
16421642
mod s2169_count_operations_to_obtain_zero;
16431643
mod s2170_minimum_operations_to_make_the_array_alternating;
16441644
mod s2171_removing_minimum_number_of_magic_beans;
1645+
mod s2172_maximum_and_sum_of_array;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* [2172] Maximum AND Sum of Array
3+
*
4+
* You are given an integer array nums of length n and an integer numSlots such that 2 * numSlots >= n. There are numSlots slots numbered from 1 to numSlots.
5+
* You have to place all n integers into the slots such that each slot contains at most two numbers. The AND sum of a given placement is the sum of the bitwise AND of every number with its respective slot number.
6+
*
7+
* For example, the AND sum of placing the numbers [1, 3] into slot <u>1</u> and [4, 6] into slot <u>2</u> is equal to (1 AND <u>1</u>) + (3 AND <u>1</u>) + (4 AND <u>2</u>) + (6 AND <u>2</u>) = 1 + 1 + 0 + 2 = 4.
8+
*
9+
* Return the maximum possible AND sum of nums given numSlots slots.
10+
*
11+
* Example 1:
12+
*
13+
* Input: nums = [1,2,3,4,5,6], numSlots = 3
14+
* Output: 9
15+
* Explanation: One possible placement is [1, 4] into slot <u>1</u>, [2, 6] into slot <u>2</u>, and [3, 5] into slot <u>3</u>.
16+
* This gives the maximum AND sum of (1 AND <u>1</u>) + (4 AND <u>1</u>) + (2 AND <u>2</u>) + (6 AND <u>2</u>) + (3 AND <u>3</u>) + (5 AND <u>3</u>) = 1 + 0 + 2 + 2 + 3 + 1 = 9.
17+
*
18+
* Example 2:
19+
*
20+
* Input: nums = [1,3,10,4,7,1], numSlots = 9
21+
* Output: 24
22+
* Explanation: One possible placement is [1, 1] into slot <u>1</u>, [3] into slot <u>3</u>, [4] into slot <u>4</u>, [7] into slot <u>7</u>, and [10] into slot <u>9</u>.
23+
* This gives the maximum AND sum of (1 AND <u>1</u>) + (1 AND <u>1</u>) + (3 AND <u>3</u>) + (4 AND <u>4</u>) + (7 AND <u>7</u>) + (10 AND <u>9</u>) = 1 + 1 + 3 + 4 + 7 + 8 = 24.
24+
* Note that slots 2, 5, 6, and 8 are empty which is permitted.
25+
*
26+
*
27+
* Constraints:
28+
*
29+
* n == nums.length
30+
* 1 <= numSlots <= 9
31+
* 1 <= n <= 2 * numSlots
32+
* 1 <= nums[i] <= 15
33+
*
34+
*/
35+
pub struct Solution {}
36+
37+
// problem: https://leetcode.com/problems/maximum-and-sum-of-array/
38+
// discuss: https://leetcode.com/problems/maximum-and-sum-of-array/discuss/?currentPage=1&orderBy=most_votes&query=
39+
40+
// submission codes start here
41+
42+
impl Solution {
43+
pub fn maximum_and_sum(nums: Vec<i32>, num_slots: i32) -> i32 {
44+
0
45+
}
46+
}
47+
48+
// submission codes end
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::*;
53+
54+
#[test]
55+
#[ignore]
56+
fn test_2172_example_1() {
57+
let nums = vec![1, 2, 3, 4, 5, 6];
58+
let num_slots = 3;
59+
60+
let result = 9;
61+
62+
assert_eq!(Solution::maximum_and_sum(nums, num_slots), result);
63+
}
64+
65+
#[test]
66+
#[ignore]
67+
fn test_2172_example_2() {
68+
let nums = vec![1, 3, 10, 4, 7, 1];
69+
let num_slots = 9;
70+
71+
let result = 24;
72+
73+
assert_eq!(Solution::maximum_and_sum(nums, num_slots), result);
74+
}
75+
}

0 commit comments

Comments
 (0)