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

刚面完的Linkedin电面

 
🔗
密码 2019-5-7 09:03:45 | 只看该作者
全局:
[quote]liqingfd 发表于 2019-5-3 11:12
刚刚写了一个版本,跟楼上grandyang链接里面的差不多,欢迎指正!
  1. import java.ut ...[/quote]
  2. [hide=200]没必要用TreeMap吧,感觉PriorityQueue应该就够了吧?[code]public class RetainBestCache<K, T extends Rankable> {
  3.         int entriesToRetain;
  4.         HashMap<K, T> map = new HashMap<K,T>();
  5.         PriorityQueue<Wrapper<K, T>> pq;
  6.         DataSource<K,T> ds;

  7.         /* Constructor with a data source (assumed to be slow) and a cache size */
  8.         public RetainBestCache(DataSource<K,T> ds, int entriesToRetain) {
  9.                 //impliment here
  10.                 this.pq = new PriorityQueue<>(new Comparator<Wrapper>() {
  11.                         public int compare(Wrapper w1, Wrapper w2) {
  12.                                 return w1.data.getRank() - w2.data.getRank();
  13.                         }
  14.                 });
  15.                 this.ds = ds;
  16.                 this.entriesToRetain = entriesToRetain;
  17.         }
  18.         /* Gets some data. If possible, retrieves it from cache to be fast. If the data is not cached,
  19.         * retrieves it from the data source. If the cache is full, attempt to cache the returned data,
  20.         * evicting the T with lowest rank among the ones that it has available
  21.         * If there is a tie, the cache may choose any T with lowest rank to evict.
  22.         */
  23.         public T get(K key) {
  24.                 //impliment here
  25.                 if (map.containsKey(key)) {
  26.                         return map.get(key);
  27.                 }
  28.                 T data = DataSource.get(key);
  29.                 if (map.size() < entriesToRetain) {
  30.                         map.put(key, data);
  31.                         pq.offer(new Wrapper(key, data));
  32.                 } else {
  33.                         evict();
  34.                         map.put(key, data);
  35.                         pq.offer(new Wrapper(key, data));
  36.                 }
  37.                 return data;
  38.         }

  39.         private evict() {
  40.                 Wrapper leastRank = pq.poll();
  41.                 map.remove(leastRank.key);
  42.         }
  43. }

  44. class Wrapper<K, T> {
  45.         T data;
  46.         K key;

  47.         public Wrapper(T data, K key) {
  48.                 this.data = data;
  49.                 this.key = key;
  50.         }
  51. }

  52. /*
  53. * For reference, here are the Rankable and DataSource interfaces.
  54. * You do not need to implement them, and should not make assumptions
  55. * about their implementations.
  56. */
  57. public interface Rankable {
  58.         /**
  59.         * Returns the Rank of this object, using some algorithm and potentially
  60.         * the internal state of the Rankable.
  61.         */
  62.         long getRank();
  63. }

  64. public interface DataSource<K, T extends Rankable> {
  65.         T get(K key);
  66. }
复制代码
[/hide]
回复

使用道具 举报

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

使用道具 举报

🔗
xhdt 2021-7-8 12:46:02 | 只看该作者
全局:
Falldawn 发表于 2021-6-11 11:13
找到了这个帖子https://www.1point3acres.com/bbs/thread-279704-1-1.html,这题应该用TreeMap因为Heap的re ...

Heap remove 的时间复杂度难道不是O(log n)
回复

使用道具 举报

🔗
Falldawn 2021-7-8 12:48:39 | 只看该作者
全局:
xhdt 发表于 2021-7-8 12:46
Heap remove 的时间复杂度难道不是O(log n)

显然不是,搜一下吧
回复

使用道具 举报

🔗
xhdt 2021-7-8 20:42:38 | 只看该作者
全局:
本帖最后由 xhdt 于 2021-7-8 21:12 编辑
Falldawn 发表于 2021-6-11 11:13
找到了这个帖子https://www.1point3acres.com/bbs/thread-279704-1-1.html,这题应该用TreeMap因为Heap的re ...

再看了下,两个MAP是需要的。
回复

使用道具 举报

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

使用道具 举报

🔗
Falldawn 2021-8-25 22:31:33 | 只看该作者
全局:
biomedicineman 发表于 2021-8-24 21:31
TreeMap.remove() 应该是logN吧?
PriorityQueue remove() max/min也是logN,但remove(object)是 O(N)

是的,因为TreeMap是平衡二叉树,而Heap只有上下级的大小关系

评分

参与人数 1大米 +5 收起 理由
biomedicineman + 5 恩恩,平时都没怎么关注TreeMap的实现

查看全部评分

回复

使用道具 举报

全局:
Falldawn 发表于 2021-8-25 07:31
是的,因为TreeMap是平衡二叉树,而Heap只有上下级的大小关系
恩恩,平时都没怎么关注TreeMap的实现
回复

使用道具 举报

🔗
brucezu 2021-9-3 03:19:13 | 只看该作者
全局:
用
-HashMap keep k - v relation
-TreeSet or PriorityQueue  to keep the rank list

补充内容 (2021-09-08 09:58 +8:00):
TreeSet does not work
回复

使用道具 举报

🔗
richard_515 2021-12-1 03:55:03 | 只看该作者
全局:
提问:如果不用java怎么写?
回复

使用道具 举报

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

本版积分规则

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