0422网易雷火笔试
Q1
public class Solution {
public int putGems (int[] price, int k) {
// write code here
int n = price.length;
if (n <= k) return 0;
int[] values = new int[n - 1]; // // 枚举n-1个分裂点,每个分裂点包含其左右两个值的和
for (int i = 1; i < n; i++) values[i - 1] = price[i] + price[i - 1];
Arrays.sort(values);
int ans1 = 0, ans2 = 0;
for (int i = 0; i < k - 1; i++) ans1 += values[i];
for (int i = n - 2; i > n - 1 - k; i--) ans2 += values[i];
return ans2 - ans1;
}
}
Q2
public class Solution {
public int getEstTime (int[][] map, int a_x, int a_y, int b_x, int b_y) {
// write code here
int m = map.length, n = map[0].length;
boolean[][] vis = new boolean[m][n];
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
Queue<int[]> queue = new ArrayDeque<>();
queue.offer(new int[]{a_x, a_y});
vis[a_x][a_y] = true;
int ans = 0;
while (!queue.isEmpty()) {
for (int i = queue.size() - 1; i >= 0; i--) {
int[] curr = queue.poll();
if (curr[0] == b_x && curr[1] == b_y) return (ans & 1) == 0 ? (ans / 2) : (ans / 2 + 1);
for (int[] dir : dirs) {
int x = curr[0] + dir[0], y = curr[1] + dir[1];
if (x < 0 || x >= m || y < 0 || y >= n || map[x][y] == 0 || vis[x][y]) continue;
queue.offer(new int[]{x, y});
vis[x][y] = true;
}
}
ans++;
}
return -1;
}
}
#网易##网易雷火##网易雷火笔试##网易笔试#
查看10道真题和解析