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

狗家电面

 
全局:
hjldtc 发表于 2021-02-12 12:24:16
不行呀  k = 2
range: ,,]
那不就所有数字都到一个range里面去了?
是的,我错了,我在后面的楼里承认错误了,不好意思,误解您了

补充内容 (2021-2-13 06:37):
误导

评分

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

查看全部评分

回复

使用道具 举报

🔗
Baby2021 2021-2-15 13:36:09 | 只看该作者
全局:
第一题的利口题号是什么呀? 谢谢啦

评分

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

查看全部评分

回复

使用道具 举报

🔗
copper1820 2021-4-8 07:39:20 | 只看该作者
全局:
为了保证顺序就直接union find 复杂度nk 可以用treemap来确定最终顺序也是按照索引顺序走的

评分

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

查看全部评分

回复

使用道具 举报

全局:
本帖最后由 wengjn 于 2021-4-8 13:56 编辑

c# 实现了下
先通过union find来分组 然后一个个组打印出来就好了

  1. class HelloWorld {
  2.     static void Main() {
  3.         var test = new int[] { 4, 3, 7, 6, 9, 2 };
  4.         int k = 1;
  5.         
  6.         var uf = new UnionFind(test.Length);
  7.         
  8.         for (int i=0; i<test.Length-1; i++) {
  9.             for (int j=i+1; j<test.Length; j++) {
  10.                 if (NumsWithinRange(test[i], test[j], k)) {
  11.                     uf.Union(i, j);
  12.                 }
  13.             }
  14.         }
  15.         var res = new Dictionary<int, IList<int>>();
  16.         
  17.         for (int i=0; i<test.Length; i++) {
  18.             int groupNumber = uf.Find(i);
  19.             if (!res.ContainsKey(groupNumber)) {
  20.                 res[groupNumber] = new List<int>();
  21.             }
  22.             res[groupNumber].Add(test[i]);
  23.         }
  24.         
  25.         foreach (var ls in res) {
  26.             Console.Write("\n");
  27.             foreach (var item in ls.Value) {
  28.                 Console.Write(item + ", ");
  29.             }
  30.         }
  31.         
  32.     }
  33.    
  34.     public static bool NumsWithinRange(int x, int y, int range) {
  35.         int diff = Math.Abs(x - y);
  36.         return diff <= range;
  37.     }
  38. }

  39. public class UnionFind {
  40.     public int[] parent, rank;
  41.     public int groups;
  42.    
  43.     public UnionFind(int n) {
  44.         parent = new int[n];
  45.         rank = new int[n];
  46.         for (int i=0; i<n; i++) {
  47.             parent[i] = i;
  48.         }
  49.         groups = n;
  50.     }
  51.    
  52.     public void Union(int x, int y) {
  53.         int px = Find(x);
  54.         int py = Find(y);
  55.         if (px == py) return;
  56.         
  57.         if (rank[px] > rank[py]) {
  58.             parent[py] = px;
  59.             rank[px]++;
  60.         } else {
  61.             parent[px] = py;
  62.             rank[py]++;
  63.         }
  64.         groups--;
  65.     }
  66.    
  67.     public int Find(int x) {
  68.         while (parent[x] != x) {
  69.             return Find(parent[x]);
  70.         }
  71.         return x;
  72.     }
  73. }
复制代码
[/i][/i][/i]

评分

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

查看全部评分

回复

使用道具 举报

地里匿名用户
🔗
匿名用户-Z5KOW  2021-11-8 04:16:06
第一题lc见过,死活找不到,有大神share下题号么
回复

使用道具 举报

全局:
结合大家的答案我写个O(N)比较糙
  1. import math
  2. def find_group(numbers, k):
  3.     min_val = min(numbers)
  4.     max_val = max(numbers)
  5.     num_bucket = math.ceil(1.0*(max_val-min_val+1)/(k+1))
  6.     print(num_bucket)
  7.     buckets = [[] for _ in range(num_bucket)]

  8.     for num in numbers:
  9.         bucket_index = (num-min_val)//(k+1)
  10.         # can add local_min and local_max for each bucket
  11.         buckets[bucket_index].append(num)
  12.     print(buckets)
  13.     res = []
  14.     i = 0
  15.     while i < num_bucket:
  16.         if len(buckets[i]) == 0:
  17.             i += 1
  18.             continue

  19.         tmp = []
  20.         if i+1 < num_bucket and len(buckets[i+1]) != 0 and max(buckets[i+1])-min(buckets[i]) <= k:
  21.             tmp += buckets[i]
  22.             tmp += buckets[i+1]
  23.             i += 2
  24.         else:
  25.             tmp += buckets[i]
  26.             i += 1
  27.         res.append(tmp)
  28.    
  29.     return res
  30.         
  31. nums = [4, 3, 7, 6, 9, 2, 10, 11, 13, 20]
  32. k = 2
  33. print(find_group(nums, k))
复制代码

评分

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

查看全部评分

回复

使用道具 举报

🔗
zjspm 2021-12-7 14:13:10 | 只看该作者
全局:
本帖最后由 zjspm 于 2021-12-6 22:26 编辑
木华黎_Reid 发表于 2021-12-1 15:01
结合大家的答案我写个O(N)比较糙

非常感谢你的答案!!大赞!!
回复

使用道具 举报

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

本版积分规则

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