From b5a9afb169aa266d8fee0145558efe710e103361 Mon Sep 17 00:00:00 2001 From: aucker Date: Sat, 18 May 2024 17:32:21 +0800 Subject: [PATCH] update: May18 sort LC 2644 [E] --- .clang-tidy | 97 ------------------------------------------------- .gitignore | 2 + daily/May18.cpp | 82 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 97 deletions(-) delete mode 100644 .clang-tidy create mode 100644 daily/May18.cpp diff --git a/.clang-tidy b/.clang-tidy deleted file mode 100644 index f7d04cb..0000000 --- a/.clang-tidy +++ /dev/null @@ -1,97 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# -# Modified from the Apache Arrow project for the Terrier project. -# ---- -Checks: ' - bugprone-*, - clang-analyzer-*, - google-*, - modernize-*, - performance-*, - portability-*, - readability-*, - -bugprone-easily-swappable-parameters, - -bugprone-implicit-widening-of-multiplication-result, - -bugprone-narrowing-conversions, - -bugprone-reserved-identifier, - -bugprone-signed-char-misuse, - -bugprone-suspicious-include, - -bugprone-unhandled-self-assignment, - -clang-analyzer-cplusplus.NewDelete, - -clang-analyzer-cplusplus.NewDeleteLeaks, - -clang-analyzer-security.insecureAPI.rand, - -clang-diagnostic-implicit-int-float-conversion, - -google-readability-avoid-underscore-in-googletest-name, - -modernize-avoid-c-arrays, - -modernize-use-nodiscard, - -readability-convert-member-functions-to-static, - -readability-identifier-length, - -readability-function-cognitive-complexity, - -readability-magic-numbers, - -readability-make-member-function-const, - -readability-qualified-auto, - -readability-redundant-access-specifiers, - -bugprone-exception-escape, - ' -CheckOptions: - - { key: readability-identifier-naming.ClassCase, value: CamelCase } - - { key: readability-identifier-naming.EnumCase, value: CamelCase } - - { key: readability-identifier-naming.FunctionCase, value: CamelCase } - - { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE } - - { key: readability-identifier-naming.MemberCase, value: lower_case } - - { key: readability-identifier-naming.MemberSuffix, value: _ } - - { key: readability-identifier-naming.NamespaceCase, value: lower_case } - - { key: readability-identifier-naming.StructCase, value: CamelCase } - - { key: readability-identifier-naming.UnionCase, value: CamelCase } - - { key: readability-identifier-naming.VariableCase, value: lower_case } -WarningsAsErrors: '*' -HeaderFilterRegex: '/(src|test)/include' -AnalyzeTemporaryDtors: true - -#### Disabled checks and why: ##### -# -# -readability-convert-member-functions-to-static, -# This check started going off in the upgrade from clang-tidy-8 to clang-tidy-12. It is not always correct because -# we hide the reference implementation in another repository. -# -clang-analyzer-security.insecureAPI.rand, -clang-analyzer-security.insecureAPI.rand, -bugprone-unhandled-self-assignment, -# -bugprone-implicit-widening-of-multiplication-result -# These have not been investigated yet. -# -bugprone-reserved-identifier, -# Fails due to use of some __SHORT_FILE__ symbol, originating from very old code. -# -bugprone-suspicious-include, -# False positive due to GTest code. -# -bugprone-too-small-loop-variable, -# Complains about uint8_t or uint16_t when the limit on the loop is a container's .size() (size_t). -# We usually do this when we know the maximum size of the container though, so propose leaving disabled. -# -clang-analyzer-cplusplus.NewDelete, -# Seems to generate false positives. Suggest relying on ASAN and valgrind for memory stuff. -# -clang-analyzer-cplusplus.NewDeleteLeaks, -# Seems to generate false positives. Suggest relying on ASAN and valgrind for memory stuff. -# -modernize-use-nodiscard, -# New C++17 feature, slightly polarizing. Would clutter codebase. -# -modernize-avoid-c-arrays, -# We use C-style arrays in page.h, type.h and logger.h. They're a little more ergonomic than std::array. Thoughts? -# -readability-magic-numbers, -# Let's not deal with people doing ridiculous things to hack around this. If it bites them, it bites them. -# -bugprone-signed-char-misuse, -clang-diagnostic-implicit-int-float-conversion, -readability-make-member-function-const, -# -readability-qualified-auto, -readability-redundant-access-specifiers -# These were previously disabled for not being available in clang-tidy-8. They are now available on our clang-tidy-12, -# and potentially worth investigating/fixing. -# -bugprone-exception-escape -# Weird. No idea how to resolve. diff --git a/.gitignore b/.gitignore index 360af95..10dfe84 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ target/ *.exe *.o .* + +.clang-tidy diff --git a/daily/May18.cpp b/daily/May18.cpp new file mode 100644 index 0000000..780806a --- /dev/null +++ b/daily/May18.cpp @@ -0,0 +1,82 @@ +#include + +#include +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& nums, vector& divisors) { + vector 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 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& nums, vector& 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 nums = {4, 7, 9, 3, 9}; + vector divisors = {5, 2, 3}; + int res = s.maxDivScore(nums, divisors); + assert(res == 3); + + return 0; +}