1

I'm trying to prove the solution for the problem Gas Station (LeetCode 134)

Let's define d_i to be the difference g_i - c_i.

Basically it's a greedy solution where you look for the first k s.t.

\sum_{i=k}^n d_i >= 0

Let's denote the following:

A = \sum_{i=1}^k d_i

B = \sum_{i=k+1}^n d_i

Notice that by definition of k, A<0 and B >= 0.

Since we know that there's a unique solution:

A+B >= 0 --> B >= -A

We want to show that B-A >= 0. This will show that we have enough gas for A after finishing the B part.

So B-A >= B+B >= 0

Is my proof correct?

Thanks

Elimination
  • 2,619
  • 4
  • 22
  • 38
  • 5
    Hi! You should note that readers usually prefer self-contained questions. Personally I have no intention of watching that youtube video you linked. So, even if I wanted to answer, I couldn't. Have a good day! – Stef Sep 09 '21 at 09:39
  • You should always post complete description. For this question, you can re-watch the same video, pay attention at/around 10 minutes~ again. – Daniel Hao Sep 09 '21 at 10:00
  • I have failed to understand few points related to your proof. *Is there any room we can discuss that?* I do not think comment will be a good place for it. – AKSingh Sep 10 '21 at 05:10

0 Answers0