Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 75ec445

Browse files
20200927
1 parent cd2f5b6 commit 75ec445

22 files changed

+819
-0
lines changed

‎Java/1036.escape-a-large-maze.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode id=1036 lang=java
3+
*
4+
* [1036] Escape a Large Maze
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
最多200个被block, 如果走的曼哈顿距离>200说明可以
11+
如果target四周都被block,也不行 所以需要双向dfs
12+
*/
13+
int[] dirs = new int[]{1, 0, -1, 0, 1};
14+
public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {
15+
Set<String> blockedSet = new HashSet<>();
16+
for (int[] block : blocked) {
17+
blockedSet.add(block[0] + "->" + block[1]);
18+
}
19+
return dfs(source, target, source, blockedSet, new HashSet<>()) && dfs(target, source, target, blockedSet, new HashSet<>());
20+
}
21+
22+
private boolean dfs(int[] source, int[] target, int[] cur, Set<String> blocked, Set<String> visited) {
23+
if (cur[0] == target[0] && cur[1] == target[1]) {
24+
return true;
25+
}
26+
if (Math.abs(source[0] - cur[0]) + Math.abs(source[1] - cur[1]) > 200) {
27+
return true;
28+
}
29+
for (int i = 0; i < 4; ++i) {
30+
int nextR = cur[0] + dirs[i];
31+
int nextC = cur[1] + dirs[i + 1];
32+
if (nextR >= 0 && nextR < 1e6 && nextC >= 0 && nextC < 1e6 && visited.add(nextR + "->" + nextC)
33+
&& !blocked.contains(nextR + "->" + nextC)) {
34+
if (dfs(source, target, new int[]{nextR, nextC}, blocked, visited)) {
35+
return true;
36+
}
37+
}
38+
}
39+
return false;
40+
}
41+
}
42+
// @lc code=end
43+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @lc app=leetcode id=1162 lang=java
3+
*
4+
* [1162] As Far from Land as Possible
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
判断多久1能覆盖所有0
11+
time: O(mn)
12+
space: O(mn)
13+
*/
14+
public int maxDistance(int[][] grid) {
15+
if (grid == null || grid.length == 0) return 0;
16+
int m = grid.length, n = grid[0].length;
17+
Queue<int[]> q = new LinkedList<>();
18+
for (int i = 0; i < m; ++i) {
19+
for (int j = 0; j < n; ++j) {
20+
if (grid[i][j] == 1) {
21+
q.offer(new int[]{i, j});
22+
}
23+
}
24+
}
25+
int[] dirs = new int[]{1, 0, -1, 0, 1};
26+
int step = 0;
27+
while (!q.isEmpty()) {
28+
int size = q.size();
29+
while (size-- > 0) {
30+
int[] cur = q.poll();
31+
for (int i = 0; i < 4; ++i) {
32+
int nextR = cur[0] + dirs[i];
33+
int nextC = cur[1] + dirs[i + 1];
34+
if (nextR >= 0 && nextR < m && nextC >= 0 && nextC < n && grid[nextR][nextC] == 0) {
35+
grid[nextR][nextC] = 1;
36+
q.offer(new int[]{nextR, nextC});
37+
}
38+
}
39+
}
40+
step++;
41+
}
42+
return step - 1 == 0 ? -1 : step - 1;
43+
}
44+
}
45+
// @lc code=end
46+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @lc app=leetcode id=1218 lang=java
3+
*
4+
* [1218] Longest Arithmetic Subsequence of Given Difference
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
记录以某个位置为结尾的subsequence length
11+
利用cur - diff的map key来查找count并更新
12+
time: O(n)
13+
space: O(n)
14+
*/
15+
public int longestSubsequence(int[] arr, int difference) {
16+
Map<Integer, Integer> map = new HashMap<>();
17+
int res = 0;
18+
for (int i = 0; i < arr.length; ++i) {
19+
map.put(arr[i], map.getOrDefault(arr[i] - difference, 0) + 1);
20+
res = Math.max(res, map.get(arr[i]));
21+
}
22+
return res;
23+
}
24+
}
25+
// @lc code=end
26+

