Skip to content

Commit 74ee2f9

Browse files
committed
update
1 parent 3e0c9ad commit 74ee2f9

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Runtime 2 ms Beats 100.00%
2+
// Memory 40.98 MB Beats 100.00%
3+
// .
4+
// T:O(logn), S:O(logn)
5+
//
6+
class Solution {
7+
public int maxProduct(int n) {
8+
List<Integer> digits = new ArrayList<>();
9+
while (n > 0) {
10+
digits.add(n % 10);
11+
n /= 10;
12+
}
13+
digits.sort(Collections.reverseOrder());
14+
15+
return digits.get(0) * digits.get(1);
16+
}
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Runtime 2 ms Beats 100.00%
2+
// Memory 86.73 MB Beats 92.48%
3+
// Recursion & Divide and conquer.
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public static int cur = 0;
8+
9+
public int[][] specialGrid(int n) {
10+
int row = (int) Math.pow(2, n);
11+
int[][] ret = new int[row][row];
12+
cur = 0;
13+
solve(ret, 0, 0, row - 1, row - 1);
14+
15+
return ret;
16+
}
17+
18+
private void solve(int[][] arr, int x1, int y1, int x2, int y2) {
19+
int segmentLen = x2 - x1 + 1;
20+
if (segmentLen == 1) {
21+
return;
22+
}
23+
if (segmentLen == 2) {
24+
arr[x1][y2] = cur++;
25+
arr[x2][y2] = cur++;
26+
arr[x2][y1] = cur++;
27+
arr[x1][y1] = cur++;
28+
} else {
29+
solve(arr, x1, y1 + segmentLen / 2, x1 + segmentLen / 2 - 1, y1 + segmentLen - 1);
30+
solve(arr, x1 + segmentLen / 2, y1 + segmentLen / 2, x1 + segmentLen - 1, y1 + segmentLen - 1);
31+
solve(arr, x1 + segmentLen / 2, y1, x1 + segmentLen - 1, y1 + segmentLen / 2 - 1);
32+
solve(arr, x1, y1, x1 + segmentLen / 2 - 1, y1 + segmentLen / 2 - 1);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)