logseq_notes/journals/2023_04_02.md
2023-06-14 14:27:22 +08:00

15 KiB
Raw Blame History

  • Leetcode Game of Life id:: 6428db9f-8f14-41a4-b5ec-5e79ffeb204a collapsed:: true

    • Times:
      • Time when completed: 10:19
      • Time taken to complete: 27min
      • Revisions:
    • Tags:
      • Data structures: #array
      • Difficulty: #difficulty_easy
      • Platforms: #leetcode
    • Links:
    • Problem:
      • According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

        The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

        • Any live cell with fewer than two live neighbors dies as if caused by under-population.

        • Any live cell with two or three live neighbors lives on to the next generation.

        • Any live cell with more than three live neighbors dies, as if by over-population.

        • Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

          The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.

      • Follow up:

        1. Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
        2. In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
    • Examples:
      • Example 1:
        
        Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
        Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
        
        Example 2:
        
        Input: board = [[1,1],[1,0]]
        Output: [[1,1],[1,1]]
        
    • Constraints:
      • m == board.length
      • n == board[i].length
      • 1 <= m, n <= 25
      • board[i][j] is 0 or 1.
    • Thoughts:
      • This is a simple problem about iterating over 2d arrays, two difficulties here:
        • to check for OOB
          • Just write a function
        • to do the steps in simultaneously
          • Write count of neighbors to the boards first, but remember to write 1 if the count is 0 and board value is 1, otherwise it will be mistaken for 0 in follow up counting, and its gonna die anyways :)
    • Solution:
      • class Solution {
          private boolean isLegit(int[][] board, int x, int y) {
            // returns if a location is legit
            if (x < 0 || y < 0) {
              return false;
            }
            if (x >= board.length || y >= board[0].length) {
              return false;
            }
            return true;
          }
          private int countNeighbors(int[][] board, int x, int y) {
            // returns live neighbors of the cell
            int count = 0;
            for (int a = x - 1; a <= x + 1; a++) {
              for (int b = y - 1; b <= y + 1; b++) {
                if (!isLegit(board, a, b) || (a == x && b == y)) {
                  continue;
                }
                if (board[a][b] > 0) {
                  count++;
                }
              }
            }
            // System.out.println("Neighbors for " + x + " " + y + " is " + count);
            return count;
          }
          public void gameOfLife(int[][] board) {
            // update cells
            int count;
            for (int x = 0, lx = board.length; x < lx; x++) {
              for (int y = 0, ly = board[0].length; y < ly; y++) {
                count = countNeighbors(board, x, y);
                if (board[x][y] == 1) {
                  if (count == 0) {
                    // going to die anyways
                    board[x][y] = 1;
                  } else {
                    board[x][y] = count;
                  }
                } else {
                  board[x][y] = count * -1;
                }
              }
            }
        
            for (int x = 0, lx = board.length; x < lx; x++) {
              for (int y = 0, ly = board[0].length; y < ly; y++) {
                if (board[x][y] > 0) {
                  if (board[x][y] < 2) {
                    board[x][y] = 0;
                  } else if (board[x][y] > 3) {
                    board[x][y] = 0;
                  } else {
                    board[x][y] = 1;
                  }
                } else if (board[x][y] == -3) {
                  board[x][y] = 1;
                } else {
                  board[x][y] = 0;
                }
              }
            }
          }
        }
        
  • Leetcode Find Minimum in Rotated Sorted Array id:: 64294296-85d5-4d21-b815-b4bad7525df5 collapsed:: true

    • Times:
      • Time when completed: 16:53
      • Time taken to complete: 47min
        • DONE Revisions: SCHEDULED: <2023-05-05 Fri> :LOGBOOK:
          • State "DONE" from "LATER" [2023-04-21 Fri 14:38]
          • State "DONE" from "LATER" [2023-04-21 Fri 14:39] :END:
    • Tags:
      • Algorithms: #binary_search
      • Data structures: #array #interval
      • Difficulty: #difficulty_medium
      • Platforms: #leetcode
    • Links:
    • Problem:
      • Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

        • [4,5,6,7,0,1,2] if it was rotated 4 times.
        • [0,1,2,4,5,6,7] if it was rotated 7 times.
      • Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

        Given the sorted rotated array nums of unique elements, return the minimum element of this array.

        You must write an algorithm that runs in O(log n) time.

    • Examples:
      • Example 1:
        
        Input: nums = [3,4,5,1,2]
        Output: 1
        Explanation: The original array was [1,2,3,4,5] rotated 3 times.
        
        Example 2:
        
        Input: nums = [4,5,6,7,0,1,2]
        Output: 0
        Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
        
        Example 3:
        
        Input: nums = [11,13,15,17]
        Output: 11
        Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
        
        
    • Constraints:
      • n == nums.length
      • 1 <= n <= 5000
      • -5000 <= nums[i] <= 5000
      • All the integers of nums are unique.
      • nums is sorted and rotated between 1 and n times.
    • Thoughts:
      • By analyzing the appearance of the array, we can know that there is a slope pattern:
        4,5,1,2,3
        -,t,l,-,-
        -,o,o,-,-
        -,p,w,-,-
        
        We want to find the starting point of the slope, and we can make use of three data:
        1. nums[l]
        2. nums[r]
        3. nums[mid] When nums[mid] < nums[r], this means mid is on the right side slope, and we need to move r to mid. When nums[mid] > nums[r], this means mid is on the left side slope, we need to move l to mid, and since we need the starting point of the right slope, we move l to mid + 1, and this breaks the loop when l == r - 1
    • Solution:
      • Revision 1: basic binary search method
        class Solution {
          public int findMin(int[] nums) {
            // Binary search for the change point.
            int l = 0, r = nums.length - 1;
            int mid = 0;
            while (r > l) {
              mid = l + (r - l) / 2;
              if (nums[r] - nums[mid] > 0) {
                // the change point is before mid;
                r = mid;
              } else if (nums[mid] - nums[l] >= 0) {
                // after mid
                l = mid + 1;
              }
            }
            return nums[l];
          }
        }
        
      • Revision 2: remove line 11's conditions
        class Solution {
          public int findMin(int[] nums) {
            // Binary search for the change point.
            int l = 0, r = nums.length - 1;
            int mid = 0;
            while (r > l) {
              mid = l + (r - l) / 2;
              if (nums[r] - nums[mid] > 0) {
                // the change point is before mid;
                r = mid;
              } else {
                // after mid
                l = mid + 1;
              }
            }
            return nums[l];
          }
        }
        
  • #+BEGIN_VERSE 毛概视频大纲 #to_be_deleted

    视频内容: 按照时间顺序从古到今叙述,历史部分从建党百年的艰辛探索,到北邮校史中的艰苦奋斗和国家情怀,当代部分讲北邮的创新环境和青年人的强国梦想。以“强国有我”为线索,环环相扣地指出强国有我为什么行,当代青年为什么要奋斗共筑中国梦

    特色: 采用实地取景的方式,图文并茂,身临其镜,致力于给观众带来最优质的学习体验

    分工: 马闻良:演讲中国党史部分,并撰写有关稿件。 王子昂:演讲北邮校史部分,并撰写有关稿件。 路思远:演讲北邮当代环境,并撰写有关稿件。 李王子:演讲青年强国梦想,并撰写有关稿件。 黄源源:视频拍摄,后期剪辑、配乐、和字幕等编辑作业。 #+END_VERSE

  • Todo

    • CANCELED [#B] 看电影
    • DONE [#B] 整理书单和电影清单 SCHEDULED: <2023-04-04 Tue>
    • DONE 写视频大纲 :LOGBOOK: CLOCK: [2023-04-02 Sun 19:29:35]--[2023-04-02 Sun 19:49:05] => 00:19:30 :END:
    • DONE 写一个每周 todo 的模板 SCHEDULED: <2023-04-03 Mon>
  • Leetcode Search in Rotated Sorted Array id:: 642aa126-13d8-48ff-8eb4-f17d875add97 collapsed:: true

    • Times:
      • Time when completed: 17:41
      • Time taken to complete: 26min
      • DONE Revisions: SCHEDULED: <2023-05-05 Fri> :LOGBOOK:
        • State "DONE" from "LATER" [2023-04-21 Fri 15:14] :END:
    • Tags:
      • Algorithms: #binary_search
      • Data structures: #array #interval
      • Difficulty: #difficulty_medium
      • Platforms: #leetcode
    • Links:
    • Problem:
      • There is an integer array nums sorted in ascending order (with distinct values).
      • Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
      • Given the array nums after the possible rotation and an integer target, return _the index of target if it is in nums, or -1 if it is not in _nums.
      • You must write an algorithm with O(log n) runtime complexity.
    • Examples:
      • Example 1:
        
        Input: nums = [4,5,6,7,0,1,2], target = 0
        Output: 4
        
        Example 2:
        
        Input: nums = [4,5,6,7,0,1,2], target = 3
        Output: -1
        
        Example 3:
        
        Input: nums = [1], target = 0
        Output: -1
        
    • Constraints:
      • 1 <= nums.length <= 5000
      • -104 <= nums[i] <= 104
      • All values of nums are unique.
      • nums is an ascending array that is possibly rotated.
      • -104 <= target <= 104
    • Thoughts:
      • This is a binary search problem, should take some more time to understand it, also, see ((64294296-85d5-4d21-b815-b4bad7525df5))
    • Solution:
      • class Solution {
          int biSearch(int[] nums, int l, int r, int target) {
            // System.out.printf("l: %d, r: %d\n", l, r);
            if (l > r || target < nums[l] || target > nums[r]) {
              return -1;
            }
            int mid;
            while (l <= r) {
              mid = l + (r - l) / 2;
              if (nums[mid] > target) {
                r = mid - 1;
              } else if (nums[mid] < target) {
                l = mid + 1;
              } else {
                return mid;
              }
            }
            return -1;
          }
          public int search(int[] nums, int target) {
            // first, find the pivot index, second, use binary search
            int l = 0, r = nums.length - 1, mid;
            while (l < r) {
              mid = l + (r - l) / 2; // check notes for why
              if (nums[r] > nums[mid]) {
                r = mid;
              } else {
                l = mid + 1;
              }
            }
            final int starting = l;
            // System.out.println(starting);
        
            int ansl = biSearch(nums, 0, starting - 1, target);
            int ansr = biSearch(nums, starting, nums.length - 1, target);
        
            return Math.max(ansl, ansr);
          }
        }
        
  • GPT notes collapsed:: true

    • How to write a 12 bar jazz? #music
      • To write a 12-bar blues, first, choose a key for your song. The traditional blues progression is based on the I-IV-V chord progression. In the key of C, this would be C, F, and G.

        Next, write the lyrics. A 12-bar blues typically follows an AAB lyrical structure, with the A line being repeated and the B line concluding the phrase.

        The 12-bar structure is as follows:

        | I | I | I | I | IV | IV | I | I | V | IV | I | I |
        

        In jazz, the 12-bar structure can be similar to the blues, but with more complex chords and often a different arrangement. Jazz musicians often use ii-V-I progressions to add more color and complexity to the harmony. It's important to experiment with different chord progressions and song structures to create a unique and interesting jazz composition.

    • How to learn to write music? #music
      • Learning to write music is a creative process that requires patience, practice, and dedication. Here are some steps that can help you get started:
          1. Study music theory: Understanding music theory is crucial to write good music. You should start by learning the basics of rhythm, melody, and harmony.
          1. Listen to music: Pay attention to the music you enjoy and try to analyze it. Listen to the different parts of the instrumentation and take notice of how the different elements come together to create a cohesive melody.
          1. Experiment with different instruments: If youre a beginner, experiment with different instruments to find the one that best fits you. You may also try using music software to create music.
          1. Write a melody: One of the easiest ways to start writing music is to come up with a melody. Once you have a melody in mind, experiment with different chords and harmonies to see what works best.
          1. Practice regularly: The key to getting better at writing music is to practice regularly. Set aside some time each day to create and experiment with different sounds.
          1. Get feedback: Listen to what others have to say about your music. Take constructive criticism from experienced musicians and work on improving your compositions accordingly.
        • Remember that everyone has their unique style when it comes to writing music. Don't be afraid to experiment and try new things. Keep practicing, and you will eventually find your rhythm.