‎Java/1223.dice-roll-simulation.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @lc app=leetcode id=1223 lang=java
3+
*
4+
* [1223] Dice Roll Simulation
5+
*/
6+
7+
// @lc code=start
8+
cclass Solution {
9+
/*
10+
dfs with memo
11+
dp[i][j][k] 第i次,最后一次扔出来的是j,连续了k次
12+
time: O(n * 6 * 6 * 15)
13+
space: O(n * 6 * 15)
14+
*/
15+
int[][][] dp = new int[5001][7][16];
16+
int kMod = (int)1e9 + 7;
17+
public int dieSimulator(int n, int[] rollMax) {
18+
if (rollMax == null || rollMax.length == 0 || n == 0) return 0;
19+
return dfs(rollMax, n, -1, 0);
20+
}
21+
22+
private int dfs(int[] rollMax, int n, int last, int count) {
23+
if (n == 0) return 1;
24+
if (last >= 0 && dp[n][last][count] > 0) return dp[n][last][count];
25+
int res = 0;
26+
for (int i = 0; i < 6; ++i) {
27+
if (i == last && count == rollMax[i]) continue;
28+
res = (res + dfs(rollMax, n - 1, i, i == last ? count + 1 : 1)) % kMod;
29+
}
30+
if (last >= 0) dp[n][last][count] = res;
31+
return res;
32+
}
33+
}
34+
// @lc code=end
35+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @lc app=leetcode id=1352 lang=java
3+
*
4+
* [1352] Product of the Last K Numbers
5+
*/
6+
7+
// @lc code=start
8+
class ProductOfNumbers {
9+
/*
10+
prefix array
11+
如果有0,则将之前的清空
12+
如果k > size,说明这部分有0
13+
*/
14+
List<Integer> prefixProduct;
15+
public ProductOfNumbers() {
16+
prefixProduct = new ArrayList<>();
17+
prefixProduct.add(1);
18+
}
19+
20+
public void add(int num) {
21+
if (num > 0) {
22+
prefixProduct.add(prefixProduct.get(prefixProduct.size() - 1) * num);
23+
} else {
24+
prefixProduct = new ArrayList<>();
25+
prefixProduct.add(1);
26+
}
27+
}
28+
29+
public int getProduct(int k) {
30+
int size = prefixProduct.size();
31+
return size > k ? prefixProduct.get(size - 1) / prefixProduct.get(size - k - 1) : 0;
32+
}
33+
}
34+
35+
/**
36+
* Your ProductOfNumbers object will be instantiated and called as such:
37+
* ProductOfNumbers obj = new ProductOfNumbers();
38+
* obj.add(num);
39+
* int param_2 = obj.getProduct(k);
40+
*/
41+
42+
/**
43+
* Your ProductOfNumbers object will be instantiated and called as such:
44+
* ProductOfNumbers obj = new ProductOfNumbers();
45+
* obj.add(num);
46+
* int param_2 = obj.getProduct(k);
47+
*/
48+
// @lc code=end
49+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode id=1423 lang=java
3+
*
4+
* [1423] Maximum Points You Can Obtain from Cards
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
find min subarray with length n - k
11+
time: O(n)
12+
space: O(1)
13+
*/
14+
public int maxScore(int[] cardPoints, int k) {
15+
if (cardPoints == null || cardPoints.length == 0) return 0;
16+
int n = cardPoints.length;
17+
int total = 0;
18+
int cur = 0;
19+
int min = Integer.MAX_VALUE;
20+
for (int l = 0, r = 0; r < n; ++r) {
21+
total += cardPoints[r];
22+
cur += cardPoints[r];
23+
if (r - l + 1 == (n - k)) {
24+
min = Math.min(min, cur);
25+
cur -= cardPoints[l++];
26+
}
27+
}
28+
return min == Integer.MAX_VALUE ? total : total - min;
29+
}
30+
}
31+
// @lc code=end
32+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @lc app=leetcode id=1477 lang=java
3+
*
4+
* [1477] Find Two Non-overlapping Sub-arrays Each With Target Sum
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
建立prefix sum的map
11+
同时找sum - target 和sum + target
12+
time: O(n)
13+
space: O(n)
14+
*/
15+
public int minSumOfLengths(int[] arr, int target) {
16+
if (arr == null || arr.length == 0) return -1;
17+
Map<Integer, Integer> map = new HashMap<>();
18+
int sum = 0;
19+
map.put(0, -1);
20+
for (int i = 0; i < arr.length; ++i) {
21+
sum += arr[i];
22+
map.put(sum, i);
23+
}
24+
sum = 0;
25+
int leftMin = Integer.MAX_VALUE, res = Integer.MAX_VALUE;
26+
for (int i = 0; i < arr.length; ++i) {
27+
sum += arr[i];
28+
if (map.containsKey(sum - target)) {
29+
leftMin = Math.min(leftMin, i - map.get(sum - target));
30+
}
31+
if (map.containsKey(sum + target) && leftMin < Integer.MAX_VALUE) {
32+
res = Math.min(res, map.get(sum + target) - i + leftMin);
33+
}
34+
}
35+
return res == Integer.MAX_VALUE ? -1: res;
36+
}
37+
}
38+
// @lc code=end
39+

