12
返回列表 发新帖
楼主: cherry_picker
跳转到指定楼层
上一主题 下一主题
收起左侧

FB电面

🔗
fay1224 2019-9-10 05:02:47 | 只看该作者
本楼:
全局:
wood cut
回复

使用道具 举报

🔗
 楼主| cherry_picker 2019-9-10 11:33:32 | 只看该作者
全局:
oml 发表于 2019-9-9 21:01
should be this one

Given an int array wood representing the length of n pieces of wood and an int ...

是的!字数字数
回复

使用道具 举报

🔗
梅亮 2019-9-10 12:35:39 | 只看该作者
全局:
binary search, trial and error, 类似于koko eating bananas

  1.     public static int woodcut(int[] woods, int target) {
  2.         if (woods == null || woods.length == 0) return -1;
  3.         int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, sum = 0;
  4.         for (int i = 0; i < woods.length; ++i) {
  5.             min = Math.min(min,woods[i]);
  6.             max = Math.max(max,woods[i]);
  7.             sum += woods[i];
  8.         }
  9.         if (target > sum || min <= 0 ) return -1;
  10.         int l = min, r = max;
  11.         while (l < r) {
  12.             int m = (l+r)/2 + 1;
  13.             int count = countWood(woods,m);
  14.             if (count < target) {
  15.                 r = m - 1;
  16.             } else {
  17.                 l = m;
  18.             }
  19.         }
  20.         return countWood(woods,l) >= target ? l : -1;

  21.     }

  22.     private static int countWood(int[] woods, int cutLen) {
  23.         int count = 0;
  24.         for (int wood : woods) {
  25.             count += wood/cutLen;
  26.         }
  27.         return count;
  28.     }
复制代码
[/i][/i][/i]

评分

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

查看全部评分

回复

使用道具 举报

🔗
Joey_Hu 2019-9-10 14:06:14 | 只看该作者
全局:
梅亮 发表于 2019-9-10 12:35
binary search, trial and error, 类似于koko eating bananas

[mw_shl_code=java,true]    public stati ...

代码有点问题。左边要从1开始,而不是从数组中的最小数字开始。
回复

使用道具 举报

🔗
minggo 2019-9-10 17:28:01 | 只看该作者
全局:
是,左边应该从 1 开。

回复

使用道具 举报

🔗
梅亮 2019-9-10 20:27:26 | 只看该作者
全局:
minggo 发表于 2019-9-10 17:28
是,左边应该从 1 开。

确实,已经改过来了
回复

使用道具 举报

🔗
oml 2019-9-12 08:18:29 | 只看该作者
全局:
leetcode 875蕾丝

评分

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

查看全部评分

回复

使用道具 举报

🔗
valentin508 2019-9-26 16:01:40 | 只看该作者
全局:
  1. class Solution {
  2.     public int cutWood(int[] wood, int k){
  3.         if(wood.length == 0 || k == 0) return 0;
  4.         int left = 1, right = Integer.MAX_VALUE;
  5.         int res = 0;
  6.         if(!canCut(wood, left, k)) return 0;
  7.         while(left <= right){
  8.             int mid = left + (right - left) / 2;
  9.             boolean valid = canCut(wood, mid, k);
  10.             if(valid){
  11.                 left = mid + 1;
  12.                 res = mid;
  13.             }
  14.             else right = mid - 1;
  15.         }
  16.         return res;
  17.     }
  18.    
  19.     private boolean canCut(int[] woods, int n, int k){
  20.         int count = 0;
  21.         for(int wood: woods) count += wood;
  22.         return count / n >= k;
  23.     }
  24. }
复制代码


这边写了个稍微concise的版本
回复

使用道具 举报

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

本版积分规则

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