回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

雨林OA过经顺便讨论一下prime air解法

🔗
匿名用户-7UIKE  2021-3-27 11:41:04 |倒序浏览

2021(1-3月) 码农类General 硕士 全职@amazon - 猎头 - 在线笔试  | | Pass | 在职跳槽

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

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

x
第一题是刷题网酒伞其的变种。
第二题是prime air,我大部分test case都
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
41 +8:00):
还有求米看面经!谢谢!

评分

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

查看全部评分


上一篇:脸书onsite
下一篇:AI/ML engineer intern@Apple Pass
🔗
JLucsd 2021-3-29 08:48:21 | 只看该作者
全局:
双指针的话应该是要loop 两遍, 但是还是要考虑去重的问题
比如 第一遍 A 指针从0 开始, B 指针从末尾开始
第二遍 A 指针从 末尾开始, B 指针从0开始。 这个解法我也没有非常确定,但想不出反例。

最好的解法还是用map,可以参考这个老哥的代码,这是在刷题网discuss下面的答案,coding style不好,但是思路肯定是对的

  1. public static void main(String[] args) {
  2.     int arr1[][] = { {1, 2000}, {2, 3000}, {3, 4000}, {4, 2000} };
  3.     int arr2[][] = { { 1, 5000 }, {2, 3000} };

  4.    List<int[]> re = getClosestPairs(arr1, arr2, 7500);

  5.    for(int[] r : re)
  6.    {
  7.        System.out.println(r[0] + " " + r[1]);
  8.    }
  9. }


  10. private  static  List<int[]> getClosestPairs(int[][] A, int[][] B, int target)
  11. {

  12.     TreeMap<Integer, List<Integer>> valueWithIds = new TreeMap<>();

  13.     List<int[]> result = new ArrayList<>();

  14.     int closestTarget = 0;

  15.   /*
  16.        Loop through the first Array and insert the values into the map with 'value' (A[1]) as key and id (A[0]) add to the list as value.
  17.   */

  18.     for(int[] a  : A)
  19.     {
  20.         int currentTarget = a[1], id = a[0];
  21.         valueWithIds.putIfAbsent(currentTarget, new ArrayList<>());
  22.         valueWithIds.get(currentTarget).add(id);

  23.     }


  24.     for(int[] b : B)
  25.     {
  26.         int remainingTarget = target - b[1];  // We will find the remaining target by the value from the value of B.
  27.       // For example if target = 5000, b[1] = 3000, then remainingTarget = 2000

  28.         if(remainingTarget < 0) continue;

  29.         if(valueWithIds.containsKey(remainingTarget)) // That means we find the closest target == currentTarget
  30.         {
  31.              if(closestTarget != target)
  32.              {
  33.                  closestTarget = target;
  34.                  result = new ArrayList<>();
  35.              }

  36.              for(int id : valueWithIds.get(remainingTarget) )
  37.              {

  38.                  result.add(new int[] {id, b[0]});
  39.              }

  40.         }

  41.         else
  42.         {
  43.             Integer floor = valueWithIds.floorKey(remainingTarget);  // Check if we have any least value which is less than remainingTarget.

  44.             if(floor == null) continue;

  45.             int currentTarget = b[1] + floor;

  46.             if(  currentTarget >= closestTarget )
  47.             {
  48.                 if(currentTarget != closestTarget)
  49.                 {
  50.                     closestTarget = currentTarget;
  51.                     result = new ArrayList<>();
  52.                 }

  53.                 for(int id : valueWithIds.get(floor) )
  54.                 {
  55.                     result.add(new int[] {id, b[0]});
  56.                 }
  57.             }

  58.         }
  59.     }


  60.     return result;
  61. }}
复制代码

评分

参与人数 1大米 +1 收起 理由
caudalienature + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

全局:
谢谢楼主分享
回复

使用道具 举报

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

本版积分规则

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