Posts

Flatten a Multilevel Doubly Linked List

Image
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below. Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.   Example 1: Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12] Output: [1,2,3,7,8,11,12,9,10,4,5,6] Explanation: The multilevel linked list in the input is as follows: After flattening the multilevel linked list it becomes: Example 2: Input: head = [1,2,null,3] Output: [1,3,2] Explanation: The input multilevel linked list is as follows: 1---2---NULL | 3---NULL Example 3: Input: head = [] Output: []   How multilevel linked list is represented in test case: We use the

Maximum Width of Binary Tree

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree , but some nodes are null. The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation. Example 1: Input: 1 / \ 3 2 / \ \ 5 3 9 Output: 4 Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9). Example 2: Input: 1 / 3 / \ 5 3 Output: 2 Explanation: The maximum width existing in the third level with the length 2 (5,3). Example 3: Input: 1 / \ 3 2 / 5 Output: 2 Explanation: The maximum width

3Sum

Given an array nums of n integers, are there elements a , b , c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]   class Solution {     public List<List<Integer>> threeSum(int[] nums) {         if (nums.length < 3) return new ArrayList<>();         Arrays.sort(nums);         Set<List<Integer>> triplet= new HashSet<>();         for (int i = 0; i < nums.length - 2; i++) {             int j = i + 1;             int k = nums.length - 1;             while (j < k) {                 int sum = nums[i] + nums[j] + nums[k];                 if (sum == 0) triplet.add(Arrays.asList(nums[i], nums[j++], nums[k--]));                 else if (sum > 0) k--;                 else if (sum < 0) j++;             }   

Island Perimeter

Image
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island. Example: Input: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] Output: 16 Explanation: The perimeter is the 16 yellow stripes in the image below:   class Solution {     public int islandPerimeter(int[][] grid) {         int lands = 0, neighbours = 0;         for (int i = 0; i < grid.length; i++) {             for (int j = 0; j < grid[i].length; j++) {                 if (grid[i][j] == 1) {     

Plus One

Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself. Example 1: Input: [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Example 2: Input: [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. FAILED APPROACH:   class Solution {     public int[] plusOne(int[] digits) {         digits[digits.length-1]+=1;         return digits;     } } Try it on Leetcode Above code seems to be correct, the mistake occurs when at end contains 9      Input        Output   Output according to our code i.e., [9]      -  [1, 0]    -    [10]        [9, 9]  - [1, 0, 0] -    [9, 10] Thus above code is not a solution for this problem. SUCCESSFULL APPR

Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y , calculate the Hamming distance. Note: 0 ≤ x , y < 2 31 . Example: Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.   class Solution {     public int hammingDistance(int x, int y) {         String str = Integer.toBinaryString(x^y);         return str.replaceAll("0","").length();     } } Try it on Leetcode Here, we are going to solve in more easiest way. The hint hidden in this problem is, we need to convert integer to binary and compare both for any differences in 1's position. 1) Do XOR for both numbers.(XOR of same digits (0, 1) will be same). From this, we get difference in 1's position, but it will be in int type. 2) Convert int to binary using toBinaryString(). 3) R