218. The Skyline Problem

218. The Skyline Problem A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Given the locations and heights of all the buildings, return the skyline formed by these buildings collectively. The geometric information of each building is given in the array buildings where buildings[i] = [lefti, righti, heighti]: lefti is the x coordinate of the left edge of the ith building. righti is the x coordinate of the right edge of the ith building. heighti is the height of the ith building. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0. ...

July 30, 2021 · 3 min · volyx

987. Vertical Order Traversal of a Binary Tree

987. Vertical Order Traversal of a Binary Tree Given the root of a binary tree, calculate the vertical order traversal of the binary tree. For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0). The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values. ...

July 22, 2021 · 5 min · volyx

1235. Maximum Profit in Job Scheduling

1235. Maximum Profit in Job Scheduling We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i]. You’re given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range. If you choose a job that ends at time X you will be able to start another job that starts at time X. ...

July 10, 2021 · 2 min · volyx

51. N-Queens

51. N-Queens The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order. Each solution contains a distinct board configuration of the n-queens’ placement, where ‘Q’ and ‘.’ both indicate a queen and an empty space, respectively. ...

July 6, 2021 · 2 min · volyx

340. Longest Substring with At Most K Distinct Characters

340. Longest Substring with At Most K Distinct Characters Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters. Example 1: Input: s = "eceba", k = 2 Output: 3 Explanation: The substring is "ece" with length 3. Example 2: Input: s = "aa", k = 1 Output: 2 Explanation: The substring is "aa" with length 2. Constraints: ...

June 23, 2021 · 2 min · volyx

76. Minimum Window Substring

76. Minimum Window Substring Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string “”. The testcases will be generated such that the answer is unique. A substring is a contiguous sequence of characters within the string. Example 1: Input: s = "ADOBECODEBANC", t = "ABC" Output: "BANC" Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t. Example 2: Input: s = "a", t = "a" Output: "a" Explanation: The entire string s is the minimum window. Example 3: Input: s = "a", t = "aa" Output: "" Explanation: Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string. Constraints: ...

June 23, 2021 · 2 min · volyx

301. Remove Invalid Parentheses

