All Posts

Leetcode 1963: Minimum Number of Swaps to Make the String Balanced

You are given a string s of even length n. The string contains exactly n/2 opening brackets [ and n/2 closing brackets ]. A string is called balanced if for every opening bracket there exists a corresponding closing bracket, with each closing bracket having a matching opening bracket before it. Your task is to return the minimum number of swaps needed to make the string balanced.

Leetcode 1968: Array With Elements Not Equal to Average of Neighbors

You are given an array nums containing distinct integers. You need to rearrange the elements of the array such that no element is equal to the average of its two adjacent elements. Specifically, for every index i (1 <= i < nums.length - 1), the condition (nums[i-1] + nums[i+1]) / 2 != nums[i] should hold. Your task is to return any valid rearrangement of nums that satisfies this condition.

Leetcode 1969: Minimum Non-Zero Product of the Array Elements

You are given a positive integer p. Consider an array nums (1-indexed) that consists of the integers in the inclusive range [1, 2p - 1] represented in binary form. You are allowed to perform the following operation any number of times: Choose two elements x and y from nums. Choose a bit in x and swap it with its corresponding bit in y. The goal is to find the minimum non-zero product of the elements in nums after performing the above operation any number of times. Return this minimum product modulo (10^9 + 7).

Leetcode 1974: Minimum Time to Type Word Using Special Typewriter

You are given a special typewriter with lowercase English letters (‘a’ to ‘z’) arranged in a circle. A pointer initially starts at the character ‘a’. Each second, you may perform one of two operations: move the pointer one character clockwise or counterclockwise, or type the character the pointer is currently on. Your task is to determine the minimum number of seconds needed to type the given string word.

Leetcode 1975: Maximum Matrix Sum

You are given a square matrix of integers. You can perform the following operation multiple times: choose any two adjacent elements in the matrix and multiply both of them by -1. Your goal is to maximize the sum of the matrix elements after performing the operation any number of times.

Leetcode 1996: The Number of Weak Characters in the Game

You are given a list of characters’ properties, where each character has two main attributes: attack and defense. A character is weak if another character exists with both greater attack and defense values. Return the number of weak characters in the list.