3447. Assign Elements to Groups with Constraints Medium

@problem@discussion
#Array#Hash Table



1/**
2 * [3447] Assign Elements to Groups with Constraints
3 *
4 * You are given an integer array groups, where groups[i] represents the size of the i^th group. You are also given an integer array elements.
5 * Your task is to assign one element to each group based on the following rules:
6 * 
7 * 	An element at index j can be assigned to a group i if groups[i] is divisible by elements[j].
8 * 	If there are multiple elements that can be assigned, assign the element with the smallest index j.
9 * 	If no element satisfies the condition for a group, assign -1 to that group.
10 * 
11 * Return an integer array assigned, where assigned[i] is the index of the element chosen for group i, or -1 if no suitable element exists.
12 * Note: An element may be assigned to more than one group.
13 *  
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input: <span class="example-io">groups = [8,4,3,2,4], elements = [4,2]</span>
17 * Output: <span class="example-io">[0,0,-1,1,0]</span>
18 * Explanation:
19 * 
20 * 	elements[0] = 4 is assigned to groups 0, 1, and 4.
21 * 	elements[1] = 2 is assigned to group 3.
22 * 	Group 2 cannot be assigned any element.
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">groups = [2,3,5,7], elements = [5,3,3]</span>
27 * Output: <span class="example-io">[-1,1,0,-1]</span>
28 * Explanation:
29 * 
30 * 	elements[1] = 3 is assigned to group 1.
31 * 	elements[0] = 5 is assigned to group 2.
32 * 	Groups 0 and 3 cannot be assigned any element.
33 * </div>
34 * <strong class="example">Example 3:
35 * <div class="example-block">
36 * Input: <span class="example-io">groups = [10,21,30,41], elements = [2,1]</span>
37 * Output: <span class="example-io">[0,1,0,1]</span>
38 * Explanation:
39 * elements[0] = 2 is assigned to the groups with even values, and elements[1] = 1 is assigned to the groups with odd values.
40 * </div>
41 *  
42 * Constraints:
43 * 
44 * 	1 <= groups.length <= 10^5
45 * 	1 <= elements.length <= 10^5
46 * 	1 <= groups[i] <= 10^5
47 * 	1 <= elements[i] <= 10^5
48 * 
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/assign-elements-to-groups-with-constraints/
53// discuss: https://leetcode.com/problems/assign-elements-to-groups-with-constraints/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58    pub fn assign_elements(groups: Vec<i32>, elements: Vec<i32>) -> Vec<i32> {
59        vec![]
60    }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_3447() {
71    }
72}
73

Back
© 2026 bowen.ge All Rights Reserved.