Category: Java programming question
-
LeetCode Problem 430: Flatten a Multilevel Doubly Linked List
Problem Statement You are given a doubly linked list where in addition to the next and prev pointers, each node also has a child pointer that may point to a separate doubly linked list. These child lists may have one or more levels of children themselves. Your task is to flatten the list so that…
-
LeetCode Problem 662: Maximum Width of Binary Tree
Problem Statement Given the root of a binary tree, return the maximum width of the given tree. The width of a level** is defined as the length between the leftmost and rightmost non-null nodes, considering the null nodes between them in the complete binary tree structure. If the input tree is empty, return 0. Example:…
-
LeetCode Problem 15: 3Sum
Problem Statement Given an integer array nums, return all unique triplets [nums[i], nums[j], nums[k]] such that: The solution set must not contain duplicate triplets. Example: Intuition We need to find all unique triplets that sum up to zero.A brute-force approach would try all possible triplets (O(n³)), but we can do much better using sorting and…
-
LeetCode Problem 463: Island Perimeter
Problem Statement You are given a 2D grid map where: The grid is completely surrounded by water, and there is exactly one island (a group of connected lands).The island doesn’t have any lakes (water inside the island). You need to return the perimeter of the island. Example: Approach: Count Land Edges and Subtract Shared Sides…
-
LeetCode Problem 66: Plus One
Problem Statement You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the number.Add one to the integer and return the resulting array of digits. Example: Approach Java Solution Complexity Analysis Dry Run Input: [9, 9, 9] Step-by-step: Output: [1, 0, 0, 0]
-
LeetCode Problem 461: Hamming Distance
Problem Statement The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, return the Hamming distance between them. Example: Approach 1: XOR + Bit Count Java Solution Alternative using built-in method: Complexity Analysis Dry Run Input: x = 1 (0001), y…
-
LeetCode Problem 264: Ugly Number II
1. Problem Description You are asked to find the n-th ugly number. An ugly number is a positive integer whose prime factors are only 2, 3, or 5.The sequence of ugly numbers starts with: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … Your task:Given an integer n, return the n-th ugly…
-
LeetCode Problem 957: Prison Cells After N Days
1. Problem Description You are given an array cells of length 8, where each cell can be either occupied (1) or vacant (0).Each day, every cell changes state based on the following rule: Given an integer N, you need to return the state of the prison after N days. 2. Example Input:cells = [0,1,0,1,1,0,0,1], N…
-
LeetCode Problem 107: Binary Tree Level Order Traversal II
1. Problem Statement Given the root of a binary tree, return the bottom-up level order traversal of its nodes’ values.That is, traverse the tree level by level from left to right, but return the levels from bottom to top. Example 1:Input: Output: [[15,7],[9,20],[3]] Example 2:Input: root = [1]Output: [[1]] Example 3:Input: root = []Output: []…
-
LeetCode Problem 306: Additive Number
1. Problem Statement An additive number is a string whose digits can form an additive sequence.A valid additive sequence is a sequence of numbers where each number (after the first two) is the sum of the previous two. You are given a string num representing a non-negative integer.Return true if num is an additive number,…
