📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

导大师

🔗
匿名用户-P250F  2021-9-14 03:40:39 |倒序浏览

2021(7-9月) 码农类General 硕士 全职@doordash - 猎头 - 技术电面  | 😃 Positive 😐 Average | Fail | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
hackerrank 上一道题 我事后一查和这个一样
我给的idea  O(NlogN+M*N)time 不是最优的 还因为紧张说错了
M: queried city number
N: given city number

K: max number in the same x or y line.

It can be O(NlogK+MlogK)average time


我早早写完,花很多时间 fix compile 错误,没时间跑cases了

如感觉对您有帮助,求赏米。




补充内容 (2021-09-16 01:06 +8:00):
事后作了一个O(NlogK) time 的

public List<String> closestStraightCity(String[] citys, int[] xs, int[] ys, String[] queryCitys) {
    Map<String, int[]> cs = new HashMap<>(); // city name: (x,y)
    Map<I
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
eger, String> hi = L.get(l).higherEntry(v);
      Map.Entry<Integer, String> low = L.get(l).lowerEntry(v);
      if (hi != null) four.add(new Choice(hi.getValue(), hi.getKey() - v));
      if (low != null) four.add(new Choice(low.getValue(), v - low.getKey()));
    }
  }

  class Choice {
    String name;
    int distance;

    public Choice(String na...

评分

参与人数 7大米 +8 收起 理由
5222464 + 1 赞一个
A5kx8B + 1 很有用的信息!
sherrill_lam + 1 很有用的信息!
lrance + 2 很有用的信息!
咱们时差一小时 + 1 赞一个

查看全部评分


上一篇:9.13亚麻OA1, 附图片
下一篇:热带雨林 9.13 OA1
地里匿名用户
推荐
匿名用户-P250F  2021-9-15 14:19:19
本帖最后由 匿名 于 2021-9-14 23:20 编辑

share 一个solution 求米
  1. public List<String> closestStraightCity(String[] citys, int[] xs, int[] ys, String[] queryCitys) {
  2.     Map<String, int[]> cs = new HashMap<>(); // city name: (x,y)
  3.     Map<Integer, TreeMap<Integer, String>> X = new HashMap<>();
  4.     // x line:  y1: city2 name( < city1name)  y3: city3 name
  5.     Map<Integer, TreeMap<Integer, String>> Y = new HashMap<>();
  6.     // y line:  x1: city1 name, x2: city2 name

  7.     int N = citys.length;
  8.     for (int i = 0; i < N; i++) {
  9.       int x = xs[i], y = ys[i];
  10.       String name = citys[i];
  11.       X.computeIfAbsent(x, k -> new TreeMap<>());
  12.       if (!X.get(x).containsKey(y) || name.compareTo(X.get(x).get(y)) < 0) {
  13.         X.get(x).put(y, name); // lexicographically order name
  14.       }

  15.       Y.computeIfAbsent(y, k -> new TreeMap<>());
  16.       if (!Y.get(y).containsKey(x) || name.compareTo(Y.get(y).get(x)) < 0) {
  17.         Y.get(y).put(x, name); // lexicographically order name
  18.       }
  19.       cs.put(name, new int[] {x, y});
  20.     }

  21.     List<String> r = new ArrayList<>();
  22.     for (String c : queryCitys) {
  23.       int x = cs.get(c)[0], y = cs.get(c)[1];
  24.       List<Choice> four = new ArrayList<>();
  25.       collect(X, x, y, four);
  26.       collect(Y, y, x, four);
  27.       Collections.sort(
  28.           four,
  29.           (a, b) -> {
  30.             if (a.distance == b.distance) return a.name.compareTo(b.name);
  31.             return a.distance - b.distance;
  32.           });
  33.       r.add(four.isEmpty() ? "NONE" : four.get(0).name);
  34.     }
  35.     return r;
  36.   }

  37.   private void collect(Map<Integer, TreeMap<Integer, String>> L, int l, int v, List<Choice> four) {
  38.     if (L.get(l) != null) {
  39.       Map.Entry<Integer, String> hi = L.get(l).higherEntry(v);
  40.       Map.Entry<Integer, String> low = L.get(l).lowerEntry(v);
  41.       if (hi != null) four.add(new Choice(hi.getValue(), hi.getKey() - v));
  42.       if (low != null) four.add(new Choice(low.getValue(), v - low.getKey()));
  43.     }
  44.   }

  45.   class Choice {
  46.     String name;
  47.     int distance;

  48.     public Choice(String name, int distance) {
  49.       this.name = name;
  50.       this.distance = distance;
  51.     }
  52.   }
复制代码

评分

参与人数 1大米 +1 收起 理由
A5kx8B + 1 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
xiangwei36 2022-2-10 23:28:22 | 只看该作者
全局:
谢谢分享
回复

使用道具 举报

🔗
pcheng11 2022-6-5 11:36:27 | 只看该作者
本楼:
全局:
MlogK ...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表