ARTS Week 20

Algorithm

本周的 LeetCode 题目为 56. 合并区间

以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [start_i, end_i]。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。

输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
输出:[[1,6],[8,10],[15,18]]
解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

首先将这个二维数组进行排序,排序的标准为:先按照 intervals[][0]start[i] 从小到大排序,如果 intervals[][0] 相等,则按照 intervals[][1]end[i] 从大到小进行排序。这样排好序后,第一个出现的 start[i] = x 必然对应着最大的区间,则后面再遇到 start[i+1] = x 时就可以跳过。后面遍历到 start[j] = y > x, j > i 时,判断 start[j] < end[i] 是否成立,如果成立,则说明 [start[j], end[j]][start[i], end[i]] 是有交集的,可以合并。反之则说明没有交集,不能合并。

class Solution {
    public int[][] merge(int[][] intervals) {
        /**
         * Custom Sort, rule is as bellow:
         * intervals[][0]: From small to big
         * if interval[i][0] == interval[j][1], then intervals[][1]: From big to small
         */
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] < o2[0]) {
                    return -1;
                } else if (o1[0] > o2[0]) {
                    return 1;
                } else {
                    if (o1[1] < o2[1]) {
                        return 1;
                    } else if (o1[1] > o2[1]) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            }
        });
        int startVal = -1;
        int endVal = -1;
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < intervals.length; i++) {
            if (startVal != intervals[i][0]) {
                if (intervals[i][0] <= endVal) {
                    endVal = intervals[i][1] > endVal? intervals[i][1] : endVal;
                } else {
                    int[] tmp = {startVal, endVal};
                    list.add(tmp);
                    startVal = intervals[i][0];
                    endVal = intervals[i][1];
                }
            } else {
                continue;
            }
        }

        // Add last pair and remove first pair ([-1, -1])
        int[] tmp = {startVal, endVal};
        list.add(tmp);
        list.remove(0);

        int[][] ans = new int[list.size()][2];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = list.get(i);
        }
        return ans;
    }
}

Review

本周 Review 的英文文章为:大多数建议都很糟糕

作者先在文中举出了一些听起来很有用的建议,例如:“努力工作”、“无法击败、无法忽视”、“遇到挫折永不言弃”等这类建议,听起来是有帮助,但它不具有实践可行性,我们都知道希望自己无法被击败、无法被忽视,但是怎么才能做到这些呢?作者最后给出他认为好的建议需要包含的三个主要组成部分:

  • 不明显
  • 它是具有可操作性的
  • 它基于一些真实的洞察力

作者用自己的另一篇文章为例,其中介绍了 Anki 并给出如何学习使用它的文章和视频链接。

Tip

Java 中 Math.abs(int a) 函数,当 a == Integer.MIN_VALUEa = -2147483648 时,返回的结果并不是 2147483648,而仍然是 -2147483648,如果想要得到 2147483648,那么则需要将 a 强制转型为 long 类型。下面是文档的原文:链接

public static int abs(int a)

Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

示例代码如下:

public class Main {
    public static void main(String[] args) {
        int a = Integer.MIN_VALUE;
        System.out.println(Math.abs(a)); // -2147483648
        System.out.println(Math.abs((long) a)); // 2147483648
    }
}

Share

本周分享一个 Python 的 Cheatsheet,链接如下:https://gto76.github.io/python-cheatsheet/index.html

里面包含了 Python 的集合、类型、语法、系统、数据、高级、库等诸多方面。

posted @ 2022-01-08 18:27  永远是萌新的阿岩  阅读(26)  评论(0编辑  收藏  举报