[LeetCode] Find the Maximum Divisibility Score

2644. Find the Maximum Divisibility Score

You are given two 0-indexed integer arrays nums and divisors.

The divisibility score of divisors[i] is the number of indices j such that nums[j] is divisible by divisors[i].

Return the integer divisors[i] with the maximum divisibility score. If there is more than one integer with the maximum score, return the minimum of them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int maxDivScore(vector<int>& nums, vector<int>& divisors) {
int res = -1, cnt = -1;
for(auto d : divisors) {
int now = 0;
for(auto n : nums) if(n % d == 0) now += 1;
if(cnt < now) {
cnt = now, res = d;
} else if(cnt == now) {
res = min(res, d);
}
}
return res;
}
};


Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/16/PS/LeetCode/find-the-maximum-divisibility-score/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.