All Posts

Leetcode 2104: Sum of Subarray Ranges

You are given an integer array nums. A subarray of nums is a contiguous sequence of elements. The range of a subarray is defined as the difference between the maximum and minimum elements within that subarray.

Your task is to calculate the sum of the ranges for all possible subarrays in the given array nums.

Leetcode 2116: Check if a Parentheses String Can Be Valid

You are given a string s consisting of parentheses ‘(’ and ‘)’, and a binary string locked of the same length. Each position in locked determines whether the corresponding character in s can be changed or not. If locked[i] is ‘1’, the character s[i] cannot be modified, but if locked[i] is ‘0’, you can change s[i] to either ‘(’ or ‘)’. Your task is to determine if it is possible to make the string s a valid parentheses string by changing the positions where locked[i] is ‘0’. A valid parentheses string is defined by the following conditions: It is either ‘()’, or a concatenation of valid parentheses strings, or it can be written as (A) where A is a valid parentheses string.

Leetcode 2130: Maximum Twin Sum of a Linked List

You are given a linked list with an even number of nodes. Each node in the list has a twin located symmetrically from the center of the list. Specifically, the ith node is the twin of the (n-1-i)th node, where n is the total number of nodes in the list. For example, the first node is the twin of the last node, the second node is the twin of the second last node, and so on. The twin sum is defined as the sum of a node and its twin. Your task is to return the maximum twin sum of the linked list.

Leetcode 2211: Count Collisions on a Road

You have n cars on a straight road, each numbered from 0 to n-1 from left to right, with each car positioned at a unique location. You are given a string directions where each character represents the direction of a car (‘L’ for left, ‘R’ for right, and ‘S’ for stationary). Each moving car has the same speed, and the cars can collide under the following conditions: Two cars moving in opposite directions (‘L’ and ‘R’) will collide, counting as 2 collisions. A moving car colliding with a stationary car (‘S’) will result in 1 collision. After a collision, the cars involved stay at the collision point and no longer move. Your task is to calculate the total number of collisions that will happen.

Leetcode 2216: Minimum Deletions to Make Array Beautiful

You are given an integer array nums. The array is considered beautiful if it satisfies the following conditions: the length of nums is even, and for every index i that is even (i % 2 == 0), nums[i] should not be equal to nums[i + 1]. You can remove any number of elements from nums to make it beautiful. Your goal is to return the minimum number of elements that need to be removed to make the array beautiful.

Leetcode 2375: Construct Smallest Number From DI String

You are given a string pattern consisting of the characters ‘I’ and ‘D’, where ‘I’ indicates that the next number in a sequence should be greater, and ‘D’ means the next number should be smaller. You need to construct the lexicographically smallest string num of length n + 1 such that the digits in num follow the conditions set by the pattern. The digits in num must be distinct and range from ‘1’ to ‘9’.