0%

LeetCode 669 - Trim a Binary Search Tree

Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, high]. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node’s descendant should remain a descendant). It can be proven that there is a unique answer.

LeetCode 700 - Search in a Binary Search Tree

You are given the root of a binary search tree (BST) and an integer val.

Find the node in the BST that the node’s value equals val and return the subtree rooted with that node. If such a node does not exist, return null.

example

1
2
Input: root = [4,2,7,1,3], val = 2
Output: [2,1,3]
1
2
Input: root = [4,2,7,1,3], val = 5
Output: []

How can we solve this problem?

在解決問題之前,我們需要知道什麼是Binary Search Tree。根據BST的定義:

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

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