Skip to content

Commit

Permalink
update: May18 sort LC 2644 [E]
Browse files Browse the repository at this point in the history
  • Loading branch information
aucker committed May 18, 2024
1 parent 3b9127d commit b5a9afb
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 97 deletions.
97 changes: 0 additions & 97 deletions .clang-tidy

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ target/
*.exe
*.o
.*

.clang-tidy
82 changes: 82 additions & 0 deletions daily/May18.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <bits/stdc++.h>

#include <algorithm>
using namespace std;

class Solution {
public:
/**
* @brief LC: 2644, Too many loops, too slow
* Time: O(N^2)
*
* @param nums
* @param divisors
* @return int
*/
int maxDivScore(vector<int>& nums, vector<int>& divisors) {
vector<int> tmp(divisors.size(), 0);
for (int i = 0; i < divisors.size(); i++) {
for (int j = 0; j < nums.size(); j++) {
if (nums[j] % divisors[i] == 0) {
tmp[i]++;
}
}
}

int max_val = *max_element(tmp.begin(), tmp.end());
vector<int> indices;
for (int i = 0; i < tmp.size(); i++) {
if (tmp[i] == max_val) {
indices.push_back(i);
}
}

int ans = divisors[indices[0]];
for (int idx : indices) {
ans = min(ans, divisors[idx]);
}

return ans;
}

/**
* @brief Optimize
* Time: O(nlogn + nm), we use sort here
*
* @param nums
* @param divisors
* @return int
*/
int maxDivScoreOP(vector<int>& nums, vector<int>& divisors) {
sort(nums.rbegin(), nums.rend());
int max_cnt = -1, ans = 0;
for (int d : divisors) {
int cnt = 0;
for (int x : nums) {
if (x < d) {
break;
}
if (x % d == 0) {
cnt++;
}
}

if (cnt > max_cnt || cnt == max_cnt && d < ans) {
max_cnt = cnt;
ans = d;
}
}

return ans;
}
};

int main() {
Solution s;
vector<int> nums = {4, 7, 9, 3, 9};
vector<int> divisors = {5, 2, 3};
int res = s.maxDivScore(nums, divisors);
assert(res == 3);

return 0;
}

0 comments on commit b5a9afb

Please sign in to comment.