‎Java/397.integer-replacement.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode id=397 lang=java
3+
*
4+
* [397] Integer Replacement
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
末位为0,除以2
11+
末位为11,加一, 01则减1
12+
time: O(bitCount)
13+
space: O(1)
14+
*/
15+
public int integerReplacement(int n) {
16+
int count = 0;
17+
while (n != 1) {
18+
if ((n & 1) == 0) {
19+
n >>>= 1;
20+
} else if ((((n >>> 1) & 1) == 0) || n == 3) {
21+
n--;
22+
} else {
23+
n++;
24+
}
25+
count++;
26+
}
27+
return count;
28+
}
29+
}
30+
// @lc code=end
31+

‎Java/415.add-strings.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode id=415 lang=java
3+
*
4+
* [415] Add Strings
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public String addStrings(String num1, String num2) {
10+
StringBuilder sb = new StringBuilder();
11+
int sum = 0;
12+
int i = num1.length() - 1, j = num2.length() - 1;
13+
while (i >= 0 || j >= 0) {
14+
if (i >= 0) {
15+
sum += num1.charAt(i) - '0';
16+
}
17+
if (j >= 0) {
18+
sum += num2.charAt(j) - '0';
19+
}
20+
--i;
21+
--j;
22+
sb.append(sum % 10);
23+
sum /= 10;
24+
}
25+
if (sum > 0) sb.append(sum);
26+
return sb.reverse().toString();
27+
}
28+
}
29+
// @lc code=end
30+

‎Java/418.sentence-screen-fitting.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode id=418 lang=java
3+
*
4+
* [418] Sentence Screen Fitting
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
一行一行的加, 如果碰到当前位置不满足末行条件,将count退格代表一部分字符不能加在这一行得加在下一行
11+
a _ b c d _ e _
12+
0 1 0 -1 -2 1 0 1
13+
第一次加count = 6, 下一行开始的是e, 所以之前所有的都可以放进这一行(如果是空格就补一个空格)
14+
第二次count = 12, 下一行开始的是d, 所以要退两格用空格补充,下一行开头的是bcd count = 12 - 2 = 10
15+
第三次count = 16, 下一行开始是a, 满足
16+
time: O(len + cols)
17+
sapce: O(len)
18+
*/
19+
public int wordsTyping(String[] sentence, int rows, int cols) {
20+
String s = String.join(" ", sentence) + " ";
21+
int len = s.length(), count = 0;
22+
int[] map = new int[len];
23+
for (int i = 1; i < len; ++i) {
24+
map[i] = s.charAt(i) == ' ' ? 1 : map[i-1] - 1;
25+
}
26+
for (int i = 0; i < rows; ++i) {
27+
count += cols;
28+
count += map[count % len];
29+
}
30+
return count / len;
31+
}
32+
}
33+
// @lc code=end
34+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /