diff --git a/src/main/java/g0101_0200/s0173_binary_search_tree_iterator/readme.md b/src/main/java/g0101_0200/s0173_binary_search_tree_iterator/readme.md new file mode 100644 index 000000000..d291fa408 --- /dev/null +++ b/src/main/java/g0101_0200/s0173_binary_search_tree_iterator/readme.md @@ -0,0 +1,29 @@ +173\. Binary Search Tree Iterator + +Medium + +Implement the `BSTIterator` class that represents an iterator over the **[in-order traversal](https://en.wikipedia.org/wiki/Tree_traversal#In-order_(LNR))** of a binary search tree (BST): + +* `BSTIterator(TreeNode root)` Initializes an object of the `BSTIterator` class. The `root` of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST. +* `boolean hasNext()` Returns `true` if there exists a number in the traversal to the right of the pointer, otherwise returns `false`. +* `int next()` Moves the pointer to the right, then returns the number at the pointer. + +Notice that by initializing the pointer to a non-existent smallest number, the first call to `next()` will return the smallest element in the BST. + +You may assume that `next()` calls will always be valid. That is, there will be at least a next number in the in-order traversal when `next()` is called. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2018/12/25/bst-tree.png) + +**Input** \["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"\] \[\[\[7, 3, 15, null, null, 9, 20\]\], \[\], \[\], \[\], \[\], \[\], \[\], \[\], \[\], \[\]\] **Output** \[null, 3, 7, true, 9, true, 15, true, 20, false\] **Explanation** BSTIterator bSTIterator = new BSTIterator(\[7, 3, 15, null, null, 9, 20\]); bSTIterator.next(); // return 3 bSTIterator.next(); // return 7 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 9 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 15 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 20 bSTIterator.hasNext(); // return False + +**Constraints:** + +* The number of nodes in the tree is in the range `[1, 105]`. +* `0 <= Node.val <= 106` +* At most `105` calls will be made to `hasNext`, and `next`. + +**Follow up:** + +* Could you implement `next()` and `hasNext()` to run in average `O(1)` time and use `O(h)` memory, where `h` is the height of the tree? \ No newline at end of file diff --git a/src/main/java/g0101_0200/s0174_dungeon_game/readme.md b/src/main/java/g0101_0200/s0174_dungeon_game/readme.md new file mode 100644 index 000000000..ac54619ff --- /dev/null +++ b/src/main/java/g0101_0200/s0174_dungeon_game/readme.md @@ -0,0 +1,38 @@ +174\. Dungeon Game + +Hard + +The demons had captured the princess and imprisoned her in **the bottom-right corner** of a `dungeon`. The `dungeon` consists of `m x n` rooms laid out in a 2D grid. Our valiant knight was initially positioned in **the top-left room** and must fight his way through `dungeon` to rescue the princess. + +The knight has an initial health point represented by a positive integer. If at any point his health point drops to `0` or below, he dies immediately. + +Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers). + +To reach the princess as quickly as possible, the knight decides to move only **rightward** or **downward** in each step. + +Return _the knight's minimum initial health so that he can rescue the princess_. + +**Note** that any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg) + +**Input:** dungeon = \[\[-2,-3,3\],\[-5,-10,1\],\[10,30,-5\]\] + +**Output:** 7 + +**Explanation:** The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN. + +**Example 2:** + +**Input:** dungeon = \[\[0\]\] + +**Output:** 1 + +**Constraints:** + +* `m == dungeon.length` +* `n == dungeon[i].length` +* `1 <= m, n <= 200` +* `-1000 <= dungeon[i][j] <= 1000` \ No newline at end of file diff --git a/src/main/java/g0101_0200/s0175_combine_two_tables/readme.md b/src/main/java/g0101_0200/s0175_combine_two_tables/readme.md new file mode 100644 index 000000000..8838b73f1 --- /dev/null +++ b/src/main/java/g0101_0200/s0175_combine_two_tables/readme.md @@ -0,0 +1,27 @@ +175\. Combine Two Tables + +Easy + +SQL Schema + +Table: `Person` + ++-------------+---------+ | Column Name | Type | +-------------+---------+ | personId | int | | lastName | varchar | | firstName | varchar | +-------------+---------+ personId is the primary key column for this table. This table contains information about the ID of some persons and their first and last names. + +Table: `Address` + ++-------------+---------+ | Column Name | Type | +-------------+---------+ | addressId | int | | personId | int | | city | varchar | | state | varchar | +-------------+---------+ addressId is the primary key column for this table. Each row of this table contains information about the city and state of one person with ID = PersonId. + +Write an SQL query to report the first name, last name, city, and state of each person in the `Person` table. If the address of a `personId` is not present in the `Address` table, report `null` instead. + +Return the result table in **any order**. + +The query result format is in the following example. + +**Example 1:** + +**Input:** Person table: +----------+----------+-----------+ | personId | lastName | firstName | +----------+----------+-----------+ | 1 | Wang | Allen | | 2 | Alice | Bob | +----------+----------+-----------+ Address table: +-----------+----------+---------------+------------+ | addressId | personId | city | state | +-----------+----------+---------------+------------+ | 1 | 2 | New York City | New York | | 2 | 3 | Leetcode | California | +-----------+----------+---------------+------------+ + +**Output:** +-----------+----------+---------------+----------+ | firstName | lastName | city | state | +-----------+----------+---------------+----------+ | Allen | Wang | Null | Null | | Bob | Alice | New York City | New York | +-----------+----------+---------------+----------+ + +**Explanation:** There is no address in the address table for the personId = 1 so we return null in their city and state. addressId = 1 contains information about the address of personId = 2. \ No newline at end of file diff --git a/src/main/java/g0101_0200/s0176_second_highest_salary/readme.md b/src/main/java/g0101_0200/s0176_second_highest_salary/readme.md new file mode 100644 index 000000000..103749033 --- /dev/null +++ b/src/main/java/g0101_0200/s0176_second_highest_salary/readme.md @@ -0,0 +1,25 @@ +176\. Second Highest Salary + +Medium + +SQL Schema + +Table: `Employee` + ++-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | salary | int | +-------------+------+ id is the primary key column for this table. Each row of this table contains information about the salary of an employee. + +Write an SQL query to report the second highest salary from the `Employee` table. If there is no second highest salary, the query should report `null`. + +The query result format is in the following example. + +**Example 1:** + +**Input:** Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ + +**Output:** +---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+ + +**Example 2:** + +**Input:** Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | +----+--------+ + +**Output:** +---------------------+ | SecondHighestSalary | +---------------------+ | null | +---------------------+ \ No newline at end of file diff --git a/src/main/java/g0101_0200/s0177_nth_highest_salary/readme.md b/src/main/java/g0101_0200/s0177_nth_highest_salary/readme.md new file mode 100644 index 000000000..578e57bb3 --- /dev/null +++ b/src/main/java/g0101_0200/s0177_nth_highest_salary/readme.md @@ -0,0 +1,25 @@ +177\. Nth Highest Salary + +Medium + +SQL Schema + +Table: `Employee` + ++-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | salary | int | +-------------+------+ id is the primary key column for this table. Each row of this table contains information about the salary of an employee. + +Write an SQL query to report the `nth` highest salary from the `Employee` table. If there is no `nth` highest salary, the query should report `null`. + +The query result format is in the following example. + +**Example 1:** + +**Input:** Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ n = 2 + +**Output:** +------------------------+ | getNthHighestSalary(2) | +------------------------+ | 200 | +------------------------+ + +**Example 2:** + +**Input:** Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | +----+--------+ n = 2 + +**Output:** +------------------------+ | getNthHighestSalary(2) | +------------------------+ | null | +------------------------+ \ No newline at end of file diff --git a/src/main/java/g0101_0200/s0178_rank_scores/readme.md b/src/main/java/g0101_0200/s0178_rank_scores/readme.md new file mode 100644 index 000000000..f5d9ab408 --- /dev/null +++ b/src/main/java/g0101_0200/s0178_rank_scores/readme.md @@ -0,0 +1,25 @@ +178\. Rank Scores + +Medium + +SQL Schema + +Table: `Scores` + ++-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | score | decimal | +-------------+---------+ id is the primary key for this table. Each row of this table contains the score of a game. Score is a floating point value with two decimal places. + +Write an SQL query to rank the scores. The ranking should be calculated according to the following rules: + +* The scores should be ranked from the highest to the lowest. +* If there is a tie between two scores, both should have the same ranking. +* After a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no holes between ranks. + +Return the result table ordered by `score` in descending order. + +The query result format is in the following example. + +**Example 1:** + +**Input:** Scores table: +----+-------+ | id | score | +----+-------+ | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | | 5 | 4.00 | | 6 | 3.65 | +----+-------+ + +**Output:** +-------+------+ | score | rank | +-------+------+ | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 2 | | 3.65 | 3 | | 3.65 | 3 | | 3.50 | 4 | +-------+------+ \ No newline at end of file diff --git a/src/main/java/g0101_0200/s0179_largest_number/readme.md b/src/main/java/g0101_0200/s0179_largest_number/readme.md new file mode 100644 index 000000000..882ef78a9 --- /dev/null +++ b/src/main/java/g0101_0200/s0179_largest_number/readme.md @@ -0,0 +1,36 @@ +179\. Largest Number + +Medium + +Given a list of non-negative integers `nums`, arrange them such that they form the largest number. + +**Note:** The result may be very large, so you need to return a string instead of an integer. + +**Example 1:** + +**Input:** nums = \[10,2\] + +**Output:** "210" + +**Example 2:** + +**Input:** nums = \[3,30,34,5,9\] + +**Output:** "9534330" + +**Example 3:** + +**Input:** nums = \[1\] + +**Output:** "1" + +**Example 4:** + +**Input:** nums = \[10\] + +**Output:** "10" + +**Constraints:** + +* `1 <= nums.length <= 100` +* `0 <= nums[i] <= 109` \ No newline at end of file diff --git a/src/main/java/g0201_0300/s0210_course_schedule_ii/readme.md b/src/main/java/g0201_0300/s0210_course_schedule_ii/readme.md new file mode 100644 index 000000000..c7ab74685 --- /dev/null +++ b/src/main/java/g0201_0300/s0210_course_schedule_ii/readme.md @@ -0,0 +1,40 @@ +210\. Course Schedule II + +Medium + +There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`. + +* For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`. + +Return _the ordering of courses you should take to finish all courses_. If there are many valid answers, return **any** of them. If it is impossible to finish all courses, return **an empty array**. + +**Example 1:** + +**Input:** numCourses = 2, prerequisites = \[\[1,0\]\] + +**Output:** \[0,1\] + +**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is \[0,1\]. + +**Example 2:** + +**Input:** numCourses = 4, prerequisites = \[\[1,0\],\[2,0\],\[3,1\],\[3,2\]\] + +**Output:** \[0,2,1,3\] + +**Explanation:** There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is \[0,1,2,3\]. Another correct ordering is \[0,2,1,3\]. + +**Example 3:** + +**Input:** numCourses = 1, prerequisites = \[\] + +**Output:** \[0\] + +**Constraints:** + +* `1 <= numCourses <= 2000` +* `0 <= prerequisites.length <= numCourses * (numCourses - 1)` +* `prerequisites[i].length == 2` +* `0 <= ai, bi < numCourses` +* `ai != bi` +* All the pairs `[ai, bi]` are **distinct**. \ No newline at end of file diff --git a/src/main/java/g0201_0300/s0211_design_add_and_search_words_data_structure/readme.md b/src/main/java/g0201_0300/s0211_design_add_and_search_words_data_structure/readme.md new file mode 100644 index 000000000..8e7d5989a --- /dev/null +++ b/src/main/java/g0201_0300/s0211_design_add_and_search_words_data_structure/readme.md @@ -0,0 +1,22 @@ +211\. Design Add and Search Words Data Structure + +Medium + +Design a data structure that supports adding new words and finding if a string matches any previously added string. + +Implement the `WordDictionary` class: + +* `WordDictionary()` Initializes the object. +* `void addWord(word)` Adds `word` to the data structure, it can be matched later. +* `bool search(word)` Returns `true` if there is any string in the data structure that matches `word` or `false` otherwise. `word` may contain dots `'.'` where dots can be matched with any letter. + +**Example:** + +**Input** \["WordDictionary","addWord","addWord","addWord","search","search","search","search"\] \[\[\],\["bad"\],\["dad"\],\["mad"\],\["pad"\],\["bad"\],\[".ad"\],\["b.."\]\] **Output** \[null,null,null,null,false,true,true,true\] **Explanation** WordDictionary wordDictionary = new WordDictionary(); wordDictionary.addWord("bad"); wordDictionary.addWord("dad"); wordDictionary.addWord("mad"); wordDictionary.search("pad"); // return False wordDictionary.search("bad"); // return True wordDictionary.search(".ad"); // return True wordDictionary.search("b.."); // return True + +**Constraints:** + +* `1 <= word.length <= 500` +* `word` in `addWord` consists lower-case English letters. +* `word` in `search` consist of `'.'` or lower-case English letters. +* At most `50000` calls will be made to `addWord` and `search`. \ No newline at end of file diff --git a/src/main/java/g0201_0300/s0212_word_search_ii/readme.md b/src/main/java/g0201_0300/s0212_word_search_ii/readme.md new file mode 100644 index 000000000..aa7eb7cc8 --- /dev/null +++ b/src/main/java/g0201_0300/s0212_word_search_ii/readme.md @@ -0,0 +1,34 @@ +212\. Word Search II + +Hard + +Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_. + +Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/07/search1.jpg) + +**Input:** board = \[\["o","a","a","n"\],\["e","t","a","e"\],\["i","h","k","r"\],\["i","f","l","v"\]\], words = \["oath","pea","eat","rain"\] + +**Output:** \["eat","oath"\] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/11/07/search2.jpg) + +**Input:** board = \[\["a","b"\],\["c","d"\]\], words = \["abcb"\] + +**Output:** \[\] + +**Constraints:** + +* `m == board.length` +* `n == board[i].length` +* `1 <= m, n <= 12` +* `board[i][j]` is a lowercase English letter. +* `1 <= words.length <= 3 * 104` +* `1 <= words[i].length <= 10` +* `words[i]` consists of lowercase English letters. +* All the strings of `words` are unique. \ No newline at end of file