楼主: 堕落的猴子
跳转到指定楼层
上一主题 下一主题
收起左侧

Facebook Intern 电面一轮 + Onsite一轮

🔗
ccgogo123 2015-6-26 04:38:40 | 只看该作者
全局:
说说我的想法:
Sort the input and then think about the following two cases:
1. k is larger than the length or all elements are negative or positive
Compute the product of the k rightmost elements
2. Otherwise
Two pointers say i and j initially at the beginning and the end respectively. Traverse the sorted array until we find k numbers and during the traversal, we keep track of the largest product met so far. Every step, we compare a[i] * a[i + 1] with a[j] and pick up the larger one and multiply it by the largest product so far. Update the number of elements we choose so far. Here is the code:
  1. public long findLeastProduct(int[] nums, int k) {
  2.         long product = 1;

  3.         Arrays.sort(nums);
  4.         //special case when k is larger than the length or all elements are negative or positive
  5.         if (k >= nums.length || nums[nums.length - 1] <= 0 || nums[0] >= 0) {
  6.             for (int i = nums.length - 1; i >= Math.max(0, nums.length - k); i--) {
  7.                 product *= nums[i];
  8.             }
  9.         }else {
  10.             int l = 0, r = nums.length - 1;
  11.             while (k > 0) {
  12.                 if (nums[l] * nums[l + 1] > nums[r]) {
  13.                     product *= nums[l] * nums[l + 1];
  14.                     l += 2;
  15.                     k -= 2;
  16.                 }else {
  17.                     product *= nums[r--];
  18.                     k--;
  19.                 }
  20.             }
  21.             if (product < 0) product = product / nums[r + 1] * nums[l] * nums[l + 1];
  22.         }
  23.         return product;
  24.     }

  25.     @Test
  26.     public void test() {
  27.         int[] nums = {-1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -10000};
  28.         System.out.println(findLeastProduct(nums, 3));
  29.     }
复制代码


补充内容 (2015-6-26 04:40):
Modify the 22 line:
if (product < 0) product = product / (nums[r + 1] * nums[l]) * nums[l + 1];
回复

使用道具 举报

🔗
ccgogo123 2015-6-26 04:53:19 | 只看该作者
全局:
晕菜,刚刚查处了bug,对上个帖子做个更正:
  1. public long findLeastProduct(int[] nums, int k) {
  2.         long product = 1;

  3.         Arrays.sort(nums);
  4.         //special case when k is larger than the length or all elements are negative or positive
  5.         if (k >= nums.length || nums[nums.length - 1] <= 0 || nums[0] >= 0) {
  6.             for (int i = nums.length - 1; i >= Math.max(0, nums.length - k); i--) {
  7.                 product *= nums[i];
  8.             }
  9.         }else {
  10.             int l = 0, r = nums.length - 1;
  11.             while (k > 0) {
  12.                 if (k == 1) {
  13.                     if (nums[r] >= 0) product *= nums[r];
  14.                     else product = product / nums[r + 1] * nums[l] * nums[l + 1];
  15.                     k--;
  16.                 } else if (nums[l] * nums[l + 1] > nums[r]) {
  17.                     product *= nums[l] * nums[l + 1];
  18.                     l += 2;
  19.                     k -= 2;
  20.                 }else {
  21.                     product *= nums[r--];
  22.                     k--;
  23.                 }
  24.             }
  25.         }
  26.         return product;
  27.     }

  28.     @Test
  29.     public void test() {
  30.         int[] nums = {-1, -2, -3, -4, 5, 6, 7};
  31.         System.out.println(findLeastProduct(nums, 4));
  32.     }
复制代码
回复

使用道具 举报

🔗
 楼主| 堕落的猴子 2015-8-10 01:22:17 | 只看该作者
全局:
ccgogo123 发表于 2015-6-26 04:53
晕菜,刚刚查处了bug,对上个帖子做个更正:

Exactly,现在回头来看,这题大概就一个普通的算法题,不过还是很巧妙的就是。

这种先排序的思路,都快被各种O(n)的题目给挤压没了。
回复

使用道具 举报

🔗
xiaoma318 2015-8-24 07:00:08 | 只看该作者
全局:
绝对值排序挺好思考的感觉~排好后取 K 个最大的,如果负数个数是 even,那就完事了;如果是 odd,那么两种方法,减少一个负数,或者增加一个负数,两种情况比较下哪个大就好了。
回复

使用道具 举报

🔗
frederickyl 2015-8-24 10:51:53 | 只看该作者
全局:
ccgogo123 发表于 2015-6-26 04:53
晕菜,刚刚查处了bug,对上个帖子做个更正:

有个test case不对,{-7, -7, -2, -1, 0, 0, 9}, 结果应该是98(-7*-7*-2*-1), 但你的程序结果是0
回复

使用道具 举报

🔗
CiDut 2015-10-16 10:57:48 | 只看该作者
全局:
ccgogo123 发表于 2015-6-26 04:53
晕菜,刚刚查处了bug,对上个帖子做个更正:

这个代码不能解决:-7 ~ -1的case
回复

使用道具 举报

🔗
JamesJi 2015-10-23 01:48:53 | 只看该作者
全局:
不用swap用write
这句话猴哥可以详细解释一下吗
回复

使用道具 举报

🔗
 楼主| 堕落的猴子 2015-10-23 02:02:20 | 只看该作者
全局:
JamesJi 发表于 2015-10-23 01:48
不用swap用write
这句话猴哥可以详细解释一下吗

就是直接A[l] = A[r],A[r]上什么值留下来无所谓了。

评分

参与人数 1大米 +3 收起 理由
frank11118 + 3 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
invinsibility 2016-2-12 09:39:01 | 只看该作者
全局:
xiaoma318 发表于 2015-8-24 07:00
绝对值排序挺好思考的感觉~排好后取 K 个最大的,如果负数个数是 even,那就完事了;如果是 odd,那么两种 ...

我和你想的方法一样
回复

使用道具 举报

🔗
frank11118 2016-2-14 11:18:08 | 只看该作者
全局:
堕落的猴子 发表于 2015-10-23 02:02
就是直接A[l] = A[r],A[r]上什么值留下来无所谓了。

請問 A[r] 不用覆寫成 0 嗎?
回复

使用道具 举报

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

本版积分规则

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