Skip to content

Commit c332d78

Browse files
committed
finish 173-177
1 parent 2212eb2 commit c332d78

5 files changed

+294
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 173. Binary Search Tree Iterator
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Stack, Tree, Design.
5+
- Similar Questions: Binary Tree Inorder Traversal, Flatten 2D Vector, Zigzag Iterator, Peeking Iterator, Inorder Successor in BST.
6+
7+
## Problem
8+
9+
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
10+
11+
Calling ```next()``` will return the next smallest number in the BST.
12+
13+
*Note:* ```next()``` and ```hasNext()``` should run in average O(1) time and uses O(*h*) memory, where *h* is the height of the tree.
14+
15+
**Credits:**Special thanks to @ts for adding this problem and creating all test cases.
16+
17+
## Solution
18+
19+
```javascript
20+
/**
21+
* Definition for binary tree
22+
* function TreeNode(val) {
23+
* this.val = val;
24+
* this.left = this.right = null;
25+
* }
26+
*/
27+
28+
/**
29+
* @constructor
30+
* @param {TreeNode} root - root of the binary search tree
31+
*/
32+
var BSTIterator = function(root) {
33+
this.stack = [];
34+
this.pushAll(root);
35+
};
36+
37+
38+
/**
39+
* @this BSTIterator
40+
* @returns {boolean} - whether we have a next smallest number
41+
*/
42+
BSTIterator.prototype.hasNext = function() {
43+
return this.stack.length !== 0;
44+
};
45+
46+
/**
47+
* @this BSTIterator
48+
* @returns {number} - the next smallest number
49+
*/
50+
BSTIterator.prototype.next = function() {
51+
var node = this.stack.pop();
52+
this.pushAll(node.right);
53+
return node.val;
54+
};
55+
56+
/**
57+
* Your BSTIterator will be called like this:
58+
* var i = new BSTIterator(root), a = [];
59+
* while (i.hasNext()) a.push(i.next());
60+
*/
61+
62+
BSTIterator.prototype.pushAll = function (node) {
63+
while (node) {
64+
this.stack.push(node);
65+
node = node.left;
66+
}
67+
};
68+
```
69+
70+
**Explain:**
71+
72+
nope.
73+
74+
**Complexity:**
75+
76+
* Time complexity :
77+
* Space complexity :

