Brain Teasers¶
Table of Contents¶
- 2733. Neither Minimum nor Maximum (Easy)
- 1903. Largest Odd Number in String (Easy)
- 2549. Count Distinct Numbers on Board (Easy)
- 3432. Count Partitions with Even Sum Difference (Easy)
- 2396. Strictly Palindromic Number (Medium)
- 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers (Medium)
- 598. Range Addition II (Easy)
- 521. Longest Uncommon Subsequence I (Easy)
- 3227. Vowels Game in a String (Medium)
- 2419. Longest Subarray With Maximum Bitwise AND (Medium)
- 3424. Minimum Cost to Make Arrays Identical (Medium)
- 1992. Find All Groups of Farmland (Medium)
- 1007. Minimum Domino Rotations For Equal Row (Medium)
- 2811. Check if it is Possible to Split Array (Medium)
- 2211. Count Collisions on a Road (Medium)
- 3207. Maximum Points After Enemy Battles (Medium)
- 2546. Apply Bitwise Operations to Make Strings Equal (Medium)
- 1503. Last Moment Before All Ants Fall Out of a Plank (Medium)
- 2860. Happy Students (Medium)
- 1332. Remove Palindromic Subsequences (Easy)
- 1975. Maximum Matrix Sum (Medium)
- 1145. Binary Tree Coloring Game (Medium)
- 1297. Maximum Number of Occurrences of a Substring (Medium)
- 3282. Reach End of Array With Max Score (Medium)
- 2332. The Latest Time to Catch a Bus (Medium)
- 2680. Maximum OR (Medium)
- 2731. Movement of Robots (Medium)
- 2556. Disconnect Path in a Binary Matrix by at Most One Flip (Medium)
- 3125. Maximum Number That Makes Result of Bitwise AND Zero (Medium) 👑
- 1794. Count Pairs of Equal Substrings With Minimum Difference (Medium) 👑
2733. Neither Minimum nor Maximum¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, sorting
1903. Largest Odd Number in String¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: math, string, greedy
2549. Count Distinct Numbers on Board¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table, math, simulation
3432. Count Partitions with Even Sum Difference¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, math, prefix sum
2396. Strictly Palindromic Number¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, two pointers, brainteaser
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, greedy
598. Range Addition II¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, math
521. Longest Uncommon Subsequence I¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: string
3227. Vowels Game in a String¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, string, brainteaser, game theory
2419. Longest Subarray With Maximum Bitwise AND¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, bit manipulation, brainteaser
3424. Minimum Cost to Make Arrays Identical¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
1992. Find All Groups of Farmland¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, depth first search, breadth first search, matrix
1007. Minimum Domino Rotations For Equal Row¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy
2811. Check if it is Possible to Split Array¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming, greedy
2211. Count Collisions on a Road¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, stack, simulation
3207. Maximum Points After Enemy Battles¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy
2546. Apply Bitwise Operations to Make Strings Equal¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, bit manipulation
1503. Last Moment Before All Ants Fall Out of a Plank¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, brainteaser, simulation
2860. Happy Students¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, sorting, enumeration
from typing import List
# Sort
def countWays(nums: List[int]) -> int:
nums.sort()
n = len(nums)
count = 0
if nums[0] > 0:
count += 1
for x in range(1, n):
if nums[x - 1] < x < nums[x]:
count += 1
if nums[n - 1] < n:
count += 1
return count
nums = [6, 0, 3, 3, 6, 7, 2, 7]
print(countWays(nums)) # 3
1332. Remove Palindromic Subsequences¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: two pointers, string
1975. Maximum Matrix Sum¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, matrix
1145. Binary Tree Coloring Game¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: tree, depth first search, binary tree
1297. Maximum Number of Occurrences of a Substring¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, sliding window
3282. Reach End of Array With Max Score¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy
2332. The Latest Time to Catch a Bus¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, binary search, sorting
2680. Maximum OR¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, bit manipulation, prefix sum
from typing import List
# Greedy
def maximumOr(nums: List[int], k: int) -> int:
"""Maximum OR of Array After k Operations
Args:
nums (List[int]): provided list of integers
k (int): number of operations
Returns:
int: maximum OR of array after k operations
"""
n = len(nums)
suffix = [0 for _ in range(n)]
for i in range(n - 2, -1, -1):
suffix[i] = suffix[i + 1] | nums[i + 1]
res, pre = 0, 0
for num, suf in zip(nums, suffix):
res = max(res, pre | (num << k) | suf)
pre |= num
return res
if __name__ == "__main__":
print(maximumOr(nums=[8, 1, 2], k=2)) # 35
2731. Movement of Robots¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, brainteaser, sorting, prefix sum
2556. Disconnect Path in a Binary Matrix by at Most One Flip¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming, depth first search, breadth first search, matrix
3125. Maximum Number That Makes Result of Bitwise AND Zero¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, greedy, sorting
1794. Count Pairs of Equal Substrings With Minimum Difference¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, greedy