![京东笔试全ac太快乐了](https://imgs.xnip.cn/cj/jy/30/d6586ead-841b-4b6e-ab8b-a22044b69bbb.jpg)
京东客户端笔试算法全ac 感觉和后端的漂亮串差了2个leetcode hard难度啊
第一题 大小写切换直接三行api
第二题 二维矩阵直接s写法
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
第三题 长城 直接分奇数城墙和偶数城墙两个队列去看变成一个高度各需要多少次 再排除一下奇偶城墙高度一样的边界情况
import java.util.*;
public class 嘤嘤的长城 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n == 1) {
System.out.println(0);
} else {
Map<Integer, Integer> map1 = new TreeMap<>();
Map<Integer, Integer> map2 = new TreeMap<>();
int wall1 = 0, wall2 = 0;
for (int i = 0; i < n; i++) {
int wall = scanner.nextInt();
if (i % 2 == 0) {
if (map1.containsKey(wall)) {
map1.put(wall, map1.get(wall) + 1);
} else {
map1.put(wall, 1);
}
wall1++;
} else {
if (map2.containsKey(wall)) {
map2.put(wall, map2.get(wall) + 1);
} else {
map2.put(wall, 1);
}
wall2++;
}
}
List<Map.Entry<Integer, Integer>> entries1 = new ArrayList<>(map1.entrySet());
List<Map.Entry<Integer, Integer>> entries2 = new ArrayList<>(map2.entrySet());
entries1.sort(((o1, o2) -> o2.getValue() - o1.getValue()));
entries2.sort(((o1, o2) -> o2.getValue() - o1.getValue()));
if (entries1.get(0).getKey() - entries2.get(0).getKey() != 0) {
System.out.println(wall1 - entries1.get(0).getValue() + wall2 - entries2.get(0).getValue());
} else {
int first1, first2, next1 = 0, next2 = 0;
first1 = wall1 - entries1.get(0).getValue();
first2 = wall2 - entries2.get(0).getValue();
if (entries1.size() > 1) {
next1 = wall1 - entries1.get(1).getValue();
next2 = wall2 - entries2.get(1).getValue();
}
System.out.println(Math.min(first1 + next2, first2 + next1));
}
}
}
}
看了一会后端的漂亮串 看了时间限制2s应该是有什么公式之类的吧 想了一会没有思路 有大佬讲讲嘛
#校招##京东##京东笔试#