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

Wish 最新OA

全局:

2019(1-3月) 码农类General 硕士 全职@wish - 网上海投 - Onsite  | | Fail | 应届毕业生

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

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

x
Wish OA 一题六十分钟,见图。已跪,希望能帮助其他人吧!(渣渣不会做,大神们可以说说自己的解法)题目叫:Prority Caching
给定一个List<List<Integer>> callLogs, format:<timestamp><id>
每一个i
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies




补充内容 (2019-2-21 09:24):
真是醉了,把OA选出Onsite了,最近真的是迷啊,我重新开一贴。

评分

参与人数 1大米 +10 收起 理由
匿名用户-TGWEM + 10

查看全部评分


上一篇:亚麻 OA2经
下一篇:tusimple两轮电面
🔗
eileenliu 2019-2-21 10:51:30 | 只看该作者
全局:
直接onsite了吗,没有电面?
回复

使用道具 举报

🔗
 楼主| bhlpku 2019-2-21 11:58:26 | 只看该作者
全局:
eileenliu 发表于 2019-2-21 10:51
直接onsite了吗,没有电面?

对不起我写错了,这是OA
回复

使用道具 举报

🔗
jyttwc901231 2019-7-26 05:08:58 | 只看该作者
全局:
package wish;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class prorityCache {
       
        public static void main(String[] args) {
               
                List<List<Integer>> id = new ArrayList<>();;
                List<Integer> id1 = new ArrayList<Integer>();
                id1.add(1); id1.add(5);
                List<Integer> id2 = new ArrayList<Integer>();
                id2.add(2); id2.add(0);
                List<Integer> id3 = new ArrayList<Integer>();
                id3.add(3); id3.add(5);
                List<Integer> id4 = new ArrayList<Integer>();
                id4.add(4); id4.add(0);
                List<Integer> id5 = new ArrayList<Integer>();
                id5.add(5); id5.add(5);
                List<Integer> id6 = new ArrayList<Integer>();
                id6.add(6); id6.add(0);
                id.add(id1);
                id.add(id2);
                id.add(id3);
                id.add(id4);
                id.add(id5);
                id.add(id6);
               
               
               
                List<List<Integer>> callLogs = new ArrayList<>();;
                List<Integer> log1 = new ArrayList<Integer>();
                log1.add(1); log1.add(1);
               
                List<Integer> log2 = new ArrayList<Integer>();
                log2.add(2); log2.add(2);
               
                List<Integer> log3 = new ArrayList<Integer>();
                log3.add(3); log3.add(3);
               
                List<Integer> log4 = new ArrayList<Integer>();
                log4.add(4); log4.add(4);
               
                List<Integer> log5 = new ArrayList<Integer>();
                log5.add(5); log5.add(5);
               
                List<Integer> log6 = new ArrayList<Integer>();
                log6.add(6); log6.add(6);
               
                callLogs.add(log1);
                callLogs.add(log2);
                callLogs.add(log3);
                callLogs.add(log4);
                callLogs.add(log5);
                callLogs.add(log6);
               
                prorityCache pc = new prorityCache();
                System.out.println(pc.getCache(callLogs, id));
        }
       
        Map<Integer, Integer> cache = new HashMap<Integer, Integer>();
        Map<Integer, Integer> disk = new HashMap<Integer, Integer>();
       
        public List<Integer> getCache(List<List<Integer>> callLogs, List<List<Integer>> id){
                List<Integer> result = new LinkedList<Integer>();
                initial(id);
                for(List<Integer> cl : callLogs) {
                        //System.out.println("------------------");
                        process(cl.get(1));
                        /*
                         * System.out.println("cache"); for(Map.Entry<Integer, Integer> c:
                         * cache.entrySet()) { System.out.println(c.getKey() + "    "+ c.getValue()); }
                         * System.out.println("disk"); for(Map.Entry<Integer, Integer> d:
                         * disk.entrySet()) { System.out.println(d.getKey() + "    "+ d.getValue()); }
                         */
                }
               
                if(cache.size() == 0) {
                        result.add(-1);
                }
               
                for(Map.Entry<Integer, Integer> m : cache.entrySet()) {
                        result.add(m.getKey());
                }
               
                Collections.sort(result);
                return result;
        }
        public void initial(List<List<Integer>> id) {
                for(List<Integer> i : id) {
                        if(i.get(1) > 5) {
                                cache.put(i.get(0), i.get(1));
                        } else {
                                disk.put(i.get(0), i.get(1));
                        }
                }
        }
        public void process(int id) {
                addPrority(id);
                reducePrority();
                moveTask();
        }
       
        public void addPrority(int id) {
                if(disk.containsKey(id)) {
                        disk.put(id, disk.get(id) + 3);
                }
                if(cache.containsKey(id)) {
                        cache.put(id, cache.get(id) + 3);
                }
        }
       
        public void reducePrority() {
                for(Map.Entry<Integer, Integer> c: cache.entrySet()) {
                        cache.put(c.getKey(),c.getValue() - 1 < 0 ? 0 : c.getValue() - 1);
                }
               
                for(Map.Entry<Integer, Integer> d: disk.entrySet()) {
                        disk.put(d.getKey(),d.getValue() - 1 < 0 ? 0 : d.getValue() - 1);
                }
        }
       
        public void moveTask() {
                List<Integer> cRemove = new ArrayList<>();
                for(Map.Entry<Integer, Integer> c: cache.entrySet()) {
                        if(c.getValue() <= 3) {
                                cRemove.add(c.getKey());
                                disk.put(c.getKey(), c.getValue());
                        }
                }
               
                for(int c: cRemove) {
                        cache.remove(c);
                }
               
                List<Integer> dRemove = new ArrayList<>();
                for(Map.Entry<Integer, Integer> d: disk.entrySet()) {
                        if(d.getValue() > 5) {
                                dRemove.add(d.getKey());
                                cache.put(d.getKey(), d.getValue());
                        }
                }
               
                for(int d: dRemove) {
                        disk.remove(d);
                }
        }
}
回复

使用道具 举报

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

本版积分规则

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