2025-04¶
Table of Contents¶
- 2140. Solving Questions With Brainpower (Medium)
- 2873. Maximum Value of an Ordered Triplet I (Easy)
- 2874. Maximum Value of an Ordered Triplet II (Medium)
- 1123. Lowest Common Ancestor of Deepest Leaves (Medium)
- 1863. Sum of All Subset XOR Totals (Easy)
2140. Solving Questions With Brainpower¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming
2140. Solving Questions With Brainpower - Python Solution
from functools import cache
from typing import List
# Memoization
def mostPoints(questions: List[List[int]]) -> int:
@cache
def dfs(i: int) -> int:
if i >= len(questions):
return 0
return max(dfs(i + 1), dfs(i + questions[i][1] + 1) + questions[i][0])
return dfs(0)
if __name__ == "__main__":
questions = [[3, 2], [4, 3], [4, 4], [2, 5]]
print(mostPoints(questions)) # 5
2873. Maximum Value of an Ordered Triplet I¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array
2873. Maximum Value of an Ordered Triplet I - Python Solution
from typing import List
def maximumTripletValue(nums: List[int]) -> int:
res, max_diff, pre_max = 0, 0, 0
for num in nums:
res = max(res, max_diff * num)
max_diff = max(max_diff, pre_max - num)
pre_max = max(pre_max, num)
return res
if __name__ == "__main__":
nums = [12, 6, 1, 2, 7]
print(maximumTripletValue(nums)) # 77
2874. Maximum Value of an Ordered Triplet II¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array
2874. Maximum Value of an Ordered Triplet II - Python Solution
from typing import List
def maximumTripletValue(nums: List[int]) -> int:
res = 0
max_diff = 0
max_prev = 0
for num in nums:
res = max(res, max_diff * num)
max_diff = max(max_diff, max_prev - num)
max_prev = max(max_prev, num)
return res
if __name__ == "__main__":
nums = [12, 6, 1, 2, 7]
print(maximumTripletValue(nums)) # 77
1123. Lowest Common Ancestor of Deepest Leaves¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, tree, depth first search, breadth first search, binary tree
1123. Lowest Common Ancestor of Deepest Leaves - Python Solution
from typing import Optional
from binarytree import Node as TreeNode
from binarytree import build
# Lowest Common Ancestor
def lcaDeepestLeaves(root: Optional[TreeNode]) -> Optional[TreeNode]:
res = None
max_depth = -1
def dfs(node, depth) -> int:
nonlocal res, max_depth
if not node:
max_depth = max(max_depth, depth)
return depth
left_max_depth = dfs(node.left, depth + 1)
right_max_depth = dfs(node.right, depth + 1)
if left_max_depth == right_max_depth == max_depth:
res = node
return max(left_max_depth, right_max_depth)
dfs(root, 0)
return res
if __name__ == "__main__":
root = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]
root = build(root)
print(root)
# ______3__
# / \
# 5__ 1
# / \ / \
# 6 2 0 8
# / \
# 7 4
print(lcaDeepestLeaves(root)) # 2
# 2
# / \
# 7 4
1863. Sum of All Subset XOR Totals¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, math, backtracking, bit manipulation, combinatorics, enumeration
1863. Sum of All Subset XOR Totals - Python Solution
from functools import reduce
from operator import or_
from typing import List
def subsetXORSum(nums: List[int]) -> int:
return reduce(or_, nums) << (len(nums) - 1)
if __name__ == "__main__":
nums = [5, 1, 6]
print(subsetXORSum(nums)) # 28