注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
本帖最后由 nedved 于 2022-4-2 18:32 编辑
karat,面试官比较冷漠。。
开始是设计五小题,地里都有。
代码题我感觉还是有点难度对于我(lc只刷了100来道题),还好最后做出来了。
编程题如下:
/**
You're developing a system for scheduling advising meetings with students in a Computer Science program. Each meeting should be scheduled when a student has completed 50% of their academic program.
Each course at our university has at most one prerequisite that must be taken first. No two courses share a prerequisite. There is only one path through the program.
您好! 本帖隐藏的内容需要积分高于 188 才可浏览 您当前积分为 0。 使用VIP即刻解锁阅读权限或查看其他获取积分的方式 游客,您好! 本帖隐藏的内容需要积分高于 188 才可浏览 您当前积分为 0。 VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Sample output 1:
"Data Structures"
Sample input 2:
prereqs_courses2 = [
["Algorithms", "Foundations of Computer Science"],
["Data Structures", "Algorithms"],
["Foundations of Computer Science", "Logic"],
["Logic", "Compilers"],
["Compilers", "Distributed Systems"],
]
Sample output 2:
"Foundations of Computer Science"
Sample input 3:
prereqs_courses3 = [
["Data Structures", "Algorithms"],
]
Sample output 3:
"Data Structures"
Complexity analysis variables:
n: number of pairs in the input
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] argv) {
String[][] prereqsCourses1 = {
{"Foundations of Computer Science", "Operating Systems"},
{"Data Structures", "Algorithms"},
{"Computer Networks", "Computer Architecture"},
{"Algorithms", "Foundations of Computer Science"},
{"Computer Architecture", "Data Structures"},
{"Software Design", "Computer Networks"}
};
String[][] prereqsCourses2 = {
{"Algorithms", "Foundations of Computer Science"},
{"Data Structures", "Algorithms"},
{"Foundations of Computer Science", "Logic"},
{"Logic", "Compilers"},
{"Compilers", "Distributed Systems"},
};
String[][] prereqsCourses3 = {
{"Data Structures", "Algorithms"}
};
System.out.println(findHalfway(prereqsCourses3));
}
public static String findHalfway (String[][] courses) {
}
}
答案:(recruiter反馈我的解法是optimal solution)- </blockquote></div><div class="blockcode"><blockquote>public static String findHalfway (String[][] courses) {
- LinkedList<String> lst = new LinkedList<>();
- int x = courses.length;
- // assume there is always records.
- //"Data Structures" -> "Algorithms" -> "Foundations of Computer Science" -> "Operating Systems"
- Map<String, String> ba = new HashMap<>();
- Map<String, String> ab = new HashMap<>();
- for(int i=0; i<x; i++) {
- String before = courses[i][0];
- String after = courses[i][1];
- ba.put(before, after);
- ab.put(after, before);
- }
- lst.add(courses[0][0]);
- lst.add(courses[0][1]);
- for(int j=1; j<x; j++){
- String head = lst.getFirst();
- String tail = lst.getLast();
- if(ab.get(head) != null) {
- lst.addFirst(ab.get(head));
- }
- if(ba.get(tail) != null) {
- lst.addLast(ba.get(tail));
- }
- }
- return lst.get(x/2);
- }
复制代码 求求求大米!~~
|