由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - What's the algorithm to solve this problem?
相关主题
一道java面试题这道facebook的题怎么解
问道算法题。问一下这样的情况很常见吗
Amazon面试题offer报告 (附带找工作感言)
一道rf的面试题分享面试题目
请教一题,关于intervalgraph如何找最短路径?
请教一个API设计的面试题about DFS
贡献Rocket Fuel 4 hour online teststartup招data scientist, software engineer
急求rocket fuel 3小时的online test!!!有HP公司的么(加州),求内推工作
相关话题的讨论汇总
话题: jobs话题: job话题: int话题: what话题: represents
进入JobHunting版参与讨论
1 (共1页)
d****n
发帖数: 233
1
you are provided a String array like so {"1 5 40", "6 7 20","4 7 40"}
Each row item represents the "JOB" you can do..
first number represents the Start time and Second number represents the end
time.. and third number represents the money you will get if you do this job
..
In the example above, obviously you cannot do all the three jobs, because
jobs 1 and 3 clash..so you can either do Jobs 1 and 2 or you can do job 3..
It is profittable to do Jobs 1 and 2 as that will give you 20+40, 60$.
Your
B*****t
发帖数: 335
2
two approaches. first sort the jobs according the starting time.
1) backtracking + branch pruning
2) dp, f[i][j] = max(f[k][j], f[k-1][t.start[i]] + value[i]) 0<=k=t.
end[i]; greedy search the max value in above dp equation would optimize it.
d****n
发帖数: 233
3
BlueAnt, can you write more details? Thx.
b********h
发帖数: 119
4
A possible DP solution could be formulated as follow:
1. sort the jobs by finishing time
2. define D[i] as the maximum money you can make by considering jobs 1 to i.
define job i and j compatible if their time don't overlap.Then
D[i] = max{D[k]+Money[i], D[i-1]}, where k is the latest job that is
compatible to i
d****n
发帖数: 233
5
Thanks bladesmith, your DP solution looks correct to me.
I also worked out a solution without using DP.
I paste it here and hope someone can correct me if I'm wrong, some basic
validations are skipped:
typedef struct Job {
int payAmount;
int startTime; // [0 23]
int endTime // [1, 24] endtime >= startime+1;
}
// This function returns the max amount of payment a worker
// can make from the jobs
int FindMaxTotalPayment(Job *jobs, int N)
{
//
// (Skipped) 1.
// Sort

【在 b********h 的大作中提到】
: A possible DP solution could be formulated as follow:
: 1. sort the jobs by finishing time
: 2. define D[i] as the maximum money you can make by considering jobs 1 to i.
: define job i and j compatible if their time don't overlap.Then
: D[i] = max{D[k]+Money[i], D[i-1]}, where k is the latest job that is
: compatible to i

b********h
发帖数: 119
6
Your implementation doesn't seem right to me. Try the following test case:
"1,2,2"
"1,3,3"
"1,4,4"
The expected result is 4. Your algorithm returns 7, i think. I could be
wrong.
d****n
发帖数: 233
7
I verified it's correct:
int _tmain(int argc, _TCHAR* argv[])
{
Job jobs[] = { {2, 1, 2}, {3, 1, 3}, {4, 1, 4} };
int maxPay = FindMaxTotalPayment(jobs, 3);
assert(maxPay == 4);
return 0;
}

【在 b********h 的大作中提到】
: Your implementation doesn't seem right to me. Try the following test case:
: "1,2,2"
: "1,3,3"
: "1,4,4"
: The expected result is 4. Your algorithm returns 7, i think. I could be
: wrong.

B*****t
发帖数: 335
8
代码才是硬道理! 贴个我的code,可能有bug,欢迎指出。另外,DFS没有优化。
#include
#include
using namespace std;
const int N = 10000, MaxTimeValue = 100000;
class Job {
public:
int st, ed, money;
}nd[N];
int n, maxV = 0x80000000;
bool cmp(const Job &a, const Job &b) {
return a.st }
void dfs(int ix, int endTime, int tot) {
if(ix==n) {
if(tot>maxV) maxV = tot;
return;
}
if(nd[ix].st>=endTime) dfs(ix+1, max(nd[ix].ed,
endTime), tot+nd[ix].mone

【在 d****n 的大作中提到】
: BlueAnt, can you write more details? Thx.
1 (共1页)
进入JobHunting版参与讨论
相关主题
有HP公司的么(加州),求内推工作请教一题,关于interval
Topologicl Sort或类似graph algorithms面试时候题目多吗?请教一个API设计的面试题
一道linkedin常见题,我的答案对吗?贡献Rocket Fuel 4 hour online test
这道题的follow up不会做,感觉跪了,求大牛指教急求rocket fuel 3小时的online test!!!
一道java面试题这道facebook的题怎么解
问道算法题。问一下这样的情况很常见吗
Amazon面试题offer报告 (附带找工作感言)
一道rf的面试题分享面试题目
相关话题的讨论汇总
话题: jobs话题: job话题: int话题: what话题: represents