Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve Problem 1906. Minimum Absolute Difference Queries #1889

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ LeetSpace serves as a dedicated workspace and archive for my [LeetCode](https://
| [1903. Largest Odd Number in String](https://leetcode.com/problems/largest-odd-number-in-string/) | Easy | [C](./old-problems/1903/c/solution.c) [C++](./old-problems/1903/solution.cpp) |
| [1904. The Number of Full Rounds You Have Played](https://leetcode.com/problems/the-number-of-full-rounds-you-have-played/) | Medium | [C++](./old-problems/1904/solution.cpp) |
| [1905. Count Sub Islands](https://leetcode.com/problems/count-sub-islands/) | Medium | [C](./old-problems/1905/c/solution.c) [C++](./old-problems/1905/solution.cpp) |
| [1906. Minimum Absolute Difference Queries](https://leetcode.com/problems/minimum-absolute-difference-queries/) | Medium | [C++](./old-problems/1906/solution.cpp) |
| [1913. Maximum Product Difference Between Two Pairs](https://leetcode.com/problems/maximum-product-difference-between-two-pairs/) | Easy | [C](./old-problems/1913/c/solution.c) [C++](./old-problems/1913/solution.cpp) |
| [1915. Number of Wonderful Substrings](https://leetcode.com/problems/number-of-wonderful-substrings/) | Medium | [C](./old-problems/1915/c/solution.c) [C++](./old-problems/1915/solution.cpp) |
| [1921. Eliminate Maximum Number of Monsters](https://leetcode.com/problems/eliminate-maximum-number-of-monsters) | Medium | [C](./old-problems/1921/c/solution.c) [C++](./old-problems/1921/solution.cpp) |
Expand Down
2 changes: 2 additions & 0 deletions old-problems/1906/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
get_dir_name(id)
add_problem_test(test-${id} test.yaml)
46 changes: 46 additions & 0 deletions old-problems/1906/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <numeric>
#include <vector>

class Solution {
public:
std::vector<int> minDifference(
std::vector<int>& nums, std::vector<std::vector<int>>& queries) {
std::vector<std::vector<int>> freqs{};
freqs.reserve(nums.size() + 1);

std::vector<int> freq(100, 0);
freqs.push_back(freq);

for (const auto num : nums) {
++freq[num - 1];
freqs.push_back(freq);
}

std::vector<int> diffs{};
diffs.reserve(queries.size());
for (const auto& query : queries) {
int diff{std::numeric_limits<int>::max()};

int prev{0}, i{0};
while (i < 100) {
if (freqs[query[1] + 1][i] - freqs[query[0]][i] > 0) {
prev = i;
break;
}
++i;
}

for (int i{prev + 1}; i < 100; ++i) {
if (freqs[query[1] + 1][i] - freqs[query[0]][i] > 0) {
if (i - prev < diff) diff = i - prev;
prev = i;
}
}

if (diff == std::numeric_limits<int>::max()) diff = -1;
diffs.push_back(diff);
}

return diffs;
}
};
24 changes: 24 additions & 0 deletions old-problems/1906/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: 1906. Minimum Absolute Difference Queries

types:
inputs:
nums: std::vector<int>
queries: std::vector<std::vector<int>>
output: std::vector<int>

solutions:
cpp:
function: minDifference

test_cases:
example_1:
inputs:
nums: [1, 3, 4, 8]
queries: [[0, 1], [1, 2], [2, 3], [0, 3]]
output: [2, 1, 4, 1]

example_2:
inputs:
nums: [4, 5, 2, 2, 7, 10]
queries: [[2, 3], [0, 2], [0, 5], [3, 5]]
output: [-1, 1, 1, 3]