📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: chenzhan171
跳转到指定楼层
上一主题 下一主题
收起左侧

狗家电面

🔗
 楼主| chenzhan171 2016-6-7 12:36:49 | 只看该作者
全局:
handsomecool 发表于 2016-6-7 12:34
第二天sorted greedy approach

对, 就是这样搞。
回复

使用道具 举报

🔗
robinali 2016-6-7 12:50:59 | 只看该作者
全局:
题二应该就是一次循环,记录所读到的最大子序列就好了吧。 :D
回复

使用道具 举报

🔗
dietpepsi 2016-6-10 00:04:57 | 只看该作者
全局:
  1. public class Solution {
  2.     public int maxElementsInOne(double[] arr) {
  3.         int n = arr.length;
  4.         // defensive copy
  5.         double[] sorted = Arrays.copyOf(arr, n);
  6.         Arrays.sort(sorted);

  7.         int ans = 0;
  8.         for (int i = 0, j = 0; j < n; ++j) {
  9.             if (j < n && sorted[j] < sorted[i] + 1) continue;
  10.             ans = Math.max(ans, j - i++);
  11.         }
  12.         return ans;
  13.     }

  14.     public static void main(String... args) {
  15.         Solution sol = new Solution();
  16.         int ans = sol.maxElementsInOne(new double[]{1.0, 1.3, 1.5, 2.3, 3.5});
  17.         System.out.println(ans);
  18.     }
  19. }
复制代码
回复

使用道具 举报

🔗
dietpepsi 2016-6-10 00:06:13 | 只看该作者
全局:

10行多余j<n,if (sorted[j] < sorted[i] + 1) continue;
回复

使用道具 举报

🔗
handsomecool 2016-6-10 02:36:21 | 只看该作者
全局:

好像bug, 比如[1.1,1.2,1.3,1.4,1.5], ans 最后应该再update一次
回复

使用道具 举报

🔗
readman 2016-6-10 03:00:37 | 只看该作者
全局:
handsomecool 发表于 2016-6-7 12:34
第二天sorted greedy approach

那个right++ 是怎么来的...
回复

使用道具 举报

🔗
handsomecool 2016-6-10 03:08:42 | 只看该作者
全局:
readman 发表于 2016-6-10 03:00
那个right++ 是怎么来的...

本来是两行:
  1. ans = max(ans, right-left+1);
  2.         right++;
复制代码
回复

使用道具 举报

🔗
jjustc 2016-6-10 04:59:08 | 只看该作者
全局:
问一下楼主 第一题 如果 是 1->2->3->4->1 这种环需要找出来吗?
回复

使用道具 举报

🔗
dietpepsi 2016-6-10 06:09:51 | 只看该作者
全局:
handsomecool 发表于 2016-6-10 02:36
好像bug, 比如[1.1,1.2,1.3,1.4,1.5], ans 最后应该再update一次

你说的太对了....
  1. public class Solution {
  2.     public int maxElementsInOne(double[] arr) {
  3.         int n = arr.length;
  4.         // defensive copy
  5.         double[] copy = Arrays.copyOf(arr, n);
  6.         Arrays.sort(copy);
  7.         int ans = 0, i = 0;
  8.         for (int j = 0; j < n; ++j) {
  9.             if (copy[j] < copy[i] + 1) continue;
  10.             ans = Math.max(ans, j - i++);
  11.         }
  12.         return Math.max(ans, n - i);
  13.     }

  14.     public static void main(String... args) {
  15.         Solution sol = new Solution();
  16.         int ans = sol.maxElementsInOne(new double[]{1.0, 1.3, 1.5, 1.7, 1.9});
  17.         System.out.println(ans);
  18.     }
  19. }
复制代码
回复

使用道具 举报

🔗
 楼主| chenzhan171 2016-6-10 07:51:29 | 只看该作者
全局:
jjustc 发表于 2016-6-10 04:59
问一下楼主 第一题 如果 是 1->2->3->4->1 这种环需要找出来吗?

不用。字数字数字数
回复

使用道具 举报

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

本版积分规则

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