- Leetcode - Merge Intervals
  id:: 642edfd2-c458-484e-9e94-8802771475d5
  collapsed:: true
  - Times:
    - Time when completed: 09:34
    - Time taken to complete: 37mins
    - LATER Revisions:
  - Tags:
    - Algorithms:
    - Data structures: #array #interval
    - Difficulty: #difficulty_medium
    - Platforms: #leetcode
  - Links:
    - [link to the problem](https://leetcode.com/problems/merge-intervals/description/)
  - Problem:
    - Given an array of `intervals` where `intervals[i] = [starti, endi]`, merge all overlapping intervals, and return _an array of the non-overlapping intervals that cover all the intervals in the input_.
  - Examples:
    - ```
      Example 1:

      Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
      Output: [[1,6],[8,10],[15,18]]
      Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].

      Example 2:

      Input: intervals = [[1,4],[4,5]]
      Output: [[1,5]]
      Explanation: Intervals [1,4] and [4,5] are considered overlapping.
      ```
  - Constraints:
    - `1 <= intervals.length <= 104`
    - `intervals[i].length == 2`
    - `0 <= starti <= endi <= 104`
  - Thoughts:
    - My take:
      - Intuition:
        - Similar to ((642cd1a8-9707-4dc2-9d03-11464553e618)), to merge all intervals in O(n), we have to sort the intervals array first, which takes $$O(nlogn)$$, and then we use the merging techniques similar to aforementioned solution.
      - Approach:
        - Sort the intervals
        - use a `merged` to keep track current array, and use `ans` to keep track of the answer
        - loop over the intervals, merge any intersecting intervals into merged
        - when no intersections are found, push `merged` into `ans`
        - return the array version of `ans`: `ans.toArray(..)`
  - Solution:
    - Code
      - ```java
        class Solution {
            public int[][] merge(int[][] intervals) {
                ArrayList<int[]> ans = new ArrayList();
                Arrays.sort(intervals, (a, b) -> a[0] - b[0]);

                int[] merged = intervals[0];
                int i = 1;
                while (i < intervals.length) {
                    if (merged[1] >= intervals[i][0]) {
                        // they must overlap, because intervals are sorted.
                        merged[1] = Math.max(intervals[i][1], merged[1]);
                    } else {
                        ans.add(merged);
                        merged = intervals[i];
                    }
                    i++;
                }
                // there will always be a merged left not added.
                ans.add(merged);
                return ans.toArray(new int[ans.size()][2]);
            }
        }
        ```
-
- Leetcode - Palindrome Number
  collapsed:: true
  - Times:
    - Time when completed: 13:55
    - Time taken to complete: 3min
    - Revisions:
  - Tags:
    - Difficulty: #difficulty_easy
    - Platforms: #leetcode
  - Links:
    - [link to the problem](https://leetcode.com/problems/palindrome-number/description/)
  - Problem:
    - Given an integer `x`, return `true` if `x` is a _palindrome_, and `false` otherwise
  - Examples:
    - ```
      Example 1:

      Input: x = 121
      Output: true
      Explanation: 121 reads as 121 from left to right and from right to left.

      Example 2:

      Input: x = -121
      Output: false
      Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

      Example 3:

      Input: x = 10
      Output: false
      Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
      ```
  - Constraints:
    - `-2^31 <= x <= 2^31 - 1`
  - Thoughts:
    - Intuition:
      - If a number is a palindrome, the reverse of it equals its original. We reverse a number and check if they are equal
      - Special cases should be considered:
        - 0
        - negative
        - has 0 on the end
    - Approach:
      - Reverse a number
      - check if they equals
  - Solution:
    - Code
      - ```java
        class Solution {
          public boolean isPalindrome(int x) {
            if (x == 0) {
              return true;
            }
            if (x < 0 || x % 10 == 0) {
              return false;
            }

            // make an reverse number
            int reverse = 0;
            int copy = x;
            while (copy > 0) {
              reverse = reverse * 10 + copy % 10;
              copy /= 10;
            }

            return reverse == x;
          }
        }
        ```
-
- DONE 把智慧树剩下的一点课刷了
  SCHEDULED: <2023-04-06 Thu>
-
- DONE 规划乐理学习
  - Open Music Theory 的记录
  - 日常 ear training
-
- Daily reflection [[Daily reflections]]
  collapsed:: true
  - What I've done
    - Music theory course
    - Future plans for music theory study
    - Spent a lot of time improvising
    - properly configured ardour and stuff
  - What I've thought #thoughts
    - Music theory is fun, I want to freely express myself one day.
    - I figured reasons for not learning bass in school, I should learn more music theory instead.
  - Mood
