my image

Dmitrii Volyx

Performance Engineer

1319. Number of Operations to Make Network Connected

![https://leetcode.com/problems/remove-outermost-parentheses/] There are n computers numbered from 0 to n-1 connected by ethernet cables connections forming a network where connections[i] = [a, b] represents a connection between computers a and b. Any computer can reach any other computer directly or indirectly through the network. Given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected. Return the minimum number of times you need to do this in order to make all the computers connected. If it’s not possible, return -1. ...

February 22, 2021 · 3 min · volyx

150. Evaluate Reverse Polish Notation

![https://leetcode.com/problems/evaluate-reverse-polish-notation/] Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Note: Division between two integers should truncate toward zero. The given RPN expression is always valid. That means the expression would always evaluate to a result and there won’t be any divide by zero operation. Example 1: Input: ["2", "1", "+", "3", "*"] Output: 9 Explanation: ((2 + 1) * 3) = 9 Example 2: Input: ["4", "13", "5", "/", "+"] Output: 6 Explanation: (4 + (13 / 5)) = 6 Example 3: Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] Output: 22 Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 = ((10 * (6 / (12 * -11))) + 17) + 5 = ((10 * (6 / -132)) + 17) + 5 = ((10 * 0) + 17) + 5 = (0 + 17) + 5 = 17 + 5 = 22 class Solution { public int evalRPN(String[] tokens) { int[] stack = new int[tokens.length]; int size = 0; for (String token: tokens) { if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/") ) { int b = stack[--size]; int a = stack[--size]; int c; switch (token) { case "+": c = a + b; break; case "-": c = a - b; break; case "*": c = a * b; break; case "/": c = a / b; break; default: throw new RuntimeException(); } stack[size++] = c; } else { stack[size++] = Integer.valueOf(token); } } return stack[0]; } }

February 22, 2021 · 2 min · volyx

1598. Crawler Log Folder

![https://leetcode.com/problems/crawler-log-folder/] The Leetcode file system keeps a log each time some user performs a change folder operation. The operations are described below: “../” : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder). “./” : Remain in the same folder. “x/” : Move to the child folder named x (This folder is guaranteed to always exist). You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step. ...

February 22, 2021 · 3 min · volyx

1598. Crawler Log Folder

![https://leetcode.com/problems/crawler-log-folder/] The Leetcode file system keeps a log each time some user performs a change folder operation. The operations are described below: “../” : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder). “./” : Remain in the same folder. “x/” : Move to the child folder named x (This folder is guaranteed to always exist). You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step. ...

February 22, 2021 · 3 min · volyx

1021. Remove Outermost Parentheses

![https://leetcode.com/problems/remove-outermost-parentheses/] A valid parentheses string is either empty (""), “(” + A + “)”, or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, “”, “()”, “(())()”, and “(()(()))” are all valid parentheses strings. A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings. ...

February 20, 2021 · 2 min · volyx

1047. Remove All Adjacent Duplicates In String

Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them. We repeatedly make duplicate removals on S until we no longer can. Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique. Example 1: Input: "abbaca" Output: "ca" Explanation: For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca". Note: ...

February 20, 2021 · 2 min · volyx

1209. Remove All Adjacent Duplicates in String II

![https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/] Given a string s, a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them causing the left and the right side of the deleted substring to concatenate together. We repeatedly make k duplicate removals on s until we no longer can. Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is unique. Example 1: Input: s = "abcd", k = 2 Output: "abcd" Explanation: There's nothing to delete. Example 2: Input: s = "deeedbbcccbdaa", k = 3 Output: "aa" Explanation: First delete "eee" and "ccc", get "ddbbbdaa" Then delete "bbb", get "dddaa" Finally delete "ddd", get "aa" Example 3: Input: s = "pbbcggttciiippooaais", k = 2 Output: "ps" Constraints: ...

February 20, 2021 · 3 min · volyx

155. Min Stack

![https://leetcode.com/problems/min-stack/] Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. getMin() – Retrieve the minimum element in the stack. Example 1: Input ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] Output [null,null,null,null,-3,null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return -3 minStack.pop(); minStack.top(); // return 0 minStack.getMin(); // return -2 Constraints: ...

February 20, 2021 · 1 min · volyx

227. Basic Calculator II

![https://leetcode.com/problems/basic-calculator-ii/] Given a string s which represents an expression, evaluate this expression and return its value. The integer division should truncate toward zero. Example 1: Input: s = "3+2*2" Output: 7 Example 2: Input: s = " 3/2 " Output: 1 Example 3: Input: s = " 3+5 / 2 " Output: 5 Constraints: 1 <= s.length <= 3 * 105 s consists of integers and operators (’+’, ‘-’, ‘*’, ‘/’) separated by some number of spaces. s represents a valid expression. All the integers in the expression are non-negative integers in the range [0, 231 - 1]. The answer is guaranteed to fit in a 32-bit integer. class Solution { public int calculate(String s) { var values = new int[s.length()]; var signs = new char[s.length()]; int valuesSize = 0; int signsSize = 0; char[] symbols = s.toCharArray(); for (int i = 0; i < symbols.length; i++) { char c = symbols[i]; if (c == ' ') { continue; } if (Character.isDigit(c)) { int j = i; int val = 0; while (j < symbols.length && Character.isDigit(symbols[j])) { val = (val * 10) + (symbols[j] - '0'); j++; } i = j - 1; if (signsSize > 0) { char op = signs[signsSize - 1]; if (op == '*' || op == '/') { signsSize--; int prev = values[valuesSize - 1]; valuesSize--; if (op == '*') { val = val * prev; } else { val = prev / val; } } } values[valuesSize++] = val; } else { signs[signsSize++] = c; } // System.out.println("signs = " + Arrays.toString(signs)); // System.out.println("values = " + Arrays.toString(values)); } int j = 0; int res = values[0]; for (int i = 0; i < valuesSize - 1; i++) { char op = signs[j++]; int a = values[i]; int b = values[i + 1]; res = (op == '+') ? a + b : a - b; values[i + 1] = res; } return res; } }

February 20, 2021 · 2 min · volyx

844. Backspace String Compare

![https://leetcode.com/problems/backspace-string-compare/] Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Note that after backspacing an empty text, the text will continue empty. Example 1: Input: S = "ab#c", T = "ad#c" Output: true Explanation: Both S and T become "ac". Example 2: Input: S = "ab##", T = "c#d#" Output: true Explanation: Both S and T become "". Example 3: Input: S = "a##c", T = "#a#c" Output: true Explanation: Both S and T become "c". Example 4: Input: S = "a#c", T = "b" Output: false Explanation: S becomes "c" while T becomes "b". Note: ...

February 20, 2021 · 2 min · volyx