[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: ...

<span title='2022-11-16 22:57:11 +0800 +0800'>November 16, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;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. ...

<span title='2022-11-15 23:45:49 +0800 +0800'>November 15, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 198. House Robber

198. House Robber 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, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. ...

<span title='2022-11-15 23:33:01 +0800 +0800'>November 15, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 947. Most Stones Removed with Same Row or Column

947. Most Stones Removed with Same Row or Column Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Depth-First Search、Union Find、Graph 一、題目 On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either the same row or the same column as another stone that has not been removed. Given an array stones of length n where stones[i] = [xi, yi] represents the location of the ith stone, return the largest possible number of stones that can be removed. ...

<span title='2022-11-15 00:12:30 +0800 +0800'>November 15, 2022</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 151. Reverse Words in a String

151. Reverse Words in a String Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Two Pointers、String 一、題目 Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. ...

<span title='2022-11-13 17:35:59 +0800 +0800'>November 13, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 23. Merge k Sorted Lists

23. Merge k Sorted Lists Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: Linked List、Divide and Conquer、Heap (Priority Queue)、Merge Sort 一、題目 You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Example 1: Input: lists = [[1,4,5],[1,3,4],[2,6]] Output: [1,1,2,3,4,4,5,6] Explanation: The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted list: 1->1->2->3->4->4->5->6 Example 2: ...

<span title='2022-11-11 00:24:47 +0800 +0800'>November 11, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 22. Generate Parentheses

22. Generate Parentheses Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: String、Dynamic Programming、Backtracking 一、題目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] Example 2: Input: n = 1 Output: ["()"] Constraints: 1 <= n <= 8 二、分析 DFS 演算法是在遍歷「節點」,而回溯法是在遍歷「樹枝」。站在一個節點上,需思考三個問題: 路徑(PATH):已做出的選擇。 選項(OPTION):當前可以做的選擇。 終止條件(TERMINATE):到達決策樹的底層,無法再做其它選擇。 以下為回溯法的框架: vector<PATH> res; void backtrack(PATH, OPTION) { if (TERMINATE) { res.push_back(PATH); return; } for (CHOICE : OPTION) { DO OPTION; backtrack(PATH, OPTION); CANCEL OPTION; } } 本題的終止條件是當 path 的長度為 2n 的時候。 而選項是增加左括號 ( 與增加右括號 )。 加上兩個子節點的條件便完成, 左節點需滿足 left < n。 右節點需滿足 right < n && right < left。 DP 動態規劃則需觀察轉移方程式。 dp[0] base case: `` dp[1] 很容易得到:() dp[2] 也不難:()()、(()) 接下來觀察 dp[3],可以分解為下面三個: ( + dp[0] + ) + dp[2]:()()()、()(()) ( + dp[1] + ) + dp[1]:(())() ( + dp[2] + ) + dp[0]:(()())、((())) 換句話說,轉移方程式可以寫成:dp[i] = "(" + dp[j] + ")" + dp[i-j-1] 三、解題 1. Backtrack Time complexity: \(O(2^{2n})\),Wiki - Catalan number Space complexity: \(O(n)\) vector<string> generateParenthesis(int n) { string path; vector<string> res; backtrack(n, 0, 0, res, path); return res; } void backtrack(int n, int left, int right, vector<string>& res, string& path) { // terminate if (path.length() == 2*n) { res.push_back(path); return; } // select if (left < n) { path.push_back('('); backtrack(n, left+1, right, res, path); path.pop_back(); } if (right < n && right < left) { path.push_back(')'); backtrack(n, left, right+1, res, path); path.pop_back(); } } 2. Dynamic Programming Time complexity: \(O(n^4)\) Space complexity: \(O(n)\) vector<string> generateParenthesis(int n) { vector<vector<string>> dp(n+1); dp[0] = {""}; for(int i=1;i<=n;i++){ for(int j=0;j<i;j++){ vector<string> left = dp[j]; vector<string> right = dp[i-j-1]; for(int k=0;k<left.size();k++){ for(int l=0;l<right.size();l++){ dp[i].push_back("(" + left[k] + ")" + right[l]); } } } } return dp[n]; } 回目錄 Catalog ...

<span title='2022-11-10 23:44:24 +0800 +0800'>November 10, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 21. Merge Two Sorted Lists

21. Merge Two Sorted Lists Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Linked List、Recursion 一、題目 You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Example 1: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: ...

<span title='2022-11-10 20:25:07 +0800 +0800'>November 10, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 1047. Remove All Adjacent Duplicates In String

1047. Remove All Adjacent Duplicates In String Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: String、Stack 一、題目 You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them. We repeatly make duplicate removals on s until we no longer can. Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique. ...

<span title='2022-11-10 20:01:29 +0800 +0800'>November 10, 2022</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 901. Online Stock Span

901. Online Stock Span Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Stack、Design、Monotonic Stack、Data Stream 一、題目 Design an algorithm that collects daily price quotes for some stock and returns the span of that stock’s price for the current day. The span of the stock’s price today is defined as the maximum number of consecutive days (starting from today and going backward) for which the stock price was less than or equal to today’s price. ...

<span title='2022-11-09 23:35:42 +0800 +0800'>November 9, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu