All Posts

Leetcode 2971: Find Polygon With the Largest Perimeter

You are given an array nums consisting of positive integers. A polygon can be formed from the integers if it satisfies the following condition: the sum of the lengths of any k-1 sides must be greater than the length of the remaining side. The perimeter of a polygon is the sum of the lengths of its sides. Your task is to find the largest possible perimeter of a polygon that can be formed using the sides from nums, or return -1 if it is not possible to form such a polygon.

Leetcode 3026: Maximum Good Subarray Sum

You are given an array nums of length n and a positive integer k. A subarray is called good if the absolute difference between its first and last element is exactly k. The task is to return the maximum sum of any good subarray of nums. If no such subarray exists, return 0.

Leetcode 3043: Find the Length of the Longest Common Prefix

You are given two arrays arr1 and arr2 containing positive integers. Your task is to find the length of the longest common prefix between all pairs of integers (x, y) such that x belongs to arr1 and y belongs to arr2. A prefix of a number is any integer formed by one or more digits starting from its leftmost digit. For example, 123 is a prefix of 12345, but 234 is not. If no common prefix exists for any pair, return 0.

Leetcode 3044: Most Frequent Prime

You are given a matrix of integers, where each cell contains a digit from 1 to 9. Starting from any cell in the matrix, you can move in one of eight possible directions (east, south-east, south, south-west, west, north-west, north, and north-east) and create numbers by appending the digits along the path. For each valid path, numbers greater than 10 are generated. The task is to find the most frequent prime number greater than 10 among all the numbers generated by traversing the matrix. If there are multiple such prime numbers, return the largest one. If no prime number exists, return -1.

Leetcode 3081: Replace Question Marks in String to Minimize Its Value

You are given a string s consisting of lowercase English letters and question marks (?). Your task is to replace all occurrences of ? with any lowercase English letter in such a way that the total cost of the resulting string is minimized. The cost of a string is the sum of how many times each character has appeared before its current position. If there are multiple solutions with the same minimal cost, return the lexicographically smallest one.

Leetcode 3100: Water Bottles II

You are given two integers: numBottles, representing the number of full water bottles you initially have, and numExchange, representing the number of empty bottles required to exchange for a full bottle. In one operation, you can drink any number of full water bottles, turning them into empty bottles, or exchange numExchange empty bottles for one full bottle, with numExchange increasing by 1 after each exchange. Return the maximum number of water bottles you can drink.