0%

• 134 Gas Station
• 135 Candy ### 134. Gas Station

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.

• gas[a] - cost[a] >=0 （这是枚举的条件）
• gas[a] - cost[a] + gas[b] - cost[b] >=0
• gas[a] - cost[a] + gas[b] - cost[b]  + gas[c] - cost[c] >=0
• gas[a] - cost[a] + gas[b] - cost[b]  + gas[c] - cost[c]  + gas[d] - cost[d] < 0

C++

Python

### 135. Candy

There are N children standing in a line. Each child is assigned a rating value.

You are giving candies to these children subjected to the following requirements:

• Each child must have at least one candy.
• Children with a higher rating get more candies than their neighbors.

What is the minimum candies you must give?

• 每个孩子至少需要一颗糖
• 如果一个孩子的分数大于旁边的孩子，那么他的糖果要比旁边的多一颗。

code1:

code2:

O(nlogn)太慢，虽然可以过。但是可以优化到O(n)。

Python

C++