All Posts

Leetcode 2012: Sum of Beauty in the Array

You are given an integer array ’nums’. For each index i (1 <= i <= nums.length - 2), calculate the beauty of nums[i] based on the following conditions:

  • The beauty of nums[i] is 2 if for all indices j before i and for all indices k after i, nums[j] < nums[i] < nums[k].
  • The beauty of nums[i] is 1 if nums[i-1] < nums[i] < nums[i+1] but the previous condition is not met.
  • The beauty of nums[i] is 0 if neither of the above conditions holds.

Return the sum of beauty values for all nums[i] where 1 <= i <= nums.length - 2.

Leetcode 2013: Detect Squares

You are given a stream of 2D points on the XY-plane. Your task is to design an algorithm that adds new points into a data structure and counts how many sets of three points, along with a given query point, can form an axis-aligned square. An axis-aligned square is a square where the edges are parallel or perpendicular to the X-axis and Y-axis, with all sides of equal length.

Leetcode 2016: Maximum Difference Between Increasing Elements

You are given an integer array nums of size n. Your task is to find the maximum difference between two elements in the array such that the element on the right is strictly greater than the element on the left. Specifically, you need to find the largest difference between nums[j] - nums[i] where 0 <= i < j < n and nums[i] < nums[j]. If no such valid pair exists, return -1.

Leetcode 2017: Grid Game

You are given a 2D grid of size 2 x n, where each cell contains points. Two robots play a game where they start at (0, 0) and aim to reach (1, n-1). The first robot moves first, collecting points along its path. Then, the second robot moves, trying to maximize the points it collects. The task is to find the number of points collected by the second robot if both play optimally.

Leetcode 2018: Check if Word Can Be Placed In Crossword

You are given an m x n matrix representing a crossword puzzle. The matrix contains lowercase English letters, empty spaces (’ ‘), and blocked cells (’#’). Given a word, determine if it can be placed horizontally or vertically in the crossword while adhering to the following constraints.

Leetcode 2022: Convert 1D Array Into 2D Array

You are given a 1D integer array called ‘original’ and two integers ’m’ and ’n’. Your task is to convert this 1D array into a 2D array with ’m’ rows and ’n’ columns. The elements from the ‘original’ array should be placed row-wise in the new 2D array.