my image

Dmitrii Volyx

Performance Engineer

319. Bulb Switcher

319. Bulb Switcher There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off if it’s on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Return the number of bulbs that are on after n rounds. ...

September 1, 2021 · 1 min · volyx

581. Shortest Unsorted Continuous Subarray

581. Shortest Unsorted Continuous Subarray Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order. Return the shortest such subarray and output its length. Example 1: Input: nums = [2,6,4,8,10,9,15] Output: 5 Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order. Example 2: Input: nums = [1,2,3,4] Output: 0 Example 3: Input: nums = [1] Output: 0 Constraints: ...

August 26, 2021 · 1 min · volyx

907. Sum of Subarray Minimums

907. Sum of Subarray Minimums Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7. Example 1: Input: arr = [3,1,2,4] Output: 17 Explanation: Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1. Sum is 17. Example 2: Input: arr = [11,81,94,43,3] Output: 444 Constraints: ...

August 25, 2021 · 2 min · volyx

398. Random Pick Index

398. Random Pick Index Given an integer array nums with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Implement the Solution class: Solution(int[] nums) Initializes the object with the array nums. int pick(int target) Picks a random index i from nums where nums[i] == target. If there are multiple valid i’s, then each index should have an equal probability of returning. Example 1: Input ["Solution", "pick", "pick", "pick"] [[[1, 2, 3, 3, 3]], [3], [1], [3]] Output [null, 4, 0, 2] Explanation Solution solution = new Solution([1, 2, 3, 3, 3]); solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning. solution.pick(1); // It should return 0. Since in the array only nums[0] is equal to 1. solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning. Constraints: ...

August 23, 2021 · 2 min · volyx

364. Nested List Weight Sum II

364. Nested List Weight Sum II You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. The depth of an integer is the number of lists that it is inside of. For example, the nested list [1,[2,2],[[3],2],1] has each integer’s value set to its depth. Let maxDepth be the maximum depth of any integer. ...

August 20, 2021 · 3 min · volyx

901. Online Stock Span

901. Online Stock Span Design an algorithm that collects daily price quotes for some stock and returns the span of that stock’s price for the current day. The span of the stock’s price today is defined as the maximum number of consecutive days (starting from today and going backward) for which the stock price was less than or equal to today’s price. For example, if the price of a stock over the next 7 days were [100,80,60,70,60,75,85], then the stock spans would be [1,1,1,2,1,4,6]. Implement the StockSpanner class: ...

August 19, 2021 · 2 min · volyx

1762. Buildings With an Ocean View

1762. Buildings With an Ocean View There are n buildings in a line. You are given an integer array heights of size n that represents the heights of the buildings in the line. The ocean is to the right of the buildings. A building has an ocean view if the building can see the ocean without obstructions. Formally, a building has an ocean view if all the buildings to its right have a smaller height. ...

August 18, 2021 · 2 min · volyx

84. Largest Rectangle in Histogram

84. Largest Rectangle in Histogram Given an array of integers heights representing the histogram’s bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram. Example 1: Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units. ...

August 17, 2021 · 3 min · volyx

1360. Number of Days Between Two Dates

1360. Number of Days Between Two Dates Write a program to count the number of days between two dates. The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples. Example 1: Input: date1 = "2019-06-29", date2 = "2019-06-30" Output: 1 Example 2: Input: date1 = "2020-01-15", date2 = "2019-12-31" Output: 15 Constraints: The given dates are valid dates between the years 1971 and 2100. Solution import java.time.*; class Solution { public int daysBetweenDates(String date1, String date2) { return Math.abs(countDays(date1) - countDays(date2)); } int [] MONTHS = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int countDays(String date) { String[] parts = date.split("-"); Integer years = Integer.parseInt(parts[0]); Integer months = Integer.parseInt(parts[1]); Integer days = Integer.parseInt(parts[2]); for (int year = 1971; year < years; year++) { if (isLeapYear(year)) { days += 366; } else { days += 365; } } for (int month = 1; month < months; month++) { if (isLeapYear(years) && month == 2) { days += 29; } else { days += MONTHS[month]; } } return days; } boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } public int daysBetweenDates2(String date1, String date2) { LocalDateTime time1 = LocalDate.parse(date1).atTime(LocalTime.NOON); LocalDateTime time2 = LocalDate.parse(date2).atTime(LocalTime.NOON); Duration duration = Duration.between(time1, time2); return (int) Math.abs(duration.toDays()); } }

August 15, 2021 · 2 min · volyx

827. Making A Large Island

827. Making A Large Island You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1. Return the size of the largest island in grid after applying this operation. An island is a 4-directionally connected group of 1s. Example 1: Input: grid = [[1,0],[0,1]] Output: 3 Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3. Example 2: Input: grid = [[1,1],[1,0]] Output: 4 Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4. Example 3: Input: grid = [[1,1],[1,1]] Output: 4 Explanation: Can't change any 0 to 1, only one island with area = 4. Constraints: ...

August 6, 2021 · 3 min · volyx