Skip to content

Commit 722e789

Browse files
authored
Create 3447.Assign-Elements-to-Groups-with-Constraints.cpp
1 parent 171203a commit 722e789

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<int> assignElements(vector<int>& groups, vector<int>& elements)
4+
{
5+
int n = *max_element(groups.begin(), groups.end());
6+
vector<int>arr(n+1, -1);
7+
8+
for (int j=0; j<elements.size(); j++)
9+
{
10+
int x0 = elements[j];
11+
if (x0>n) continue;
12+
13+
if (arr[x0]!=-1) continue;
14+
15+
int x = x0;
16+
while (x<=n)
17+
{
18+
if (arr[x]==-1)
19+
arr[x] = j;
20+
x+=x0;
21+
}
22+
}
23+
24+
vector<int>rets;
25+
for (int g: groups)
26+
rets.push_back(arr[g]);
27+
return rets;
28+
}
29+
};

0 commit comments

Comments
 (0)