返回介绍

solution / 2700-2799 / 2739.Total Distance Traveled / README_EN

发布于 2024-06-17 01:03:00 字数 3868 浏览 0 评论 0 收藏 0

2739. Total Distance Traveled

中文文档

Description

A truck has two fuel tanks. You are given two integers, mainTank representing the fuel present in the main tank in liters and additionalTank representing the fuel present in the additional tank in liters.

The truck has a mileage of 10 km per liter. Whenever 5 liters of fuel get used up in the main tank, if the additional tank has at least 1 liters of fuel, 1 liters of fuel will be transferred from the additional tank to the main tank.

Return _the maximum distance which can be traveled._

Note: Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed.

 

Example 1:

Input: mainTank = 5, additionalTank = 10
Output: 60
Explanation: 
After spending 5 litre of fuel, fuel remaining is (5 - 5 + 1) = 1 litre and distance traveled is 50km.
After spending another 1 litre of fuel, no fuel gets injected in the main tank and the main tank becomes empty.
Total distance traveled is 60km.

Example 2:

Input: mainTank = 1, additionalTank = 2
Output: 10
Explanation: 
After spending 1 litre of fuel, the main tank becomes empty.
Total distance traveled is 10km.

 

Constraints:

  • 1 <= mainTank, additionalTank <= 100

Solutions

Solution 1

class Solution:
  def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:
    ans = cur = 0
    while mainTank:
      cur += 1
      ans += 10
      mainTank -= 1
      if cur % 5 == 0 and additionalTank:
        additionalTank -= 1
        mainTank += 1
    return ans
class Solution {
  public int distanceTraveled(int mainTank, int additionalTank) {
    int ans = 0, cur = 0;
    while (mainTank > 0) {
      cur++;
      ans += 10;
      mainTank--;
      if (cur % 5 == 0 && additionalTank > 0) {
        additionalTank--;
        mainTank++;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int distanceTraveled(int mainTank, int additionalTank) {
    int ans = 0, cur = 0;
    while (mainTank > 0) {
      cur++;
      ans += 10;
      mainTank--;
      if (cur % 5 == 0 && additionalTank > 0) {
        additionalTank--;
        mainTank++;
      }
    }
    return ans;
  }
};
func distanceTraveled(mainTank int, additionalTank int) (ans int) {
  cur := 0
  for mainTank > 0 {
    cur++
    ans += 10
    mainTank--
    if cur%5 == 0 && additionalTank > 0 {
      additionalTank--
      mainTank++
    }
  }
  return
}
impl Solution {
  pub fn distance_traveled(mut main_tank: i32, mut additional_tank: i32) -> i32 {
    let mut cur = 0;
    let mut ans = 0;

    while main_tank > 0 {
      cur += 1;
      main_tank -= 1;
      ans += 10;

      if cur % 5 == 0 && additional_tank > 0 {
        additional_tank -= 1;
        main_tank += 1;
      }
    }

    ans
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文