101-200/174. Dungeon Game.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 174. Dungeon Game
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Binary Search, Dynamic Programming.
5+
- Similar Questions: Unique Paths, Minimum Path Sum, Cherry Pickup.
6+
7+
## Problem
8+
9+
The demons had captured the princess (**P**) 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 (**K**) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
10+
11+
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.
12+
13+
Some of the rooms are guarded by demons, so the knight loses health (**negative** integers) upon entering these rooms; other rooms are either empty (**0's**) or contain magic orbs that increase the knight's health (**positive** integers).
14+
15+
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
16+
17+
**Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.**
18+
19+
For example, given the dungeon below, the initial health of the knight must be at least **7** if he follows the optimal path ```RIGHT-> RIGHT -> DOWN -> DOWN```.
20+
21+
| | | |
22+
| -- | -- | -- |
23+
| -2 (K) | -3 | 3 |
24+
| -5 | -10 | 1 |
25+
| 10 | 30 | -5 (P) |
26+
27+
**Note:**
28+
29+
- The knight's health has no upper bound.
30+
- 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.
31+
32+
## Solution
33+
34+
```javascript
35+
/**
36+
* @param {number[][]} dungeon
37+
* @return {number}
38+
*/
39+
var calculateMinimumHP = function(dungeon) {
40+
var m = dungeon.length;
41+
var n = dungeon[0].length;
42+
var dp = Array(m + 1).fill(0).map(_ => Array(n + 1).fill(Number.MAX_SAFE_INTEGER));
43+
var tmp = 0;
44+
45+
dp[m][ n - 1] = 1;
46+
dp[m - 1][n] = 1;
47+
48+
for (var i = m - 1; i >= 0; i--) {
49+
for (var j = n - 1; j >= 0; j--) {
50+
tmp = Math.min(dp[i][j + 1], dp[i + 1][j]) - dungeon[i][j];
51+
dp[i][j] = tmp <= 0 ? 1 : tmp;
52+
}
53+
}
54+
55+
return dp[0][0];
56+
};
57+
```
58+
59+
**Explain:**
60+
61+
nope.
62+
63+
**Complexity:**
64+
65+
* Time complexity : O(m*n).
66+
* Space complexity : O(m*n).

101-200/175. Combine Two Tables.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 175. Combine Two Tables
2+
3+
- Difficulty: Easy.
4+
- Related Topics: .
5+
- Similar Questions: Employee Bonus.
6+
7+
## Problem
8+
9+
Table: ```Person```
10+
11+
```
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| PersonId | int |
16+
| FirstName | varchar |
17+
| LastName | varchar |
18+
+-------------+---------+
19+
PersonId is the primary key column for this table.
20+
```
21+
22+
Table: ```Address```
23+
24+
```
25+
+-------------+---------+
26+
| Column Name | Type |
27+
+-------------+---------+
28+
| AddressId | int |
29+
| PersonId | int |
30+
| City | varchar |
31+
| State | varchar |
32+
+-------------+---------+
33+
AddressId is the primary key column for this table.
34+
```
35+
36+
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
37+
38+
```
39+
FirstName, LastName, City, State
40+
```
41+
42+
## Solution
43+
44+
```sql
45+
# Write your MySQL query statement below
46+
select a.FirstName, a.LastName, b.City, b.State from Person a left join Address b on a.PersonId = b.PersonId;
47+
```
48+
49+
**Explain:**
50+
51+
nope.
52+
53+
**Complexity:**
54+
55+
* Time complexity :
56+
* Space complexity :

101-200/176. Second Highest Salary.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 176. Second Highest Salary
2+
3+
- Difficulty: Easy.
4+
- Related Topics: .
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Write a SQL query to get the second highest salary from the ```Employee``` table.
10+
11+
```
12+
+----+--------+
13+
| Id | Salary |
14+
+----+--------+
15+
| 1 | 100 |
16+
| 2 | 200 |
17+
| 3 | 300 |
18+
+----+--------+
19+
```
20+
21+
For example, given the above Employee table, the query should return ```200``` as the second highest salary. If there is no second highest salary, then the query should return ```null```.
22+
23+
```
24+
+---------------------+
25+
| SecondHighestSalary |
26+
+---------------------+
27+
| 200 |
28+
+---------------------+
29+
```
30+
31+
## Solution
32+
33+
```javascript
34+
# Write your MySQL query statement below
35+
select max(Salary) as SecondHighestSalary from Employee where Salary < (select max(Salary) from Employee)
36+
```
37+
38+
**Explain:**
39+
40+
nope.
41+
42+
**Complexity:**
43+
44+
* Time complexity :
45+
* Space complexity :

101-200/177. Nth Highest Salary.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 177. Nth Highest Salary
2+
3+
- Difficulty: Medium.
4+
- Related Topics: .
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Write a SQL query to get the **n**th highest salary from the ```Employee``` table.
10+
11+
```
12+
+----+--------+
13+
| Id | Salary |
14+
+----+--------+
15+
| 1 | 100 |
16+
| 2 | 200 |
17+
| 3 | 300 |
18+
+----+--------+
19+
```
20+
21+
For example, given the above Employee table, the **n**th highest salary where **n** = 2 is ```200```. If there is no **n**th highest salary, then the query should return ```null```.
22+
23+
```
24+
+------------------------+
25+
| getNthHighestSalary(2) |
26+
+------------------------+
27+
| 200 |
28+
+------------------------+
29+
```
30+
31+
## Solution
32+
33+
```sql
34+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
35+
BEGIN
36+
RETURN (
37+
# Write your MySQL query statement below.
38+
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1
39+
);
40+
END
41+
```
42+
43+
**Explain:**
44+
45+
nope.
46+
47+
**Complexity:**
48+
49+
* Time complexity :
50+
* Space complexity :

0 commit comments

Comments
 (0)