목록Programming/LeetCode (34)
MJay
Do you know when we should use two stacks to implement a queue? I was asked in an internship interview with a company two years ago. The application for this implementation is to separate read & write of a queue in multi-processing. One of the stacks is for reading, and another is to write. They only interfere with each other when the former one is full or the latter is empty. Python에서는 Stack이 L..
1135. Connecting Cities With Minimum Cost Medium 1155FavoriteShare There are N cities numbered from 1 to N. You are given connections, where each connections[i] = [city1, city2, cost] represents the cost to connect city1 and city2 together. (A connection is bidirectional: connecting city1 and city2 is the same as connecting city2 and city1.) Return the minimum cost so that for every pair of citi..
1064. Fixed Point Easy 8429FavoriteShare Given an array A of distinct integers sorted in ascending order, return the smallest index i that satisfies A[i] == i. Return -1 if no such i exists. Example 1: Input: [-10,-5,0,3,7] Output: 3 Explanation: For the given array, A[0] = -10, A[1] = -5, A[2] = 0, A[3] = 3, thus the output is 3. Example 2: Input: [0,2,5,8,17] Output: 0 Explanation: A[0] = 0, t..
647. Palindromic Substrings Medium 173587FavoriteShare 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 E..
241. Different Ways to Add Parentheses Medium 118661FavoriteShare Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *. Example 1: Input: "2-1-1" Output: [0, 2] Explanation: ((2-1)-1) = 0 (2-(1-1)) = 2 Example 2: Input: "2*3-4*5" Output: [-34, -14, -10, -10, 10] Expla..
841. Keys and Rooms Medium 61152 FavoriteShare There are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2,..., N-1, and each room may have some keys to access the next room. Formally, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1,..., N-1] where N = rooms.length. A key rooms[i][j] = v opens the room with number v. Initially, a..
1046. Last Stone Weight Easy 23714FavoriteShare We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose the two heaviest rocks and smash them together. Suppose the stones have weights x and y with x
1221. Split a String in Balanced Strings Easy 9660 FavoriteShare Balanced strings are those who have an equal quantity of 'L' and 'R' characters. Given a balanced string s split it in the maximum amount of balanced strings. Return the maximum amount of splitted balanced strings. Example 1: Input: s = "RLRRLLRLRL" Output: 4 Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring..