436. Find Right Interval

本文介绍了一种算法,用于解决给定一系列区间后,如何找出对于每个区间i是否存在另一个区间j,使得j的起始点大于等于i的结束点的问题。通过使用TreeMap,实现了高效的查找最接近的符合条件的区间j,并返回所有区间的索引结果。

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

Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.

For any interval i, you need to store the minimum interval j's index, which means that the interval j has the minimum start point to build the "right" relationship for interval i. If the interval j doesn't exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.

Note:

  1. You may assume the interval's end point is always bigger than its start point.
  2. You may assume none of these intervals have the same start point.

Example 1:

Input: [ [1,2] ]

Output: [-1]

Explanation: There is only one interval in the collection, so it outputs -1.

Example 2:

Input: [ [3,4], [2,3], [1,2] ]

Output: [-1, 0, 1]

Explanation: There is no satisfied "right" interval for [3,4].
For [2,3], the interval [3,4] has minimum-"right" start point;
For [1,2], the interval [2,3] has minimum-"right" start point.

Example 3:

Input: [ [1,4], [2,3], [3,4] ]

Output: [-1, 2, -1]

Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
For [2,3], the interval [3,4] has minimum-"right" start point.
首先,这道题让我们按照每个间隔的右点,遍历间隔数组,寻找大于或等于的最接近的间隔的左节点。那么我们需要一个map存左节点和index,这里用treemap保存自动排序,并用ceilingkey寻找lowbound的key,使代码极为简洁。代码如下:

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public int[] findRightInterval(Interval[] intervals) {
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
        int[] res = new int[intervals.length];
        for (int i = 0; i < intervals.length; i ++) {
            map.put(intervals[i].start, i);
        }
        for (int j = 0; j < intervals.length; j ++) {
            Integer key = map.ceilingKey(intervals[j].end);
            res[j] = key == null? -1: map.get(key);
        }
        return res;
    }
}

import os import numpy as np import matplotlib.pyplot as plt import re from matplotlib.ticker import MaxNLocator # 解决中文显示问题 plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'WenQuanYi Micro Hei'] plt.rcParams['axes.unicode_minus'] = False def natural_sort_key(s): """自然排序算法:确保文件名按数字顺序排列""" return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', s)] def find_stable_intervals(counts, min_window=300, max_window=2000, std_threshold=10.0, merge_gap=300, min_length=500): """ 改进版稳定区间检测:使用标准差作为稳定性指标 :param counts: 预测框数量列表 :param min_window: 最小窗口尺寸 :param max_window: 最大窗口尺寸 :param std_threshold: 标准差阈值(波动范围) :param merge_gap: 相邻区间合并的最大间隔 :param min_length: 最小有效区间长度 :return: 优化后的稳定区间列表 """ n = len(counts) if n == 0: return [] # 1. 自适应窗口机制 window_size = min(max_window, max(min_window, n // 10)) step_size = max(1, window_size // 2) # 50%重叠滑动 # 2. 初始检测稳定区间(使用标准差) base_intervals = [] for i in range(0, n - window_size + 1, step_size): window = counts[i:i + window_size] if len(window) < 2: # 至少需要2个点计算标准差 continue # 计算标准差作为稳定性指标 std_dev = np.std(window) if std_dev < std_threshold: base_intervals.append((i, i + window_size - 1)) # 如果没有检测到任何区间,直接返回 if not base_intervals: return [] # 3. 合并相邻平稳段 base_intervals.sort(key=lambda x: x[0]) # 确保按起始索引排序 merged_intervals = [] current_start, current_end = base_intervals[0] for start, end in base_intervals[1:]: if start - current_end <= merge_gap: # 间隔小于合并阈值 current_end = max(current_end, end) # 扩展当前区间 else: merged_intervals.append((current_start, current_end)) current_start, current_end = start, end merged_intervals.append((current_start, current_end)) # 4. 过滤短时伪平稳段 final_intervals = [ (start, end) for start, end in merged_intervals if (end - start + 1) >= min_length # 区间长度包含两端点 ] return final_intervals def plot_box_count_trend_with_stable_intervals(file_list, box_counts, stable_intervals, output_path): """ 绘制预测框数量变化趋势图并标记稳定区间 :param file_list: 文件名列表 :param box_counts: 预测框数量列表 :param stable_intervals: 稳定区间列表 :param output_path: 输出图片路径 """ plt.figure(figsize=(20, 10)) # 绘制整体趋势 plt.plot(file_list, box_counts, 'b-', linewidth=1.5, label='预测框数量') # 标记稳定区间 for i, (start, end) in enumerate(stable_intervals): interval_files = file_list[start:end + 1] interval_counts = box_counts[start:end + 1] if not interval_counts: # 确保区间有效 continue # 计算区间统计量 avg_count = np.mean(interval_counts) min_count = np.min(interval_counts) max_count = np.max(interval_counts) std_dev = np.std(interval_counts) # 绘制稳定区间 plt.fill_between(interval_files, min_count, max_count, color='green', alpha=0.2, label=f'稳定区间{i + 1}' if i == 0 else "") # 添加区间标注 mid_idx = start + (end - start) // 2 plt.annotate(f"区间{i + 1}: {start + 1}-{end + 1}\n均值: {avg_count:.1f}±{std_dev:.1f}", (file_list[mid_idx], avg_count), xytext=(0, 20), textcoords='offset points', ha='center', fontsize=10, bbox=dict(boxstyle="round,pad=0.3", fc="yellow", alpha=0.7)) # 设置图表属性 plt.title('预测框数量变化趋势及稳定区间分析', fontsize=18) plt.xlabel('图像文件名', fontsize=14) plt.ylabel('预测框数量', fontsize=14) plt.xticks(rotation=90, fontsize=7) plt.grid(True, linestyle='--', alpha=0.6) plt.legend(loc='upper right') # 添加统计信息 stats_text = f"总文件数: {len(file_list)}\n稳定区间数: {len(stable_intervals)}" plt.figtext(0.95, 0.95, stats_text, ha='right', va='top', bbox=dict(facecolor='white', alpha=0.8), fontsize=12) # 限制X轴刻度数量 plt.gca().xaxis.set_major_locator(MaxNLocator(20)) plt.tight_layout() plt.savefig(output_path, dpi=150, bbox_inches='tight') plt.close() # 配置路径 label_dir = "D:/630-3-label-combine" # 替换为您的标签文件夹路径 output_dir = "D:/630-report" # 输出目录 os.makedirs(output_dir, exist_ok=True) # 获取文件列表并按自然顺序排序 file_list = [f for f in os.listdir(label_dir) if f.endswith(".txt")] file_list.sort(key=natural_sort_key) # 提取文件名(不含扩展名) file_names = [os.path.splitext(f)[0] for f in file_list] # 统计每个文件的预测框数量 box_counts = [] for file in file_list: file_path = os.path.join(label_dir, file) count = 0 with open(file_path, 'r') as f: for line in f: if line.strip(): # 非空行 count += 1 box_counts.append(count) # 计算整体统计数据 total_mean = np.mean(box_counts) total_std = np.std(box_counts) # 找出稳定区间(使用标准差作为指标) stable_intervals = find_stable_intervals( box_counts, min_window=300, # 最小检测窗口 max_window=2000, # 最大检测窗口 std_threshold=total_std * 0.5, # 基于整体标准差设置阈值 merge_gap=300, # 合并最大间隔 min_length=500 # 最小有效长度 ) # 生成结果图片 output_path = os.path.join(output_dir, "box_count_stable_intervals_std.png") plot_box_count_trend_with_stable_intervals(file_names, box_counts, stable_intervals, output_path) # 输出详细结果 print(f"分析完成! 共处理 {len(file_list)} 个文件") print(f"整体平均框数: {total_mean:.2f} ± {total_std:.2f}") print(f"发现 {len(stable_intervals)} 个稳定区间:") for i, (start, end) in enumerate(stable_intervals): interval_counts = box_counts[start:end + 1] avg_count = np.mean(interval_counts) std_dev = np.std(interval_counts) print(f"区间{i + 1}:") print(f" - 文件范围: {start + 1}-{end + 1} (共{end - start + 1}个文件)") print(f" - 平均框数: {avg_count:.2f} ± {std_dev:.2f}") print(f" - 最小值: {min(interval_counts)}, 最大值: {max(interval_counts)}") print(f"结果图片已保存至: {output_path}") # 保存区间信息到文本文件 interval_info_path = os.path.join(output_dir, "stable_intervals_report_std.txt") with open(interval_info_path, 'w') as f: f.write(f"稳定区间分析报告(标准差指标)\n") f.write(f"总文件数: {len(file_list)}\n") f.write(f"整体平均框数: {total_mean:.2f} ± {total_std:.2f}\n") f.write(f"稳定区间数: {len(stable_intervals)}\n\n") for i, (start, end) in enumerate(stable_intervals): interval_counts = box_counts[start:end + 1] avg_count = np.mean(interval_counts) std_dev = np.std(interval_counts) f.write(f"区间 {i + 1}:\n") f.write(f" 起始文件索引: {start + 1} ({file_names[start]})\n") f.write(f" 结束文件索引: {end + 1} ({file_names[end]})\n") f.write(f" 文件数量: {end - start + 1}\n") f.write(f" 平均预测框数: {avg_count:.2f} ± {std_dev:.2f}\n") f.write(f" 最小值: {min(interval_counts)}, 最大值: {max(interval_counts)}\n") f.write("-" * 50 + "\n") print(f"详细区间报告已保存至: {interval_info_path}") 将代码中的标准差改为标准差和变异系数和趋势斜率,分别生成三张图,再将三个指标生成的稳定区间合一起生成一张图
最新发布
07-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值