一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

一亩三分地官方iOS手机应用下载
查看: 501|回复: 3
收起左侧

Indeed OA 15

[复制链接] |试试Instant~ |关注本帖
helloc93 发表于 2016-10-2 16:17:47 | 显示全部楼层 |阅读模式

2016(7-9月) 码农类 硕士 全职@Indeed - 内推 - 在线笔试 |Otherfresh grad应届毕业生

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

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
刚做的Indeed OA 15,挂了两个test case 4跟6,估计是挂了。附上写的代码,求大神帮忙debug。hackerrank不能看到test case是什么,真的很坑 085435d3g5fnijc5taicbr.png
[size=100%][size=100%]
[size=100%]1


[size=100%]import java.io.*;-google 1point3acres
[size=100%][size=100%]
[size=100%]2

. visit 1point3acres.com for more.
[size=100%]import java.util.*;
[size=100%][size=100%]
[size=100%]3


[size=100%]. 1point3acres.com/bbs
[size=100%][size=100%]
[size=100%]4

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

. from: 1point3acres.com/bbs

[size=100%]public class Solution {
[size=100%][size=100%]
[size=100%]5
. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%][size=100%][size=1.25em]▾
.1point3acres缃


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

[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

. from: 1point3acres.com/bbs
[size=100%]        int M = sc.nextInt();.鏈枃鍘熷垱鑷1point3acres璁哄潧
[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

. visit 1point3acres.com for more.
[size=100%]            pairs = new Pair(sc.nextInt(), sc.nextInt());
[size=100%][size=100%]
[size=100%]13


[size=100%]        }. visit 1point3acres.com for more.
[size=100%][size=100%]
[size=100%]14


[size=100%]        int[] res = calculate(pairs, Q);
[size=100%][size=100%]
[size=100%]15


[size=100%]        for (int n: res) System.out.println(n);
[size=100%][size=100%]
[size=100%]16


[size=100%]    }
[size=100%][size=100%]
[size=100%]17


[size=100%]   
[size=100%][size=100%]
[size=100%]18

[size=100%][size=100%][size=1.25em]▾
.1point3acres缃
. 1point3acres.com/bbs

[size=100%]    public static int[] calculate(Pair[] pairs, int Q) {
[size=100%][size=100%]
[size=100%]19


[size=100%]        if (pairs.length == 0 || Q < 2) return new int[0];
[size=100%][size=100%]
[size=100%]20

. from: 1point3acres.com/bbs
[size=100%]        
[size=100%][size=100%]
[size=100%]21

[size=100%][size=100%][size=1.25em]▾
. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷


[size=100%]        Arrays.sort(pairs, new Comparator<Pair>(){
[size=100%][size=100%]
[size=100%]22

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



[size=100%]            public int compare(Pair a, Pair b) {
[size=100%][size=100%]
[size=100%]23


[size=100%]                return a.val-b.val;
[size=100%][size=100%]
[size=100%]24


[size=100%]            }
[size=100%]
[size=100%]
[size=100%]25


[size=100%]        });
[size=100%][size=100%]
[size=100%]26


[size=100%]        
[size=100%][size=100%]
[size=100%]27

.鏈枃鍘熷垱鑷1point3acres璁哄潧
[size=100%]        int[] indexs = new int[pairs.length];
[size=100%][size=100%]
[size=100%]28


[size=100%]        int sum = 1;
[size=100%][size=100%]
[size=100%]29

[size=100%][size=100%][size=1.25em]▾
. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴


[size=100%]        for (int i = 0; i < indexs.length; i++) {
[size=100%][size=100%]
[size=100%]30


[size=100%]            indexs = sum;.鐣欏璁哄潧-涓浜-涓夊垎鍦
[size=100%][size=100%]
[size=100%]31


[size=100%]            sum += pairs.count;
[size=100%][size=100%]
[size=100%]32


[size=100%]        }
[size=100%][size=100%]
[size=100%]33


[size=100%]        
[size=100%][size=100%]
[size=100%]34


[size=100%]        int[] res = new int[Q-1];
[size=100%][size=100%]
[size=100%]35

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



[size=100%]        for (int k = 1; k < Q; k++) {
[size=100%][size=100%]
[size=100%]36

鏉ユ簮涓浜.涓夊垎鍦拌鍧.

[size=100%]            int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
[size=100%][size=100%]
[size=100%]37
. Waral 鍗氬鏈夋洿澶氭枃绔,

[size=100%]            if (index < 0) index = -index-1;
[size=100%][size=100%]
[size=100%]38


[size=100%]            res[k-1] = pairs[index].val;
[size=100%][size=100%]
[size=100%]39


[size=100%]        }
[size=100%][size=100%]
[size=100%]40


[size=100%]        return res;
[size=100%][size=100%]
[size=100%]41


[size=100%]    } 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
[size=100%][size=100%]
[size=100%]42

