Given a list of coordinates representing points on a Cartesian plane, we need to find the maximum number of points that lie on a single line. This problem requires calculating slopes between points and grouping them accordingly. So, if the input is like coordinates = [[6, 2], [8, 3], [10, 4], [1, 1], [2, 2], [6, 6], [7, 7]], then the output will be 4, as the points [1, 1], [2, 2], [6, 6], [7, 7] lie on the same line with slope 1. Algorithm To solve this problem, we follow these steps ? Initialize ... Read More
Suppose we have a 2D matrix and some other values like row, col, erow0, ecol0, erow1, and ecol1. If our current position is matrix[row, col] and we want to pick up gold that is at matrix[erow0, ecol0] and matrix[erow1, ecol1]. We can go up, down, left, and right but when we are at a cell (r, c), we have to pay cost matrix[r, c]. However, if we land at a cell more than once, we do not need to pay cost for that cell again. We have to find the minimum cost to pick up gold at both locations. ... Read More
Suppose we have a lowercase string s, we can partition s into as many pieces as possible such that each letter appears in at most one piece and find the sizes of the partitions as a list. So, if the input is like s = "momoplaykae", then the output will be [4, 1, 1, 4, 1], as the string is split into ["momo", "p", "l", "ayka", "e"]. Algorithm To solve this, we will follow these steps − count := a map that contains characters in s and their occurrences ... Read More
Suppose we have a 2D binary matrix and another value k. Starting from the top-left cell, we need to reach the bottom-right cell. In one step, we can only move down or right. The score of a path is the sum of values in all cells on that path. We have to find the number of paths from start to end with score exactly k. If there are many possible paths, we return the result modulo 10^9 + 7. Example Consider this matrix with k = 2: 0 0 1 1 0 ... Read More
Suppose we want to paint a row of N fences with K different colors. We want to minimize the cost while ensuring that no two neighboring fences are of the same color. So if we have an N x K matrix where the nth row and kth column represents the cost to paint the nth fence with kth color, we have to find the minimum cost which achieves this goal. So, if the input is like ? 6 4 5 3 2 7 3 4 5 5 4 4 ... Read More
Given a list of positive numbers, we can move any single element to any position to maximize the list's power. The power of a list is calculated as the sum of (index + 1) × value_at_index for all indices. $$\displaystyle\sum\limits_{i=0}^{n-1} (i+1)\times list[i]$$ For example, if nums = [6, 2, 3], we can move 6 to the end to get [2, 3, 6]. The power becomes: (2 × 1) + (3 × 2) + (6 × 3) = 26. Algorithm To solve this problem, we follow these steps − Create a prefix sum array P ... Read More
Finding the length of the longest increasing subsequence with at least k odd values is a dynamic programming problem. We need to explore all possible increasing subsequences and track how many odd numbers each contains. So, if the input is like nums = [12, 14, 16, 5, 7, 8] and k = 2, then the output will be 3, as the longest increasing subsequence with at least 2 odd values is [5, 7, 8]. Algorithm Approach To solve this, we will follow these steps − Initialize best := 0 to track ... Read More
Suppose we have a string s representing the initial positions of animals in a line. Each character represents an animal's movement direction: L indicates the animal moves left, R indicates the animal moves right, and @ indicates the animal is standing still. Moving animals will influence other animals in their path unless they encounter a force from the opposite direction, which causes them to stop. We need to find the final orientation of each animal when all movement stops. Problem Understanding The algorithm uses a level-based approach where animals spread their influence outward. Animals moving in opposite directions ... Read More
Suppose we have a list of numbers called nums that contains 0s and 1s, and another value k representing the sublist length. We can perform an operation where we flip a sublist of length k such that all 1s become 0s and all 0s become 1s. We need to find the minimum number of operations required to change all numbers in nums to 0s. If it's impossible, we return -1. So, if the input is like nums = [1, 1, 1, 0, 0, 1, 1, 1], k = 3, then the output will be 2, as we can ... Read More
Suppose we have a list of numbers called nums that represent positions of houses on a 1-dimensional line. We have 3 street lights that can be placed anywhere, and a light at position x with radius r lights up all houses in range [x - r, x + r], inclusive. We need to find the smallest radius r required to light up all houses. For example, if the input is nums = [4, 5, 6, 7], then the output will be 0.5. We can place the lamps at positions 4.5, 5.5, and 6.5 with radius r = 0.5 to ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance