183.Wood Cut【hard】
183.Wood Cut【hard】
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.
For L=[232, 124, 456], k=7, return 114.
O(n log Len), where Len is the longest length of the wood.
这个题一上来一点思路没有,参考:https://algorithm.yuanbin.me/zh-hans/binary_search/wood_cut.html里面的思路
这道题要直接想到二分搜素其实不容易,但是看到题中 Challenge 的提示后你大概就能想到往二分搜索上靠了。首先来分析下题意,题目意思是说给出 n 段木材L[i], 将这 n 段木材切分为至少 k 段,这 k 段等长,求能从 n 段原材料中获得的最长单段木材长度。以 k=7 为例,要将 L 中的原材料分为7段,能得到的最大单段长度为114, 232/114 = 2, 124/114 = 1, 456/114 = 4, 2 + 1 + 4 = 7。
理清题意后我们就来想想如何用算法的形式表示出来,显然在计算如2, 1, 4等分片数时我们进行了取整运算,在计算机中则可以使用下式表示:
其中 l 为单段最大长度,显然有 1 ≤ l ≤ max(L[i]). 单段长度最小为1,最大不可能超过给定原材料中的最大木材长度。
Warning 注意求和与取整的顺序,是先求
L[i]/l的单个值,而不是先对L[i]求和。
分析到这里就和题 sqrt(x) 差不多一样了,要求的是 l 的最大可能取值,同时 l 可以看做是从有序序列[1, max(L[i])]的一个元素,典型的二分搜素!
代码参考了:http://www.jiuzhang.com/solution/wood-cut/
解法一:
public class Solution {
/**
*@param L: Given n pieces of wood with length L[i]
*@param k: An integer
*return: The maximum length of the small pieces.
*/
public int woodCut(int[] L, int k) {
int max = ;
for (int i = ; i < L.length; i++) {
max = Math.max(max, L[i]);
}
// find the largest length that can cut more than k pieces of wood.
int start = , end = max;
while (start + < end) {
int mid = start + (end - start) / ;
if (count(L, mid) >= k) {
start = mid;
} else {
end = mid;
}
}
if (count(L, end) >= k) {
return end;
}
if (count(L, start) >= k) {
return start;
}
return ;
}
private int count(int[] L, int length) {
int sum = ;
for (int i = ; i < L.length; i++) {
sum += L[i] / length;
}
return sum;
}
}
对于上面发现还有可以优化的地方,那就是我们二分找长度的时候只需要找所有木块里面最短的即可,就是所谓的木桶原理,那么end上界又可以进一步减少。
解法二:
class Solution {
public:
/*
* @param L: Given n pieces of wood with length L[i]
* @param k: An integer
* @return: The maximum length of the small pieces
*/
int woodCut(vector<int> &L, int k) {
if (L.empty() || k <= ) {
return ;
}
//get min
int min = INT_MIN;
for (int i = ; i < L.size(); ++i) {
min = (min < L[i] ? L[i] : min);
}
int start = ;
int end = min;
while (start + < end) {
int mid = start + (end - start) / ;
if (cal(L, mid) >= k) {
start = mid;
}
else {
end = mid;
}
}
if (cal(L, end) >= k) {
return end;
}
else if (cal(L, start) >= k) {
return start;
}
else {
return ;
}
}
int cal(vector<int> & L, int len) {
int sum = ;
for (int i = ; i < L.size(); ++i) {
sum += L[i] / len;
}
return sum;
}
};
183.Wood Cut【hard】的更多相关文章
- hdu 6214 : Smallest Minimum Cut 【网络流】
题目链接 ISAP写法 #include <bits/stdc++.h> using namespace std; typedef long long LL; namespace Fast ...
- 【转载】用C#编写一个简单的记事本
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 【AOP】Spring AOP基础 + 实践 完整记录
Spring AOP的基础概念 ============================================================= AOP(Aspect-Oriented Pr ...
- 【转】linux中的cut/tr/join/split/xargs命令
1. cut命令 cut命令用于从文件或者标准输入中读取内容并截取每一行的特定部分并送到标准输出. 截取的方式有三种:一是按照字符位置,二是按照字节位置,三是使用一个分隔符将一行分割成多个field, ...
- POJ 2914 Minimum Cut【最小割 Stoer-Wangner】
题意:求全局最小割 不能用网络流求最小割,枚举举汇点要O(n),最短增广路最大流算法求最大流是O(n2m)复杂度,在复杂网络中O(m)=O(n2),算法总复杂度就是O(n5):就算你用其他求最大流的算 ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- LG3690 【模板】Link Cut Tree (动态树)
题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...
- AC日记——【模板】Link Cut Tree 洛谷 P3690
[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...
- HDU 6214 Smallest Minimum Cut 【网络流最小割+ 二种方法只能一种有效+hdu 3987原题】
Problem Description Consider a network G=(V,E) with source s and sink t . An s-t cut is a partition ...
随机推荐
- Python中内置的日志模块logging用法详解
logging模块简介 Python的logging模块提供了通用的日志系统,可以方便第三方模块或者是应用使用.这个模块提供不同的日志级别,并可以采用不同的方式记录日志,比如文件,HTTP GET/P ...
- 1.1(java学习笔记) 面向过程与面向对象
面向过程思考时,我们会先思考具体的步骤,第一步走什么,第二步做什么. 比如电脑新建文件夹,第一步:打开电脑 第二步:按下鼠标右键. 第三步:找到新建选项 第四步:点击新建选项下的文件夹 c语言是典型的 ...
- 输入格式CombineFileInput
此输入格式的作用就是可以将来自多个不同文件的物理块作为一个split,然后由一个map进行处理. http://www.blogjava.net/shenh062326/archive/2012/07 ...
- iOS 读取Json 代码
保存一下iOS 读取Json的代码,留着以后Copy用,哈哈. NSString* path = [[NSBundle mainBundle] pathForResource: @"Sand ...
- spark checkpoint机制
首先rdd.checkpoint()本身并没有执行任何的写操作,只是做checkpointDir是否为空,然后生成一个ReliableRDDCheckpointData对象checkpointData ...
- mysql 5.7 安装手册(for linux)
1.下载和解压mysql数据库 wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.9-linux-glibc2.5-x86_6 ...
- Python 获取图片格式及像素宽高信息
# coding: utf8 from PIL import Image img = Image.open("img.jpg") print img.sizeprint img.f ...
- 深入解析淘宝Diamond之客户端架构
转载:http://blog.csdn.net/u013970991/article/details/52088350 一.什么是Diamond diamond是淘宝内部使用的一个管理持久配置的系统, ...
- Mysql 中文中繁杂的字 插入报错的 解决方案
首先 数据库默认编码选用 utf8 连接字符串也相应改成utf8,不能是gb2312
- java集合框架小结
总结例如以下: 1.假设要求线程安全的, 使用Vector.Hashtable 2.假设不要求线程安全,应该使用ArrayList.LinkedList.HashMap 3.假设要求有映射关系,键值对 ...