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

square 电面

全局:

2019(7-9月) 码农类General 硕士 全职@ - 网上海投 - 技术电面  | | Other | 在职跳槽

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

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

x
Square 电面, coder pad 上写代码


抓动物算积分游戏, A 抓了 3 头牛,2头猪
积分是按照 抓到动物的头数的排名来算积分, 如果有三个玩家,第一名就是三分

最后算出每个人的积分,并且打印出
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies

// B  .    3         1
// C .     4        3

A 和 B 每人都拿到 (1 +2)=  1.5 分



评分

参与人数 10大米 +42 收起 理由
ImBJJ + 2 给你点个赞!
whdawn + 10
luckyjessica + 2 给你点个赞!
zjsth92 + 1 赞一个
navoval + 1 很有用的信息!

查看全部评分


上一篇:巨硬 店面 + 2 昂赛
下一篇:Paypal Infra Phone + Remote
🔗
 楼主| skyrainX 2019-7-19 11:23:09 | 只看该作者
本楼:
全局:
求加米啊
回复

使用道具 举报

🔗
zjsth92 2019-7-30 06:12:05 | 只看该作者
全局:
请问楼主是怎么样解的?
可以对每种动物列一个List,然后直接Arrays.sort 送大到小排序?用一个HashMap记录每个人的分数?
回复

使用道具 举报

🔗
littleric 2020-3-15 07:21:44 | 只看该作者
全局:
  1. class Person {
  2.         public String name;
  3.         public int pigNumber;
  4.         public int cowNumber;
  5.         public int finalScore;
  6.     }

  7.     public void caclulateCowsAndPigs(List<Person> personList) {

  8.         HashMap<String, Integer> pigScores = new HashMap<>();
  9.         HashMap<String, Integer> cowScores = new HashMap<>();

  10.         String pigKey = "PIG";
  11.         String cowKey = "COW";


  12.         PriorityQueue<Person> pigQueue = new PriorityQueue<>(new Comparator<Person>() {
  13.             @Override
  14.             public int compare(Person o1, Person o2) {
  15.                 return o1.pigNumber - o2.pigNumber;
  16.             }
  17.         });
  18.         handleScore(pigQueue, personList, pigScores, pigKey);


  19.         PriorityQueue<Person> cowQueue = new PriorityQueue<>(new Comparator<Person>() {
  20.             @Override
  21.             public int compare(Person o1, Person o2) {
  22.                 return o1.cowNumber - o2.cowNumber;
  23.             }
  24.         });

  25.         handleScore(cowQueue, personList, cowScores, cowKey);


  26.         for(int i = 0 ;i < personList.size() ;i++){
  27.             Person currentPerson = personList.get(i);
  28.             int cowScore = cowScores.get(getUserAnimalScoreKey(currentPerson.name, cowKey));
  29.             int pigScore = pigScores.get(getUserAnimalScoreKey(currentPerson.name, pigKey));
  30.             currentPerson.finalScore = cowScore+pigScore;
  31.         }
  32.     }

  33.     private void handleScore( PriorityQueue<Person> priorityQueue , List<Person> persons, HashMap<String, Integer> scores,  String key ){
  34.         priorityQueue.addAll(persons);

  35.         int currentScore = 1;

  36.         while (!priorityQueue.isEmpty()) {
  37.             Person person = priorityQueue.poll();
  38.             scores.put(getUserAnimalScoreKey(person.name, key), currentScore);
  39.             currentScore++;
  40.         }
  41.     }

  42.     private String getUserAnimalScoreKey(String userName, String animalName){
  43.         return userName+"-"+animalName;
  44.     }
复制代码
回复

使用道具 举报

🔗
littleric 2020-3-15 07:22:21 | 只看该作者
全局:
随便写了一个版本,follow up的话就要排序之后只要数字相同的平均分一下
回复

使用道具 举报

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

本版积分规则

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