算法两题:
第一题,leetcode 上有个合并区间的变种
第二题,最小编辑距离
很简单,就是第一题这个输入输出,真是b了了,要不是因为这个输入输出,我今晚还能写b站的笔试,又要延期了
贴个代码吧:
1
public int getSumTime(List<int[]> times){
Collections.sort(times, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
List<int[]> list = new LinkedList<>();
int start = times.get(0)[0];
int end = times.get(0)[1];
for (int i = 1; i < times.size(); i++){
if(end < times.get(i)[0]){
list.add(new int[]{start, end});
start = times.get(i)[0];
end = times.get(i)[1];
}else{
end = Math.max(end, times.get(i)[1]);
}
}
list.add(new int[]{start, end});
int sum = 0;
for (int i = 0; i < list.size(); i++){
sum += list.get(i)[1] - list.get(i)[0];
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<int[]> times = new LinkedList<>();
while (sc.hasNextLine()){
String input = sc.nextLine();
if(input.equals("")){
break;
}
String[] numsStr = input.split(" ");
int[] nums = {Integer.parseInt(numsStr[0]), Integer.parseInt(numsStr[1])};
times.add(nums);
}
Main main = new Main();
System.out.println(main.getSumTime(times));
}
2
public int getAns(String str, String target){
char[] chars = str.toCharArray();
char[] tar = target.toCharArray();
int[][] dp = new int[chars.length + 1][tar.length + 1];
dp[0][0] = 0;
for (int i = 1; i <= tar.length; i++){
dp[0][i] = dp[0][i-1] + 1;
}
for (int i = 1; i <= chars.length; i++){
dp[i][0] = dp[i-1][0] + 1;
}
for (int i = 1; i <= chars.length; i++){
for (int j = 1; j <= tar.length; j++){
if(chars[i - 1] != tar[j - 1]){
dp[i][j] = Math.min(dp[i][j - 1] + 1, Math.min(dp[i-1][j] + 1, dp[i-1][j-1] + 1));
}else {
dp[i][j] = dp[i-1][j-1];
}
}
}
return dp[chars.length][tar.length];
}
#小米笔试##小米2023秋招笔试心得体会#