my image

Dmitrii Volyx

Performance Engineer

Boats to Save People

he i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person. (It is guaranteed each person can be carried by a boat.) Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: ...

May 25, 2020 · 1 min · volyx

Complement of Base 10 Integer

Every non-negative integer N has a binary representation. For example, 5 can be represented as “101” in binary, 11 as “1011” in binary, and so on. Note that except for N = 0, there are no leading zeroes in any binary representation. The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1. For example, the complement of “101” in binary is “010” in binary. ...

May 25, 2020 · 2 min · volyx

Cousins in Binary Tree

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. Two nodes of a binary tree are cousins if they have the same depth, but have different parents. We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree. Return true if and only if the nodes corresponding to the values x and y are cousins. ...

May 25, 2020 · 2 min · volyx

First Unique Character in a String

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters. Solution: class Solution { public int firstUniqChar(String s) { Map<Character, Integer> freq = new HashMap<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); freq.put(c, freq.getOrDefault(c, 0) + 1); } for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (freq.get(c) == 1) { return i; } } return -1; } }

May 25, 2020 · 1 min · volyx

Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: Input: [3,2,3] Output: 3 Example 2: Input: [2,2,1,1,1,2,2] Output: 2 Solution: class Solution { public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length/2]; } }

May 25, 2020 · 1 min · volyx

Guess Number Higher or Lower

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the magazine string can only be used once in your ransom note. Example 1: Input: ransomNote = "a", magazine = "b" Output: false Example 2: Input: ransomNote = "aa", magazine = "ab" Output: false Example 3: ...

May 22, 2020 · 1 min · volyx

First Bad Version

You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels. The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so “a” is considered a different type of stone from “A”. ...

May 20, 2020 · 1 min · volyx

First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad. ...

May 19, 2020 · 3 min · volyx

Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements. Example 1: Input: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 5 / \ 3 6 / \ 2 4 / 1 Output: 3 Follow up: What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine? ...

May 18, 2020 · 1 min · volyx

Palindromic Substrings

Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. Example 1: Input: "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c". Example 2: Input: "aaa" Output: 6 Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". ``` Note: ``` The input string length won't exceed 1000. ``` Solution ```java class Solution { public int countSubstrings(String s) { int n = s.length(); int[][] a = new int[n][n]; int count = 0; for (int i = 0; i < n; i++) { a[i][i] = 1; count++; } for (int col = 1; col < n; col++) { for (int row = 0; row < col; row++) { if (row == col - 1 && s.charAt(col) == s.charAt(row)) { a[row][col] = 1; count++; } else if (a[row + 1][col - 1] == 1 && s.charAt(col) == s.charAt(row) ) { a[row][col] = 1; count++; } } } return count; } } ``` ![example](/images/2020-05-15-palindromic-substring_1_optimized.png) ![example](/images/2020-05-15-palindromic-substring-matrix_optimized.png)

May 15, 2020 · 1 min · volyx