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

DrawBridge OA 60min

🔗
匿名用户-R44LD  2018-1-19 07:32:01 |倒序浏览

2018(1-3月) 码农类General 硕士 全职@ - Other - 在线笔试  | | Other | 应届毕业生

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

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

x
网上找了一下DrawBridge的OA做了一下 把题目和贴出来分享
如果有错误的地方请大家指正 谢谢

import java.util.*;

public class DrawBridgeOA {
    public static void main(String[] args) {
        Printer.p(mySort(new int[]{1,Integer.MIN_VALUE,0}));
    }

    /*
    * Sort an array by number of 1s in the binary of integer, if number of 1s are same,
    * the smaller one should be placed first.
    *
    * Example
    *
    * input [1,2,3,4,5]
    * return [1,2,4,3,5]
    *
    * */
    public static int[] mySort(int[] nums) {
        int[] res = new int[nums.length];
        PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                int count1 = countOnes((int)o1);
                int count2 = countOnes((int)o2);
                if (count1 == count2) {
                    return Integer.compare(o1, o2);
                    // in case of integer overflow
                }
                return count1 - count2;
            }
        });

        for (int num : nums) {
            pq.offer(num);
        }
        for (int i = 0; i < res.length; i++) {
            res = pq.poll();
        }

        return res;
    }

    public static int countOnes(int num) {
        int count = 0;
        while (num != 0) {
            count += num & 1;
            num >>>= 1;
        }
        return count;
    }



    /*
    * Given a positive integer target, count all the combinations of contiguous positive
    * integers that sum up to the target.
    *
    * For Example,
    * target = 15
    * return 3
    *
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
66d">jungleBook
(int[] nums) {
        int max = 0;
        for (int i = 0; i < nums.length; i++ ){
            max = Math.max(max, cal(nums, i));
        }
        return max;
    }

    public static int cal (int[] nums, int index) {
        int predator = nums[index];
        if (predator == -1) {
            jungleMap.put(index, 1);
            return 1;
        }

        int val;
        if (jungleMap.containsKey(predator)) {
            val = jungleMap.get(predator)+1;
        } else {
            val = cal(nums, predator)+1;
        }
        jungleMap.put(index, val);
        return val;
    }


    /*
     *  Given a set of inclusive intervals
     *  Calculate the minimum size of a set of number
     *  where each interval contains at least two numbers in this set.
     *
     *  Example
     *  given [1,3], [1,4], [2,5].
     *  return 2. ([2,3])
     *
     *  given [2,4], [3,6], [0,2], [4,7].
     *  return 4. ([0,2,4,6] or [1,2,4,5])
     * */

}

评分

参与人数 5大米 +11 收起 理由
pnoxoxo + 1 很有用的信息!
Sheboke + 2 楼主真的太厉害了
cniht + 3 good
AaronZhao + 2 很有用的信息!
laviniaguo + 3 很有用的信息!

查看全部评分


上一篇:数据砖头店面
下一篇:城堡电面
地里匿名用户
🔗
匿名用户-R44LD  2018-1-26 09:54:30
  1. /*
  2.      *  Given a set of inclusive intervals
  3.      *  Calculate the minimum size of a set of number
  4.      *  where each interval contains at least two numbers in this set.
  5.      *
  6.      *  Example
  7.      *  given [1,3], [1,4], [2,5].
  8.      *  return 2. ([2,3])
  9.      *
  10.      *  given [2,4], [3,6], [0,2], [4,7].
  11.      *  return 4. ([0,2,4,6] or [1,2,4,5])
  12.      * */

  13.     public static int intersectionSizeTwo(int[][] intervals) {
  14.         if (intervals.length == 0) {
  15.             return 0;
  16.         }

  17.         Arrays.sort(intervals, (a, b) -> {
  18.             if (a[0] == b[0]) {
  19.                 return b[1] - a[1];
  20.             }
  21.             return a[0] - b[0];
  22.         });

  23.         List<int[]> list = new ArrayList<>();
  24.         for (int[] interval : intervals) {
  25.             while (!list.isEmpty() && interval[1] <= list.get(list.size() - 1)[1]) {
  26.                 list.remove(list.size() - 1);
  27.             }
  28.             list.add(interval);
  29.         }

  30.         int count = 0;
  31.         int end1 = -1;
  32.         int end2 = -1;

  33.         for (int i = 0; i < list.size(); i++) {
  34.             int start = list.get(i)[0];
  35.             if (start <= end1) {
  36.                 continue;
  37.             } else if (start > end2) {
  38.                 end2 = list.get(i)[1];
  39.                 end1 = end2 - 1;
  40.                 count += 2;
  41.             } else {
  42.                 end1 = end2;
  43.                 end2 = list.get(i)[1];
  44.                 count += 1;
  45.             }
  46.         }

  47.         return count;
  48.     }
复制代码
回复

使用道具 举报

🔗
wto2184557 2018-1-27 15:26:12 | 只看该作者
全局:
多谢lz,jungle book也是去年的IXL的题
回复

使用道具 举报

🔗
ttzztt 2018-2-1 13:21:13 | 只看该作者
全局:
最后一题错误

补充内容 (2018-2-1 13:22):
楼主,最后一题我们试了一下最后一题的input觉得不太对
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-R44LD  2018-2-1 14:23:14
ttzztt 发表于 2018-2-1 13:21
最后一题错误

补充内容 (2018-2-1 13:22):

可以给个test case吗
回复

使用道具 举报

🔗
ttzztt 2018-2-2 07:11:03 | 只看该作者
全局:

就是题目给的example input [1,3], [1,4], [2,5] 和 [2,4], [3,6], [0,2], [4,7]
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-R44LD  2018-2-2 11:16:28
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

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

本版积分规则

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