2019-06-16 14:35:52
- 1089. Duplicate Zeros - Easy
问题描述:
问题求解:
很显然的可以使用O(n), O(n)的解法。主要问题在于如何在O(1)空间复杂度完成求解。
答案就是通过两次遍历,第一次求出复制后的数组的长度,第二次遍历填写数组。
public void duplicateZeros(int[] arr) {
if (arr.length == 1) return;
final int n = arr.length;
int newLen = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) newLen++;
newLen++;
}
for (int i = n - 1; i >= 0; i--) {
if (--newLen < n) arr[newLen] = arr[i];
if (arr[i] == 0 && --newLen < n) arr[newLen] = 0;
}
}
- 1090. Largest Values From Labels - Medium
问题描述:
问题求解:
本题要求的是挑选至多num_wanted个数的物品,并且每个物品不能超过limit的数目。
显然的是,可以根据价值来贪心的选取,问题就是如何不超过limit的数目,也就是将value和label建立联系。这个建立联系的方式是需要经验的,这里采用的是组pair的方式建立联系,事实上,很多时候这种建立pair的方式是最行之有效的,可以特别关注一下。
public int largestValsFromLabels(int[] values, int[] labels, int num_wanted, int use_limit) {
List<int[]> pairs = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
final int n = values.length;
for (int i = 0; i < n; i++) {
if (!map.containsKey(labels[i])) map.put(labels[i], 0);
pairs.add(new int[]{values[i], labels[i]});
}
PriorityQueue<int[]> pq = new PriorityQueue<>((int[] o1, int[] o2) -> o2[0] - o1[0]);
pq.addAll(pairs);
int res = 0;
for (int i = 0; i < num_wanted && !pq.isEmpty();) {
int[] curPair = pq.poll();
if (map.get(curPair[1]) < use_limit) {
res += curPair[0];
map.put(curPair[1], map.get(curPair[1]) + 1);
i++;
}
}
return res;
}
- 1091. Shortest Path in Binary Matrix - Medium
问题描述:
问题求解:
裸的BFS。
public int shortestPathBinaryMatrix(int[][] grid) {
if (grid.length == 0 || grid[0].length == 0) return -1;
final int n = grid.length;
final int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
if (grid[0][0] != 0 || grid[n - 1][n - 1] != 0) return -1;
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{0, 0});
grid[0][0] = 2;
int step = 0;
while (!q.isEmpty()) {
int size = q.size();
step++;
for (int i = 0; i < size; i++) {
int[] curNode = q.poll();
for (int[] dir : dirs) {
int x = curNode[0] + dir[0];
int y = curNode[1] + dir[1];
if (x < 0 || x >= n || y < 0 || y >= n || grid[x][y] != 0) continue;
if (x == n - 1 && y == n - 1) return step + 1;
q.add(new int[]{x, y});
grid[x][y] = 2;
}
}
}
return -1;
}
- 1092. Shortest Common Supersequence - Hard
问题描述:
问题求解:
问题需要的是最短的supersequence,那么就是需要将两个字符串拼接后能够最大限度的去除重复的字符,因此本题就变为了求最长公共子序列的问题,并且需要构造出最长公共子序列。
这里直接暴力的进行构造。
public String shortestCommonSupersequence(String s1, String s2) {
if (s1.length() == 0) return s2;
if (s2.length() == 0) return s1;
int m = s1.length();
int n = s2.length();
String common = lcs(s1, s2);
System.out.println(common);
StringBuffer sb = new StringBuffer();
int i = 0;
int j = 0;
for (char c : common.toCharArray()) {
while (i < m && s1.charAt(i) != c) {
sb.append(s1.charAt(i));
i++;
}
while (j < n && s2.charAt(j) != c) {
sb.append(s2.charAt(j));
j++;
}
sb.append(c);
i++;
j++;
}
while (i < m) sb.append(s1.charAt(i++));
while (j < n) sb.append(s2.charAt(j++));
return sb.toString();
}
private String lcs(String s1, String s2) {
int m = s1.length();
int n = s2.length();
String[][] dp = new String[m + 1][n + 1];
for (int j = 0; j <= n; j++) dp[0][j] = "";
for (int i = 0; i <= m; i++) dp[i][0] = "";
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1] + s1.charAt(i - 1);
else if (dp[i - 1][j].length() > dp[i][j - 1].length()) dp[i][j] = dp[i - 1][j];
else dp[i][j] = dp[i][j - 1];
}
}
return dp[m][n];
}