Skip to content

Commit 5539d92

Browse files
committed
Merge branch 'master' of github.com:OneCodeMonkey/DailyAlgo
� Conflicts: � leetcode_solved/leetcode_0047_Permutations_II.java
2 parents 249792b + 432b241 commit 5539d92

11 files changed

+197
-13
lines changed

leetcode_solved/[editing]leetcode_0047_Permutations_II.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0060_Permutation_Sequence.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0078_Subsets.cpp

Lines changed: 0 additions & 6 deletions
This file was deleted.

leetcode_solved/[editing]leetcode_0090_Subsets_II.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0240_Search_a_2D_Matrix_II.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_1248_Count_Number_of_Nice_Subarrays.cpp

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
1+
// AC: Runtime: 83 ms, faster than 9.78% of Java online submissions for Permutations II.
2+
// Memory Usage: 40.1 MB, less than 24.22% of Java online submissions for Permutations II.
3+
// backtracking
4+
// T:O(2^n), S:O(n * 2^n)
5+
//
16
class Solution {
27
public List<List<Integer>> permuteUnique(int[] nums) {
3-
8+
HashSet<List<Integer>> record = new HashSet<>();
9+
int[] used = new int[nums.length];
10+
backtracking(nums, used, new LinkedList<>(), record);
11+
12+
return new LinkedList<>(record);
13+
}
14+
15+
private void backtracking(int[] nums, int[] used, List<Integer> path, HashSet<List<Integer>> out) {
16+
List<Integer> pathCopy = new LinkedList<>(path);
17+
if (pathCopy.size() >= nums.length) {
18+
out.add(pathCopy);
19+
return;
20+
}
21+
22+
for (int i = 0; i < nums.length; i++) {
23+
if (used[i] == 1) {
24+
continue;
25+
}
26+
pathCopy.add(nums[i]);
27+
used[i] = 1;
28+
backtracking(nums, used, pathCopy, out);
29+
pathCopy.remove(pathCopy.size() - 1);
30+
used[i] = 0;
31+
}
432
}
533
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// AC: Runtime: 1 ms, faster than 98.32% of Java online submissions for Permutation Sequence.
2+
// Memory Usage: 36 MB, less than 99.03% of Java online submissions for Permutation Sequence.
3+
// get the forwarding bit of every position, then construct the string by the bit-list
4+
// T:O(n^2), S:O(n)
5+
//
6+
class Solution {
7+
public String getPermutation(int n, int k) {
8+
k--;
9+
// get the forwarding bit of every position on the base of [1,2,3,4,5,...]
10+
List<Integer> bit = new LinkedList<>();
11+
for (int i = n - 1; i >= 1; i--) {
12+
int factorI = getFactor(i);
13+
if (k >= factorI) {
14+
int temp = k / factorI;
15+
bit.add(temp);
16+
k = k % factorI;
17+
} else {
18+
bit.add(0);
19+
}
20+
}
21+
bit.add(0);
22+
23+
// forwarding by every bit
24+
int[] used = new int[n + 1];
25+
StringBuilder ret = new StringBuilder();
26+
for (int i = 0; i < n; i++) {
27+
if (bit.get(i) != 0) {
28+
int count = 0;
29+
for (int j = 1; j < used.length; j++) {
30+
if (used[j] == 0) {
31+
if (count < bit.get(i)) {
32+
count++;
33+
} else {
34+
ret.append(j);
35+
used[j] = 1;
36+
break;
37+
}
38+
}
39+
}
40+
} else {
41+
for (int j = 1; j < used.length; j++) {
42+
if (used[j] == 0) {
43+
ret.append(j);
44+
used[j] = 1;
45+
break;
46+
}
47+
}
48+
}
49+
}
50+
51+
return ret.toString();
52+
}
53+
54+
private int getFactor(int n) {
55+
int ret = 1;
56+
while (n > 1) {
57+
ret *= n;
58+
n--;
59+
}
60+
61+
return ret;
62+
}
63+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// AC: Runtime: 6 ms, faster than 11.49% of Java online submissions for Subsets II.
2+
// Memory Usage: 39.3 MB, less than 46.18% of Java online submissions for Subsets II.
3+
// backtracking
4+
// T:O(2^n), S:O(n * 2^n)
5+
//
6+
class Solution {
7+
public List<List<Integer>> subsetsWithDup(int[] nums) {
8+
HashSet<List<Integer>> record = new HashSet<>();
9+
record.add(new LinkedList<>());
10+
// int[] used = new int[nums.length];
11+
backtracking(nums, new LinkedList<>(), record, 0);
12+
13+
return new LinkedList<>(record);
14+
}
15+
16+
private void backtracking(int[] nums, List<Integer> path, HashSet<List<Integer>> out, int startIndex) {
17+
List<Integer> pathCopy = new LinkedList<>(path);
18+
// combination does not varify by order, so first sort to remove dup.
19+
Collections.sort(pathCopy);
20+
out.add(pathCopy);
21+
22+
for (int i = startIndex; i < nums.length; i++) {
23+
pathCopy.add(nums[i]);
24+
backtracking(nums, pathCopy, out, i + 1);
25+
pathCopy.remove(pathCopy.size() - 1);
26+
}
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Solution 1: binary search of every row(or column)
2+
// AC: Runtime: 5 ms, faster than 49.82% of Java online submissions for Search a 2D Matrix II.
3+
// Memory Usage: 45 MB, less than 23.65% of Java online submissions for Search a 2D Matrix II.
4+
// binary search, but not use the row and column both ordered condition, so there are better solution
5+
// T:O(row * log(col)), S:O(log(col))
6+
//
7+
class Solution {
8+
public boolean searchMatrix(int[][] matrix, int target) {
9+
int row = matrix.length, col = matrix[0].length;
10+
11+
for (int[] ints : matrix) {
12+
if (Arrays.binarySearch(ints, target) >= 0) {
13+
return true;
14+
}
15+
}
16+
17+
return false;
18+
}
19+
}
20+
21+
// Solution 2: search from top-right, move left or down to search
22+
// AC: Runtime: 4 ms, faster than 100.00% of Java online submissions for Search a 2D Matrix II.
23+
// Memory Usage: 44.5 MB, less than 78.90% of Java online submissions for Search a 2D Matrix II.
24+
// two-direction move, see the annotation
25+
// T:O(row + col), S:O(1)
26+
//
27+
class Solution {
28+
public boolean searchMatrix(int[][] matrix, int target) {
29+
int row = matrix.length, col = matrix[0].length;
30+
int rowPos = 0, colPos = col - 1;
31+
while (matrix[rowPos][colPos] != target) {
32+
// left move for one column
33+
if (matrix[rowPos][colPos] > target) {
34+
colPos--;
35+
if (colPos < 0) {
36+
return false;
37+
}
38+
}
39+
// down move for one row
40+
else {
41+
rowPos++;
42+
if (rowPos >= row) {
43+
return false;
44+
}
45+
}
46+
}
47+
48+
// cannot remove, check the final position is target or not.
49+
return matrix[rowPos][colPos] == target;
50+
}
51+
}

0 commit comments

Comments
 (0)