Category: Java programming question
-
LeetCode Problem 222: Count Complete Tree Nodes
Problem Overview You are given the root of a complete binary tree, and your task is to count the total number of nodes present in the tree. A complete binary tree is a special type of binary tree where: Your goal is to determine the total number of nodes efficiently without performing a simple traversal…
-
LeetCode Problem 174: Dungeon Game
Problem Overview The Dungeon Game is a classic dynamic programming problem that tests your ability to plan backward from a goal. You are given a 2D grid dungeon where each cell contains an integer representing the health impact on a knight: The knight starts at the top-left cell (0,0) and must reach the bottom-right cell…
-
LeetCode Problem 60: Permutation Sequence
Problem Overview You are given two integers n and k.You need to return the k-th permutation sequence (in lexicographic order) of the numbers [1, 2, 3, …, n]. Example 1: Example 2: Example 3: Intuition / Approach The naive way would be to generate all permutations using backtracking and then pick the k-th one.However, that…
-
LeetCode Problem 1044: Longest Duplicate Substring
Problem Overview You are given a string s, and your task is to find the longest substring that appears at least twice in the string.These two occurrences may overlap. If there is no duplicate substring, return an empty string. Example 1: Example 2: Intuition / Approach This is a hard problem that combines binary search…
-
LeetCode Problem 275: H-Index II
Problem Overview You are given an integer array citations sorted in ascending order, where citations[i] represents the number of citations a researcher received for their i-th paper.You need to compute the researcher’s h-index. The h-index is defined as the maximum value h such that the researcher has at least h papers with at least h…
-
LeetCode Problem 130: Surrounded Regions
Problem Overview You are given an m x n matrix board containing characters ‘X’ and ‘O’.You need to capture all regions surrounded by ‘X’. A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region. A region is considered surrounded if it is completely enclosed by ‘X’s from all sides — top,…
-
LeetCode Problem 468: Validate IP Address
Problem Overview You are given a string queryIP, and you need to determine whether it is a valid IPv4 address, a valid IPv6 address, or neither. Return: Example 1 Example 2 Example 3 Understanding IP Formats IPv4 Rules IPv6 Rules Approach We’ll check both formats separately: To simplify: Algorithm Steps Java Code Implementation Example Dry…
-
LeetCode Problem 700: Search in a Binary Search Tree
Problem Overview You are given the root of a Binary Search Tree (BST) and an integer val.Your task is to find the node in the BST whose value equals val and return the subtree rooted at that node.If such a node does not exist, return null. A Binary Search Tree (BST) has the property that…
-
LeetCode Problem 787: Cheapest Flights Within K Stops
Problem Overview You are given: Your task is to find the cheapest price to travel from src to dst with at most k stops.If there is no such route, return -1. Example 1 Example 2 Intuition This problem can be viewed as finding the shortest path in a weighted directed graph with an additional constraint…
-
LeetCode Problem 368: Largest Divisible Subset
Problem Overview You are given a set of distinct positive integers nums.Your task is to return the largest subset such that every pair (a, b) in the subset satisfies the condition: If there are multiple answers, return any of them. Example 1: Example 2: Intuition The subset condition a % b == 0 or b…