301. Remove Invalid Parentheses Given a string s that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid. Return all the possible results. You may return the answer in any order. Example 1: Input: s = "()())()" Output: ["(())()","()()()"] Example 2: Input: s = "(a)())()" Output: ["(a())()","(a)()()"] Example 3: Input: s = ")(" Output: [""] Constraints: 1 <= s.length <= 25 s consists of lowercase English letters and parentheses ‘(’ and ‘)’. There will be at most 20 parentheses in s. Solution class Solution { public List<String> removeInvalidParentheses(String s) { TreeMap<Integer, Set<String>> res = new TreeMap<>(); back(0, s, "", res, 0, 0); return new ArrayList<>(res.firstEntry().getValue()); } void back(int index, String s, String current, Map<Integer, Set<String>> res, int open, int closed) { if (index == s.length()) { if (open == closed) { int removed = s.length() - current.length(); Set<String> values = res.getOrDefault(removed, new TreeSet<>()); values.add(current); res.put(removed, values); } return; } if (closed > open) return; char c = s.charAt(index); if (c == '(' || c == ')') { back(index + 1, s, current + c, res, (c == '(') ? open + 1: open, (c == ')') ? closed + 1: closed); back(index + 1, s, current, res, open, closed); } else { back(index + 1, s, current + c, res, open, closed); } } } Solution 2021-10-15 class Solution { public List<String> removeInvalidParentheses(String s) { int opened = 0; int closed = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '(') { opened++; } else if (c == ')') { if (opened > 0) { opened--; } else { closed++; } } } List<String> res = new ArrayList<>(); StringBuilder sb = new StringBuilder(); backtrack(0, s, res, opened, closed, 0, sb); return res; } void backtrack(int pos, String s, List<String> res, int opened, int closed, int currOpened, StringBuilder sb) { if (opened < 0 || closed < 0 || currOpened < 0) return; if (pos == s.length()) { if (opened == 0 && closed == 0 && currOpened == 0) { if (!res.contains(sb.toString())) { res.add(sb.toString()); } } return; } if (opened < 0 || closed < 0 || currOpened < 0) return; // "()())()" // 0123456 // (())() 0 0 0 pos = 2 char c = s.charAt(pos); if (c != '(' && c != ')') { sb.append(c); backtrack(pos + 1, s, res, opened, closed, currOpened, sb); } if (c == '(') { // remove backtrack(pos + 1, s, res, opened - 1, closed, currOpened, sb); //skip sb.append(c); backtrack(pos + 1, s, res, opened, closed, currOpened + 1, sb); } else if (c == ')') { // remove backtrack(pos + 1, s, res, opened, closed - 1, currOpened, sb); //skip sb.append(c); backtrack(pos + 1, s, res, opened, closed, currOpened - 1, sb); } sb.deleteCharAt(sb.length() - 1); } } Solution 2021-10-23 class Solution { public List<String> removeInvalidParentheses(String s) { int opened = 0; int closed = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '(') { opened++; } else if (c == ')') { if (opened > 0) { opened--; } else { closed++; } } } List<String> res = new ArrayList<>(); StringBuilder sb = new StringBuilder(); back(s, 0, opened, closed, sb, res, 0, 0); return res; } void back(String s, int index, int opened, int closed, StringBuilder sb, List<String> res, int currOpen, int currClose) { if (opened < 0 || closed < 0) return; if (currClose > currOpen) return; if (index == s.length()) { if (closed == 0 && opened == 0) { String str = sb.toString(); if (!res.contains(str)) { res.add(str); } } return; } char c = s.charAt(index); if (c == '(') { back(s, index + 1, opened - 1, closed, sb, res, currOpen, currClose); } else if (c == ')') { back(s, index + 1, opened, closed - 1, sb, res, currOpen, currClose); } sb.append(c); back(s, index + 1, opened, closed, sb, res, c == '('? currOpen + 1: currOpen, c == ')'? currClose + 1: currClose); sb.deleteCharAt(sb.length() - 1); } } Solution 2022-01-24 class Solution { public List<String> removeInvalidParentheses(String s) { StringBuilder sb = new StringBuilder(); Set<String> res = new HashSet<>(); int open = 0; for (char c: s.toCharArray()) { if (c == '(') { open++; } else if (c == ')') { if (open > 0) { open--; } } } int close = 0; for (int i = s.length() - 1; i >= 0; i--) { char c = s.charAt(i); if (c == '(') { if (close > 0) { close--; } } else if (c == ')') { close++; } } // System.out.println(open + " " + close); backtrack(0, s, 0, open, close, sb, res); return new ArrayList<>(res); } void backtrack(int index, String s, int openClosed, int open, int close, StringBuilder sb, Set<String> res) { if (open < 0 || close < 0) return; if (index == s.length()) { if (open == 0 && close == 0) { res.add(sb.toString()); } return; } char c = s.charAt(index); if (c == '(') { sb.append(c); // add open backtrack(index + 1, s, openClosed + 1, open, close, sb, res); sb.deleteCharAt(sb.length() - 1); // skip open backtrack(index + 1, s, openClosed, open - 1, close, sb, res); } else if (c == ')') { if (openClosed > 0) { sb.append(c); // add close backtrack(index + 1, s, openClosed - 1, open, close, sb, res); sb.deleteCharAt(sb.length() - 1); } // skip close backtrack(index + 1, s, openClosed, open, close - 1, sb, res); } else { sb.append(c); backtrack(index + 1, s, openClosed, open, close, sb, res); sb.deleteCharAt(sb.length() - 1); } } }

June 6, 2021 · 5 min · volyx

472. Concatenated Words

472. Concatenated Words Given an array of strings words (without duplicates), return all the concatenated words in the given list of words. A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array. Example 1: Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"] Output: ["catsdogcats","dogcatsdog","ratcatdogcat"] Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats"; "dogcatsdog" can be concatenated by "dog", "cats" and "dog"; "ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat". Example 2: Input: words = ["cat","dog","catdog"] Output: ["catdog"] Constraints: ...

May 23, 2021 · 2 min · volyx

72. Edit Distance

72. Edit Distance Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = "horse", word2 = "ros" Output: 3 Explanation: horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e') Example 2: Input: word1 = "intention", word2 = "execution" Output: 5 Explanation: intention -> inention (remove 't') inention -> enention (replace 'i' with 'e') enention -> exention (replace 'n' with 'x') exention -> exection (replace 'n' with 'c') exection -> execution (insert 'u') Constraints: ...

May 5, 2021 · 2 min · volyx

1192. Critical Connections in a Network

1514. Path with Maximum Probability There are n servers numbered from 0 to n-1 connected by undirected server-to-server connections forming a network where connections[i] = [a, b] represents a connection between servers a and b. Any server can reach any other server directly or indirectly through the network. A critical connection is a connection that, if removed, will make some server unable to reach some other server. Return all critical connections in the network in any order. ...

April 8, 2021 · 2 min · volyx