avatar
寂寞的订书钉城市# Joke - 肚皮舞运动
x*0
1
Suppose there is a circle. There are n petrol pumps on that circle. You are
given two sets of data.
1. The amount of petrol that petrol pump will give.
2. Distance from that petrol pump to the next petrol pump.
Calculate the first point from where a truck will be able to complete the
circle (The truck will stop at each petrol pump and it has infinite capacity
). Expected time complexity is O(n). Assume for 1 litre petrol, the truck
can go 1 unit of distance.
For example, let there be 4 petrol pumps with amount of petrol and distance
to next petrol pump value pairs as {4, 6}, {6, 5}, {7, 3} and {4, 5}. The
first point from where truck can make a circular tour is 2nd petrol pump.
Output should be “start = 1″ (index of 2nd petrol pump).
这道似乎是一经典老题。
大家知道怎么解的么?
avatar
x*o
2
h
avatar
l*i
3
这个看起来像算法课的作业题
avatar
H*g
4
good
avatar
j*y
5
int indexToStart(pair A[], int n)
{
int lowestGasInTheTank = 0;
int gasInTheTank = 0;
int index = 0;
for(int i = 1; i < n ; ++i)
{
gasInTheTank += A[i - 1].first - A[i - 1].second;
if(lowestGasInTheTank > gasInTheTank)
{
lowestGasInTheTank = gasInTheTank;
index = i;
}
}
gasInTheTank += A[n - 1].first - A[n - 1].second;
if(lowestGasInTheTank > gasInTheTank)
{
lowestGasInTheTank = gasInTheTank;
index = 0;
}
return index;
}

are
capacity
distance

【在 x*****0 的大作中提到】
: Suppose there is a circle. There are n petrol pumps on that circle. You are
: given two sets of data.
: 1. The amount of petrol that petrol pump will give.
: 2. Distance from that petrol pump to the next petrol pump.
: Calculate the first point from where a truck will be able to complete the
: circle (The truck will stop at each petrol pump and it has infinite capacity
: ). Expected time complexity is O(n). Assume for 1 litre petrol, the truck
: can go 1 unit of distance.
: For example, let there be 4 petrol pumps with amount of petrol and distance
: to next petrol pump value pairs as {4, 6}, {6, 5}, {7, 3} and {4, 5}. The

avatar
s*a
6
寂寞爆了
avatar
q*l
7
摆了这么一堆订书针的人寂寞爆了
avatar
c*7
8
真的摆的就是寂寞
avatar
r*e
9
微观世界的精彩

【在 x****o 的大作中提到】
: h
avatar
z*n
10
明明是蛋疼。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。