[LeetCode] 1926. Nearest Exit from Entrance in Maze

1926. Nearest Exit from Entrance in Maze Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Breadth-First Search、Matrix 一、題目 You are given an m x n matrix maze (0-indexed) with empty cells (represented as '.') and walls (represented as '+'). You are also given the entrance of the maze, where entrance = [entrancerow, entrancecol] denotes the row and column of the cell you are initially standing at. In one step, you can move one cell up, down, left, or right. You cannot step into a cell with a wall, and you cannot step outside the maze. Your goal is to find the nearest exit from the entrance. An exit is defined as an empty cell that is at the border of the maze. The entrance does not count as an exit. ...

November 22, 2022 · 3 分鐘 · Rain Hu

[LeetCode] 279. Perfect Squares

279. Perfect Squares Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Math、Dynamic Programming、Breadth-First Search 一、題目 Given an integer n, return the least number of perfect square numbers that sum to n. A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not. Example 1: ...

November 22, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 337. House Robber III

337. House Robber III Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Dynamic Programming、Tree、Depth-First Search、Binary Tree 一、題目 The thief has found himself a new place for his thievery again. There is only one entrance to this area, called root. Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night. Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police. ...

November 19, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 587. Erect the Fence

587. Erect the Rence Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: Array、Math、Geometry 一、題目 You are given an array trees where trees[i] = [xi, yi] represents the location of a tree in the garden. You are asked to fence the entire garden using the minimum length of rope as it is expensive. The garden is well fenced only if all the trees are enclosed. Return the coordinates of trees that are exactly located on the fence perimeter. ...

November 19, 2022 · 1 分鐘 · Rain Hu

[LeetCode] 322. Coin Change

322. Coin Change Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Dynamic Programming、Breadth-First Search 一、題目 You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. You may assume that you have an infinite number of each kind of coin. ...

November 17, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2466. Count Ways To Build Good Strings

2466. Count Ways To Build Good Strings Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Dynamic Programming \(\color{blue}\textsf{Biweekly Contest 91}\) 一、題目 Given the integers zero, one, low, and high, we can construct a string by starting with an empty string, and then at each step perform either of the following: Append the character '0' zero times. Append the character '1' one times. This can be performed any number of times. A good string is a string constructed by the above process having a length between low and high (inclusive). Return the number of different good strings that can be constructed satisfying these properties. Since the answer can be large, return it modulo 10^9 + 7. Example 1: ...

November 17, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2465. Number of Distinct Averages

2465. Number of Distinct Averages Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Array、Hash Table、Two Pointers、Sorting \(\color{blue}\textsf{Biweekly Contest 91}\) 一、題目 You are given a 0-indexed integer array nums of even length. As long as nums is not empty, you must repetitively: Find the minimum number in nums and remove it. Find the maximum number in nums and remove it. Calculate the average of the two removed numbers. The average of two numbers a and b is (a + b) / 2. For example, the average of 2 and 3 is (2 + 3) / 2 = 2.5. Return the number of distinct averages calculated using the above process. Note that when there is a tie for a minimum or maximum number, any can be removed. Example 1: ...

November 17, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 223. Rectangle Area

223. Rectangle Area Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Math、Geometry 一、題目 Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles. The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2). The second rectangle is defined by its bottom-left corner (bx1, by1) and tis top-right corner (bx2, by2). Example 1: Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2 Output: 45 Example 2: ...

November 17, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 374. Guess Number Higher or Lower

374. Guess Number Higher or Lower Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Binary Search、Interactive 一、題目 We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess. You call a pre-defined API int guess(int num), which returns three possible results: ...

November 16, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 213. House Robber II

213. House Robber II Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Dynamic Programming 一、題目 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night. ...

November 15, 2022 · 2 分鐘 · Rain Hu