my image

Dmitrii Volyx

Performance Engineer

523. Continuous Subarray Sum

523. Continuous Subarray Sum Given an integer array nums and an integer k, return true if nums has a continuous subarray of size at least two whose elements sum up to a multiple of k, or false otherwise. An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k. Example 1: Input: nums = [23,2,4,6,7], k = 6 Output: true Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. Example 2: Input: nums = [23,2,6,4,7], k = 6 Output: true Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. Example 3: Input: nums = [23,2,6,4,7], k = 13 Output: false Constraints: ...

June 14, 2021 · 2 min · volyx

67. Add Binary

67. Add Binary Given two binary strings a and b, return their sum as a binary string. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010", b = "1011" Output: "10101" Constraints: 1 <= a.length, b.length <= 104 a and b consist only of ‘0’ or ‘1’ characters. Each string does not contain leading zeros except for the zero itself. Solution class Solution { public String addBinary(String a, String b) { int i = a.length() - 1; int j = b.length() - 1; int carry = 0; StringBuilder sb = new StringBuilder(); while (i >= 0 || j >= 0) { int c1 = (i >= 0) ? a.charAt(i) - '0': 0; int c2 = (j >= 0) ? b.charAt(j) - '0': 0; int c3 = c1 + c2 + carry; if (carry > 0) { carry = 0; } if (c3 == 3) { carry = 1; sb.insert(0, '1'); } else if (c3 == 2) { carry = 1; sb.insert(0, '0'); } else if (c3 == 1) { sb.insert(0, '1'); } else { sb.insert(0, '0'); } i--; j--; } if (carry > 0) { sb.insert(0, '1'); } return sb.toString(); } public String addBinary2(String a, String b) { Integer i1 = Integer.parseInt(a, 2); Integer i2 = Integer.parseInt(b, 2); return Integer.toBinaryString(i1 + i2); } }

June 14, 2021 · 2 min · volyx

974. Subarray Sums Divisible by K

974. Subarray Sums Divisible by K Given an array nums of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by k. Example 1: Input: nums = [4,5,0,-2,-3,1], k = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by k = 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3] Note: ...

June 14, 2021 · 1 min · volyx

1897. Redistribute Characters to Make All Strings Equal

1897. Redistribute Characters to Make All Strings Equal You are given an array of strings words (0-indexed). In one operation, pick two distinct indices i and j, where words[i] is a non-empty string, and move any character from words[i] to any position in words[j]. Return true if you can make every string in words equal using any number of operations, and false otherwise. Example 1: Input: words = ["abc","aabc","bc"] Output: true Explanation: Move the first 'a' in words[1] to the front of words[2], to make words[1] = "abc" and words[2] = "abc". All the strings are now equal to "abc", so return true. Example 2: Input: words = ["ab","a"] Output: false Explanation: It is impossible to make all the strings equal using the operation. Constraints: ...

June 13, 2021 · 1 min · volyx

1898. Maximum Number of Removable Characters

1898. Maximum Number of Removable Characters You are given two strings s and p where p is a subsequence of s. You are also given a distinct 0-indexed integer array removable containing a subset of indices of s (s is also 0-indexed). You want to choose an integer k (0 <= k <= removable.length) such that, after removing k characters from s using the first k indices in removable, p is still a subsequence of s. More formally, you will mark the character at s[removable[i]] for each 0 <= i < k, then remove all marked characters and check if p is still a subsequence. ...

June 13, 2021 · 3 min · volyx

1899. Merge Triplets to Form Target Triplet

1899. Merge Triplets to Form Target Triplet A triplet is an array of three integers. You are given a 2D integer array triplets, where triplets[i] = [ai, bi, ci] describes the ith triplet. You are also given an integer array target = [x, y, z] that describes the triplet you want to obtain. To obtain target, you may apply the following operation on triplets any number of times (possibly zero): ...

June 13, 2021 · 2 min · volyx

31. Next Permutation

31. Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). The replacement must be in place and use only constant extra memory. Example 1: Input: nums = [1,2,3] Output: [1,3,2] Example 2: Input: nums = [3,2,1] Output: [1,2,3] Example 3: Input: nums = [1,1,5] Output: [1,5,1] Example 4: Input: nums = [1] Output: [1] Constraints: ...

June 13, 2021 · 3 min · volyx

338. Counting Bits"

338. Counting Bits Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1’s in the binary representation of i. Example 1: Input: n = 2 Output: [0,1,1] Explanation: 0 --> 0 1 --> 1 2 --> 10 Example 2: Input: n = 5 Output: [0,1,1,2,1,2] Explanation: 0 --> 0 1 --> 1 2 --> 10 3 --> 11 4 --> 100 5 --> 101 Constraints: ...

June 13, 2021 · 1 min · volyx

136. Single Number

136. Single Number Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. Example 1: Input: nums = [2,2,1] Output: 1 Example 2: Input: nums = [4,1,2,1,2] Output: 4 Example 3: Input: nums = [1] Output: 1 Constraints: 1 <= nums.length <= 3 * 104 -3 * 10^4 <= nums[i] <= 3 * 10^4 Each element in the array appears twice except for one element which appears only once. Solution class Solution { public int singleNumber(int[] nums) { int single = 0; for (int num: nums) { single ^= num; } return single; } } Solved 2022-01-15

June 11, 2021 · 1 min · volyx

137. Single Number II

137. Single Number II Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it. You must implement a solution with a linear runtime complexity and use only constant extra space. Example 1: Input: nums = [2,2,3,2] Output: 3 Example 2: Input: nums = [0,1,0,1,0,1,99] Output: 99 Constraints: 1 <= nums.length <= 3 * 104 -231 <= nums[i] <= 231 - 1 Each element in nums appears exactly three times except for one element which appears once. Solution class Solution { public int singleNumber(int[] nums) { long sum = 0; Set<Integer> set = new HashSet<>(); for (int num: nums) { set.add(num); sum+=num; } long uniqueSum = 0; for (int num: set) { uniqueSum += num; } long remainder = (3 * uniqueSum) - sum; return (int) (remainder / 2); } }

June 11, 2021 · 1 min · volyx