19、最小区间

博客给出一个算法问题,即有 k 个升序排列的整数数组,要找到一个最小区间,使 k 个列表中每个列表至少有一个数包含在其中,并定义了区间大小的比较规则,还给出示例及相关注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:
你有 k 个升序排列的整数数组。找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中。

我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b] 比 [c,d] 小。

示例 1:

输入:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
输出: [20,24]
解释:
列表 1:[4, 10, 15, 24, 26],24 在区间 [20,24] 中。
列表 2:[0, 9, 12, 20],20 在区间 [20,24] 中。
列表 3:[5, 18, 22, 30],22 在区间 [20,24] 中。
注意:

给定的列表可能包含重复元素,所以在这里升序表示 >= 。
1 <= k <= 3500
-105 <= 元素的值 <= 105
对于使用Java的用户,请注意传入类型已修改为List<List>。重置代码模板后可以看到这项改动。

有问题没整理出来

class Solution {
    public int[] smallestRange(List<List<Integer>> nums) {
    PriorityQueue<int[]> priorityQueue = new PriorityQueue(
				(new Comparator<int[]>() {
					@Override
					public int compare(int[] o1, int[] o2) {
						// TODO 自动生成的方法存根
						return o2[0] - o1[0];
					}
				}));
//		每次都弹出最上面的,保存的是list的下标和索引数值
		for (int i = 0; i < nums.size(); i++) {
			int si = nums.get(i).size();
			priorityQueue.offer(new int[]{nums.get(i).get(si - 1),i,si - 1});
		}
		while (true) {
			int [] tem = priorityQueue.poll();
			int index1 = tem[1];
			int index2 = tem[2];
			if(index2 == 0){
				priorityQueue.offer(tem);
				break;
			}
			int newtem = nums.get(index1).get(index2 - 1);
			priorityQueue.offer(new int[]{newtem,index1,index2 - 1});
		}
		int tem1 [] = priorityQueue.poll();
		int num1 = tem1[0];
		int num2 = 0;
		while (!priorityQueue.isEmpty()) {
			num2 = priorityQueue.peek()[0];
			priorityQueue.poll();
		}
		return new int[]{num2,num1};
    }
}
### VGG19模型中的内容损失函数计算方法 #### 计算方法 内容损失函数通常用于衡量生成图像与原始输入图像之间的相似度。具体来说,它是基于VGG19预训练模型的某些中间层特征图来计算的。这些特征图能够捕捉到图像的内容信息而非风格信息。 假设 \( F_{ij} \) 表示通过VGG19网络某一层提取得到的特征矩阵,其中 \( i \) 是通道索引,\( j \) 是空间位置索引,则内容损失可以表示为: \[ L_{content}(P, X) = \frac{1}{2} \sum_k (F^l(P)_k - F^l(X)_k)^2 \] 这里的 \( P \) 和 \( X \) 分别代表原图像和生成图像,而 \( l \) 则指定了用来比较的特定层[^1]。该公式实际上是在逐像素地对比两幅图像经过相同卷积层变换后的激活值差异,并求其平方误差之和作为最终的结果。 #### 应用场景 1. **图像修复**:当部分区域损坏或者缺失时,可以通过最小化未受损区间的这种类型的距离来进行合理的填补。 2. **超分辨率重建(Super Resolution)**:提高低质量图片清晰度的同时保持原有细节不变形。 3. **风格迁移(Style Transfer)**:除了考虑样式匹配外还需要保留源素材的主要构成要素即所谓的“内容”。 ```python import torch from torchvision import models class ContentLoss(torch.nn.Module): def __init__(self, target, weight=1): super(ContentLoss, self).__init__() # we 'detach' the target content from the tree used self.target = target.detach() * weight # to dynamically compute the gradient: this is a stated value, # not a variable. Otherwise the forward method of the criterion # will throw an error. self.weight = weight def forward(self, input_): self.loss = self.weight * torch.sum((input_.view(input_.size()[0], -1)-self.target.view(self.target.size()[0], -1))**2 / (input_.size()[0]*input_.nelement()) return input_ def get_content_loss_model_and_losses(cnn, normalization_mean, normalization_std, style_img, content_img, content_layers=['conv_4']): cnn = copy.deepcopy(cnn) # normalization module normalization = Normalization(normalization_mean, normalization_std).cuda() # just in order to have an iterable access to or list of content/syle losses content_losses = [] model = torch.nn.Sequential(normalization) i = 0 for layer in cnn.children(): if isinstance(layer, torch.nn.Conv2d): i += 1 name = f'conv_{i}' elif isinstance(layer, torch.nn.ReLU): name = f'relu_{i}' layer = torch.nn.ReLU(inplace=False) elif isinstance(layer, torch.nn.MaxPool2d): name = f'pool_{i}' elif isinstance(layer, torch.nn.BatchNorm2d): name = f'bnorm_{i}' model.add_module(name, layer) if name in content_layers: # add content loss: target_feature_map = model(content_img).detach() content_loss = ContentLoss(target_feature_map) model.add_module(f"content_loss_{i}", content_loss) content_losses.append(content_loss) # now we trim off the layers after the last content and style losses for i in range(len(model) - 1, -1, -1): if isinstance(model[i], ContentLoss): break model = model[:(i+1)] return model, content_losses ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值