Posts

Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree

Image
Given a binary tree where each path going from the root to any leaf form a valid sequence , check if a given string is a valid sequence in such binary tree. We get the given string from the concatenation of an array of integers arr and the concatenation of all values of the nodes along a path results in a sequence in the given binary tree. Example 1: Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,0,1] Output: true Explanation: The path 0 -> 1 -> 0 -> 1 is a valid sequence (green color in the figure). Other valid sequences are: 0 -> 1 -> 1 -> 0 0 -> 0 -> 0   Example 2: Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,0,1] Output: false Explanation: The path 0 -> 0 -> 1 does not exist, therefore it is not even a sequence.   Example 3: Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,1] Output: false Explanation: The path 0 -> 1 -> 1 is a sequence, but it is not a

Binary Tree Maximum Path Sum

Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. Example 1: Input: [1,2,3] 1 / \ 2 3 Output: 6   Example 2: Input: [-10,9,20,null,null,15,7]   -10    / \   9   20     /  \     15   7 Output: 42       /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { int result = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { findMax(root); return result; } int findMax(TreeNode root) { if (root == null) return 0; int left = Math.max(findMax(root.left), 0); int right = Math.

First Unique Number

You have a queue of integers, you need to retrieve the first unique integer in the queue. Implement the FirstUnique  class: FirstUnique(int[] nums) Initializes the object with the numbers in the queue. int showFirstUnique()  returns the value of the first unique integer of the queue, and returns -1 if there is no such integer. void add(int value)  insert value to the queue. Example 1: Input: ["FirstUnique","showFirstUnique","add","showFirstUnique","add","showFirstUnique","add","showFirstUnique"] [[[2,3,5]],[],[5],[],[2],[],[3],[]] Output: [null,2,null,2,null,3,null,-1] Explanation: FirstUnique firstUnique = new FirstUnique([2,3,5]); firstUnique.showFirstUnique(); // return 2 firstUnique.add(5); // the queue is now [2,3,5,5] firstUnique.showFirstUnique(); // return 2 firstUnique.add(2);            // the queue is now [2,3,5,5,2] firstUnique.showFirstUnique(); // return 3 first

Xiaomi Announces MIUI 12 and it's new features

Image
MIUI 12 will be rolled out in Q3, which was officially mentioned here. If you are looking for a resource that compiles all of the new MIUI 12 features in one place, then you’ve arrived at the right place. Here are all of the new features in MIUI 12: Also visit here to find list of devices getting MIUI 12 updates. New Features in MIUI 12 Key UI Changes Xiaomi has been working hard to refine the UI for the past few software releases. A close look at MIUI 12 should be enough to tell you that it’s the most minimal and clean UI that the software has offered to date.  The company has also improved app opening and closing animations in MIUI 12 to reduce stutter and tearing. The company has integrated its own technologies and algorithms into its Android skin to get real-time blur in the background while closing an app, for example. Several other new animations can be found across the board – even within system apps. Also visit here to find list of devices getting MIUI 12 u

Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4     public class Solution { public int maximalSquare(char[][] matrix) { int rows = matrix.length, cols = rows > 0 ? matrix[0].length : 0; int maxlen = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix[i][j] == '1') { int len = 1; boolean flag = true; while (len + i < rows && len + j < cols && flag) { for (int k = j; k <= len + j; k++) { if (matrix[i + len][k] == '0') { flag = false; break; } }

How to convert Int to String in Java?

In this article we are going to learn about how to convert int to string in java. It is necessary to know type conversion because, when a number is converted to string it is useful for many easy manipulation. In this you will find all the ways to convert int to string in java and at end of article you will find code containing all learned methods. Integer.toString(int num) It is one of best method to remember, because toString() has ability to convert any data type to string representation. It is also used to provide the string representation of entire class in default way and if you want to retrieve in your wish, then you can override that method and we will discuss about this later on another topic. public static String toString(int num) Parameters num, a integer   Returns string representation of any data type valueOf(int num) It is also another best method which has ability to covert to any data type. valueOf() returns string containing a number.