From 384ffe88c74fac49d9580f1efc3e6dd0084d40de Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Sat, 22 May 2021 20:33:02 +0800 Subject: [PATCH] New Problem Solution - "1862. Sum of Floored Pairs" --- README.md | 1 + .../sumOfFlooredPairs/SumOfFlooredPairs.cpp | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 algorithms/cpp/sumOfFlooredPairs/SumOfFlooredPairs.cpp diff --git a/README.md b/README.md index 2831bf96..7f411baf 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | +|1862|[Sum of Floored Pairs](https://leetcode.com/problems/sum-of-floored-pairs/) | [C++](./algorithms/cpp/sumOfFlooredPairs/SumOfFlooredPairs.cpp)|Hard| |1861|[Rotating the Box](https://leetcode.com/problems/rotating-the-box/) | [C++](./algorithms/cpp/rotatingTheBox/RotatingTheBox.cpp)|Medium| |1860|[Incremental Memory Leak](https://leetcode.com/problems/incremental-memory-leak/) | [C++](./algorithms/cpp/incrementalMemoryLeak/IncrementalMemoryLeak.cpp)|Medium| |1859|[Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence/) | [C++](./algorithms/cpp/sortingTheSentence/SortingTheSentence.cpp)|Easy| diff --git a/algorithms/cpp/sumOfFlooredPairs/SumOfFlooredPairs.cpp b/algorithms/cpp/sumOfFlooredPairs/SumOfFlooredPairs.cpp new file mode 100644 index 00000000..3a366da7 --- /dev/null +++ b/algorithms/cpp/sumOfFlooredPairs/SumOfFlooredPairs.cpp @@ -0,0 +1,68 @@ +// Source : https://leetcode.com/problems/sum-of-floored-pairs/ +// Author : Hao Chen +// Date : 2021-05-22 + +/***************************************************************************************************** + * + * Given an integer array nums, return the sum of floor(nums[i] / nums[j]) for all pairs of indices 0 + * <= i, j < nums.length in the array. Since the answer may be too large, return it modulo 10^9 + 7. + * + * The floor() function returns the integer part of the division. + * + * Example 1: + * + * Input: nums = [2,5,9] + * Output: 10 + * Explanation: + * floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0 + * floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1 + * floor(5 / 2) = 2 + * floor(9 / 2) = 4 + * floor(9 / 5) = 1 + * We calculate the floor of the division for every pair of indices in the array then sum them up. + * + * Example 2: + * + * Input: nums = [7,7,7,7,7,7,7] + * Output: 49 + * + * Constraints: + * + * 1 <= nums.length <= 10^5 + * 1 <= nums[i] <= 10^5 + ******************************************************************************************************/ + +class Solution { +public: + int sumOfFlooredPairs(vector& nums) { + const int MAX_NUM = 100001; + int cnt[MAX_NUM] = {0}; + int maxn = 0; + for(auto& n : nums) { + cnt[n]++; + maxn = max(maxn, n); + } + + vector> stats; + for(int i=1; i 0) { + stats.push_back({i, cnt[i]}); + } + cnt[i] += cnt[i-1]; + } + + const int MOD = 1e9+7; + int result = 0; + for(int i=0; i < stats.size(); i++) { + int n = stats[i][0]; + int c = stats[i][1]; + + for(int x=2; x <= maxn/n+1; x++) { + int pre = (x-1) * n - 1; + int cur = min( x * n - 1, MAX_NUM-1); + result = (result + (cnt[cur] - cnt[pre]) * long(x-1) * c) % MOD; + } + } + return result; + } +};