2023年7月9日 星期日

[134] Gas Station

感覺不難的題目,但是寫的亂七八糟XD
並且被 circular array 迷惑了QQ
最後發現別人寫的好簡單
應該只有證明的部分比較麻煩吧......(嘆氣)


int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
int total = 0, remain = 0, i = 0 , idx =0 ;
for ( i =0; i < gasSize; i++)
{
remain += gas[i]- cost[i];
total += gas[i]- cost[i];

if (remain < 0)
{
remain = 0;
idx = i+1;
}
}
return (total>=0)? idx : (-1);
}

沒有留言:

張貼留言