LintCode Wood Cut java solution
来源:互联网 发布:华为运营商网络业务 编辑:程序博客网 时间:2024/05/17 01:44
题目要求:
Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get from the n pieces of wood? Given L & k, return the maximum length of the small pieces.
Notice
You couldn’t cut wood into float length.
If you couldn’t get >= k pieces, return 0
Example
For L=[232, 124, 456], k=7, return 114.
解题思路:题目中的最大的长度是数组中的最长数字, 最短是1.
public class Solution { public int woodCut(int[] L, int k) { if (L == null || L.length == 0) { return 0; } int start = 1; int end = findMax(L); while (start + 1 < end) { int mid = start + (end - start) / 2; if (countWood(L, mid) == k) { start = mid; } else if (countWood(L, mid) < k) { end = mid; } else { start = mid; } } if (countWood(L, end) <= k) { return end; } if (countWood(L, start) <= k) { return start; } return 0; } private int findMax(int[] nums) { int max = nums[0]; for (int i = 0; i < nums.length; i++) { max = (max < nums[i]) ? nums[i] : max; } return max; } private int countWood(int[] nums, int len) { int sum = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i] / len; } return sum; }}
0 0
- LintCode Wood Cut java solution
- lintcode:Wood Cut
- [LintCode]Wood Cut
- lintcode之Wood-Cut问题
- wood cut
- wood-cut
- Wood Cut
- Wood Cut
- Binary Search---Wood Cut
- #183 Wood Cut
- LintCode Minimum Subtree java solution
- 二分查找——Wood Cut
- 02分治算法-03wood-cut
- wood
- 183.Wood Cut-木材加工(中等题)
- Lintcode 74 First Bad Version solution 题解
- Lintcode 74 First Bad Version solution 题解
- LeetCode78. Subsets java solution
- 如何利用keil C实现单片机中断功能(interrupt,using关键字的用法)
- Spring boot 实现json和jsonp格式数据,接口共用
- 字符串
- 常用 Git 命令清单
- 2,Qt第一个程序(Hello World!)
- LintCode Wood Cut java solution
- SQL优化(索引、物化视图、分区)
- Arch Linux
- Android动态创建ListView视图,动态增加和删除Item.
- 浅谈计算机内存中浮点数的表示
- pat-b1004. 成绩排名 (20)
- 简单实现ToolBar阴影效果(完美适配安卓5.0以下)
- android6.0的写入SD卡的权限申请
- Android 百度地图sdk集成--超超超详细版