由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 新题gas station,献丑了,没过一个case,帮看看
相关主题
Leetcode Copy List with Random Pointer Runtime Error?这段代码在leetcode上面跑不了??
哪位大侠帮我看看这个codeleetcode 上通不过
find the first missing positive numberleetcode是不是最近有点问题?
请教一下leetcode gas station大家帮忙分析下leetcode一个题目的复杂度
帮俺看一下代码DP+DFS为什么过不了work break II 那个大case : aaaaaaa...java 基本问题
Surrounded Regions, dfs solution. 过不了online testcareercup上看的一道题
divide two integerssingleton without static?
lc 上面4 sum的时间复杂度要求多少?讨论个subarray sum的变种问题
相关话题的讨论汇总
话题: reach话题: int话题: gas话题: return话题: cost
进入JobHunting版参与讨论
1 (共1页)
t******d
发帖数: 1383
1
说是runtime error,有一个很大的case没过去。runtime error是说我的index过了界
么?还是说运行速度不够?请帮我看看。另外,reach+1改成 reach++,是一样的么
?把reach加了1之后的值传给新call的isEnough.我知道++reach,肯定是和reach+1等
效可以的。
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
// Note: The Solution object is instantiated only once and is reused
by each test case.

for (int i = 0; i < gas.length;i++) {
if (isEnough(gas, cost, i, 0, i))
return i;
}

return -1;
}

public boolean isEnough(int[] gas, int[] cost, int start, int leftover,
int reach) {
if (reach == gas.length)
reach -= gas.length;
if (reach == start -1 || (start == 0 && reach == gas.length - 1)) {
if (leftover + gas[reach] >= cost[reach])
return true;
else
return false;
}
if (reach == start) {
if (gas[reach] < cost[reach])
return false;
else
return isEnough(gas, cost, start, gas[reach]-cost[reach],
reach+1);
}

if (leftover + gas[reach] >= cost[reach])
return isEnough(gas, cost, start, leftover + gas[reach]-cost[
reach], reach+1);
else return false;
}
}
c*****o
发帖数: 1702
2
run time error基本是segmentation error
t******d
发帖数: 1383
3
貌似小case过了。我也觉得我改用dp做的。

【在 c*****o 的大作中提到】
: run time error基本是segmentation error
t**********r
发帖数: 2153
4
自己debug一下呗
t******d
发帖数: 1383
5
那个没过的case,很大。2个数组,放main里面,都超过了size,jvm跑都不让跑!

【在 t**********r 的大作中提到】
: 自己debug一下呗
t******d
发帖数: 1383
6
说是runtime error,有一个很大的case没过去。runtime error是说我的index过了界
么?还是说运行速度不够?请帮我看看。另外,reach+1改成 reach++,是一样的么
?把reach加了1之后的值传给新call的isEnough.我知道++reach,肯定是和reach+1等
效可以的。
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
// Note: The Solution object is instantiated only once and is reused
by each test case.

for (int i = 0; i < gas.length;i++) {
if (isEnough(gas, cost, i, 0, i))
return i;
}

return -1;
}

public boolean isEnough(int[] gas, int[] cost, int start, int leftover,
int reach) {
if (reach == gas.length)
reach -= gas.length;
if (reach == start -1 || (start == 0 && reach == gas.length - 1)) {
if (leftover + gas[reach] >= cost[reach])
return true;
else
return false;
}
if (reach == start) {
if (gas[reach] < cost[reach])
return false;
else
return isEnough(gas, cost, start, gas[reach]-cost[reach],
reach+1);
}

if (leftover + gas[reach] >= cost[reach])
return isEnough(gas, cost, start, leftover + gas[reach]-cost[
reach], reach+1);
else return false;
}
}
c*****o
发帖数: 1702
7
run time error基本是segmentation error
t******d
发帖数: 1383
8
貌似小case过了。我也觉得我改用dp做的。

【在 c*****o 的大作中提到】
: run time error基本是segmentation error
t**********r
发帖数: 2153
9
自己debug一下呗
t******d
发帖数: 1383
10
那个没过的case,很大。2个数组,放main里面,都超过了size,jvm跑都不让跑!

【在 t**********r 的大作中提到】
: 自己debug一下呗
b********r
发帖数: 620
11
贴个自己写的,所有test都pass了。不知道怎么用地跪。恳请大牛批评指正。
public static int selectGasStation(int[] stationGasAvailability, int[]
requiredGasAvailabilityForDriving) {
if (stationGasAvailability.length == 0 ||
requiredGasAvailabilityForDriving.length == 0) {
return -1;
} else if (stationGasAvailability.length == 1) {
// if there is only one station we can always do
return 0;
}
// check total availability vs total requirement
int totalAvailability = 0;
int totalRequirement = 0;
for (int i = 0;i < stationGasAvailability.length;i ++) {
totalAvailability += stationGasAvailability[i];
totalRequirement += requiredGasAvailabilityForDriving[i];
}
if (totalAvailability < totalRequirement) {
// total availability must be greater than or equal to
consumption
return -1;
}
int[] diff = new int[stationGasAvailability.length];
for (int i = 0;i < stationGasAvailability.length;i ++) {
diff[i] = stationGasAvailability[i] -
requiredGasAvailabilityForDriving[i];
}
int index = 0;
int remaining = 0;
int startIndex = -1;
boolean first = true;
while (true) {
if (diff[index] + remaining >= 0) {
if (first) {
startIndex = index;
first = false;
}
remaining += diff[index];
} else {
first = true;
remaining = 0;
}
index = (index + 1) % stationGasAvailability.length;
if (first == false && startIndex == index) {
return startIndex;
} else if (index == 0 && remaining <= 0) {
return -1;
}
}
}

【在 t******d 的大作中提到】
: 那个没过的case,很大。2个数组,放main里面,都超过了size,jvm跑都不让跑!
1 (共1页)
进入JobHunting版参与讨论
相关主题
讨论个subarray sum的变种问题帮俺看一下代码DP+DFS为什么过不了work break II 那个大case : aaaaaaa...
发面经攒rp —— BloombergSurrounded Regions, dfs solution. 过不了online test
问个c++的问题divide two integers
上一算法面试题lc 上面4 sum的时间复杂度要求多少?
Leetcode Copy List with Random Pointer Runtime Error?这段代码在leetcode上面跑不了??
哪位大侠帮我看看这个codeleetcode 上通不过
find the first missing positive numberleetcode是不是最近有点问题?
请教一下leetcode gas station大家帮忙分析下leetcode一个题目的复杂度
相关话题的讨论汇总
话题: reach话题: int话题: gas话题: return话题: cost