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

Affirm 新鲜出炉电面

🔗
匿名用户-0ENPO  2021-9-8 10:08:46 |倒序浏览

2021(7-9月) 码农类General 硕士 全职@affirm - 内推 - 技术电面  | 😃 Positive 😐 Average | Pass | 在职跳槽

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

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

x
这个是一道地里出现过很多次的店面问题,小伙伴们可以在地里找找。楼主用的是UndirectedGraph方法解出来的。如果小伙伴有其他的解法求一个。发面经攒人品,求大家加米。

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


评分

参与人数 4大米 +10 收起 理由
admin + 3 很有用的信息!
Fzyyy + 1 给你点个赞!
lrt98802 + 3 给你点个赞!
bryanjhy + 3 给你点个赞!

查看全部评分


上一篇:希望电免紫薯
下一篇:9月OA面经 InterSystem PET
地里匿名用户
推荐
匿名用户-0ENPO  2021-10-20 23:51:36
匿名者 发表于 2021-10-20 10:35
请问楼主可以提供一下undirectedgraph的思路么,我想的是用hashmap➕排序。返回value最大的。感觉时 ...
  1.     private Map<String, Map<String, Integer>> graph = new HashMap<>();
  2.     private Map<String, Integer> countMap = new HashMap<>();

  3.     public void addWord(List<String> words) {
  4.         Collections.sort(words);
  5.         for (int i = 0; i < words.size(); i++) {
  6.             String from = words.get(i);
  7.             //if words are duplicated
  8.             if (i != 0 && from.equals(words.get(i - 1))) {
  9.                 continue;
  10.             }

  11.             for (int j = i + 1; j < words.size(); j++) {
  12.                 String to = words.get(j);
  13.                 //if words are duplicated
  14.                 if (from.equals(to) || to.equals(words.get(j - 1))) {
  15.                     continue;
  16.                 }
  17.                 //Adding from/to nodes
  18.                 Map<String, Integer> toMap = graph.getOrDefault(from, new HashMap<>());
  19.                 int count = toMap.getOrDefault(to, 0);
  20.                 toMap.put(to, count + 1);
  21.                 graph.put(from, toMap);

  22.                 //Adding to/from nodes
  23.                 Map<String, Integer> fromMap = graph.getOrDefault(to, new HashMap<>());
  24.                 int fromCount = fromMap.getOrDefault(from, 0);
  25.                 fromMap.put(from, fromCount + 1);
  26.                 graph.put(to, fromMap);

  27.                 countMap.put(from, Math.max(countMap.getOrDefault(from, 0), count + 1));
  28.                 countMap.put(to, Math.max(countMap.getOrDefault(to, 0), fromCount + 1));
  29.             }
  30.         }
  31.     }

  32.     public Map<String, List<String>> printValue() {
  33.         Map<String, List<String>> res = new HashMap<>();

  34.         for (Map.Entry<String, Map<String, Integer>> entry : graph.entrySet()) {
  35.             String from = entry.getKey();
  36.             Map<String, Integer> toMap = entry.getValue();
  37.             int maxCount = countMap.get(from);

  38.             for (Map.Entry<String, Integer> toMapEntry : toMap.entrySet()) {
  39.                 String to = toMapEntry.getKey();
  40.                 int times = toMapEntry.getValue();

  41.                 if (times == maxCount) {
  42.                     List<String> list = res.getOrDefault(from, new ArrayList<>());
  43.                     list.add(to);
  44.                     res.put(from, list);
  45.                 }
  46.             }
  47.         }
  48.         return res;
  49.     }
复制代码
回复

使用道具 举报

地里匿名用户
推荐
匿名用户-TZLQC  2021-10-20 23:35:25
请问楼主可以提供一下undirectedgraph的思路么,我想的是用hashmap➕排序。返回value最大的。感觉时间复杂度很高

评分

参与人数 1大米 +1 收起 理由
wangbd + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

地里匿名用户
🔗
匿名用户-TZLQC  2021-10-21 05:24:04
回复

使用道具 举报

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

使用道具 举报

地里匿名用户
🔗
匿名用户-2SZHI  2021-12-17 08:54:30
  1. import collections
  2. arr = [['a', 'b', 'c'],['b', 'c', 'd'], ['c', 'd', 'e']]

  3. graph = collections.defaultdict(collections.Counter)
  4.     for lst in arr:
  5.         counter = collections.Counter(list(set(lst)))
  6.         for elem in counter:
  7.             counter[elem] -= 1
  8.             graph[elem] += counter
  9.             counter[elem] += 1
  10.             print(counter)
复制代码
Python Hashtable of {neighbor: freq}

后面应该就很明朗了

for key in graph:
    maxFreq = max(graph[elem])
    cur = []
.....
....
....

回复

使用道具 举报

🔗
李晟之 2022-9-2 06:29:06 | 只看该作者
全局:
  1. from itertools import permutations
  2. # Time compexity O(number of stirng * longest_string_length ^2)
  3. # Space Complexity O(26 ^2)
  4. def leeters_applying_most(string_list):
  5.     matrix = [[0] * 26 for _ in range(26)]
  6.     for string in string_list:
  7.         for first, second in itertools.permutations(string, 2):
  8.             rol_idx, col_idx = ord(first) - ord('a'), ord(second) - ord('a')
  9.             matrix[rol_idx][col_idx] += 1
  10.     res = defaultdict(list)
  11.     for rol_idx in range(26):
  12.         max_row_val = max(matrix[rol_idx])
  13.         if max_row_val == 0:
  14.             continue
  15.         for col_idx in range(26):  
  16.             if matrix[rol_idx][col_idx] == max_row_val:
  17.                 res[chr(rol_idx + ord('a'))].append(chr(col_idx + ord('a')))
  18.     return res
  19. print(leeters_applying_most(["abc", "bcd", "cde"]))
复制代码

评分

参与人数 1大米 +1 收起 理由
司南 + 1 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
idkwhodatis 2024-11-25 02:56:46 | 只看该作者
全局:
有一个偷鸡的做法,用python的库和list/dict comprehension,因为这些是底层c写的,虽然complexity更高但runtime并不慢,不知道面试中这样写行不行
  1. from collections import defaultdict
  2. from collections import Counter

  3. def test(l):
  4.     d=defaultdict(Counter)
  5.     for i in l:
  6.         for j in range(len(i)):
  7.             for k in i[:j]+i[j+1:]:
  8.                 d[i[j]][k]+=1
  9.     return {i:[k for (k,v) in d[i].items() if v==max(d[i].values())] for i in d}
复制代码
回复

使用道具 举报

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

本版积分规则

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