文章目录
美团 3.25
1 模拟栈
给出栈的输入序列和弹出序列,判断是否合法
- 直接模拟
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
// 模拟栈
int n = pushed.length;
int inPtr = 0;
int outPtr = 0;
Deque<Integer> stack = new LinkedList<>();
while (inPtr < n && outPtr < n) {
if (stack.size() == 0) {
stack.offerLast(pushed[inPtr]);
inPtr++;
} else if (stack.peekLast() == popped[outPtr]) {
stack.pollLast();
outPtr++;
} else {
stack.offerLast(pushed[inPtr]);
inPtr++;
}
}
while (outPtr < n) {
if (stack.pollLast() == popped[outPtr]) {
outPtr++;
} else {
return false;
}
}
return true;
}
}
2 最大美味值 I
吃第i个糖果就不能吃第i-1, i-2 , i+1 ,i+2个,每个糖果有一个美味值,求最大美味值
- 打家劫舍 I 的变形
dp[i]
代表处理到糖果 i (无论是否吃第 i 个)的时候的最大值- 需要手动设置三个初始状态
dp[0]=a[0], dp[1] = max(dp[0], a[1]), dp[2] = max(dp[0], dp[1], a[2])
- 状态转移
dp[i] = max(dp[i - 3] + a[i], dp[i - 1])
(因为dp
是递增的,所以无序比较dp[i - 2]
)
3 每个背包最多装巧克力个数
给定一些巧克力,质量已知,给一些背包容量,输出每个背包最多可装巧克力数量
- 直观采用贪心,从重量小的巧克力开始装,会超时
- 将巧克力按照种类递增排序,再计算前缀和,拿每个背包在前缀和数组中二分查找
4 类 Redis 实现
输入字符串,形如“HOME=\bash\ssh;LONGNAME=xiaomei;”,构建键值对;输入查询字符串(键),输出其值,若不存在,输出EMPTY,若存在重复Key,则输出相同Key的最后一组
- 结合 Map 的字符串处理
5 最大美味值 II
不能连续两天吃糖果,但有K次机会打破规则
- 类比股票的最大利润,多增加一个状态记录打破规则的次数
dp[i][j] = max(dp[i - 1][j - 1] + a[i], dp[i - 2][j] + a[i], dp[i - 1][j])
美团 4.1
1 计算练习 18/100
class Main01 {
static class OpTurple {
int idx;
String operation;
public OpTurple(int idx, String operation) {
this.idx = idx;
this.operation = operation;
}
}
public List<Double> calculate(int[] nums, List<OpTurple> opTurpleList) {
// 计算总和
int sum = Arrays.stream(nums).sum();
List<Double> result = new ArrayList<>(opTurpleList.size());
for (OpTurple opTurple : opTurpleList) {
if ("-".equals(opTurple.operation)) {
result.add(sum - 2.0 * nums[opTurple.idx]);
} else if ("*".equals(opTurple.operation)) {
result.add((double) (sum - nums[opTurple.idx] - nums[opTurple.idx - 1] + nums[opTurple.idx] * nums[opTurple.idx - 1]));
} else {
result.add((double) (sum - nums[opTurple.idx] - nums[opTurple.idx - 1] + (double) nums[opTurple.idx - 1] / nums[opTurple.idx]));
}
}
return result;
}
}
2 最小差距和 100/100
class Main02 {
public long minDiff(long[] nums) {
Arrays.sort(nums);
long result = 0L;
for (int i = 1; i < nums.length; i++) {
result += nums[i] - nums[i - 1];
}
return result;
}
}
3 收藏夹 81/100
class Main03{
List<Long> query(int n, int opNum, int[] opType, int[] x, int[] y) {
List<Long> result = new LinkedList<>();
Map<Integer, Long> fold2Score = new TreeMap<>(); // 存放收藏夹到分数的映射
for (int i = 1; i <= n; i++) {
fold2Score.put(i, 0L);
}
for (int i = 0; i < opNum; i++) {
if (opType[i] == 0) {
fold2Score.put(x[i], (long) y[i]);
} else {
// 计算x[i]~y[i]范围内的和
Long currSum = 0L;
for (int j = x[i]; j <= y[i]; j++) {
currSum += fold2Score.get(j);
}
result.add(currSum);
}
}
return result;
}
}
4 魔法水杯 100/100
class Main04{
public long[] minCost(int n, int[] cupCapacity, int[] cupInitial, int[] cupPerMLCost, int times, int[] targetCup) {
int cupMaxIndex = Arrays.stream(targetCup).max().getAsInt();
// mlNeed[i]代表从0到杯子i需要的总毫升数
long[] mlNeed = new long[n];
mlNeed[0] = cupCapacity[0] - cupInitial[0];
// dp[i]代表倒满杯子i的最小代价
long[] dp = new long[n];
dp[0] = (long) (cupCapacity[0] - cupInitial[0]) * cupPerMLCost[0];
for (int i = 1; i <= cupMaxIndex - 1; i++) {
long totalML = mlNeed[i - 1] + cupCapacity[i] - cupInitial[i];
mlNeed[i] = totalML;
long minCost = Long.MAX_VALUE;
for (int j = 0; j <= i; j++) {
minCost = Math.min(minCost, totalML * cupPerMLCost[j]);
totalML -= cupCapacity[j] - cupInitial[j];
}
// 最小代价
dp[i] = minCost;
}
// 存放结果
long[] result = new long[times];
for (int i = 0; i < times; i++) {
result[i] = dp[targetCup[i] - 1];
}
return result;
}
}
5 计算树节点价值 100/100
class Main05 {
Map<TreeNode, Long> node2value = new HashMap<>();
public long getValue(int n, int[] fathers, int[] colors) {
List<TreeNode> treeNodeList = new LinkedList<>();
for (int i = 1; i <= n; i++) {
TreeNode curr = new TreeNode(i, colors[i]);
treeNodeList.add(curr);
// 连接父节点
if (i > 1) {
TreeNode fatherNode = treeNodeList.get(fathers[i] - 1);
if (fatherNode.left == null) {
fatherNode.left = curr;
} else {
fatherNode.right = curr;
}
}
}
TreeNode root = treeNodeList.get(0);
return calculate(root);
}
private long calculate(TreeNode node) {
if (node == null) {
return 0;
}
if (node2value.containsKey(node)) {
return node2value.get(node);
}
if (node.left == null && node.right == null) {
node2value.put(node, 1L);
return 1;
}
Long l = calculate(node.left);
Long r = calculate(node.right);
if (node.color == 1) {
node2value.put(node, l + r);
} else {
node2value.put(node, l ^ r);
}
return node2value.get(node);
}
static class TreeNode {
int val;
int color;
TreeNode left;
TreeNode right;
public TreeNode(int val, int color) {
this.val = val;
this.color = color;
}
}
}
亚马逊 4.6
1 最少按键次数 100/100
给定字符串,将其分配到1~9中,每个数字最多代表三个字母,求打出输入字符串按键的最小次数
- 统计字母频率,再贪心
2 无重复元素窗口的最大和(固定窗口容量)100/100
给出整数序列和窗口长度,求无重复元素的窗口的最大和
- 滑动窗口法,做法类似无重复字母的最大子数组