Category: Java programming question
-
LeetCode Problem 1266: Minimum Time Visiting All Points
Problem Overview You are given an array points where points[i] = [xᵢ, yᵢ] represents a point on a 2D plane.You start at the first point and must visit all points sequentially (in the order they appear). In one second, you can: You need to return the minimum time (in seconds) required to visit all the…
-
LeetCode Problem 380: Insert Delete GetRandom O(1)
Problem Overview You need to design a data structure that supports the following operations in average O(1) time complexity: Example: Explanation: Intuition / Approach We need O(1) time for insert, remove, and getRandom, which rules out typical data structures like plain lists or sets. We can achieve this by combining: Key Idea: This avoids O(n)…
-
LeetCode Problem 75: Sort Colors
Problem Overview You are given an integer array nums with n elements, where each element is 0, 1, or 2.You need to sort the array in-place so that all 0s come first, followed by all 1s, and then all 2s. This problem is also known as the Dutch National Flag problem, introduced by Edsger W.…
-
LeetCode Problem 35: Search Insert Position
Problem Overview You are given a sorted array of distinct integers nums and a target value target.Your task is to find the index of the target in the array if it exists.If it does not exist, return the index where it would be inserted to maintain the sorted order. Example 1: Example 2: Example 3:…
-
LeetCode Problem 392: Is Subsequence
Problem Overview You are given two strings s and t. Your task is to determine whether s is a subsequence of t. A string s is a subsequence of t if all characters of s appear in t in the same order, but not necessarily contiguously. Example 1: Example 2: Constraints: Intuition / Approach This…
-
LeetCode Problem 547: Number of Provinces
Problem Overview You are given an n x n matrix isConnected where isConnected[i][j] = 1 means that city i and city j are directly connected, and isConnected[i][j] = 0 means they are not. A province is a group of directly or indirectly connected cities, and no other cities outside the group are connected to that…
-
LeetCode Problem 231: Power of Two
Problem Overview You are given an integer n. The task is to determine whether n is a power of two.A number is a power of two if it can be expressed as 2^x, where x is a non-negative integer. In other words: Example 1: Example 2: Example 3: Constraints: Intuition / Approach A number that…
-
LeetCode Problem 518: Coin Change II
Problem Overview You are given an integer amount representing a total sum of money and an integer array coins representing the denominations of available coins.Your task is to find the number of combinations that make up the given amount using any number of coins.Each coin can be used unlimited times, but the order of coins…
-
LeetCode Problem 406: Queue Reconstruction by Height
Problem Overview You are given an array of people represented as pairs of integers, where each pair is in the form (h, k): Your task is to reconstruct the original queue that satisfies all the given conditions. Example 1: Example 2: Intuition / Approach This problem seems tricky at first because both height and k…
-
LeetCode Problem 528: Random Pick with Weight
Problem Overview You are given an array of positive integers w, where each element represents a weight associated with an index.Your task is to design a function pickIndex() that randomly returns an index in proportion to its weight. In other words, an index with a larger weight should be more likely to be chosen. Example:…
