注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
首先是timeline: 10/27/17
90min 2道老题 以及 15min 问卷调查(我也是醉了。。)
两题地里都有,下面是我的答案, local run 可过, huan'ying'zhi'zheng:
1. amazon warehouse: 给卡车载重M 以及一系列地点(类似 a[x][y] = z,(x, y)为以卡车为原点的坐标 z为该点货物重量) 输出距离最近的 N个点的坐标
import java.util.*;
public class Solution {
public List<List<Integer>> topK(List<List<Integer>> input, int n,int m){
PriorityQueue<List<Integer>> pq = new PriorityQueue<List<Integer>>(n,
new Comparator<List<Integer>>(){
public int compare (List<Integer> e1,List<Integer> e2){
return e1.get(0)*e1.get(0) + e1.get(1)*e1.get(1) - e2.get(0)*e2.get(0) - e2.get(1)*e2.get(1);
}
});
for(List<Integer> e1:input){
pq.add(e1);
}
List<List<Integer>> result = new ArrayList<>();
for(int i = 0;i < m && i < n;i++){
result.add(pq.remove());
}
return result;
}
}
2. distance between two nodes in a bst
public class Solution {
public static class TreeN
public TreeNode binaryTreeLCA(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) {
return root;
}
TreeNode left = binaryTreeLCA(root.left, p, q);
TreeNode right = binaryTreeLCA(root.right, p, q);
if (left == null && right == null) {
// not found
return null;
} else if (left == null) {
// both on right side, and right is LCA
return right;
} else if (right == null) {
return left;
} else {
// one of left the other on right
return root;
}
}
}
|