. visit 1point3acres.com for more.

[size=100%]   
[size=100%][size=100%]
[size=100%]43

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



[size=100%]    static class Pair{. From 1point 3acres bbs
[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
. 1point 3acres 璁哄潧

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


[size=100%]}


 楼主| helloc93 发表于 2016-10-2 16:18:38 | 显示全部楼层
关注一亩三分地公众号:
Warald_一亩三分地
  1. 1

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

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

  6. . Waral 鍗氬鏈夋洿澶氭枃绔,

  7. 4


  8. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  9. public class Solution {
  10. 5



  11.     public static void main(String args[] ) throws Exception {
  12. 6.鐣欏璁哄潧-涓浜-涓夊垎鍦

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

  15.         Scanner sc = new Scanner(System.in);
  16. 8. Waral 鍗氬鏈夋洿澶氭枃绔,
  17. .鐣欏璁哄潧-涓浜-涓夊垎鍦
  18.         int Q = sc.nextInt();
  19. 9

  20.         int M = sc.nextInt();
  21. 10

  22.         Pair[] pairs = new Pair[M];.鏈枃鍘熷垱鑷1point3acres璁哄潧
  23. 11



  24.         for (int i = 0; i < M; i++) {
  25. 12
  26. . from: 1point3acres.com/bbs
  27.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  28. 13

  29.         }
  30. 14.鐣欏璁哄潧-涓浜-涓夊垎鍦

  31.         int[] res = calculate(pairs, Q);
  32. 15

  33.         for (int n: res) System.out.println(n);
  34. 16

  35.     }. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  36. 17


  37. 18

  38. .鏈枃鍘熷垱鑷1point3acres璁哄潧

  39.     public static int[] calculate(Pair[] pairs, int Q) {
  40. 19. 鍥磋鎴戜滑@1point 3 acres

  41.         if (pairs.length == 0 || Q < 2) return new int[0];. 1point3acres.com/bbs
  42. 20
    .鐣欏璁哄潧-涓浜-涓夊垎鍦


  43. 21
  44. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴


  45.         Arrays.sort(pairs, new Comparator<Pair>(){
  46. 22


  47. . from: 1point3acres.com/bbs
  48.             public int compare(Pair a, Pair b) {
  49. 23. from: 1point3acres.com/bbs

  50.                 return a.val-b.val;
  51. 24. more info on 1point3acres.com

  52.             }
  53. 25. 1point3acres.com/bbs

  54.         });
  55. 26. Waral 鍗氬鏈夋洿澶氭枃绔,


  56. 27

  57.         int[] indexs = new int[pairs.length];. 鍥磋鎴戜滑@1point 3 acres
  58. 28. 鍥磋鎴戜滑@1point 3 acres

  59.         int sum = 1;
  60. 29



  61.         for (int i = 0; i < indexs.length; i++) {. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  62. 30
  63. . more info on 1point3acres.com
  64.             indexs[i] = sum;
  65. 31

  66.             sum += pairs[i].count; 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  67. 32
    .鐣欏璁哄潧-涓浜-涓夊垎鍦

  68.         }
  69. 33


  70. 34. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴

  71.         int[] res = new int[Q-1];
  72. 35

  73. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  74.         for (int k = 1; k < Q; k++) {
  75. 36

  76.             int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));.鐣欏璁哄潧-涓浜-涓夊垎鍦
  77. 37
  78. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  79.             if (index < 0) index = -index-1;. From 1point 3acres bbs
  80. 38

  81.             res[k-1] = pairs[index].val;
  82. 39

  83.         }. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  84. 40
  85. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  86.         return res;
  87. 41

  88.     }
  89. 42


  90. 43



  91.     static class Pair{
  92. 44. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴

  93.         public int val = 0;
  94. 45
  95. . more info on 1point3acres.com
  96.         public int count = 0;
  97. 46. more info on 1point3acres.com



  98.         public Pair(int val, int count) {
  99. 47

  100.             this.val = val;
  101. 48
  102. . 鍥磋鎴戜滑@1point 3 acres
  103.             this.count = count;
  104. 49
  105. .1point3acres缃
  106.         }
  107. 50

  108.     }. Waral 鍗氬鏈夋洿澶氭枃绔,
  109. 51

  110. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| helloc93 发表于 2016-10-2 16:19:16 | 显示全部楼层
