File tree 1 file changed +37
-0
lines changed
scripts/algorithms/F/Find the Maximum Divisibility Score
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 100 ms (Top 96.09%) | Memory: 44.90 MB (Top 25.98%)
2
+
3
+ class Solution {
4
+ private int binarySearch (int [] nums , int target ) {
5
+ int i = 0 ;
6
+ int j = nums .length ;
7
+ while (i < j ) {
8
+ int mid = i + (j - i ) / 2 ;
9
+ if (nums [mid ] < target ) {
10
+ i = mid + 1 ;
11
+ } else {
12
+ j = mid ;
13
+ }
14
+ }
15
+ return i ;
16
+ }
17
+ public int maxDivScore (int [] nums , int [] divisors ) {
18
+ Arrays .sort (nums );
19
+ int max = 0 ;
20
+ int res = divisors [0 ];
21
+ for (int d : divisors ) {
22
+ int score = 0 ;
23
+ for (int i = binarySearch (nums , d ); i < nums .length ; ++i ) {
24
+ if ( nums [i ] % d == 0 ) {
25
+ ++score ;
26
+ }
27
+ }
28
+ if (score > max ) {
29
+ max = score ;
30
+ res = d ;
31
+ } else if (score == max ) {
32
+ res = Math .min (res , d );
33
+ }
34
+ }
35
+ return res ;
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments