my image

Dmitrii Volyx

Performance Engineer

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

Roman to Integer

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. ...

September 6, 2020 · 3 min · volyx

Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle. ...

September 5, 2020 · 2 min · volyx

Populating Next Right Pointers in Each Node

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. ...

August 26, 2020 · 2 min · volyx