Skip to content

Commit ee9fe6b

Browse files
committed
feat: leetcode_week_254
1 parent 6006ac3 commit ee9fe6b

4 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Number of Strings That Appear as Substrings in Word.
2+
// Memory Usage: 39.1 MB, less than 75.00% of Java online submissions for Number of Strings That Appear as Substrings in Word.
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int numOfStrings(String[] patterns, String word) {
8+
int ret = 0;
9+
for (String str: patterns) {
10+
if (word.contains(str)) {
11+
ret++;
12+
}
13+
}
14+
15+
return ret;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// AC:Runtime: 33 ms, faster than 21.43% of Java online submissions for Array With Elements Not Equal to Average of Neighbors.
2+
// Memory Usage: 63.5 MB, less than 21.43% of Java online submissions for Array With Elements Not Equal to Average of Neighbors.
3+
// .
4+
// T:O(n) ~ O(n^2), S:O(n)
5+
//
6+
class Solution {
7+
public int[] rearrangeArray(int[] nums) {
8+
int size = nums.length;
9+
List<Integer> temp = new ArrayList<>(size);
10+
for (int i: nums) {
11+
temp.add(i);
12+
}
13+
while (!check(temp)) {
14+
Collections.shuffle(temp);
15+
}
16+
int[] ret = new int[size];
17+
for (int i = 0; i < temp.size(); i++) {
18+
ret[i] = temp.get(i);
19+
}
20+
21+
return ret;
22+
}
23+
24+
private boolean check(List<Integer> nums) {
25+
for (int i = 1; i < nums.size() - 1; i++) {
26+
if (nums.get(i) * 2 == nums.get(i - 1) + nums.get(i + 1)) {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Minimum Non-Zero Product of the Array Elements.
2+
// Memory Usage: 35.4 MB, less than 100.00% of Java online submissions for Minimum Non-Zero Product of the Array Elements.
3+
// 思路:观察规律可以猜测,最小乘积时,除了 2^p - 1 外,剩下的最大值和最小值两两配对,形成 2^p - 2, 共 (2^p - 2) / 2 对。再计算 pow(x, y), 递归计算,复杂度降为 O(n), n 为原指数
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int minNonZeroProduct(int p) {
8+
long mod = 1000_000_000 + 7, divider = (1L << p) - 1;
9+
long powResult = myPow(divider - 1, (divider - 1) / 2, mod);
10+
int ret = (int) (((divider % mod) * powResult) % mod);
11+
return ret;
12+
}
13+
14+
private long myPow(long x, long y, long mod) {
15+
if (y == 0) {
16+
return 1;
17+
}
18+
long temp = myPow(x, y / 2, mod);
19+
long p = (temp * temp) % mod;
20+
21+
return y % 2 == 1 ? (p * (x % mod)) % mod : p;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// AC: Runtime: 81 ms, faster than 33.33% of Java online submissions for Last Day Where You Can Still Cross.
2+
// Memory Usage: 48 MB, less than 100.00% of Java online submissions for Last Day Where You Can Still Cross.
3+
// binary search & bfs search of graph
4+
// T:(nlogn), S:O(n), n = row * col
5+
//
6+
class Solution {
7+
public int latestDayToCross(int row, int col, int[][] cells) {
8+
int left = 0, right = row * col - 1, ret = -1;
9+
while (left <= right) {
10+
int mid = (left + right) / 2;
11+
if (check(cells, row, col, mid)) {
12+
ret = mid;
13+
left = mid + 1;
14+
} else {
15+
right = mid - 1;
16+
}
17+
}
18+
19+
return ret;
20+
}
21+
22+
private boolean check(int[][] cells, int row, int col, int day) {
23+
int[][] grid = new int[row][col];
24+
for (int i = 0; i < day; i++) {
25+
grid[cells[i][0] - 1][cells[i][1] - 1] = 1;
26+
}
27+
Queue<int[]> bfs = new ArrayDeque<>();
28+
for (int i = 0; i < col; i++) {
29+
if (grid[0][i] == 0) {
30+
bfs.offer(new int[]{0, i});
31+
grid[0][i] = 1;
32+
}
33+
}
34+
int[] dir = new int[]{0, 1, 0, -1, 0};
35+
while (!bfs.isEmpty()) {
36+
int[] curPoint = bfs.poll();
37+
int curRow = curPoint[0], curCol = curPoint[1];
38+
if (curRow == row - 1) {
39+
return true;
40+
}
41+
for (int i = 0; i < 4; i++) {
42+
int tempRow = curRow + dir[i], tempCol = curCol + dir[i + 1];
43+
if (tempRow < 0 || tempRow >= row || tempCol < 0 || tempCol >= col || grid[tempRow][tempCol] == 1) {
44+
continue;
45+
}
46+
grid[tempRow][tempCol] = 1;
47+
bfs.offer(new int[]{tempRow, tempCol});
48+
}
49+
}
50+
51+
return false;
52+
}
53+
}

0 commit comments

Comments
 (0)