0%

LeetCode 1260 - Shift 2D Grid

Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.

In one shift operation:

  • Element at grid[i][j] moves to grid[i][j + 1].
  • Element at grid[i][n - 1] moves to grid[i + 1][0].
  • Element at grid[m - 1][n - 1] moves to grid[0][0].

Return the 2D grid after applying shift operation k times.

example

LeetCode 59 - Spiral Matrix II

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

example

1
2
Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]
1
2
Input: n = 1
Output: [[1]]

How can we solve this problem?

這題跟Spiral Matrix做法差不多,我們不難發現他的移動模式就是(右→下↓左←上),我們只需要通過幾個變數來限制移動的步數。另外,這題需要我們返回一個n2的array,所以,需要額外定義一個counter來記錄每次插入的數值為多少,因此,只要counter 到達n2的大小就知道是否完成插入所需的值。

LeetCode 289 - Game of Life

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):

LeetCode 682 - Baseball Game

You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds’ scores.

At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:

LeetCode 347 - Top K Frequent Elements

Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

example

1
2
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
1
2
Input: nums = [1], k = 1
Output: [1]

How can we solve this problem?

這一題需要我們返回K個數量最多的element。所以,我們可以使用map記錄我們array中element的個數,然後在把他們以<frequency,element>存到priority queue/max queue,最後只要返回priority queue中的k個element即可。

LeetCode 703 - Kth Largest Element in a Stream

Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Implement KthLargest class:

  • KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.
  • int add(int val) Appends the integer val to the stream and returns the element representing the kth largest element in the stream.

example:

Reward

這是我跟組員們一起討論、辛苦了多個日夜,能一起做寫程式,分享知識的感覺真的很棒><。雖然不知道以後還沒有機會一起合作,但還是非常感謝他們願意陪我躲在實驗室裡面一整天(╥╯^╰╥)。希望他們能好好生活,好好學習!

什麼是Abstract Factory(抽象工廠)呢?

定義: 又稱為Kit模式。提供一個創建系列相關或者互相依賴的Interface,而無需指定其具體的class

簡單例子

注: 以下程式單純用於解釋,並不能實際執行

基於Gin實作Rate Limiter

假設我們有2個APIs,而每個API都需要消耗1個Tokens

urimethoddesc
/api/posts/{id}GETreturn a simple demo message
/pingGETreturn pong

我們先設置一下rate limiter桶子的容量只能放下一個tokens,而tokens則會每秒生產5個。存取API時,若沒有Token可以用,就需要等待token下一次生產並放到桶子裡,才能繼續進行下去。