All Posts

Leetcode 1572: Matrix Diagonal Sum

You are given a square matrix of integers. Your task is to calculate the sum of all the elements located on the primary diagonal and the secondary diagonal, excluding the element that appears on both diagonals (i.e., the center element in an odd-sized matrix).

Leetcode 1577: Number of Ways Where Square of Number Is Equal to Product of Two Numbers

Given two integer arrays, nums1 and nums2, return the total number of valid triplets that can be formed under two conditions:

Type 1: A triplet (i, j, k) is valid if nums1[i]^2 == nums2[j] * nums2[k] where 0 <= i < nums1.length and 0 <= j < k < nums2.length. Type 2: A triplet (i, j, k) is valid if nums2[i]^2 == nums1[j] * nums1[k] where 0 <= i < nums2.length and 0 <= j < k < nums1.length.

Leetcode 1578: Minimum Time to Make Rope Colorful

Alice has a rope with balloons arranged in a sequence where each balloon has a color. She wants the rope to be colorful, meaning no two consecutive balloons should have the same color. Bob is tasked with helping Alice by removing some balloons. Each balloon has an associated cost, and Bob needs to minimize the total time spent removing the balloons to achieve the desired colorful sequence. Return the minimum time Bob needs to make the rope colorful.

Leetcode 1582: Special Positions in a Binary Matrix

You are given a binary matrix of size m x n, where each element is either 0 or 1. A position (i, j) in the matrix is considered special if mat[i][j] = 1 and all other elements in the same row i and column j are 0. Your task is to find how many such special positions exist in the matrix.

Leetcode 1583: Count Unhappy Friends

You are given a list of preferences for n friends, where n is always even. Each person has a list of friends they prefer, and these friends are represented by integers from 0 to n-1. The friends are divided into pairs, where each pair is denoted by a list [xi, yi], meaning xi is paired with yi and yi with xi. However, some of the pairings may cause unhappiness. A person is unhappy if they prefer someone who is paired with someone else, and that person also prefers them over their current partner. Your task is to return the number of unhappy friends.

Leetcode 1584: Min Cost to Connect All Points

You are given a list of points on a 2D plane, represented as coordinates [x, y]. The cost of connecting two points is defined by the Manhattan distance, which is calculated as |xi - xj| + |yi - yj|. Your task is to return the minimum total cost required to connect all the points. A valid connection between points must form a connected graph, where there is exactly one simple path between any two points.