赞
踩
给定一个实数序列,设计一个最有效的算法,找到一个总和数最大的区间等于某个事先给定的数字。
我们可以使用前缀和和哈希表来设计一个高效的算法。这个算法的时间复杂度是 O(n),空间复杂度也是 O(n)。
- #include <vector>
- #include <unordered_map>
- #include <iostream>
-
- std::pair<int, int> findMaxLengthSubarrayWithSum(const std::vector<double>& arr, double targetSum) {
- std::unordered_map<double, int> sumIndex; // 用于存储前缀和及其索引
- double currentSum = 0; // 当前的前缀和
- int maxLength = 0; // 最长子数组的长度
- int endIndex = -1; // 最长子数组的结束索引
-
- sumIndex[0] = -1; // 初始化前缀和为0的索引为-1
-
- for (int i = 0; i < arr.size(); ++i) {
- currentSum += arr[i]; // 计算当前前缀和
-
- double complement = currentSum - targetSum; // 计算需要寻找的互补和
-
- if (sumIndex.find(complement) != sumIndex.end()) { // 如果找到了互补和
- int length = i - sumIndex[complement]; // 计算子数组长度
- if (length > maxLength) { // 如果这个子数组更长
- maxLength = length; // 更新最大长度
- endIndex = i; // 更新结束索引
- }
- }
-
- if (sumIndex.find(currentSum) == sumIndex.end()) { // 如果这个前缀和之前没出现过
- sumIndex[currentSum] = i; // 记录这个前缀和的索引
- }
- }
-
- if (endIndex != -1) { // 如果找到了符合条件的子数组
- return {endIndex - maxLength + 1, endIndex}; // 返回起始和结束索引
- } else {
- return {-1, -1}; // 如果没找到,返回{-1, -1}
- }
- }
-
- int main() {
- std::vector<double> arr = {1.0, 4.0, 20.0, 3.0, 10.0, 5.0}; // 示例数组
- double targetSum = 33.0; // 目标和
-
- auto result = findMaxLengthSubarrayWithSum(arr, targetSum); // 调用函数查找子数组
-
- if (result.first != -1) { // 如果找到了符合条件的子数组
- std::cout << "找到了和为 " << targetSum << " 的最长子数组,索引范围: ["
- << result.first << ", " << result.second << "]" << std::endl;
- } else { // 如果没有找到符合条件的子数组
- std::cout << "没有找到和为 " << targetSum << " 的子数组" << std::endl;
- }
-
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
这个算法的工作原理如下:
sumIndex
来存储每个前缀和及其对应的索引。currentSum
。complement = currentSum - targetSum
。如果这个 complement
存在于我们的哈希表中,那么说明我们找到了一个和为 targetSum
的子数组。currentSum
还没有在哈希表中,我们就将它加入哈希表。这个算法的优点是:
这个算法比简单的滑动窗口方法更有效,因为它可以处理包含负数的情况,并且可以在一次遍历中找到最长的符合条件的子数组。
第二个问题,在一个二维矩阵中,寻找一个矩阵的区域,使其中的数字之和达到最大值,著名的"最大子矩阵和"问题。我们可以使用Kadane算法的二维扩展来解决这个问题。以下是C++实现,并附有中文注释:
- #include <vector>
- #include <climits>
- #include <iostream>
-
- struct SubMatrix {
- int top, left, bottom, right, sum;
- };
-
- SubMatrix findMaxSumSubMatrix(const std::vector<std::vector<int>>& matrix) {
- int rows = matrix.size();
- if (rows == 0) return {0, 0, 0, 0, 0};
- int cols = matrix[0].size();
- if (cols == 0) return {0, 0, 0, 0, 0};
-
- SubMatrix result = {0, 0, 0, 0, INT_MIN}; // 存储最终结果
- std::vector<int> temp(rows, 0); // 临时数组,用于存储列的和
-
- // 枚举所有可能的左右列边界
- for (int left = 0; left < cols; ++left) {
- std::fill(temp.begin(), temp.end(), 0); // 重置临时数组
- for (int right = left; right < cols; ++right) {
- // 将当前列加到临时数组中
- for (int i = 0; i < rows; ++i) {
- temp[i] += matrix[i][right];
- }
-
- // 在临时数组上应用一维Kadane算法
- int currentSum = 0;
- int maxSum = INT_MIN;
- int tempTop = 0, top = 0, bottom = 0;
-
- for (int i = 0; i < rows; ++i) {
- currentSum += temp[i];
- if (currentSum > maxSum) {
- maxSum = currentSum;
- top = tempTop;
- bottom = i;
- }
- if (currentSum < 0) {
- currentSum = 0;
- tempTop = i + 1;
- }
- }
-
- // 更新全局最大值
- if (maxSum > result.sum) {
- result = {top, left, bottom, right, maxSum};
- }
- }
- }
-
- return result;
- }
-
- int main() {
- std::vector<std::vector<int>> matrix = {
- {1, 2, -1, -4, -20},
- {-8, -3, 4, 2, 1},
- {3, 8, 10, 1, 3},
- {-4, -1, 1, 7, -6}
- };
-
- SubMatrix result = findMaxSumSubMatrix(matrix);
-
- std::cout << "最大子矩阵和为: " << result.sum << std::endl;
- std::cout << "子矩阵位置: (" << result.top << "," << result.left << ") 到 ("
- << result.bottom << "," << result.right << ")" << std::endl;
-
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
这个算法的工作原理如下:
这个算法的时间复杂度是O(n^3),其中n是矩阵的边长(假设矩阵是方阵)。虽然这不是最优的解法,但它是较为直观且易于实现的方法。
对于更大的矩阵,存在更高效的算法,如使用二维线段树或者二维树状数组的方法,它们可以将时间复杂度降低到O(n^2 log n)或O(n^2)。但这些方法的实现会更加复杂。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。