晨哥Leetcode 689. Maximum Sum of 3 Non-Overlapping Subarrays

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum.
Each subarray will be of size k, and we want to maximize the sum of all 3*k entries.
Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.
Example:
Input: [1,2,1,2,6,7,5,1], 2
Output: [0, 3, 5]
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.

三个不重叠的subarrays,
我们可以划分为三个区间 [0…i-1] [i… i +k-1] [i+k …]
从这个区间开始枚举 [0…k-1] [k…2k-1] [2k …]

事先先算好从左边过来,最大的 size为k的窗口的值,对应的Index
从右边过来,最大的 size为k的窗口的值,对应的Index
就枚举中间这个k大小的窗口,复杂度为O(N)

此题的下标太乱很容易错,一定要把区间画出来
另外注意从右边过来的时候,max值相等,要替换(因为我们求从左边来最小的Index)

public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
        int n = nums.length;
        int[] sums = new int[n+1];
        sums[0] = 0;
        for(int i = 0; i< nums.length;i++) {
            sums[i+1] = sums[i] + nums[i]; //sums[i], i前面所有的数之和,不包括i
        } // 本题也可以直接求出 size 为k的窗口的和
        int[] left = new int[n];  int[] right = new int[n];
        int max = Integer.MIN_VALUE;
        for(int i=0;i<nums.length-k;i++) {
            int cur = sums[i+k] - sums[i]; 
            if(cur > max) { //大于号,因为相等的话不用替换
                left[i] = i; //left[i]: i+k之前的数中 k大小的窗口里最大时,起始index
                max = cur;
            } else {
                left[i] = left[i-1];
            }        
        }
        max = Integer.MIN_VALUE;
        for(int i = nums.length - k; i>=0;i--) {
            int cur = sums[i+k] - sums[i];
            if(cur >= max) { //大于等于号,因为相等的话需要替换
                right[i] = i; //right[i]: i之后的数中 k大小的窗口里最大时,起始index
                max = cur;
            } else {
                right[i] = right[i+1];
            }  
        }
        
        int[] res = new int[3]; max = Integer.MIN_VALUE;
        for(int i=k; i<=nums.length-k*2;i++) {
            int l = left[i-k], r = right[i+k];
            int cur = sums[l+k] - sums[l] + sums[r+k] -sums[r] + sums[i+k] - sums[i];
            if(cur > max) {
                res[0] = l; res[1] = i; res[2] = r;
                max = cur;
            }
        }
        return res;
    }
### CNN卷积核的空间不变性 卷积神经网络(CNN)中的卷积操作具备空间不变性的特性,这意味着无论特征出现在输入数据的哪个位置,只要该模式存在,模型都能识别出来[^1]。具体来说,在卷积过程中使用的滤波器(即卷积核)在整个图像上滑动并执行相同的操作,这使得同一组权重能够检测到不同位置上的相似图案或边缘。 这种机制允许CNN自动学习如何捕捉平移不变性——即使目标物体在图片的不同区域移动,仍然能被正确分类。例如,如果一张照片里有一只猫位于画面左侧还是右侧并不影响最终判断其为“猫”。因此,通过共享权值的方式实现了高效而强大的表示能力。 ### 卷积核的冗余性及其影响 关于卷积核中存在的潜在冗余现象,研究表明并非所有的通道都对特定任务有用;某些过滤器可能携带重复的信息或者贡献较小。当多个卷积核捕获几乎相同的视觉模式时就会形成所谓的“冗余”。 一方面,过多不必要的参数不仅增加了计算成本还可能导致过拟合问题的发生。另一方面,适当减少这些无意义连接有助于简化架构设计、加速训练过程以及提高泛化性能。为了应对这种情况,研究者们提出了诸如剪枝(pruning)[^2]等技术来消除不重要的连接从而构建更紧凑有效的模型。 ```python import torch.nn as nn class PrunedCNN(nn.Module): def __init__(self, num_classes=10): super(PrunedCNN, self).__init__() # 假设这里已经应用了一些方法去除了部分冗余卷积核 self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3) def forward(self, x): x = F.relu(self.conv1(x)) return x ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值