Regions Cut By Slashes

An a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, , or blank space. These characters divide the square into contiguous regions. (Note that backslash characters are escaped, so a \ is represented as “\”.) Return the number of regions. Example 1: Input: [ " /", "/ " ] Output: 2 Explanation: The 2x2 grid is as follows: ...

November 8, 2020 · 6 min · volyx

Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Example 4: ...

November 7, 2020 · 1 min · volyx

Buddy Strings

Given two strings A and B of lowercase letters, return true if you can swap two letters in A so the result is equal to B, otherwise, return false. Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at A[i] and A[j]. For example, swapping at indices 0 and 2 in “abcd” results in “cbad”. Example 1: Input: A = “ab”, B = “ba” Output: true Explanation: You can swap A[0] = ‘a’ and A[1] = ‘b’ to get “ba”, which is equal to B. ...

October 28, 2020 · 2 min · volyx

Sqrt(x)

Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. Example 1: Input: 4 Output: 2 Example 2: Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842…, and since the decimal part is truncated, 2 is returned. ...

October 28, 2020 · 1 min · volyx

Pow(x, n)

Implement pow(x, n), which calculates x raised to the power n (i.e. xn). Example 1: Input: x = 2.00000, n = 10 Output: 1024.00000 Example 2: Input: x = 2.10000, n = 3 Output: 9.26100 Example 3: Input: x = 2.00000, n = -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/4 = 0.25 Constraints: -100.0 < x < 100.0 -231 <= n <= 231-1 -104 <= xn <= 104 ...

October 27, 2020 · 1 min · volyx

Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its zigzag level order traversal as: [ [3], [20,9], [15,7] ] Solution: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> result = new ArrayList<>(); dfs(root, result, 0); return result; } void dfs(TreeNode node, List<List<Integer>> result, int level) { if (node == null) { return; } checkSize(result, level); if (level % 2 == 1) { result.get(level).add(0, node.val); } else { result.get(level).add(node.val); } dfs(node.left, result, level + 1); dfs(node.right, result, level + 1); } void checkSize(List<List<Integer>> result, int level) { // 0 [ [] ] // 1 [[][]] // 2 [ [][] ] if (result.size() <= level) { result.add(new ArrayList<>()); } } } Solution 2021-11-18 DFS, DFS + Stack, BFS /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); Queue<TreeNode> q = new ArrayDeque<>(); if (root != null) { q.add(root); } int level = 0; while (q.size() > 0) { int size = q.size(); List<Integer> levelList = new ArrayList(); for (int i = 0; i < size; i++) { TreeNode node = q.poll(); if (level % 2 == 0) { levelList.add(node.val); } else { levelList.add(0, node.val); } if (node.left != null) { q.add(node.left); } if (node.right != null) { q.add(node.right); } } res.add(levelList); level++; } return res; } public List<List<Integer>> zigzagLevelOrderDfsStack(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); Stack<Pair<TreeNode, Integer>> stack = new Stack<>(); if (root != null) { stack.push(new Pair<>(root, 0)); } while (stack.size() > 0) { Pair<TreeNode, Integer> curr = stack.pop(); TreeNode node = curr.getKey(); Integer level = curr.getValue(); if (res.size() == level) { res.add(new ArrayList<>()); } if (level % 2 == 0) { res.get(level).add(node.val); } else { res.get(level).add(0, node.val); } if (node.right != null) { stack.push(new Pair<>(node.right, level + 1)); } if (node.left != null) { stack.push(new Pair<>(node.left, level + 1)); } } return res; } public List<List<Integer>> zigzagLevelOrderDfsRecursive(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); dfs(root, 0, res); return res; } void dfs(TreeNode node, int level, List<List<Integer>> res) { if (node == null) return; if (res.size() == level) { res.add(new ArrayList<>()); } if (level % 2 == 0) { res.get(level).add(node.val); } else { res.get(level).add(0, node.val); } dfs(node.left, level + 1, res); dfs(node.right, level + 1, res); } }

October 21, 2020 · 3 min · volyx

Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size. It’s guaranteed that the answer is unique, in other words the set of the top k frequent elements is unique. You can return the answer in any order. Solution: ...

October 18, 2020 · 2 min · volyx

Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000]. Multiplication of any three numbers in the input won’t exceed the range of 32-bit signed integer. Solution: class Solution { public int maximumProduct(int[] nums) { int s1 = Integer.MAX_VALUE; int s2 = Integer.MAX_VALUE; int b1 = Integer.MIN_VALUE; int b2 = Integer.MIN_VALUE; int b3 = Integer.MIN_VALUE; for (int value: nums) { if (value < s1) { s2 = s1; s1 = value; } else if (value < s2) { s2 = value; } if (value > b3) { b1 = b2; b2 = b3; b3 = value; } else if (value > b2) { b1 = b2; b2 = value; } else if (value > b1) { b1 = value; } } return Math.max(s1 * s2 * b3, b1 * b2 * b3); } }

October 17, 2020 · 1 min · volyx

Time Needed to Inform All Employees

A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company has is the one with headID. Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also it’s guaranteed that the subordination relationships have a tree structure. The head of the company wants to inform all the employees of the company of an urgent piece of news. He will inform his direct subordinates and they will inform their subordinates and so on until all employees know about the urgent news. ...

September 16, 2020 · 3 min · volyx

Flip Equivalent Binary Trees

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations. Given the roots of two binary trees root1 and root2, return true if the two trees are flip equivelent or false otherwise. ...

September 7, 2020 · 2 min · volyx