关注一亩三分地微博:
Warald
第一次在帖子了插入代码,不会搞。。
回复 支持 反对

使用道具 举报

 楼主| helloc93 发表于 2016-10-2 16:19:51 | 显示全部楼层
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。-google 1point3acres
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南
  1. 1
    鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  2. import java.io.*;. from: 1point3acres.com/bbs
  3. 2
  4. . 1point 3acres 璁哄潧
  5. import java.util.*;.1point3acres缃
  6. 3
    .鐣欏璁哄潧-涓浜-涓夊垎鍦



  7. 4


  8. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  9. public class Solution {
  10. 5
  11. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  12. -google 1point3acres
  13.     public static void main(String args[] ) throws Exception {
  14. 6
  15. .鏈枃鍘熷垱鑷1point3acres璁哄潧
  16.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  17. 7

  18.         Scanner sc = new Scanner(System.in);
  19. 8

  20.         int Q = sc.nextInt();
  21. 9.1point3acres缃

  22.         int M = sc.nextInt();
  23. 10

  24.         Pair[] pairs = new Pair[M];
  25. 11



  26.         for (int i = 0; i < M; i++) {
  27. 12

  28.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  29. 13

  30.         }
  31. 14.鐣欏璁哄潧-涓浜-涓夊垎鍦

  32.         int[] res = calculate(pairs, Q);
  33. 15.1point3acres缃
  34. . visit 1point3acres.com for more.
  35.         for (int n: res) System.out.println(n);
  36. 16

  37.     }
  38. 17


  39. 18 鏉ユ簮涓浜.涓夊垎鍦拌鍧.



  40.     public static int[] calculate(Pair[] pairs, int Q) {
  41. 19

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


  44. 21



  45.         Arrays.sort(pairs, new Comparator<Pair>(){
  46. 22



  47.             public int compare(Pair a, Pair b) {
  48. 23

  49.                 return a.val-b.val;
  50. 24

  51.             }
  52. 25

  53.         });
  54. 26


  55. 27

  56.         int[] indexs = new int[pairs.length];. visit 1point3acres.com for more.
  57. 28

  58.         int sum = 1;
  59. 29-google 1point3acres



  60.         for (int i = 0; i < indexs.length; i++) {
  61. 30

  62.             indexs[i] = sum;
  63. 31

  64.             sum += pairs[i].count;
  65. 32.鐣欏璁哄潧-涓浜-涓夊垎鍦

  66.         }
  67. 33
  68. .鐣欏璁哄潧-涓浜-涓夊垎鍦

  69. 34.鐣欏璁哄潧-涓浜-涓夊垎鍦

  70.         int[] res = new int[Q-1];
  71. 35



  72.         for (int k = 1; k < Q; k++) {.鐣欏璁哄潧-涓浜-涓夊垎鍦
  73. 36

  74. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  75.             int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
  76. 37. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  77.             if (index < 0) index = -index-1;
  78. 38
  79. .鐣欏璁哄潧-涓浜-涓夊垎鍦
  80.             res[k-1] = pairs[index].val;
  81. 39

  82.         }.鏈枃鍘熷垱鑷1point3acres璁哄潧
  83. 40

  84.         return res;
  85. 41.鐣欏璁哄潧-涓浜-涓夊垎鍦
  86. . 鍥磋鎴戜滑@1point 3 acres
  87.     }
  88. 42
  89. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

  90. 43.1point3acres缃
  91. . From 1point 3acres bbs


  92.     static class Pair{
  93. 44

  94.         public int val = 0;
  95. 45

  96.         public int count = 0;
  97. 46
  98. . more info on 1point3acres.com
  99. . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴

  100.         public Pair(int val, int count) {
  101. 47. 1point3acres.com/bbs
  102. .1point3acres缃
  103.             this.val = val;
  104. 48

  105.             this.count = count;. visit 1point3acres.com for more.
  106. 49
  107. . visit 1point3acres.com for more.
  108.         }
  109. 50

  110.     }. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  111. 51.1point3acres缃

  112. }
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2017-3-27 12:50

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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