Greedy Others¶
Table of Contents¶
- 2740. Find the Value of the Partition (Medium)
- 1033. Moving Stones Until Consecutive (Medium)
- 1864. Minimum Number of Swaps to Make the Binary String Alternating (Medium)
- 1899. Merge Triplets to Form Target Triplet (Medium)
- 2498. Frog Jump II (Medium)
- 134. Gas Station (Medium)
- 2311. Longest Binary Subsequence Less Than or Equal to K (Medium)
- 3443. Maximum Manhattan Distance After K Changes (Medium)
- 3002. Maximum Size of a Set After Removals (Medium)
- 2412. Minimum Money Required Before Transactions (Hard)
- 659. Split Array into Consecutive Subsequences (Medium)
- 2732. Find a Good Subset of the Matrix (Hard)
- 2790. Maximum Number of Groups With Increasing Length (Hard)
- 782. Transform to Chessboard (Hard)
- 420. Strong Password Checker (Hard)
- 2753. Count Houses in a Circular Street II (Hard) 👑
2740. Find the Value of the Partition¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, sorting
1033. Moving Stones Until Consecutive¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, brainteaser
1864. Minimum Number of Swaps to Make the Binary String Alternating¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, greedy
1899. Merge Triplets to Form Target Triplet¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy
from typing import List
def mergeTriplets(triplets: List[List[int]], target: List[int]) -> bool:
can_form = [False, False, False]
for triplet in triplets:
if all(triplet[i] <= target[i] for i in range(3)):
for i in range(3):
if triplet[i] == target[i]:
can_form[i] = True
return all(can_form)
triplets = [[2, 5, 3], [1, 8, 4], [1, 7, 5]]
target = [2, 7, 5]
print(mergeTriplets(triplets, target)) # True
2498. Frog Jump II¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, binary search, greedy
134. Gas Station¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy
from typing import List
# Greedy
def canCompleteCircuit(gas: List[int], cost: List[int]) -> int:
curSum = 0
totalSum = 0
start = 0
for i in range(len(gas)):
curSum += gas[i] - cost[i]
totalSum += gas[i] - cost[i]
if curSum < 0:
start = i + 1
curSum = 0
if totalSum < 0:
return -1
return start
gas = [1, 2, 3, 4, 5]
cost = [3, 4, 5, 1, 2]
print(canCompleteCircuit(gas, cost)) # 3
2311. Longest Binary Subsequence Less Than or Equal to K¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, dynamic programming, greedy, memoization
3443. Maximum Manhattan Distance After K Changes¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, math, string, counting
3002. Maximum Size of a Set After Removals¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, greedy
2412. Minimum Money Required Before Transactions¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, greedy, sorting
659. Split Array into Consecutive Subsequences¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, greedy, heap priority queue
2732. Find a Good Subset of the Matrix¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, hash table, bit manipulation, matrix
2790. Maximum Number of Groups With Increasing Length¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, math, binary search, greedy, sorting
782. Transform to Chessboard¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, math, bit manipulation, matrix
420. Strong Password Checker¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, greedy, heap priority queue
2753. Count Houses in a Circular Street II¶
- LeetCode | LeetCode CH (Hard)