牛客2024 【牛客&赛文X】春招冲刺 ONT34 加油站【中等 贪心 C++、Java、Go、PHP】

题目

在这里插入图片描述题目链接:
https://www.nowcoder.com/practice/a013a0691a0343aeb262ca1450d2fe4e

思路

贪心:
如果总的gas小于走完全程的cost,直接返回-1不需要再找了
如果确保了可以走完一圈之后,那么从index = 0开始找,
当gas_remain的油箱低于0了就代表从index出发不可以,index += 1
循环一遍即可,因为已经肯定了有答案,所有只要有一个i可以一直走到结束都可以保持油箱 > 0,
那么它肯定是答案。

参考答案C++

class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param gas int整型vector
     * @param cost int整型vector
     * @return int整型
     */
    int gasStation(vector<int>& gas, vector<int>& cost) {
        // 贪心
        int sum = 0, allgas = 0, allcost = 0, start = 0;
        for (int i = 0; i < gas.size(); i++) {
            sum = sum + gas[i] - cost[i];
            allgas += gas[i];
            allcost += cost[i];
            if (sum < 0) { //尝试从下一个位置出发
                sum = 0;
                start = i + 1;
            }
        }
        return allgas >= allcost ? start : -1;
    }
};

参考答案Java

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param gas int整型一维数组
     * @param cost int整型一维数组
     * @return int整型
     */
    public int gasStation (int[] gas, int[] cost) {
        //贪心
        int sum = 0, allgas = 0, allcost = 0, start = 0;
        for (int i = 0; i < gas.length ; i++) {
            sum = sum + gas[i] - cost[i];
            allgas += gas[i];
            allcost += cost[i];
            if (sum < 0) { //尝试换下一个位置出发
                sum = 0;
                start = i + 1;
            }
        }
        return allgas >= allcost ? start : -1;
        /*
        示例1
        输入:

        [1,2,3,4,5],[3,4,5,1,2]

        返回值:

        3

        说明:

        只能从下标为 3 的加油站开始完成 (即第四个加油站):

        1:  index=3   start=4 cost=1  rest=3
        2:   index=4  start=8  cost=2  rest= 6
        2:   index=0  start=7  cost=3   rest=4
        2:   index=1  start=6  cost=2   rest=4
        2:   index=2  start=7  cost=5   rest=2
        2:   index=6  start=7  cost=5   rest=2
                  */
    }
}

参考答案Go

package main



/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param gas int整型一维数组
 * @param cost int整型一维数组
 * @return int整型
 */
func gasStation(gas []int, cost []int) int {
	//贪心
	sum := 0
	allgas := 0
	allcost := 0
	start := 0

	for i := 0; i < len(gas); i++ {
		sum = sum + gas[i] - cost[i]
		allgas += gas[i]
		allcost += cost[i]

		if sum < 0 { //尝试从下一个位置出发
			sum = 0
			start = i + 1
		}
	}
	if allgas >= allcost {
		return start
	}
	return -1

	/*
	   示例1
	   输入:

	   [1,2,3,4,5],[3,4,5,1,2]

	   返回值:

	   3

	   说明:

	   只能从下标为 3 的加油站开始完成 (即第四个加油站):

	   1:  index=3   start=4 cost=1  rest=3
	   2:   index=4  start=8  cost=2  rest= 6
	   2:   index=0  start=7  cost=3   rest=4
	   2:   index=1  start=6  cost=2   rest=4
	   2:   index=2  start=7  cost=5   rest=2
	   2:   index=6  start=7  cost=5   rest=2
	*/
}

参考答案PHP

<?php


/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param gas int整型一维数组 
 * @param cost int整型一维数组 
 * @return int整型
 */
function gasStation( $gas ,  $cost )
{
    //贪心
    $sum=0;
    $allgas =0;
    $allcost=0;
    $start=0;
    for($i=0;$i<count($gas);$i++){
        $sum=$sum+$gas[$i]-$cost[$i];
        $allgas+=$gas[$i];
        $allcost+=$cost[$i];

        if($sum<0){ //尝试换一个位置再出发
            $sum=0;
            $start =$i+1;
        }
    }
    return $allgas>=$allcost? $start:-1;

    /*
   示例1
   输入:

   [1,2,3,4,5],[3,4,5,1,2]

   返回值:

   3

   说明:

   只能从下标为 3 的加油站开始完成 (即第四个加油站):

   1:  index=3   start=4 cost=1  rest=3
   2:   index=4  start=8  cost=2  rest= 6
   2:   index=0  start=7  cost=3   rest=4
   2:   index=1  start=6  cost=2   rest=4
   2:   index=2  start=7  cost=5   rest=2
   2:   index=6  start=7  cost=5   rest=2
*/
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵长辉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值