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

Indeed OA 15

全局:

2016(7-9月) 码农类General 硕士 全职@indeed - 内推 - 在线笔试  | | Other | 应届毕业生
刚做的Indeed OA 15,挂了两个test case 4跟6,估计是挂了。附上写的代码,求大神帮忙debug。hackerrank不能看到test case是什么,真的很坑
[size=100%][size=100%]
[size=100%]1


[size=100%]import java.io.*;
[size=100%][size=100%]
[size=100%]2


[size=100%]import java.util.*;
[size=100%][size=100%]
[size=100%]3


[size=100%]
[size=100%][size=100%]
[size=100%]4

[size=100%][size=100%][size=1.25em]▾



[size=100%]public class Solution {
[size=100%][size=100%]
[size=100%]5

[size=100%][size=100%][size=1.25em]▾



[size=100%]    public static void main(String args[] ) throws Exception {
[size=100%][size=100%]
[size=100%]6


[size=100%]        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
[size=100%][size=100%]
[size=100%]7


[size=100%]        Scanner sc = new Scanner(System.in);
[size=100%][size=100%]
[size=100%]8


[size=100%]        int Q = sc.nextInt();
[size=100%][size=100%]
[size=100%]9


[size=100%]        int M = sc.nextInt();
[size=100%][size=100%]
[size=100%]10


[size=100%]        Pair[] pairs = new Pair[M];
[size=100%][size=100%]
[size=100%]11

[size=100%][size=100%][size=1.25em]▾



[size=100%]        for (int i = 0; i < M; i++) {
[size=100%][size=100%]
[size=100%]12


[size=100%]            pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
[size=100%][size=100%]
[size=100%]13


[size=100%]        }
[size=100%][size=100%]
[size=100%]14


[size=100%]        int[] res = calculate(pairs, Q);
[size=100%][size=100%]
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
   static class Pair{

[size=100%][size=100%]
[size=100%]44


[size=100%]        public int val = 0;
[size=100%][size=100%]
[size=100%]45


[size=100%]        public int count = 0;
[size=100%][size=100%]
[size=100%]46

[size=100%][size=100%][size=1.25em]▾



[size=100%]        public Pair(int val, int count) {
[size=100%][size=100%]
[size=100%]47


[size=100%]            this.val = val;
[size=100%][size=100%]
[size=100%]48


[size=100%]            this.count = count;
[size=100%][size=100%]
[size=100%]49


[size=100%]        }
[size=100%][size=100%]
[size=100%]50


[size=100%]    }
[size=100%][size=100%]
[size=100%]51


[size=100%]}


本帖子中包含更多资源

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

x

上一篇:Linkedin Intern OA
下一篇:【Coursera oa1 面经】新题!给的sample都是错的!真是给跪了!
🔗
 楼主| helloc93 2016-10-2 16:18:38 | 只看该作者
全局:
  1. 1

  2. import java.io.*;
  3. 2

  4. import java.util.*;
  5. 3



  6. 4



  7. public class Solution {
  8. 5



  9.     public static void main(String args[] ) throws Exception {
  10. 6

  11.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  12. 7

  13.         Scanner sc = new Scanner(System.in);
  14. 8

  15.         int Q = sc.nextInt();
  16. 9

  17.         int M = sc.nextInt();
  18. 10

  19.         Pair[] pairs = new Pair[M];
  20. 11



  21.         for (int i = 0; i < M; i++) {
  22. 12

  23.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  24. 13

  25.         }
  26. 14

  27.         int[] res = calculate(pairs, Q);
  28. 15

  29.         for (int n: res) System.out.println(n);
  30. 16

  31.     }
  32. 17


  33. 18



  34.     public static int[] calculate(Pair[] pairs, int Q) {
  35. 19

  36.         if (pairs.length == 0 || Q < 2) return new int[0];
  37. 20


  38. 21



  39.         Arrays.sort(pairs, new Comparator<Pair>(){
  40. 22



  41.             public int compare(Pair a, Pair b) {
  42. 23

  43.                 return a.val-b.val;
  44. 24

  45.             }
  46. 25

  47.         });
  48. 26


  49. 27

  50.         int[] indexs = new int[pairs.length];
  51. 28

  52.         int sum = 1;
  53. 29



  54.         for (int i = 0; i < indexs.length; i++) {
  55. 30

  56.             indexs[i] = sum;
  57. 31

  58.             sum += pairs[i].count;
  59. 32

  60.         }
  61. 33


  62. 34

  63.         int[] res = new int[Q-1];
  64. 35



  65.         for (int k = 1; k < Q; k++) {
  66. 36

  67.             int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
  68. 37

  69.             if (index < 0) index = -index-1;
  70. 38

  71.             res[k-1] = pairs[index].val;
  72. 39

  73.         }
  74. 40

  75.         return res;
  76. 41

  77.     }
  78. 42


  79. 43



  80.     static class Pair{
  81. 44

  82.         public int val = 0;
  83. 45

  84.         public int count = 0;
  85. 46



  86.         public Pair(int val, int count) {
  87. 47

  88.             this.val = val;
  89. 48

  90.             this.count = count;
  91. 49

  92.         }
  93. 50

  94.     }
  95. 51

  96. }
复制代码
回复

使用道具 举报

🔗
 楼主| helloc93 2016-10-2 16:19:16 | 只看该作者
全局:
第一次在帖子了插入代码,不会搞。。
回复

使用道具 举报

🔗
 楼主| helloc93 2016-10-2 16:19:51 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

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

本版积分规则

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