Skip to content

Commit 7e4cec1

Browse files
committed
finish 74,75,240
1 parent c78eedc commit 7e4cec1

File tree

3 files changed

+325
-0
lines changed

3 files changed

+325
-0
lines changed

001-100/74. Search a 2D Matrix.md

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# 74. Search a 2D Matrix
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Binary Search.
5+
- Similar Questions: Search a 2D Matrix II.
6+
7+
## Problem
8+
9+
Write an efficient algorithm that searches for a value in an **m** x **n** matrix. This matrix has the following properties:
10+
11+
- Integers in each row are sorted from left to right.
12+
- The first integer of each row is greater than the last integer of the previous row.
13+
14+
**Example 1:**
15+
16+
```
17+
Input:
18+
matrix = [
19+
[1, 3, 5, 7],
20+
[10, 11, 16, 20],
21+
[23, 30, 34, 50]
22+
]
23+
target = 3
24+
Output: true
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input:
31+
matrix = [
32+
[1, 3, 5, 7],
33+
[10, 11, 16, 20],
34+
[23, 30, 34, 50]
35+
]
36+
target = 13
37+
Output: false
38+
```
39+
40+
## Solution 1
41+
42+
```javascript
43+
/**
44+
* @param {number[][]} matrix
45+
* @param {number} target
46+
* @return {boolean}
47+
*/
48+
var searchMatrix = function(matrix, target) {
49+
var row = searchRow(matrix, target, 0, matrix.length - 1);
50+
return row === -1 ? false : searchArray(matrix[row], target, 0, matrix[row].length - 1);
51+
};
52+
53+
var searchRow = function (matrix, target, top, bottom) {
54+
if (top > bottom) return -1;
55+
var mid = top + Math.floor((bottom - top) / 2);
56+
var len = matrix[mid].length;
57+
if (len === 0) return -1;
58+
if (matrix[mid][0] <= target && target <= matrix[mid][len - 1]) {
59+
return mid;
60+
} else if (target < matrix[mid][0]) {
61+
return searchRow(matrix, target, top, mid - 1);
62+
} else {
63+
return searchRow(matrix, target, mid + 1, bottom);
64+
}
65+
};
66+
67+
var searchArray = function (arr, target, left, right) {
68+
if (left > right) return false;
69+
var mid = left + Math.floor((right - left) / 2);
70+
if (arr[mid] === target) {
71+
return true;
72+
} else if (arr[mid] > target) {
73+
return searchArray(arr, target, left, mid - 1);
74+
} else {
75+
return searchArray(arr, target, mid + 1, right);
76+
}
77+
};
78+
```
79+
80+
**Explain:**
81+
82+
先找行,再找列。
83+
84+
**Complexity:**
85+
86+
* Time complexity : O(log(m) + log(n)). ```n``````m``` 列。
87+
* Space complexity : O(1).
88+
89+
## Solution 2
90+
91+
```javascript
92+
/**
93+
* @param {number[][]} matrix
94+
* @param {number} target
95+
* @return {boolean}
96+
*/
97+
var searchMatrix = function(matrix, target) {
98+
var n = matrix.length;
99+
var m = (matrix[0] || []).length;
100+
var ll = 0;
101+
var rr = (n * m) - 1;
102+
var mid = 0;
103+
var tmp = 0;
104+
while (ll <= rr) {
105+
mid = ll + Math.floor((rr - ll) / 2);
106+
tmp = matrix[Math.floor(mid / m)][mid % m];
107+
if (tmp === target) {
108+
return true;
109+
} else if (tmp > target) {
110+
rr = mid - 1;
111+
} else {
112+
ll = mid + 1;
113+
}
114+
}
115+
return false;
116+
};
117+
```
118+
119+
**Explain:**
120+
121+
直接找位置。
122+
123+
**Complexity:**
124+
125+
* Time complexity : O(log(m*n)). ```n``````m``` 列。
126+
* Space complexity : O(1).

001-100/75. Sort Colors.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# 75. Sort Colors
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Two Pointers, Sort.
5+
- Similar Questions: Sort List, Wiggle Sort, Wiggle Sort II.
6+
7+
## Problem
8+
9+
Given an array with **n** objects colored red, white or blue, sort them **in-place **so that objects of the same color are adjacent, with the colors in the order red, white and blue.
10+
11+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
12+
13+
**Note:** You are not suppose to use the library's sort function for this problem.
14+
15+
**Example:**
16+
17+
```
18+
Input: [2,0,2,1,1,0]
19+
Output: [0,0,1,1,2,2]
20+
```
21+
22+
**Follow up:**
23+
24+
25+
- A rather straight forward solution is a two-pass algorithm using counting sort. First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
26+
- Could you come up with a one-pass algorithm using only constant space?
27+
28+
## Solution 1
29+
30+
```javascript
31+
/**
32+
* @param {number[]} nums
33+
* @return {void} Do not return anything, modify nums in-place instead.
34+
*/
35+
var sortColors = function(nums) {
36+
var counts = [0, 0, 0];
37+
var len = nums.length;
38+
for (var i = 0; i < len; i++) {
39+
counts[nums[i]]++;
40+
}
41+
for (var j = 0; j < len; j++) {
42+
nums[j] = j < counts[0] ? 0 : (j < counts[0] + counts[1] ? 1 : 2);
43+
}
44+
};
45+
```
46+
47+
**Explain:**
48+
49+
nope.
50+
51+
**Complexity:**
52+
53+
* Time complexity : O(2n).
54+
* Space complexity : O(1).
55+
56+
## Solution 2
57+
58+
```javascript
59+
/**
60+
* @param {number[]} nums
61+
* @return {void} Do not return anything, modify nums in-place instead.
62+
*/
63+
var sortColors = function(nums) {
64+
var m = 0;
65+
var n = 0;
66+
var k = nums.length;
67+
for (var i = 0; i < k; i++) {
68+
if (nums[i] === 0) {
69+
nums[i] = 2;
70+
nums[n++] = 1;
71+
nums[m++] = 0;
72+
} else if (nums[i] === 1) {
73+
nums[i] = 2;
74+
nums[n++] = 1;
75+
} else {
76+
nums[i] = 2;
77+
}
78+
}
79+
};
80+
```
81+
82+
**Explain:**
83+
84+
`[0, m)``0``[m, n)``1``[n, k)``2`
85+
86+
**Complexity:**
87+
88+
* Time complexity : O(n).
89+
* Space complexity : O(1).
90+
91+
## Solution 3
92+
93+
```javascript
94+
/**
95+
* @param {number[]} nums
96+
* @return {void} Do not return anything, modify nums in-place instead.
97+
*/
98+
var sortColors = function(nums) {
99+
var j = 0;
100+
var k = nums.length - 1;
101+
for (var i = 0; i <= k; i++) {
102+
if (nums[i] === 0) {
103+
swap(nums, i, j++);
104+
} else if (nums[i] === 2) {
105+
swap(nums, i--, k--);
106+
}
107+
}
108+
};
109+
110+
var swap = function (arr, a, b) {
111+
var tmp = arr[a];
112+
arr[a] = arr[b];
113+
arr[b] = tmp;
114+
};
115+
```
116+
117+
**Explain:**
118+
119+
`[0, j)``0``[j, k)``1``[k, len)``2`
120+
121+
**Complexity:**
122+
123+
* Time complexity : O(n).
124+
* Space complexity : O(1).

201-300/240. Search a 2D Matrix II.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 240. Search a 2D Matrix II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Binary Search, Divide and Conquer.
5+
- Similar Questions: Search a 2D Matrix.
6+
7+
## Problem
8+
9+
Write an efficient algorithm that searches for a value in an *m* x *n* matrix. This matrix has the following properties:
10+
11+
- Integers in each row are sorted in ascending from left to right.
12+
- Integers in each column are sorted in ascending from top to bottom.
13+
14+
Consider the following matrix:
15+
16+
```
17+
[
18+
[1, 4, 7, 11, 15],
19+
[2, 5, 8, 12, 19],
20+
[3, 6, 9, 16, 22],
21+
[10, 13, 14, 17, 24],
22+
[18, 21, 23, 26, 30]
23+
]
24+
```
25+
26+
**Example 1:**
27+
28+
```
29+
Input: matrix, target = 5
30+
Output: true
31+
```
32+
33+
**Example 2:**
34+
35+
```
36+
Input: matrix, target = 20
37+
Output: false
38+
```
39+
40+
## Solution
41+
42+
```javascript
43+
/**
44+
* @param {number[][]} matrix
45+
* @param {number} target
46+
* @return {boolean}
47+
*/
48+
var searchMatrix = function(matrix, target) {
49+
var n = matrix.length;
50+
var m = (matrix[0] || []).length;
51+
var x = m - 1;
52+
var y = 0;
53+
var tmp = 0;
54+
while (x >= 0 && y < n) {
55+
tmp = matrix[y][x];
56+
if (target === tmp) {
57+
return true;
58+
} else if (target > tmp) {
59+
y++;
60+
} else {
61+
x--;
62+
}
63+
}
64+
return false;
65+
};
66+
```
67+
68+
**Explain:**
69+
70+
nope.
71+
72+
**Complexity:**
73+
74+
* Time complexity : O(n + m). ```n``````m``` 列。
75+
* Space complexity : O(1).

0 commit comments

Comments
 (0)