Skip to content

Brain Teasers

Table of Contents

2733. Neither Minimum nor Maximum

1903. Largest Odd Number in String

2549. Count Distinct Numbers on Board

3432. Count Partitions with Even Sum Difference

2396. Strictly Palindromic Number

1689. Partitioning Into Minimum Number Of Deci-Binary Numbers

598. Range Addition II

521. Longest Uncommon Subsequence I

3227. Vowels Game in a String

2419. Longest Subarray With Maximum Bitwise AND

3424. Minimum Cost to Make Arrays Identical

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

2811. Check if it is Possible to Split Array

2211. Count Collisions on a Road

3207. Maximum Points After Enemy Battles

2546. Apply Bitwise Operations to Make Strings Equal

1503. Last Moment Before All Ants Fall Out of a Plank

2860. Happy Students

2860. Happy Students - Python Solution
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

1975. Maximum Matrix Sum

1145. Binary Tree Coloring Game

1297. Maximum Number of Occurrences of a Substring

3282. Reach End of Array With Max Score

2332. The Latest Time to Catch a Bus

2680. Maximum OR

2680. Maximum OR - Python Solution
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

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

1794. Count Pairs of Equal Substrings With Minimum Difference

Comments