- Leetcode - Merge Intervals
  id:: 642edfd2-c458-484e-9e94-8802771475d5
  collapsed:: true
	- Times:
		- Time when completed: 09:34
		- Time taken to complete: 37mins
		- DONE Revisions:
		  SCHEDULED: <2023-05-19 Fri>
		  :LOGBOOK:
		  - State "DONE" from "LATER" [2023-04-21 Fri 15:47]
		  :END:
		  * [[Apr 21st, 2023]] 10mins
	- 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(..)`
			- why we sort by their start times instead of end times like in [[Leetcode Non-Overlapping-Intervals]] ?
				- We sort the intervals by their start times because it allows us to merge overlapping intervals more efficiently.
				- Consider the case where we sort the intervals by their end times instead of start times. If we iterate over the sorted intervals and try to merge overlapping intervals by comparing their end times, we may miss some overlapping intervals.
				- For example, suppose we have the intervals [1, 4], [2, 3], and [3, 5]. If we sort these intervals by their end times, we get [2, 3], [1, 4], and [3, 5]. If we iterate over these intervals and try to merge overlapping intervals by comparing their end times, we would merge [2, 3] and [1, 4] into [1, 4], but we would not merge [1, 4] and [3, 5], because their end times do not overlap.
				- On the other hand, if we sort the intervals by their start times, we can be sure that if two intervals overlap, their start times will also overlap. This means that we can simply compare the end time of the previous interval with the start time of the current interval to determine whether they overlap, and we don't have to worry about missing any overlapping intervals.
				- In summary, sorting the intervals by their start times allows us to merge overlapping intervals more efficiently and reliably.
	- 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