题
思
我发现leetcode有一个模拟面试的功能,随机三道题(一般会有一道困难,一或两道中等),90分钟。
那以后就做这个吧。
第一题
[中等]
我看了一下其实这题以前做过,以前做的比较暴力,今天的代码效率会好一些。
第二题
[中等]
脑瘫题
第三题
[困难]
没做出来,难受。
题解我看了一下,写的乱七八糟的,百度了一下这是一个很经典的问题。
题目翻译一下:给你一个数n,给你一组区间,让你用这组区间中的最少区间数覆盖0~n。
我先按照左区间升序,因为他反正要覆盖所有,我们就把边界一步一步往右推就行了。
一开始边界在0处。
从左到右遍历这组排好序的区间,每次找出来的左区间都必须小于等于边界。
在这组区间中(左区间都必须小于等于边界),取出有最大的右区间的那个,用他就是最合适的。
用最大右区间更新边界,计数+1。
如果一次下来边界没有更新,那就说明不能再往前推了,直接return -1
.
代码
第一题
public class Solution {
public int numMatchingSubseq(String s, String[] words) {
Map<Character, List<Integer>> map = new HashMap<Character, List<Integer>>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
map.get(c).add(i);
} else {
List<Integer> list = new ArrayList<Integer>();
list.add(i);
map.put(c, list);
}
}
int count = 0;
for (String word : words) {
boolean hasWord = true;
int idx = -1;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (!map.containsKey(c)) {
hasWord = false;
break;
} else {
boolean idxChange = false;
for (Integer temp : map.get(c)) {
if (temp > idx) {
idx = temp;
idxChange = true;
break;
}
}
if (!idxChange) {
hasWord = false;
break;
}
}
}
if (hasWord) {
count++;
}
}
return count;
}
}
第二题
class Solution {
public int findPoisonedDuration(int[] timeSeries, int duration) {
int time = 0;
for (int i = 0; i < timeSeries.length - 1; i++) {
if (timeSeries[i] + duration >= timeSeries[i + 1]) {
time += timeSeries[i + 1] - timeSeries[i];
} else {
time += duration;
}
}
return timeSeries.length == 0 ? 0 : time + duration;
}
}
第三题
class Solution {
public int minTaps(int n, int[] ranges) {
// 转为区间
int[][] areas = new int[n + 1][2];
for (int i = 0; i <= n; i++) {
areas[i][0] = Math.max(0, i - ranges[i]);
areas[i][1] = Math.min(n, i + ranges[i]);
}
// 按照左侧排序
Arrays.sort(areas, (a, b) -> a[0] - b[0]);
int count = 0;
// 边界不断向右推
int border = 0;
// 遍历过的区间索引
int idx = 0;
while (border != n) {
int max = border;
for (int i = idx; i <= n; i++) {
if (areas[i][0] > border) {
idx = i;
break;
} else if (areas[i][1] > max) {
max = areas[i][1];
}
}
if (max == border) {
return -1;
} else {
border = max;
count++;
}
}
return count;
}
}