All Posts

Leetcode 1937: Maximum Number of Points with Cost

You are given a matrix of integers points with dimensions m x n (0-indexed). Initially, you are at score 0, and your goal is to maximize the score by selecting one cell in each row. To gain points, you can select a cell (r, c) from each row. The value at points[r][c] will add to your score. However, if you choose cells from different columns in adjacent rows, the difference between their column indices will subtract from your score. Specifically, if you select a cell at (r, c1) in row r and a cell at (r + 1, c2) in row r + 1, the penalty is abs(c1 - c2). Your task is to return the maximum score you can achieve by choosing cells from each row.

Leetcode 1958: Check if Move is Legal

You are given a game board represented as an 8x8 grid, where each cell can either be free (’.’), white (‘W’), or black (‘B’). The objective is to determine if placing your piece (either ‘W’ or ‘B’) at a specified location will create a valid ‘good line’ where the line consists of three or more cells. A good line is defined as a line (horizontal, vertical, or diagonal) with endpoints of the same color and any cells between them of the opposite color.

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 1981: Minimize the Difference Between Target and Chosen Elements

You are given an m x n integer matrix, where each row contains n integers. You are also given a target integer. Your task is to choose one integer from each row of the matrix such that the absolute difference between the target and the sum of the chosen elements is minimized. Return the minimum absolute difference.

Leetcode 1992: Find All Groups of Farmland

You are given an m x n binary matrix representing a piece of land, where ‘1’ denotes farmland and ‘0’ denotes forest. Your task is to identify the coordinates of each rectangular group of contiguous farmland (1’s), where each group of farmland is isolated and not adjacent to another group. Return a list of 4-length arrays, where each array represents the top-left and bottom-right coordinates of a rectangular farmland group.

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.