由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道算法题--Find the first circular tour that visits all petrol pumps
相关主题
问个ms的面试题about how to test a calculator program on computer
探讨一题分享一道trading firm的code screen,只能用c++
别了,TN的最后一贴Fresh CS PhD, MS 面经
FMC has opening for 3 positions (转载)面试题目
有没有大牛看看这题目咋办讨论个subarray sum的变种问题
问个编程题面试问题
怎么计算一个网站过去5min的访问量?Interview question from Yahoo
急问有没有面试过bloomberg的senior calculations programmer的?问一题
相关话题的讨论汇总
话题: petrol话题: int话题: pump
进入JobHunting版参与讨论
1 (共1页)
x*****0
发帖数: 452
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).
这道似乎是一经典老题。
大家知道怎么解的么?
l***i
发帖数: 1309
2
这个看起来像算法课的作业题
j*****y
发帖数: 1071
3
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

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一题有没有大牛看看这题目咋办
问一道java题问个编程题
算法面试的疑惑怎么计算一个网站过去5min的访问量?
a singly-linked list question急问有没有面试过bloomberg的senior calculations programmer的?
问个ms的面试题about how to test a calculator program on computer
探讨一题分享一道trading firm的code screen,只能用c++
别了,TN的最后一贴Fresh CS PhD, MS 面经
FMC has opening for 3 positions (转载)面试题目
相关话题的讨论汇总
话题: petrol话题: int话题: pump