mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-05 05:47:41 +00:00
feat: refactored docs structure
This commit is contained in:
1 parent
d388b3000e
commit
9de25fecff
212 files changed
+641
-4169
No files matched your search
@@ -0,0 +1,48 @@
|
||||
---
|
||||
title: '134. Gas Station'
|
||||
description: There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i]
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Array", slug: "array" }
|
||||
- { name: "Greedy", slug: "greedy" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/176"
|
||||
sidebar:
|
||||
label: 'Gas Station'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
### Example 1:
|
||||
- Input: `gas = [1,2,3,4,5], cost = [3,4,5,1,2]`
|
||||
- Output: `3`
|
||||
- Explanation: Start at station `3` (index `3`) and fill up with `4` unit of `gas`. Your tank = `0 + 4 = 4` Travel to station `4`. Your tank = `4 - 1 + 5 = 8` Travel to station `0`. Your tank = `8 - 2 + 1 = 7` Travel to station `1`. Your tank = `7 - 3 + 2 = 6` Travel to station `2`. Your tank = `6 - 4 + 3 = 5` Travel to station `3`. The `cost` is `5`. Your `gas` is just enough to travel back to station `3`. Therefore, return `3` as the starting index.
|
||||
|
||||
### Example 2:
|
||||
- Input: `gas = [2,3,4], cost = [3,4,3]`
|
||||
- Output: `-1`
|
||||
- Explanation: You can't start at station `0` or `1`, as there is not enough `gas` to travel to the next station. Let's start at station `2` and fill up with `4` unit of `gas`. Your tank = `0 + 4 = 4` Travel to station `0`. Your tank = `4 - 3 + 2 = 3` Travel to station `1`. Your tank = `3 - 3 + 3 = 3` You cannot travel back to station `2`, as it requires `4` unit of `gas` but you only have `3`. Therefore, you can't travel around the circuit once no matter where you start.
|
||||
|
||||
### Constraints:
|
||||
|
||||
- `n == gas.length == cost.length`
|
||||
- `1 <= n <= 10^5`
|
||||
- `0 <= gas[i], cost[i] <= 10^4`
|
||||
- The input is generated such that the answer is unique.
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
class Solution:
|
||||
def canCompleteCircuit(self, gas: list[int], cost: list[int]) -> int:
|
||||
start, tank, total = 0, 0, 0
|
||||
|
||||
for i in range(len(gas)):
|
||||
drive = gas[i] - cost[i]
|
||||
tank = tank + drive
|
||||
|
||||
if tank < 0:
|
||||
tank = 0
|
||||
start = i + 1
|
||||
total = total + drive
|
||||
|
||||
return start if total >= 0 else -1
|
||||
```
|
||||
@@ -0,0 +1,54 @@
|
||||
---
|
||||
title: '53. Maximum Subarray'
|
||||
description: Given an integer array nums, find the subarray with the largest sum, and return its sum
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Array", slug: "array" }
|
||||
- { name: "Divide and Conquer", slug: "divide-and-conquer" }
|
||||
- { name: "Dynamic Programming", slug: "dynamic-programming" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/174"
|
||||
sidebar:
|
||||
label: 'Maximum Subarray'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
::::warning
|
||||
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
|
||||
::::
|
||||
|
||||
### Example 1:
|
||||
- Input: `nums = [-2,1,-3,4,-1,2,1,-5,4]`
|
||||
- Output: `6`
|
||||
- Explanation: The subarray `[4,-1,2,1]` has the largest sum `6`.
|
||||
|
||||
### Example 2:
|
||||
- Input: `nums = [1]`
|
||||
- Output: `1`
|
||||
- Explanation: The subarray `[1]` has the largest sum `1`.
|
||||
|
||||
### Example 3:
|
||||
- Input: `nums = [5,4,-1,7,8]`
|
||||
- Output: `23`
|
||||
- Explanation: The subarray `[5,4,-1,7,8]` has the largest sum `23`.
|
||||
|
||||
### Constraints:
|
||||
|
||||
- `1 <= nums.length <= 10^5`
|
||||
- `-10^4 <= nums[i] <= 10^4`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
class Solution:
|
||||
def maxSubArray(self, nums: list[int]) -> int:
|
||||
best, total = nums[0], nums[0]
|
||||
|
||||
for i in range(1, len(nums)):
|
||||
if total < 0:
|
||||
total = nums[i]
|
||||
else:
|
||||
total = total + nums[i]
|
||||
best = max(best, total)
|
||||
|
||||
return best
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
title: '55. Jump Game'
|
||||
description: You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Array", slug: "array" }
|
||||
- { name: "Dynamic Programming", slug: "dynamic-programming" }
|
||||
- { name: "Greedy", slug: "greedy" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/175"
|
||||
sidebar:
|
||||
label: 'Jump Game'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
### Example 1:
|
||||
- Input: `nums = [2,3,1,1,4]`
|
||||
- Output: `true`
|
||||
- Explanation: Jump `1` step from index `0` to `1`, then `3` steps to the last index.
|
||||
|
||||
### Example 2:
|
||||
- Input: `nums = [3,2,1,0,4]`
|
||||
- Output: `false`
|
||||
- Explanation: You will always arrive at index `3` no matter what. Its maximum jump length is `0`, which makes it impossible to reach the last index.
|
||||
|
||||
### Constraints:
|
||||
|
||||
- `1 <= nums.length <= 10^4`
|
||||
- `0 <= nums[i] <= 10^5`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
class Solution:
|
||||
def canJump(self, nums: list[int]) -> bool:
|
||||
best, last = nums[0], len(nums) - 1
|
||||
|
||||
for i in range(last):
|
||||
if i > best:
|
||||
return False
|
||||
else:
|
||||
pos = nums[i] + i
|
||||
best = max(best, pos)
|
||||
|
||||
return best >= last
|
||||
```
|
||||
Reference in new issue
Block a user