当前位置:   article > 正文

c++ 解决区间最大数和矩阵最大面积

c++ 解决区间最大数和矩阵最大面积

给定一个实数序列,设计一个最有效的算法,找到一个总和数最大的区间等于某个事先给定的数字。

我们可以使用前缀和和哈希表来设计一个高效的算法。这个算法的时间复杂度是 O(n),空间复杂度也是 O(n)。

  1. #include <vector>
  2. #include <unordered_map>
  3. #include <iostream>
  4. std::pair<int, int> findMaxLengthSubarrayWithSum(const std::vector<double>& arr, double targetSum) {
  5. std::unordered_map<double, int> sumIndex; // 用于存储前缀和及其索引
  6. double currentSum = 0; // 当前的前缀和
  7. int maxLength = 0; // 最长子数组的长度
  8. int endIndex = -1; // 最长子数组的结束索引
  9. sumIndex[0] = -1; // 初始化前缀和为0的索引为-1
  10. for (int i = 0; i < arr.size(); ++i) {
  11. currentSum += arr[i]; // 计算当前前缀和
  12. double complement = currentSum - targetSum; // 计算需要寻找的互补和
  13. if (sumIndex.find(complement) != sumIndex.end()) { // 如果找到了互补和
  14. int length = i - sumIndex[complement]; // 计算子数组长度
  15. if (length > maxLength) { // 如果这个子数组更长
  16. maxLength = length; // 更新最大长度
  17. endIndex = i; // 更新结束索引
  18. }
  19. }
  20. if (sumIndex.find(currentSum) == sumIndex.end()) { // 如果这个前缀和之前没出现过
  21. sumIndex[currentSum] = i; // 记录这个前缀和的索引
  22. }
  23. }
  24. if (endIndex != -1) { // 如果找到了符合条件的子数组
  25. return {endIndex - maxLength + 1, endIndex}; // 返回起始和结束索引
  26. } else {
  27. return {-1, -1}; // 如果没找到,返回{-1, -1}
  28. }
  29. }
  30. int main() {
  31. std::vector<double> arr = {1.0, 4.0, 20.0, 3.0, 10.0, 5.0}; // 示例数组
  32. double targetSum = 33.0; // 目标和
  33. auto result = findMaxLengthSubarrayWithSum(arr, targetSum); // 调用函数查找子数组
  34. if (result.first != -1) { // 如果找到了符合条件的子数组
  35. std::cout << "找到了和为 " << targetSum << " 的最长子数组,索引范围: ["
  36. << result.first << ", " << result.second << "]" << std::endl;
  37. } else { // 如果没有找到符合条件的子数组
  38. std::cout << "没有找到和为 " << targetSum << " 的子数组" << std::endl;
  39. }
  40. return 0;
  41. }

这个算法的工作原理如下:

  1. 我们使用一个哈希表 sumIndex 来存储每个前缀和及其对应的索引。
  2. 我们遍历数组,不断累加当前的和 currentSum
  3. 对于每个位置,我们计算 complement = currentSum - targetSum。如果这个 complement 存在于我们的哈希表中,那么说明我们找到了一个和为 targetSum 的子数组。
  4. 我们记录找到的最长的子数组。
  5. 如果当前的 currentSum 还没有在哈希表中,我们就将它加入哈希表。

这个算法的优点是:

  • 时间复杂度为 O(n),因为我们只需要遍历一次数组。
  • 空间复杂度为 O(n),用于存储哈希表。
  • 它可以处理包含正数、负数和零的数组。
  • 它可以找到最长的符合条件的子数组。

这个算法比简单的滑动窗口方法更有效,因为它可以处理包含负数的情况,并且可以在一次遍历中找到最长的符合条件的子数组。

第二个问题,在一个二维矩阵中,寻找一个矩阵的区域,使其中的数字之和达到最大值,著名的"最大子矩阵和"问题。我们可以使用Kadane算法的二维扩展来解决这个问题。以下是C++实现,并附有中文注释:

  1. #include <vector>
  2. #include <climits>
  3. #include <iostream>
  4. struct SubMatrix {
  5. int top, left, bottom, right, sum;
  6. };
  7. SubMatrix findMaxSumSubMatrix(const std::vector<std::vector<int>>& matrix) {
  8. int rows = matrix.size();
  9. if (rows == 0) return {0, 0, 0, 0, 0};
  10. int cols = matrix[0].size();
  11. if (cols == 0) return {0, 0, 0, 0, 0};
  12. SubMatrix result = {0, 0, 0, 0, INT_MIN}; // 存储最终结果
  13. std::vector<int> temp(rows, 0); // 临时数组,用于存储列的和
  14. // 枚举所有可能的左右列边界
  15. for (int left = 0; left < cols; ++left) {
  16. std::fill(temp.begin(), temp.end(), 0); // 重置临时数组
  17. for (int right = left; right < cols; ++right) {
  18. // 将当前列加到临时数组中
  19. for (int i = 0; i < rows; ++i) {
  20. temp[i] += matrix[i][right];
  21. }
  22. // 在临时数组上应用一维Kadane算法
  23. int currentSum = 0;
  24. int maxSum = INT_MIN;
  25. int tempTop = 0, top = 0, bottom = 0;
  26. for (int i = 0; i < rows; ++i) {
  27. currentSum += temp[i];
  28. if (currentSum > maxSum) {
  29. maxSum = currentSum;
  30. top = tempTop;
  31. bottom = i;
  32. }
  33. if (currentSum < 0) {
  34. currentSum = 0;
  35. tempTop = i + 1;
  36. }
  37. }
  38. // 更新全局最大值
  39. if (maxSum > result.sum) {
  40. result = {top, left, bottom, right, maxSum};
  41. }
  42. }
  43. }
  44. return result;
  45. }
  46. int main() {
  47. std::vector<std::vector<int>> matrix = {
  48. {1, 2, -1, -4, -20},
  49. {-8, -3, 4, 2, 1},
  50. {3, 8, 10, 1, 3},
  51. {-4, -1, 1, 7, -6}
  52. };
  53. SubMatrix result = findMaxSumSubMatrix(matrix);
  54. std::cout << "最大子矩阵和为: " << result.sum << std::endl;
  55. std::cout << "子矩阵位置: (" << result.top << "," << result.left << ") 到 ("
  56. << result.bottom << "," << result.right << ")" << std::endl;
  57. return 0;
  58. }

这个算法的工作原理如下:

  1. 我们枚举矩阵的所有可能的左右列边界。
  2. 对于每一对左右边界,我们计算这些列之间所有元素的和,存储在一个临时数组中。
  3. 在这个临时数组上,我们应用一维的Kadane算法来找到最大子数组和,这个子数组对应于我们要找的子矩阵的上下边界。
  4. 我们跟踪全局最大和及其对应的子矩阵位置。
  5. 最后返回具有最大和的子矩阵。

这个算法的时间复杂度是O(n^3),其中n是矩阵的边长(假设矩阵是方阵)。虽然这不是最优的解法,但它是较为直观且易于实现的方法。

对于更大的矩阵,存在更高效的算法,如使用二维线段树或者二维树状数组的方法,它们可以将时间复杂度降低到O(n^2 log n)或O(n^2)。但这些方法的实现会更加复杂。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/780139
推荐阅读
相关标签
  

闽ICP备14008679号