Skip to content

Commit cc72d82

Browse files
committed
finish 178, 566
1 parent 1dbca21 commit cc72d82

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

101-200/177. Nth Highest Salary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
3535
BEGIN
3636
RETURN (
3737
# Write your MySQL query statement below.
38-
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1
38+
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT N, 1
3939
);
4040
END
4141
```

101-200/178. Rank Scores.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 178. Rank Scores
2+
3+
- Difficulty: Medium.
4+
- Related Topics: .
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.
10+
11+
```
12+
+----+-------+
13+
| Id | Score |
14+
+----+-------+
15+
| 1 | 3.50 |
16+
| 2 | 3.65 |
17+
| 3 | 4.00 |
18+
| 4 | 3.85 |
19+
| 5 | 4.00 |
20+
| 6 | 3.65 |
21+
+----+-------+
22+
```
23+
24+
For example, given the above ```Scores``` table, your query should generate the following report (order by highest score):
25+
26+
```
27+
+-------+------+
28+
| Score | Rank |
29+
+-------+------+
30+
| 4.00 | 1 |
31+
| 4.00 | 1 |
32+
| 3.85 | 2 |
33+
| 3.65 | 3 |
34+
| 3.65 | 3 |
35+
| 3.50 | 4 |
36+
+-------+------+
37+
```
38+
39+
## Solution 1
40+
41+
```javascript
42+
# Write your MySQL query statement below
43+
SELECT
44+
Score,
45+
(SELECT count(distinct Score) FROM Scores WHERE Score >= s.Score) as Rank
46+
FROM
47+
Scores s
48+
ORDER BY
49+
Score DESC
50+
```
51+
52+
**Explain:**
53+
54+
nope.
55+
56+
**Complexity:**
57+
58+
* Time complexity :
59+
* Space complexity :
60+
61+
## Solution 2
62+
63+
```javascript
64+
# Write your MySQL query statement below
65+
SELECT
66+
Score,
67+
(SELECT count(*) FROM (SELECT distinct Score as s FROM Scores) tmp WHERE s >= Score) as Rank
68+
FROM
69+
Scores
70+
ORDER BY
71+
Score DESC
72+
```
73+
74+
**Explain:**
75+
76+
nope.
77+
78+
**Complexity:**
79+
80+
* Time complexity :
81+
* Space complexity :

501-600/566. Reshape the Matrix.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 566. Reshape the Matrix
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.
10+
11+
You're given a matrix represented by a two-dimensional array, and two **positive** integers **r** and **c** representing the **row** number and **column** number of the wanted reshaped matrix, respectively.
12+
13+
The reshaped matrix need to be filled with all the elements of the original matrix in the same **row-traversing** order as they were.
14+
15+
If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
16+
17+
**Example 1:**
18+
```
19+
Input:
20+
nums =
21+
[[1,2],
22+
[3,4]]
23+
r = 1, c = 4
24+
Output:
25+
[[1,2,3,4]]
26+
Explanation:The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
27+
```
28+
29+
**Example 2:**
30+
```
31+
Input:
32+
nums =
33+
[[1,2],
34+
[3,4]]
35+
r = 2, c = 4
36+
Output:
37+
[[1,2],
38+
[3,4]]
39+
Explanation:There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
40+
```
41+
42+
**Note:**
43+
44+
- The height and width of the given matrix is in range [1, 100].
45+
- The given r and c are all positive.
46+
47+
## Solution
48+
49+
```javascript
50+
/**
51+
* @param {number[][]} nums
52+
* @param {number} r
53+
* @param {number} c
54+
* @return {number[][]}
55+
*/
56+
var matrixReshape = function(nums, r, c) {
57+
var m = nums.length;
58+
var n = nums[0].length;
59+
60+
if (m * n !== r * c) return nums;
61+
62+
var res = Array(r).fill(0).map(_ => Array(c));
63+
var num = r * c;
64+
65+
for (var i = 0; i < num; i++) {
66+
res[Math.floor(i / c)][i % c] = nums[Math.floor(i / n)][i % n];
67+
}
68+
69+
return res;
70+
};
71+
```
72+
73+
**Explain:**
74+
75+
nope.
76+
77+
**Complexity:**
78+
79+
* Time complexity : O(r*c).
80+
* Space complexity : O(r*c).

0 commit comments

Comments
 (0)