注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
本帖最后由 Lambsea 于 2020-1-7 04:34 编辑
运气比较好拿到了两个最难的oa题
第一题
Product Suggestions
Runtime: 11 ms, faster than 92.26% of Java online submissions for Search Suggestions System.
- class Solution {
- public List<List<String>> suggestedProducts(String[] products, String searchWord) {
- List<List<String>> ans = new ArrayList<>();
-
- if (products.length == 0 || searchWord.length() == 0) {
- return ans;
- }
-
- Arrays.sort(products);
- char[] arr = searchWord.toCharArray();
-
- // first
- List<String> lastMatched = new ArrayList<>();
- for (String str : products) {
- if (str.charAt(0) == arr[0]) {
- lastMatched.add(str);
- }
- }
- ans.add(getFirstThree(lastMatched));
-
- //O(m)
- for (int i = 1; i < arr.length; i++) {
- List<String> matched = getMatchedStrings(lastMatched, i, arr[i]); // O(n)
- lastMatched = matched;
- ans.add(getFirstThree(matched));
- }
-
- return ans; //O(m*n) m 是搜索字符串的长度 n是总共的products个数
- }
-
- private List<String> getFirstThree(List<String> products) { //O(3)
- List<String> ans = new ArrayList<>();
- for (int i = 0; i < 3 && i < products.size(); i++) {
- ans.add(products.get(i));
- }
- return ans;
- }
-
- //O(n)
- private List<String> getMatchedStrings(List<String> products, int index, char c) {
- List<String> ans = new ArrayList<>();
- for (String str : products) {
- // 这里默认如果搜索字串长于当前产品名字, 那么我们就认为没有产品匹配
- if (index < str.length() && str.charAt(index) == c) {
- ans.add(str);
- }
- }
- return ans;
- }
- }
复制代码
第二题
Critical Routers[/i]
无向图里找相连点, 我这里的答案是看过Tarjan教程之后写的
正常人的想法应该是用bfs / dfs 把相邻点去掉一个一个试能不能联通
You are given a
Input: numNodes = 7, numEdges = 7, edges = [[0, 1], [0, 2], [1, 3], [2, 3], [2, 5], [5, 6], [3, 4]]
- public class CriticalRouters {
- public static void main(String[] args) {
- /**
- * numNodes = 7, numEdges = 7, edges = [[0, 1], [0, 2], [1, 3], [2, 3], [2, 5], [5, 6], [3, 4]]
- */
- int numNodes = 7, numEdges = 7;
- List<List<Integer>> edges = StringTransformHelper.to2DList("[[0, 1], [0, 2], [1, 3], [2, 3], [2, 5], [5, 6], [3, 4]]");
- /**
- * out put [2, 3, 5]
- */
- CriticalRouters cr = new CriticalRouters();
- List<Integer> ans = new ArrayList<>();
- int[] ids = new int[numNodes];
- int[] lowLink = new int[numNodes];
- int[] outGoingEdgeCount = new int[numNodes];
- boolean[] visited = new boolean[numNodes];
- List<Integer>[] graph = cr.getGraph(numNodes, edges);
- int startNode = 5;
- cr.getCriticalRouters(ans, ids, lowLink, outGoingEdgeCount, visited, -1, startNode, 0, startNode, graph);
- System.out.println(ans);
- }
- private void getCriticalRouters(List<Integer> ans,
- int[] ids,
- int[] lowLink,
- int[] outGoingEdgeCount,
- boolean[] visited,
- int prevNode,
- int currNode,
- Integer currentId,
- int startNode,
- List<Integer>[] graph) {
- ids[currNode] = lowLink[currNode] = currentId++;
- visited[currNode] = true;
- if (prevNode == startNode) {
- outGoingEdgeCount[prevNode]++;
- }
- for (Integer neib : graph[currNode]) {
- if (neib == prevNode) {
- continue; // ensure single directional
- }
- if (!visited[neib]) {
- getCriticalRouters(ans, ids, lowLink, outGoingEdgeCount, visited, currNode, neib, currentId, startNode, graph);
- lowLink[currNode] = Math.min(lowLink[currNode], lowLink[neib]);
- if (ids[currNode] <= lowLink[neib]) { // == -> circle < -> bridge
- if (currNode == startNode) {
- if (outGoingEdgeCount[startNode] > 1) {
- ans.add(currNode);
- }
- } else {
- ans.add(currNode);
- }
- }
- } else {
- lowLink[currNode] = Math.min(lowLink[currNode], ids[neib]);
- }
- }
- }
- private List<Integer>[] getGraph(int n, List<List<Integer>> connections) {
- List<Integer>[] graph = new List[n];
- for (int i = 0; i < n; i++) {
- graph[i] = new ArrayList<>();
- }
- for (int i = 0; i < connections.size(); i++) {
- List<Integer> edge = connections.get(i);
- int prev = edge.get(0);
- int next = edge.get(1);
- graph[prev].add(next);
- graph[next].add(prev);
- }
- return graph;
- }
- }
复制代码
当时两个题都没做好,然而还是被recruiter捞起来了 1.10 onsite,求各位加点米
[/i]
|