problem_id
int64
122
5k
question
stringlengths
50
14k
solutions
stringlengths
12
1.21M
input_output
stringlengths
0
871k
difficulty
stringclasses
3 values
url
stringlengths
36
108
starter_code
stringlengths
0
1.1k
solutions_list
listlengths
1
990
n_sols
int64
1
990
non_interactive_idx
listlengths
1
929
interaction_reason
listlengths
1
990
requires_input()
bool
1 class
122
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
["class Solution:\n def maxScore(self, cardPoints: List[int], k: int) -> int:\n max_score = 0\n curr_score= 0\n init_hand = cardPoints[len(cardPoints)-k:]\n max_score = sum(init_hand)\n curr_score = max_score\n for i in range(k):\n curr_score -= init_hand[i]\n ...
{"fn_name": "maxScore", "inputs": [[[1, 2, 3, 4, 5, 6, 1], 3]], "outputs": [12]}
interview
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int:
[ "class Solution:\n def maxScore(self, cardPoints: List[int], k: int) -> int:\n max_score = 0\n curr_score= 0\n init_hand = cardPoints[len(cardPoints)-k:]\n max_score = sum(init_hand)\n curr_score = max_score\n for i in range(k):\n curr_score -= init_hand[i]\n ...
80
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
123
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip.  You create a playlist so that: Every song is played at least once A song can only be played again only if K other songs have been played Return the number of possible playlists.  As the answe...
["import math\nclass Solution:\n def numMusicPlaylists(self, N: int, L: int, K: int) -> int:\n s=0\n c=0\n r=0\n x=math.factorial(N)\n while(True):\n c=x*((N-r-K)**(L-K))*(-1)**(r)//(math.factorial(N-r-K)*math.factorial(r))\n if(c!=0):\n s=(s+c)...
{"fn_name": "numMusicPlaylists", "inputs": [[3, 3, 1]], "outputs": [6]}
interview
https://leetcode.com/problems/number-of-music-playlists/
class Solution: def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
[ "import math\nclass Solution:\n def numMusicPlaylists(self, N: int, L: int, K: int) -> int:\n s=0\n c=0\n r=0\n x=math.factorial(N)\n while(True):\n c=x*((N-r-K)**(L-K))*(-1)**(r)//(math.factorial(N-r-K)*math.factorial(r))\n if(c!=0):\n s=(s...
36
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
124
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]). You are given a target value to search. If found in the array return true, otherwise return false. Example 1: Input: nums = [2,5,6,0,0,1,2], target = 0 Output: true ...
["class Solution:\n def search(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: int\n :rtype: bool\n \"\"\"\n return target in nums\n", "class Solution:\n def search(self, nums, target):\n \"\"\"\n :type nums: List[int]\n ...
{"fn_name": "search", "inputs": [[[2, 5, 6, 0, 0, 1, 2], 0]], "outputs": [true]}
interview
https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
class Solution: def search(self, nums: List[int], target: int) -> bool:
[ "class Solution:\n def search(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: int\n :rtype: bool\n \"\"\"\n return target in nums\n", "class Solution:\n def search(self, nums, target):\n \"\"\"\n :type nums: List[int]\n ...
15
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
125
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array. Example1: a = 2 b = [3] Result: 8 Example2: a = 2 b = [1,0] Result: 1024 Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases...
["class Solution:\n def superPow(self, a, b):\n result = 1\n fermatb = (int(''.join(map(str, b)))) % 570\n while fermatb:\n if fermatb & 1:\n result = (result * a) % 1337\n a = (a * a) % 1337\n fermatb >>= 1\n return result", "class...
{"fn_name": "superPow", "inputs": [[2, [3]]], "outputs": [8]}
interview
https://leetcode.com/problems/super-pow/
class Solution: def superPow(self, a: int, b: List[int]) -> int:
[ "class Solution:\n def superPow(self, a, b):\n result = 1\n fermatb = (int(''.join(map(str, b)))) % 570\n while fermatb:\n if fermatb & 1:\n result = (result * a) % 1337\n a = (a * a) % 1337\n fermatb >>= 1\n return result", "...
14
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
126
Given a string s, return the maximum number of ocurrences of any substring under the following rules: The number of unique characters in the substring must be less than or equal to maxLetters. The substring size must be between minSize and maxSize inclusive.   Example 1: Input: s = "aababcaab", maxLetters = 2, minSiz...
["class Solution:\n def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:\n n = len(s)\n count = collections.Counter(s[i : i + minSize] for i in range(0, n - minSize + 1))\n res = 0 \n for k, v in count.items():\n if len(set(k)) <= maxLetters:\n ...
{"fn_name": "maxFreq", "inputs": [["\"aababcaab\"", 2, 3, 4]], "outputs": [2]}
interview
https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/
class Solution: def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
[ "class Solution:\n def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:\n n = len(s)\n count = collections.Counter(s[i : i + minSize] for i in range(0, n - minSize + 1))\n res = 0 \n for k, v in count.items():\n if len(set(k)) <= maxLetters:\n ...
205
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
127
There is a group of G members, and a list of various crimes they could commit. The ith crime generates a profit[i] and requires group[i] members to participate in it. If a member participates in one crime, that member can't participate in another crime. Let's call a profitable scheme any subset of these crimes that gen...
["class Solution:\n def profitableSchemes(self, G: int, P: int, group: List[int], profit: List[int]) -> int:\n MOD = 10**9 + 7\n group_len, profit_len = len(group),len(profit)\n dp = [[0]*(G+1) for _ in range(P+1)]\n dp[0][0] = 1\n for pro, gro in zip(profit,group):\n dp...
{"fn_name": "profitableSchemes", "inputs": [[5, 3, [2, 2], [2, 3]]], "outputs": [2]}
interview
https://leetcode.com/problems/profitable-schemes/
class Solution: def profitableSchemes(self, G: int, P: int, group: List[int], profit: List[int]) -> int:
[ "class Solution:\n def profitableSchemes(self, G: int, P: int, group: List[int], profit: List[int]) -> int:\n MOD = 10**9 + 7\n group_len, profit_len = len(group),len(profit)\n dp = [[0]*(G+1) for _ in range(P+1)]\n dp[0][0] = 1\n for pro, gro in zip(profit,group):\n ...
74
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
128
Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . Example 1: Input: "1 + 1" Output: 2 Example 2: Input: " 2-1 + 2 " Output: 3 Example 3: Input: "(1+(4+5...
["class Solution:\n def calculate(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n res = 0\n num = 0\n sign = 1\n stk = []\n \n for c in s:\n if c.isdigit():\n num = 10 * num + (ord(c) - ord('0'))\n ...
{"fn_name": "calculate", "inputs": [["\"1 + 1\""]], "outputs": [2]}
interview
https://leetcode.com/problems/basic-calculator/
class Solution: def calculate(self, s: str) -> int:
[ "class Solution:\n def calculate(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n res = 0\n num = 0\n sign = 1\n stk = []\n \n for c in s:\n if c.isdigit():\n num = 10 * num + (ord(c) - ord('0'))\n ...
7
[ 0, 1, 2, 3, 4, 5, 6 ]
[ null, null, null, null, null, null, null ]
false
129
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them. The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them. ...
["class Solution:\n def maxScoreSightseeingPair(self, A: List[int]) -> int:\n curmaxsight = A[0] - 1\n curmaxpair = 0\n for sight in A[1:]:\n if sight + curmaxsight > curmaxpair:\n curmaxpair = sight + curmaxsight\n if sight > curmaxsight:\n cu...
{"fn_name": "maxScoreSightseeingPair", "inputs": [[[8, 1, 5, 2, 6]]], "outputs": [11]}
interview
https://leetcode.com/problems/best-sightseeing-pair/
class Solution: def maxScoreSightseeingPair(self, A: List[int]) -> int:
[ "class Solution:\n def maxScoreSightseeingPair(self, A: List[int]) -> int:\n curmaxsight = A[0] - 1\n curmaxpair = 0\n for sight in A[1:]:\n if sight + curmaxsight > curmaxpair:\n curmaxpair = sight + curmaxsight\n if sight > curmaxsight:\n ...
78
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
130
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array. Given the string s and the integer k. There can be multiple...
["class Solution:\n def numberOfArrays(self, s: str, k: int) -> int:\n dp = [-1] * len(s)\n return self.dfs(s, k, 0, dp)\n \n def dfs(self, s: str, k: int, start: int, dp: List[int]) -> int:\n if start == len(s):\n return 1\n if s[start] == '0':\n return 0\n ...
{"fn_name": "numberOfArrays", "inputs": [["\"1000\"", 10000]], "outputs": [4]}
interview
https://leetcode.com/problems/restore-the-array/
class Solution: def numberOfArrays(self, s: str, k: int) -> int:
[ "class Solution:\n def numberOfArrays(self, s: str, k: int) -> int:\n dp = [-1] * len(s)\n return self.dfs(s, k, 0, dp)\n \n def dfs(self, s: str, k: int, start: int, dp: List[int]) -> int:\n if start == len(s):\n return 1\n if s[start] == '0':\n return 0\n...
9
[ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
[ null, null, null, null, null, null, null, null, null ]
false
131
You are given a string expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. An expression is either an integer, a let-expression, an add-expression, a mult-expression, or an assigned variable. Expressions always evaluate to a single inte...
["class Solution(object):\n \tdef parse(self,expression,d,i):\n \t\tcount = 0\n \t\tstart = i\n \t\tif expression[i] == \"(\":\n \t\t\tcount += 1\n \t\t\ti += 1\n \t\t\twhile count != 0:\n \t\t\t\tif expression[i] == \"(\":\n \t\t\t\t\tcount += 1\n \t\t\t\telif expression[i] == \")\":\n \t\t\t\t\tcount -= 1\n \t\t\t\ti...
{"fn_name": "evaluate", "inputs": [["\"(add 1 2)\""]], "outputs": [2]}
interview
https://leetcode.com/problems/parse-lisp-expression/
class Solution: def evaluate(self, expression: str) -> int:
[ "class Solution(object):\n \tdef parse(self,expression,d,i):\n \t\tcount = 0\n \t\tstart = i\n \t\tif expression[i] == \"(\":\n \t\t\tcount += 1\n \t\t\ti += 1\n \t\t\twhile count != 0:\n \t\t\t\tif expression[i] == \"(\":\n \t\t\t\t\tcount += 1\n \t\t\t\telif expression[i] == \")\":\n \t\t\t\t\tcount -= 1\n \t\t\t...
1
[ 0 ]
[ null ]
false
132
In a country popular for train travel, you have planned some train travelling one year in advance.  The days of the year that you will travel is given as an array days.  Each day is an integer from 1 to 365. Train tickets are sold in 3 different ways: a 1-day pass is sold for costs[0] dollars; a 7-day pass is sold for...
["class Solution:\n def mincostTickets(self, days: List[int], costs: List[int]) -> int:\n dp = [0] + [-1 for i in range(days[-1])]\n \n for day in days:\n dp[day] = 0\n \n for i in range(1, len(dp)):\n if dp[i] == -1:\n dp[i] = dp[i-1]\n ...
{"fn_name": "mincostTickets", "inputs": [[[1, 4, 6, 7, 8, 20], [2, 7, 15]]], "outputs": [11]}
interview
https://leetcode.com/problems/minimum-cost-for-tickets/
class Solution: def mincostTickets(self, days: List[int], costs: List[int]) -> int:
[ "class Solution:\n def mincostTickets(self, days: List[int], costs: List[int]) -> int:\n dp = [0] + [-1 for i in range(days[-1])]\n \n for day in days:\n dp[day] = 0\n \n for i in range(1, len(dp)):\n if dp[i] == -1:\n dp[i] = dp[i-1]\n ...
21
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
133
You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string is said to be balanced if each of its characters appears n/4 times where n is the length of the string. Return the minimum length of the substring that can be replaced with any other string of the same length to make the origin...
["class Solution:\n def balancedString(self, s: str) -> int:\n # minimum window so that outside is possible\n if len(s) //4 != len(s) / 4: return -1 \n ans, lb, n_cnt = len(s), 0, collections.Counter(s)\n\n i = 0\n while i < len(s): \n n_cnt[s[i]] -= 1 \n ...
{"fn_name": "balancedString", "inputs": [["\"QWER\""]], "outputs": [-1]}
interview
https://leetcode.com/problems/replace-the-substring-for-balanced-string/
class Solution: def balancedString(self, s: str) -> int:
[ "class Solution:\n def balancedString(self, s: str) -> int:\n # minimum window so that outside is possible\n if len(s) //4 != len(s) / 4: return -1 \n ans, lb, n_cnt = len(s), 0, collections.Counter(s)\n\n i = 0\n while i < len(s): \n n_cnt[s[i]] -= 1 \n ...
7
[ 0, 1, 2, 3, 4, 5, 6 ]
[ null, null, null, null, null, null, null ]
false
135
Given two sequences pushed and popped with distinct values, return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.   Example 1: Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] Output: true Explanation: We might do the following sequence: push(...
["class Solution:\n def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:\n j = 0\n l = []\n for i in pushed:\n l.append(i)\n while l and (l[-1] == popped[j]):\n l.pop()\n j += 1\n if l:\n return Fals...
{"fn_name": "validateStackSequences", "inputs": [[[1, 2, 3, 4, 5], [4, 5, 3, 2, 1]]], "outputs": [true]}
interview
https://leetcode.com/problems/validate-stack-sequences/
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
[ "class Solution:\n def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:\n j = 0\n l = []\n for i in pushed:\n l.append(i)\n while l and (l[-1] == popped[j]):\n l.pop()\n j += 1\n if l:\n return F...
20
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
137
Given an integer n, you must transform it into 0 using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of n. Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. Return the minimum n...
["class Solution:\n def minimumOneBitOperations(self, n: int) -> int:\n s = 0\n m = n\n while m:\n s += m & 1\n m >>= 1\n\n k = 1\n while s:\n s -= bool(n & k)\n n ^= (s & 1) and k\n k <<= 1\n\n return n", "class Solutio...
{"fn_name": "minimumOneBitOperations", "inputs": [[0]], "outputs": [0]}
interview
https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/
class Solution: def minimumOneBitOperations(self, n: int) -> int:
[ "class Solution:\n def minimumOneBitOperations(self, n: int) -> int:\n s = 0\n m = n\n while m:\n s += m & 1\n m >>= 1\n\n k = 1\n while s:\n s -= bool(n & k)\n n ^= (s & 1) and k\n k <<= 1\n\n return n", "class So...
8
[ 0, 1, 2, 3, 4, 5, 6, 7 ]
[ null, null, null, null, null, null, null, null ]
false
138
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4]...
["class Solution:\n def getMaxLen(self, nums: List[int]) -> int:\n maxx = 0\n nums.append(0)\n \n # starting position\n # where we find a 0\n i = -1\n minusarr = []\n \n for j,n in enumerate(nums):\n if n == 0:\n # now figure ou...
{"fn_name": "getMaxLen", "inputs": [[[1, -2, -3, 4]]], "outputs": [4]}
interview
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/
class Solution: def getMaxLen(self, nums: List[int]) -> int:
[ "class Solution:\n def getMaxLen(self, nums: List[int]) -> int:\n maxx = 0\n nums.append(0)\n \n # starting position\n # where we find a 0\n i = -1\n minusarr = []\n \n for j,n in enumerate(nums):\n if n == 0:\n # now figure...
253
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
139
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions...
["class Solution:\n def minDeletionSize(self, A: List[str]) -> int:\n def isSorted(arr, i, j):\n return all(arr[k] <= arr[k+1] for k in range(i, j))\n ans = 0\n ranges = [[0, len(A)-1]]\n for col in zip(*A):\n if not ranges:\n break\n if all...
{"fn_name": "minDeletionSize", "inputs": [[["\"ca\"", "\"bb\"", "\"ac\""]]], "outputs": [1]}
interview
https://leetcode.com/problems/delete-columns-to-make-sorted-ii/
class Solution: def minDeletionSize(self, A: List[str]) -> int:
[ "class Solution:\n def minDeletionSize(self, A: List[str]) -> int:\n def isSorted(arr, i, j):\n return all(arr[k] <= arr[k+1] for k in range(i, j))\n ans = 0\n ranges = [[0, len(A)-1]]\n for col in zip(*A):\n if not ranges:\n break\n if ...
9
[ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
[ null, null, null, null, null, null, null, null, null ]
false
140
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum result is 5 ^ 25 = 28.
["class Solution:\n def findMaximumXOR(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ans = 0\n for bit in range(31, -1, -1) :\n ans = (ans << 1) + 1\n pre = set()\n for n in nums :\n p = (n >...
{"fn_name": "findMaximumXOR", "inputs": [[[3, 10, 5, 25, 2, 8]]], "outputs": [28]}
interview
https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/
class Solution: def findMaximumXOR(self, nums: List[int]) -> int:
[ "class Solution:\n def findMaximumXOR(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ans = 0\n for bit in range(31, -1, -1) :\n ans = (ans << 1) + 1\n pre = set()\n for n in nums :\n p = (...
2
[ 0, 1 ]
[ null, null ]
false
142
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one seque...
["class Solution:\n def findLUSlength(self, strs):\n \"\"\"\n :type strs: List[str]\n :rtype: int\n \"\"\"\n def isSubseq(s1, s2):\n i, m=0, len(s1)\n for c in s2:\n if i==m: return True\n if s1[i]==c: i+=1\n ...
{"fn_name": "findLUSlength", "inputs": [[["\"aba\"", "\"cdc\"", "\"eae\""]]], "outputs": [5]}
interview
https://leetcode.com/problems/longest-uncommon-subsequence-ii/
class Solution: def findLUSlength(self, strs: List[str]) -> int:
[ "class Solution:\n def findLUSlength(self, strs):\n \"\"\"\n :type strs: List[str]\n :rtype: int\n \"\"\"\n def isSubseq(s1, s2):\n i, m=0, len(s1)\n for c in s2:\n if i==m: return True\n if s1[i]==c: i+=1\n ...
11
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ null, null, null, null, null, null, null, null, null, null, null ]
false
143
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, s...
["class Solution:\n def totalFruit(self, tree: List[int]) -> int:\n prior_fruit = tree[0]\n prior_fruit_counter = 0\n fruits_in_basket = [tree[0]]\n fruits_in_basket_counter = 0\n max_fib = -1\n for fruit in tree: \n if prior_fruit == fruit:\n prior...
{"fn_name": "totalFruit", "inputs": [[[1, 2, 1]]], "outputs": [3]}
interview
https://leetcode.com/problems/fruit-into-baskets/
class Solution: def totalFruit(self, tree: List[int]) -> int:
[ "class Solution:\n def totalFruit(self, tree: List[int]) -> int:\n prior_fruit = tree[0]\n prior_fruit_counter = 0\n fruits_in_basket = [tree[0]]\n fruits_in_basket_counter = 0\n max_fib = -1\n for fruit in tree: \n if prior_fruit == fruit:\n pr...
206
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
144
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get ...
["class Solution:\n def minSteps(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n primeFactors=[]\n for i in range(2,int(n**.5)+1):\n while n%i==0:\n primeFactors.append(i)\n n=n//i\n if n>1:\n ...
{"fn_name": "minSteps", "inputs": [[3]], "outputs": [3]}
interview
https://leetcode.com/problems/2-keys-keyboard/
class Solution: def minSteps(self, n: int) -> int:
[ "class Solution:\n def minSteps(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n primeFactors=[]\n for i in range(2,int(n**.5)+1):\n while n%i==0:\n primeFactors.append(i)\n n=n//i\n if n>1:\n ...
11
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ null, null, null, null, null, null, null, null, null, null, null ]
false
145
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24. Example 1: Input: [4, 1, 8, 7] Output: True Explanation: (8-4) * (7-1) = 24 Example 2: Input: [1, 2, 1, 2] Output: False Note: The division operator / represe...
["class Solution(object):\n def judgePoint24(self, nums):\n bad = '\ub5a2\ube3b\uac01\uac4e\ub0c7\uac05\uac38\uae9a\ubd5f\uc223\uc684\ubd74\ubd5e\ub93c\uac08\uac0c\ub914\ub58c\uc60a\uba54\ub284\uc22d\uceb8\uae36\uae9b\uc616\uac0d\ub1d0\uca62\uacf4\ub4c7\uac6f\uad84\uc615\uc679\ub21e\uc1b4\uac43\ub057\uae2c\...
{"fn_name": "judgePoint24", "inputs": [[[4, 1, 8, 7]]], "outputs": [true]}
interview
https://leetcode.com/problems/24-game/
class Solution: def judgePoint24(self, nums: List[int]) -> bool:
[ "class Solution(object):\n def judgePoint24(self, nums):\n bad = '떢븻각걎냇갅갸꺚뵟숣욄뵴뵞뤼갈갌뤔떌옊메늄숭캸긶꺛옖갍뇐쩢곴듇걯궄옕왹눞솴걃끗긬땉궿가쌀낐걄숤뺴늘걘꽸숢걂갋갃쫐꼔솾쩡쇔솿끛뤜간븺쩬웨딴옠뤛갂뵪덠놤빐옋귒늂갰갖놥궾갆옌뼘묰거갎긷낤겼'\n return chr(int(''.join(map(str, sorted(nums)))) + 42921) not in bad\n", "class Solution:\n def judgePoint24(self, nums):\n ...
6
[ 0, 1, 2, 3, 4, 5 ]
[ null, null, null, null, null, null ]
false
146
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; No extra white spaces, square...
["class Solution:\n def decodeString(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n stack = []\n stack.append([\"\", 1])\n num = \"\"\n for ch in s:\n if ch.isdigit():\n num += ch\n elif ch == '[':\n ...
{"fn_name": "decodeString", "inputs": [["\"3[a]2[bc]\""]], "outputs": ["\"aaabcbc\""]}
interview
https://leetcode.com/problems/decode-string/
class Solution: def decodeString(self, s: str) -> str:
[ "class Solution:\n def decodeString(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n stack = []\n stack.append([\"\", 1])\n num = \"\"\n for ch in s:\n if ch.isdigit():\n num += ch\n elif ch == '[':\n...
6
[ 0, 1, 2, 3, 4, 5 ]
[ null, null, null, null, null, null ]
false
147
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^...
["class Solution:\n def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:\n mod = 10**9+7\n \n order = sorted(range(n), key=lambda i: efficiency[i], reverse=True)\n\n heap = []\n filled = 0\n rec = 0\n speed_sum = 0\n\n for i...
{"fn_name": "maxPerformance", "inputs": [[6, [2, 10, 3, 1, 5, 8], [5, 4, 3, 9, 7, 2], 2]], "outputs": [60]}
interview
https://leetcode.com/problems/maximum-performance-of-a-team/
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
[ "class Solution:\n def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:\n mod = 10**9+7\n \n order = sorted(range(n), key=lambda i: efficiency[i], reverse=True)\n\n heap = []\n filled = 0\n rec = 0\n speed_sum = 0\n\n fo...
70
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
148
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one ...
["class Solution:\n def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:\n ws = sorted(worker, reverse=True)\n dp = sorted(zip(difficulty, profit), key=lambda x: x[1], reverse=True)\n # print(list(dp))\n \n i = 0\n total = 0\n ...
{"fn_name": "maxProfitAssignment", "inputs": [[[2, 4, 6, 8, 10], [10, 20, 30, 40, 50], [4, 5, 6, 7]]], "outputs": [100]}
interview
https://leetcode.com/problems/most-profit-assigning-work/
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
[ "class Solution:\n def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:\n ws = sorted(worker, reverse=True)\n dp = sorted(zip(difficulty, profit), key=lambda x: x[1], reverse=True)\n # print(list(dp))\n \n i = 0\n total = 0\n...
89
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
150
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element in left is less than or equal to every element in right. left and right are non-empty. left has the smallest possible size. Return the length of left after such a partitioning.  It is guaranteed that such a partitioni...
["class Solution:\n def partitionDisjoint(self, A: List[int]) -> int:\n biggest = A[0]\n newbiggest = A[0]\n lenL = 1\n total = 1\n for itr in A[1:]:\n total += 1\n if itr < biggest:\n lenL = total\n biggest = newbiggest\n ...
{"fn_name": "partitionDisjoint", "inputs": [[[5, 0, 3, 8, 6]]], "outputs": [3]}
interview
https://leetcode.com/problems/partition-array-into-disjoint-intervals/
class Solution: def partitionDisjoint(self, A: List[int]) -> int:
[ "class Solution:\n def partitionDisjoint(self, A: List[int]) -> int:\n biggest = A[0]\n newbiggest = A[0]\n lenL = 1\n total = 1\n for itr in A[1:]:\n total += 1\n if itr < biggest:\n lenL = total\n biggest = newbiggest\n ...
51
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "syntax-error: cannot use starred expression here", null, null, null, ...
false
151
A password is considered strong if below conditions are all met: It has at least 6 characters and at most 20 characters. It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit. It must NOT contain three repeating characters in a row ("...aaa..." is weak, but "...aa.....
["class Solution:\n def strongPasswordChecker(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n def length_requirement(password):\n length = len(password)\n # positive means addition, negative means deletion\n if length < 6:\n ...
{"fn_name": "strongPasswordChecker", "inputs": [["\"a\""]], "outputs": [3]}
interview
https://leetcode.com/problems/strong-password-checker/
class Solution: def strongPasswordChecker(self, s: str) -> int:
[ "class Solution:\n def strongPasswordChecker(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n def length_requirement(password):\n length = len(password)\n # positive means addition, negative means deletion\n if length < 6:\n ...
2
[ 0, 1 ]
[ null, null ]
false
153
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time. Your input will be...
["class Solution:\n def makesquare(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n if len(nums) < 4:\n return False\n \n length = sum(nums)\n if length % 4:\n return False\n length = (int) (length...
{"fn_name": "makesquare", "inputs": [[[1, 1, 2, 2, 2]]], "outputs": [true]}
interview
https://leetcode.com/problems/matchsticks-to-square/
class Solution: def makesquare(self, nums: List[int]) -> bool:
[ "class Solution:\n def makesquare(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n if len(nums) < 4:\n return False\n \n length = sum(nums)\n if length % 4:\n return False\n length = (int) (len...
4
[ 0, 1, 2, 3 ]
[ null, null, null, null ]
false
154
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical c...
["class Solution:\n def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:\n mod = int(1e9)+7\n return ( ( self.getMax(horizontalCuts, h) % mod ) * ( self.getMax(verticalCuts, w) % mod ) ) % mod\n \n def getMax(self, cuts, size):\n if len(cuts) ==...
{"fn_name": "maxArea", "inputs": [[5, 4, [1, 2, 4], [1, 3]]], "outputs": [4]}
interview
https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/
class Solution: def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
[ "class Solution:\n def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:\n mod = int(1e9)+7\n return ( ( self.getMax(horizontalCuts, h) % mod ) * ( self.getMax(verticalCuts, w) % mod ) ) % mod\n \n def getMax(self, cuts, size):\n if len(cuts)...
51
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
155
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (Mo...
["class Solution:\n def maxJumps(self, arr: List[int], d: int) -> int:\n dp = [1] * (len(arr) + 1)\n stack = []\n for i, n in enumerate(arr + [1000000]):\n while stack and arr[stack[-1]] < n:\n same_height_idx = [stack.pop()]\n while stack and arr[stack[-...
{"fn_name": "maxJumps", "inputs": [[[6, 4, 14, 6, 8, 13, 9, 7, 10, 6, 12], 2]], "outputs": [4]}
interview
https://leetcode.com/problems/jump-game-v/
class Solution: def maxJumps(self, arr: List[int], d: int) -> int:
[ "class Solution:\n def maxJumps(self, arr: List[int], d: int) -> int:\n dp = [1] * (len(arr) + 1)\n stack = []\n for i, n in enumerate(arr + [1000000]):\n while stack and arr[stack[-1]] < n:\n same_height_idx = [stack.pop()]\n while stack and arr[stac...
120
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
156
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in th...
["import sys\n\ndef dp(s1, s2, i, j, mem):\n if (i, j) in mem:\n return mem[(i, j)]\n elif i >= len(s1) and j >= len(s2):\n res = ''\n elif i >= len(s1):\n res = s2[j:]\n elif j >= len(s2):\n res = s1[i:]\n else:\n if s1[i] == s2[j]:\n res = s1[i] + dp(s1, s2...
{"fn_name": "shortestCommonSupersequence", "inputs": [["\"abac\"", "\"cab\""]], "outputs": ["\"cabac\""]}
interview
https://leetcode.com/problems/shortest-common-supersequence/
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
[ "import sys\n\ndef dp(s1, s2, i, j, mem):\n if (i, j) in mem:\n return mem[(i, j)]\n elif i >= len(s1) and j >= len(s2):\n res = ''\n elif i >= len(s1):\n res = s2[j:]\n elif j >= len(s2):\n res = s1[i:]\n else:\n if s1[i] == s2[j]:\n res = s1[i] + dp(s1,...
58
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
157
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). Note: s could be empty an...
["class Solution:\n def isMatch(self, s, p):\n \"\"\"\n :type s: str\n :type p: str\n :rtype: bool\n \"\"\"\n '''\u7ef4\u62a4\u4e24\u4e2a\u4e0b\u6807\uff0c\u9010\u4e2a\u6bd4\u8f83\uff0c\u5982\u679cpj\u4e3a*\uff0c\u5219\u8bb0\u5f55*\u7684\u4f4d\u7f6e\uff0c\u5c06*\u540e...
{"fn_name": "isMatch", "inputs": [["\"aa\"", "\"a\""]], "outputs": [false]}
interview
https://leetcode.com/problems/wildcard-matching/
class Solution: def isMatch(self, s: str, p: str) -> bool:
[ "class Solution:\n def isMatch(self, s, p):\n \"\"\"\n :type s: str\n :type p: str\n :rtype: bool\n \"\"\"\n '''维护两个下标,逐个比较,如果pj为*,则记录*的位置,将*后一个元素与si进行比较,如果不相等,则将i从记录的位置+1,重新比较'''\n i=0\n j=0\n star=-1\n lenp=len(p)\n while ...
4
[ 0, 1, 2, 3 ]
[ null, null, null, null ]
false
158
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A ...
["class Solution:\n def kSimilarity(self, A: str, B: str) -> int:\n a = ''\n b = ''\n \n for i in range(len(A)):\n if A[i] != B[i]:\n a+=A[i]\n b+=B[i]\n \n return self.dfs(a,b)\n \n def dfs(self,a,b):\n if not...
{"fn_name": "kSimilarity", "inputs": [["\"ab\"", "\"ba\""]], "outputs": [1]}
interview
https://leetcode.com/problems/k-similar-strings/
class Solution: def kSimilarity(self, A: str, B: str) -> int:
[ "class Solution:\n def kSimilarity(self, A: str, B: str) -> int:\n a = ''\n b = ''\n \n for i in range(len(A)):\n if A[i] != B[i]:\n a+=A[i]\n b+=B[i]\n \n return self.dfs(a,b)\n \n def dfs(self,a,b):\n if ...
99
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
159
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of element...
["from collections import deque\nclass Solution:\n def constrainedSubsetSum(self, nums, k):\n N, queue = len(nums), deque()\n dp = [val for val in nums]\n for i, val in enumerate(nums):\n if queue and (i - queue[0] > k):\n queue.popleft()\n if queue and dp[qu...
{"fn_name": "constrainedSubsetSum", "inputs": [[[10, 2, -10, 5, 20], 2]], "outputs": [37]}
interview
https://leetcode.com/problems/constrained-subsequence-sum/
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int:
[ "from collections import deque\nclass Solution:\n def constrainedSubsetSum(self, nums, k):\n N, queue = len(nums), deque()\n dp = [val for val in nums]\n for i, val in enumerate(nums):\n if queue and (i - queue[0] > k):\n queue.popleft()\n if queue and dp...
77
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
160
Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones.  The total number of stones is odd, so there are no ties. Alex and Lee take turns, with Alex star...
["class Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n return True", "class Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n return 1;\n", "class Solution:\n def recur(self,i,j,piles,n,dp,s):\n if j==i+1:\n return max(piles[i],piles[j])\n if dp[i...
{"fn_name": "stoneGame", "inputs": [[[5, 3, 4, 5]]], "outputs": [true]}
interview
https://leetcode.com/problems/stone-game/
class Solution: def stoneGame(self, piles: List[int]) -> bool:
[ "class Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n return True", "class Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n return 1;\n", "class Solution:\n def recur(self,i,j,piles,n,dp,s):\n if j==i+1:\n return max(piles[i],piles[j])\n ...
181
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
161
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can...
["class Solution(object):\n def isValidSerialization(self, preorder):\n \"\"\"\n :type preorder: str\n :rtype: bool\n \"\"\"\n # remember how many empty slots we have\n # non-null nodes occupy one slot but create two new slots\n # null nodes occupy one slot\n ...
{"fn_name": "isValidSerialization", "inputs": [["\"9,3,4,#,#,1,#,#,2,#,6,#,#\""]], "outputs": [false]}
interview
https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/
class Solution: def isValidSerialization(self, preorder: str) -> bool:
[ "class Solution(object):\n def isValidSerialization(self, preorder):\n \"\"\"\n :type preorder: str\n :rtype: bool\n \"\"\"\n # remember how many empty slots we have\n # non-null nodes occupy one slot but create two new slots\n # null nodes occupy one slot...
10
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
[ null, null, null, null, null, null, null, null, null, null ]
false
162
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "...
["class Solution:\n def longestCommonSubsequence(self, a: str, b: str) -> int:\n last, current = [0] * (len(b) + 1), [0] * (len(b) + 1)\n \n for i in range(len(a) - 1, -1, -1):\n for j in range(len(b) - 1, -1, -1):\n if a[i] == b[j]:\n current[j] = 1 ...
{"fn_name": "longestCommonSubsequence", "inputs": [["\"abcde\"", "\"ace\""]], "outputs": [5]}
interview
https://leetcode.com/problems/longest-common-subsequence/
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int:
[ "class Solution:\n def longestCommonSubsequence(self, a: str, b: str) -> int:\n last, current = [0] * (len(b) + 1), [0] * (len(b) + 1)\n \n for i in range(len(a) - 1, -1, -1):\n for j in range(len(b) - 1, -1, -1):\n if a[i] == b[j]:\n current[j] =...
274
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
163
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by del...
["class Solution:\n def isSubsequence(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: bool\n \"\"\"\n if len(s) > len(t):\n return False\n for i in s:\n if i in t:\n index = t.find(i)\n t = ...
{"fn_name": "isSubsequence", "inputs": [["\"abc\"", "\"ahbgdc\""]], "outputs": [true]}
interview
https://leetcode.com/problems/is-subsequence/
class Solution: def isSubsequence(self, s: str, t: str) -> bool:
[ "class Solution:\n def isSubsequence(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: bool\n \"\"\"\n if len(s) > len(t):\n return False\n for i in s:\n if i in t:\n index = t.find(i)\n t...
11
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ null, null, null, null, null, null, null, null, null, null, null ]
false
165
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string. Ex...
["class Solution:\n def findLongestWord(self, s, d):\n \"\"\"\n :type s: str\n :type d: List[str]\n :rtype: str\n \"\"\"\n result = ''\n for word in d:\n lo = 0\n for l in word:\n lo = s.find(l, lo)+1\n i...
{"fn_name": "findLongestWord", "inputs": [["\"abpcplea\"", ["\"ale\"", "\"apple\"", "\"monkey\"", "\"plea\""]]], "outputs": ["\"apple\""]}
interview
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str:
[ "class Solution:\n def findLongestWord(self, s, d):\n \"\"\"\n :type s: str\n :type d: List[str]\n :rtype: str\n \"\"\"\n result = ''\n for word in d:\n lo = 0\n for l in word:\n lo = s.find(l, lo)+1\n ...
4
[ 0, 1, 2, 3 ]
[ null, null, null, null ]
false
166
Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation). Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.   Example 1: Input: a = 2, b = 6, c = 5 Output: 3 Explanation...
["class Solution:\n def minFlips(self, a: int, b: int, c: int) -> int:\n flips = 0\n print(bin(a))\n print(bin(b))\n print(bin(c))\n while a or b or c:\n # print(a, b, c)\n if c % 2:\n if not (a % 2 or b % 2):\n flips += 1\n ...
{"fn_name": "minFlips", "inputs": [[2, 6, 5]], "outputs": [3]}
interview
https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/
class Solution: def minFlips(self, a: int, b: int, c: int) -> int:
[ "class Solution:\n def minFlips(self, a: int, b: int, c: int) -> int:\n flips = 0\n print(bin(a))\n print(bin(b))\n print(bin(c))\n while a or b or c:\n # print(a, b, c)\n if c % 2:\n if not (a % 2 or b % 2):\n flips += 1\...
7
[ 0, 1, 2, 3, 4, 5, 6 ]
[ null, null, null, null, null, null, null ]
false
167
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floo...
["class Solution:\n def superEggDrop(self, K: int, N: int) -> int:\n def f(t):\n a=0\n r=1\n for i in range(1, K+1):\n r *= (t-i+1)\n r//=i\n a+=r\n if a>=N: \n break\n return a\n ...
{"fn_name": "superEggDrop", "inputs": [[1, 2]], "outputs": [2]}
interview
https://leetcode.com/problems/super-egg-drop/
class Solution: def superEggDrop(self, K: int, N: int) -> int:
[ "class Solution:\n def superEggDrop(self, K: int, N: int) -> int:\n def f(t):\n a=0\n r=1\n for i in range(1, K+1):\n r *= (t-i+1)\n r//=i\n a+=r\n if a>=N: \n break\n return a\n ...
179
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
168
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.   Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palind...
["from collections import Counter\nclass Solution:\n def canConstruct(self, s: str, k: int) -> bool:\n if k > len(s): #return False\n return False\n counter = Counter(s)\n odd_counts = 0\n \n for char in counter:\n if counter[char] % 2 == 1:\n o...
{"fn_name": "canConstruct", "inputs": [["\"annabelle\"", 2]], "outputs": [true]}
interview
https://leetcode.com/problems/construct-k-palindrome-strings/
class Solution: def canConstruct(self, s: str, k: int) -> bool:
[ "from collections import Counter\nclass Solution:\n def canConstruct(self, s: str, k: int) -> bool:\n if k > len(s): #return False\n return False\n counter = Counter(s)\n odd_counts = 0\n \n for char in counter:\n if counter[char] % 2 == 1:\n ...
35
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
169
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: You may assume that n is not less than 2 and not l...
["class Solution:\n def integerBreak(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n if n==2:return 1\n if n==3:return 2\n res=1\n while n>4:\n n=n-3\n res*=3\n return res*n\n", "class Solution:\n def inte...
{"fn_name": "integerBreak", "inputs": [[2]], "outputs": [1]}
interview
https://leetcode.com/problems/integer-break/
class Solution: def integerBreak(self, n: int) -> int:
[ "class Solution:\n def integerBreak(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n if n==2:return 1\n if n==3:return 2\n res=1\n while n>4:\n n=n-3\n res*=3\n return res*n\n", "class Solution:\n def...
14
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
170
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest su...
["class Solution:\n def findLengthOfShortestSubarray(self, arr: List[int]) -> int:\n n = len(arr)\n if n<=1: \n return 0\n l,r = n,-1\n \n for i in range(1,n):\n if arr[i]<arr[i-1]:\n l = i\n break\n # monotonicially increa...
{"fn_name": "findLengthOfShortestSubarray", "inputs": [[[1, 2, 3, 10, 4, 2, 3, 5]]], "outputs": [3]}
interview
https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/
class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
[ "class Solution:\n def findLengthOfShortestSubarray(self, arr: List[int]) -> int:\n n = len(arr)\n if n<=1: \n return 0\n l,r = n,-1\n \n for i in range(1,n):\n if arr[i]<arr[i-1]:\n l = i\n break\n # monotonicially inc...
131
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
172
You are given an integer num. You will apply the following steps exactly two times: Pick a digit x (0 <= x <= 9). Pick another digit y (0 <= y <= 9). The digit y can be equal to x. Replace all the occurrences of x in the decimal representation of num by y. The new integer cannot have any leading zeros, also the new in...
["class Solution:\n def maxDiff(self, num: int) -> int:\n if num < 10: return 8\n a = b = str(num)\n i = 0\n while i < len(a):\n if a[i] == '9':\n i += 1\n else:\n a = a.replace(a[i], '9')\n break\n\n if b[0] != '1'...
{"fn_name": "maxDiff", "inputs": [[555]], "outputs": [888]}
interview
https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/
class Solution: def maxDiff(self, num: int) -> int:
[ "class Solution:\n def maxDiff(self, num: int) -> int:\n if num < 10: return 8\n a = b = str(num)\n i = 0\n while i < len(a):\n if a[i] == '9':\n i += 1\n else:\n a = a.replace(a[i], '9')\n break\n\n if b[0] != ...
9
[ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
[ null, null, null, null, null, null, null, null, null ]
false
173
Given an array of integers arr of even length n and an integer k. We want to divide the array into exactly n / 2 pairs such that the sum of each pair is divisible by k. Return True If you can find a way to do that or False otherwise.   Example 1: Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5 Output: true Explanation: Pair...
["class Solution:\n def canArrange(self, arr: List[int], k: int) -> bool:\n freq = [0] * k\n \n for n in arr:\n freq[n%k] += 1\n \n if freq[0] % 2: return False\n \n for i in range(1, (k//2)+1):\n if freq[i] != freq[k-i]: return False\n \n ...
{"fn_name": "canArrange", "inputs": [[[1, 2, 3, 4, 5, 10, 6, 7, 8, 9], 5]], "outputs": [true]}
interview
https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/
class Solution: def canArrange(self, arr: List[int], k: int) -> bool:
[ "class Solution:\n def canArrange(self, arr: List[int], k: int) -> bool:\n freq = [0] * k\n \n for n in arr:\n freq[n%k] += 1\n \n if freq[0] % 2: return False\n \n for i in range(1, (k//2)+1):\n if freq[i] != freq[k-i]: return False\n \n ...
100
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
174
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext. The string "dir\n\...
["class Solution:\n def lengthLongestPath(self, input):\n \"\"\"\n :type input: str\n :rtype: int\n \"\"\"\n dict={0:0}\n maxlen=0\n line=input.split(\"\\n\")\n for i in line:\n name=i.lstrip('\\t')\n print(name)\n p...
{"fn_name": "lengthLongestPath", "inputs": [["\"dir\\n\\tsubdir1\\n\\tsubdir2\\n\\t\\tfile.ext\""]], "outputs": [41]}
interview
https://leetcode.com/problems/longest-absolute-file-path/
class Solution: def lengthLongestPath(self, input: str) -> int:
[ "class Solution:\n def lengthLongestPath(self, input):\n \"\"\"\n :type input: str\n :rtype: int\n \"\"\"\n dict={0:0}\n maxlen=0\n line=input.split(\"\\n\")\n for i in line:\n name=i.lstrip('\\t')\n print(name)\n ...
11
[ 3, 9 ]
[ "builtin input()", "builtin input()", "builtin input()", null, "builtin input()", "builtin input()", "builtin input()", "builtin input()", "builtin input()", null, "builtin input()" ]
false
175
Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT contain consecutive ones. Example 1: Input: 5 Output: 5 Explanation: Here are the non-negative integers Note: 1 9
["class Solution:\n def findIntegers(self, num):\n \"\"\"\n :type num: int\n :rtype: int\n \"\"\"\n dp=[1,2]\n for i in range(2,32):\n dp.append(dp[i-1]+dp[i-2])\n \n bnum=bin(num)[2:]\n size=len(bnum)\n ans=dp[size]\n ...
{"fn_name": "findIntegers", "inputs": [[1]], "outputs": [2]}
interview
https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/
class Solution: def findIntegers(self, num: int) -> int:
[ "class Solution:\n def findIntegers(self, num):\n \"\"\"\n :type num: int\n :rtype: int\n \"\"\"\n dp=[1,2]\n for i in range(2,32):\n dp.append(dp[i-1]+dp[i-2])\n \n bnum=bin(num)[2:]\n size=len(bnum)\n ans=dp[size]\n ...
8
[ 0, 1, 2, 3, 4, 5, 6, 7 ]
[ null, null, null, null, null, null, null, null ]
false
176
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / \ gr eat / \ / \ g r e at / \ a t To scramble the string, we may choose any non-leaf node...
["class Solution:\n def isScramble(self, A, B):\n if len(A) != len(B) or sorted(A) != sorted(B):\n return False\n \n if len(A) == 1 or A == B:\n return True\n \n for i in range(1, len(A)):\n if self.isScramble(A[:i], B[:i]) and self.isScramble(A[i:], B[i:]...
{"fn_name": "isScramble", "inputs": [["\"great\"", "\"rgeat\""]], "outputs": [true]}
interview
https://leetcode.com/problems/scramble-string/
class Solution: def isScramble(self, s1: str, s2: str) -> bool:
[ "class Solution:\n def isScramble(self, A, B):\n if len(A) != len(B) or sorted(A) != sorted(B):\n return False\n \n if len(A) == 1 or A == B:\n return True\n \n for i in range(1, len(A)):\n if self.isScramble(A[:i], B[:i]) and self.isScramble(A[i:], B[...
10
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
[ null, null, null, null, null, null, null, null, null, null ]
false
177
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). Example: Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC" Note: If there is no such window in S that covers all characters in T, return the empty string "". If there is s...
["class Solution:\n def minWindow(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: str\n \"\"\"\n left=-1\n right = 0\n result = \"\"\n totalMatch = 0\n d = {}\n for c in t:\n d[c] = d.get(c, 0) + 1\n \n...
{"fn_name": "minWindow", "inputs": [["\"ADOBECODEBANC\"", "\"ABC\""]], "outputs": ["\"ADOBECODEBANC\""]}
interview
https://leetcode.com/problems/minimum-window-substring/
class Solution: def minWindow(self, s: str, t: str) -> str:
[ "class Solution:\n def minWindow(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: str\n \"\"\"\n left=-1\n right = 0\n result = \"\"\n totalMatch = 0\n d = {}\n for c in t:\n d[c] = d.get(c, 0) + 1\n...
4
[ 0, 1, 2, 3 ]
[ null, null, null, null ]
false
178
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be more than one LIS combination, it is only necessary for yo...
["class Solution:\n def lengthOfLIS(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n if len(nums) == 0:\n return 0\n res = [nums[0]]\n def binarySearch(l,target):\n left , right = 0 , len(l)-1\n while l...
{"fn_name": "lengthOfLIS", "inputs": [[[10, 9, 2, 5, 3, 7, 101, 18]]], "outputs": [4]}
interview
https://leetcode.com/problems/longest-increasing-subsequence/
class Solution: def lengthOfLIS(self, nums: List[int]) -> int:
[ "class Solution:\n def lengthOfLIS(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n if len(nums) == 0:\n return 0\n res = [nums[0]]\n def binarySearch(l,target):\n left , right = 0 , len(l)-1\n whil...
15
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
181
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple...
["class Solution:\n def maxProfit(self, prices):\n \"\"\"\n :type prices: List[int]\n :rtype: int\n \"\"\"\n n = len(prices)\n \n if n < 2: return 0\n \n sells = [0] * n\n buys = [0] * n\n \n buys[0] = -prices[0]\n ...
{"fn_name": "maxProfit", "inputs": [[[1, 2, 3, 0, 2]]], "outputs": [3]}
interview
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
class Solution: def maxProfit(self, prices: List[int]) -> int:
[ "class Solution:\n def maxProfit(self, prices):\n \"\"\"\n :type prices: List[int]\n :rtype: int\n \"\"\"\n n = len(prices)\n \n if n < 2: return 0\n \n sells = [0] * n\n buys = [0] * n\n \n buys[0] = -prices[0]\n ...
14
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
182
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for cont...
["class Solution:\n def trap(self, height):\n \"\"\"\n :type height: List[int]\n :rtype: int\n \"\"\"\n if not height:\n return 0\n result = 0\n left = 0\n right = len(height) - 1\n while left < right:\n if height[left] ...
{"fn_name": "trap", "inputs": [[[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]]], "outputs": [6]}
interview
https://leetcode.com/problems/trapping-rain-water/
class Solution: def trap(self, height: List[int]) -> int:
[ "class Solution:\n def trap(self, height):\n \"\"\"\n :type height: List[int]\n :rtype: int\n \"\"\"\n if not height:\n return 0\n result = 0\n left = 0\n right = len(height) - 1\n while left < right:\n if height[lef...
13
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
183
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remai...
["class Solution:\n def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int:\n \n # DP(a=index of last, b=index of last) = max of:\n # DP(a-1, b)\n # DP(a-1, i) + nums1[a] * max_or_min(nums2[i+1:b+1])\n # same for b\n \n INF = int(1e9)\n n, m =...
{"fn_name": "maxDotProduct", "inputs": [[[2, 1, -2, 5], [3, 0, -6]]], "outputs": [18]}
interview
https://leetcode.com/problems/max-dot-product-of-two-subsequences/
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int:
[ "class Solution:\n def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int:\n \n # DP(a=index of last, b=index of last) = max of:\n # DP(a-1, b)\n # DP(a-1, i) + nums1[a] * max_or_min(nums2[i+1:b+1])\n # same for b\n \n INF = int(1e9)\n n, ...
82
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
185
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and...
["class Solution:\n def hasAllCodes(self, s: str, k: int) -> bool:\n if len(s) < 2 ** k + k - 1:\n return False # Cannot be a string, as this is the de brujin length\n target = 2 ** k\n seen = set()\n cur_len = 0\n for end in range(k, len(s) + 1):\n chunk = s[...
{"fn_name": "hasAllCodes", "inputs": [["\"00110110\"", 2]], "outputs": [true]}
interview
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/
class Solution: def hasAllCodes(self, s: str, k: int) -> bool:
[ "class Solution:\n def hasAllCodes(self, s: str, k: int) -> bool:\n if len(s) < 2 ** k + k - 1:\n return False # Cannot be a string, as this is the de brujin length\n target = 2 ** k\n seen = set()\n cur_len = 0\n for end in range(k, len(s) + 1):\n chunk =...
38
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
186
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as st...
["class Solution:\n def largestNumber(self, cost: List[int], target: int) -> str:\n dp = [0] + [-target]*target\n for t in range(1, target+1):\n dp[t] = max([dp[t-i] for i in cost if i<=t]+[dp[t]]) + 1\n if dp[-1]<=0: return '0'\n res = ''\n for i in range(8, -1, -1):\n ...
{"fn_name": "largestNumber", "inputs": [[[4, 3, 2, 5, 6, 7, 2, 5, 5], 9]], "outputs": ["7772"]}
interview
https://leetcode.com/problems/form-largest-integer-with-digits-that-add-up-to-target/
class Solution: def largestNumber(self, cost: List[int], target: int) -> str:
[ "class Solution:\n def largestNumber(self, cost: List[int], target: int) -> str:\n dp = [0] + [-target]*target\n for t in range(1, target+1):\n dp[t] = max([dp[t-i] for i in cost if i<=t]+[dp[t]]) + 1\n if dp[-1]<=0: return '0'\n res = ''\n for i in range(8, -1, -1):...
48
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
187
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving j...
["class Solution:\n def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int:\n if runningCost >= 4 * boardingCost:\n return -1\n result = sum(customers) // 4\n if (sum(customers) % 4) * boardingCost > runningCost:\n result += 1\n ...
{"fn_name": "minOperationsMaxProfit", "inputs": [[[8, 3], 5, 6]], "outputs": [3]}
interview
https://leetcode.com/problems/maximum-profit-of-operating-a-centennial-wheel/
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int:
[ "class Solution:\n def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int:\n if runningCost >= 4 * boardingCost:\n return -1\n result = sum(customers) // 4\n if (sum(customers) % 4) * boardingCost > runningCost:\n result += 1\...
572
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
188
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred ...
["class Solution:\n V1 = [\"\", \"One\", \"Two\", \"Three\", \"Four\", \"Five\", \"Six\", \"Seven\", \"Eight\", \"Nine\", \"Ten\",\n \"Eleven\", \"Twelve\", \"Thirteen\", \"Fourteen\", \"Fifteen\", \"Sixteen\", \"Seventeen\", \"Eighteen\", \"Nineteen\"]\n V2 = [\"\", \"\", \"Twenty\", \"Thirty\", \"Fo...
{"fn_name": "numberToWords", "inputs": [[123]], "outputs": ["One Hundred Twenty Three"]}
interview
https://leetcode.com/problems/integer-to-english-words/
class Solution: def numberToWords(self, num: int) -> str:
[ "class Solution:\n V1 = [\"\", \"One\", \"Two\", \"Three\", \"Four\", \"Five\", \"Six\", \"Seven\", \"Eight\", \"Nine\", \"Ten\",\n \"Eleven\", \"Twelve\", \"Thirteen\", \"Fourteen\", \"Fifteen\", \"Sixteen\", \"Seventeen\", \"Eighteen\", \"Nineteen\"]\n V2 = [\"\", \"\", \"Twenty\", \"Thirty\", \...
10
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
[ null, null, null, null, null, null, null, null, null, null ]
false
190
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 0
["class Solution:\n def findLength(self, A, B):\n def check(length):\n seen = {A[i:i+length]\n for i in range(len(A) - length + 1)}\n return any(B[j:j+length] in seen\n for j in range(len(B) - length + 1))\n \n A = ''.join(map(chr, A...
{"fn_name": "findLength", "inputs": [[[1, 2, 3, 2, 1], [3, 2, 1, 4, 7]]], "outputs": [3]}
interview
https://leetcode.com/problems/maximum-length-of-repeated-subarray/
class Solution: def findLength(self, A: List[int], B: List[int]) -> int:
[ "class Solution:\n def findLength(self, A, B):\n def check(length):\n seen = {A[i:i+length]\n for i in range(len(A) - length + 1)}\n return any(B[j:j+length] in seen\n for j in range(len(B) - length + 1))\n \n A = ''.join(map(chr...
1
[ 0 ]
[ null ]
false
191
Given an array of digits, you can write numbers using each digits[i] as many times as we want.  For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'. Return the number of positive integers that can be generated that are less than or equal to a given integer n.   Example 1: Inp...
["class Solution:\n def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:\n \n count = 0\n length = 1\n n_str = str(n)\n while length < len(n_str):\n count+= len(digits)**length\n length+=1\n\n digits_sorted = sorted(digits)\n\n\n ## ...
{"fn_name": "atMostNGivenDigitSet", "inputs": [[["\"1\"", "\"3\"", "\"5\"", "\"7\""], 100]], "outputs": [84]}
interview
https://leetcode.com/problems/numbers-at-most-n-given-digit-set/
class Solution: def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:
[ "class Solution:\n def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:\n \n count = 0\n length = 1\n n_str = str(n)\n while length < len(n_str):\n count+= len(digits)**length\n length+=1\n\n digits_sorted = sorted(digits)\n\n\n ...
3
[ 0, 1, 2 ]
[ null, null, null ]
false
192
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Y...
["class Solution:\n def maxCoins(self, piles: List[int]) -> int:\n piles.sort()\n \n i = 0\n j = len(piles) - 1\n \n max_coins = 0\n for i in range(len(piles) // 3, len(piles), 2):\n max_coins += piles[i]\n \n return max_coins", "class Solutio...
{"fn_name": "maxCoins", "inputs": [[[1, 2, 2, 4, 7, 8]]], "outputs": [9]}
interview
https://leetcode.com/problems/maximum-number-of-coins-you-can-get/
class Solution: def maxCoins(self, piles: List[int]) -> int:
[ "class Solution:\n def maxCoins(self, piles: List[int]) -> int:\n piles.sort()\n \n i = 0\n j = len(piles) - 1\n \n max_coins = 0\n for i in range(len(piles) // 3, len(piles), 2):\n max_coins += piles[i]\n \n return max_coins", "class So...
245
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
193
Given an array arr.  You can choose a set of integers and remove all the occurrences of these integers in the array. Return the minimum size of the set so that at least half of the integers of the array are removed.   Example 1: Input: arr = [3,3,3,3,5,5,5,2,2,7] Output: 2 Explanation: Choosing {3,7} will make the new ...
["from heapq import *\nfrom collections import Counter\n\nclass Solution:\n def minSetSize(self, arr: List[int]) -> int:\n \n counter = Counter(arr)\n size = len(arr)\n \n # unique elements (remove half of them)\n if len(counter) == size:\n return (size - 1) // 2 ...
{"fn_name": "minSetSize", "inputs": [[[3, 3, 3, 3, 5, 5, 5, 2, 2, 7]]], "outputs": [2]}
interview
https://leetcode.com/problems/reduce-array-size-to-the-half/
class Solution: def minSetSize(self, arr: List[int]) -> int:
[ "from heapq import *\nfrom collections import Counter\n\nclass Solution:\n def minSetSize(self, arr: List[int]) -> int:\n \n counter = Counter(arr)\n size = len(arr)\n \n # unique elements (remove half of them)\n if len(counter) == size:\n return (size - 1) //...
120
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
194
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal su...
["class Solution:\n def canPartitionKSubsets(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: bool\n \"\"\"\n target,rem=divmod(sum(nums),k)\n if rem or max(nums)>target: return False\n n=len(nums)\n seen=[0]*n\n ...
{"fn_name": "canPartitionKSubsets", "inputs": [[[5, 4, 3, 3, 2, 2, 1], 4]], "outputs": [true]}
interview
https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
class Solution: def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
[ "class Solution:\n def canPartitionKSubsets(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: bool\n \"\"\"\n target,rem=divmod(sum(nums),k)\n if rem or max(nums)>target: return False\n n=len(nums)\n seen=[0]*n\n ...
14
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
195
Given an array of integers A, find the number of triples of indices (i, j, k) such that: 0 <= i < A.length 0 <= j < A.length 0 <= k < A.length A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.   Example 1: Input: [2,1,3] Output: 12 Explanation: We could choose the following i, j, k triples: (i=0, ...
["class Solution:\n def countTriplets(self, A: List[int]) -> int:\n counters = [0] * (1 << 16)\n counters[0] = len(A)\n for num in A:\n mask = (~num) & ((1 << 16) - 1)\n sm = mask\n while sm != 0:\n counters[sm] += 1\n sm = (sm - 1) ...
{"fn_name": "countTriplets", "inputs": [[[2, 1, 3]]], "outputs": [12]}
interview
https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/
class Solution: def countTriplets(self, A: List[int]) -> int:
[ "class Solution:\n def countTriplets(self, A: List[int]) -> int:\n counters = [0] * (1 << 16)\n counters[0] = len(A)\n for num in A:\n mask = (~num) & ((1 << 16) - 1)\n sm = mask\n while sm != 0:\n counters[sm] += 1\n sm = (sm - ...
61
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
196
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C. Here, a circular array means the end of the array connects to the beginning of the array.  (Formally, C[i] = A[i] when 0 <= i < A.length, and C[i+A.length] = C[i] when i >= 0.) Also, a subarray may only in...
["class Solution:\n def maxSubarraySumCircular(self, A: List[int]) -> int:\n N = len(A)\n if(N==0):\n return 0\n curr_max = A[0]\n global_max = A[0]\n curr_min = A[0]\n global_min = A[0]\n flag = 0 \n if(A[0]>=0):\n flag=1\n \n f...
{"fn_name": "maxSubarraySumCircular", "inputs": [[[-2, 3, -2, 1]]], "outputs": [3]}
interview
https://leetcode.com/problems/maximum-sum-circular-subarray/
class Solution: def maxSubarraySumCircular(self, A: List[int]) -> int:
[ "class Solution:\n def maxSubarraySumCircular(self, A: List[int]) -> int:\n N = len(A)\n if(N==0):\n return 0\n curr_max = A[0]\n global_max = A[0]\n curr_min = A[0]\n global_min = A[0]\n flag = 0 \n if(A[0]>=0):\n flag=1\n \n ...
124
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
197
Given a string s, determine if it is valid. A string s is valid if, starting with an empty string t = "", you can transform t into s after performing the following operation any number of times: Insert string "abc" into any position in t. More formally, t becomes tleft + "abc" + tright, where t == tleft + tright. Note...
["class Solution:\n def isValid(self, s: str) -> bool:\n if not s:\n return True\n return self.isValid(s.replace('abc', '')) if s.replace('abc', '') != s else False\n", "class Solution:\n def isValid(self, S: str) -> bool:\n stack = []\n for i in S:\n if i == 'c':...
{"fn_name": "isValid", "inputs": [["\"aabcbc\""]], "outputs": [false]}
interview
https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/
class Solution: def isValid(self, s: str) -> bool:
[ "class Solution:\n def isValid(self, s: str) -> bool:\n if not s:\n return True\n return self.isValid(s.replace('abc', '')) if s.replace('abc', '') != s else False\n", "class Solution:\n def isValid(self, S: str) -> bool:\n stack = []\n for i in S:\n if i ==...
38
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
198
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters. You are also given an integer maxCost. Return the maximum length of a substring of s t...
["class Solution:\n def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n dist = [ abs( ord(s[i]) - ord(t[i]) ) for i in range(len(s))]\n \n# i = 0\n# cur = 0\n# res = 0\n# for j in range(len(s)):\n# cur += dist[j]\n# while cur>maxCost...
{"fn_name": "equalSubstring", "inputs": [["\"abcd\"", "\"bcdf\"", 3]], "outputs": [4]}
interview
https://leetcode.com/problems/get-equal-substrings-within-budget/
class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
[ "class Solution:\n def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n dist = [ abs( ord(s[i]) - ord(t[i]) ) for i in range(len(s))]\n \n# i = 0\n# cur = 0\n# res = 0\n# for j in range(len(s)):\n# cur += dist[j]\n# while cur>maxC...
34
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
199
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
["class Solution:\n def longestConsecutive(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n longest_streak = 0\n num_set = set(nums)\n for num in num_set:\n if num - 1 not in num_set:\n current_num = num\n ...
{"fn_name": "longestConsecutive", "inputs": [[[100, 4, 200, 1, 3, 2]]], "outputs": [4]}
interview
https://leetcode.com/problems/longest-consecutive-sequence/
class Solution: def longestConsecutive(self, nums: List[int]) -> int:
[ "class Solution:\n def longestConsecutive(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n longest_streak = 0\n num_set = set(nums)\n for num in num_set:\n if num - 1 not in num_set:\n current_num = num\n ...
15
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
200
Given an integer k, return the minimum number of Fibonacci numbers whose sum is equal to k. The same Fibonacci number can be used multiple times. The Fibonacci numbers are defined as: F1 = 1 F2 = 1 Fn = Fn-1 + Fn-2 for n > 2. It is guaranteed that for the given constraints we can always find such Fibonacci numbers th...
["class Solution:\n def findMinFibonacciNumbers(self, k: int) -> int:\n fib = [1, 1] # initializing a Fibonacci table with F[0] and F[1]\n i = 1 # index that will represent the last filled index of table\n temp = fib[0] + fib[1] # initial value of values to be appended\n while temp < k: #...
{"fn_name": "findMinFibonacciNumbers", "inputs": [[7]], "outputs": [2]}
interview
https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/
class Solution: def findMinFibonacciNumbers(self, k: int) -> int:
[ "class Solution:\n def findMinFibonacciNumbers(self, k: int) -> int:\n fib = [1, 1] # initializing a Fibonacci table with F[0] and F[1]\n i = 1 # index that will represent the last filled index of table\n temp = fib[0] + fib[1] # initial value of values to be appended\n while temp < k...
12
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
[ null, null, null, null, null, null, null, null, null, null, null, null ]
false
201
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example: Input: 3 Output: 5 Explanation: Given n = 3, there are a total of 5 unique BST's: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ...
["class Solution:\n hash = {}\n def numTrees(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n # return base case\n if n == 0:\n return 1\n if n == 1 or n == 2:\n return n\n \n # try fetching from hash\n ...
{"fn_name": "numTrees", "inputs": [[3]], "outputs": [5]}
interview
https://leetcode.com/problems/unique-binary-search-trees/
class Solution: def numTrees(self, n: int) -> int:
[ "class Solution:\n hash = {}\n def numTrees(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n # return base case\n if n == 0:\n return 1\n if n == 1 or n == 2:\n return n\n \n # try fetching from hash\n ...
15
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
202
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note that B could be any subarray of A, including the entire array A.) Given an array A of integer...
["class Solution:\n def longestMountain(self, A: List[int]) -> int:\n up=0\n down=0\n ans=0\n for i in range(0,len(A)-1):\n if A[i]<A[i+1]:\n if down==0:\n up+=1\n else:\n up=1\n down=0\n ...
{"fn_name": "longestMountain", "inputs": [[[2, 1, 4, 7, 3, 2, 5]]], "outputs": [5]}
interview
https://leetcode.com/problems/longest-mountain-in-array/
class Solution: def longestMountain(self, A: List[int]) -> int:
[ "class Solution:\n def longestMountain(self, A: List[int]) -> int:\n up=0\n down=0\n ans=0\n for i in range(0,len(A)-1):\n if A[i]<A[i+1]:\n if down==0:\n up+=1\n else:\n up=1\n down=0\n ...
38
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
203
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). How many possible unique paths are there? Above...
["class Solution:\n def uniquePaths(self, m, n):\n \"\"\"\n :type m: int\n :type n: int\n :rtype: int\n \"\"\"\n def f(n):\n ret = 1\n for i in range(1, n+1):\n ret *= i\n return ret\n return f(m+n-2)//(f(m-1...
{"fn_name": "uniquePaths", "inputs": [[3, 7]], "outputs": [28]}
interview
https://leetcode.com/problems/unique-paths/
class Solution: def uniquePaths(self, m: int, n: int) -> int:
[ "class Solution:\n def uniquePaths(self, m, n):\n \"\"\"\n :type m: int\n :type n: int\n :rtype: int\n \"\"\"\n def f(n):\n ret = 1\n for i in range(1, n+1):\n ret *= i\n return ret\n return f(m+n-2)//(f(...
14
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
204
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. Your algorithm's ...
["class Solution:\n def search(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: int\n :rtype: int\n \"\"\"\n # left = 0\n # right = len(nums) - 1\n # while left <= right: \n # mid = int((left + right)/2)\n # ...
{"fn_name": "search", "inputs": [[[4, 5, 6, 7, 0, 1, 2], 0]], "outputs": [4]}
interview
https://leetcode.com/problems/search-in-rotated-sorted-array/
class Solution: def search(self, nums: List[int], target: int) -> int:
[ "class Solution:\n def search(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: int\n :rtype: int\n \"\"\"\n # left = 0\n # right = len(nums) - 1\n # while left <= right: \n # mid = int((left + right)/2)\n # ...
12
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
[ null, null, null, null, null, null, null, null, null, null, null, null ]
false
205
In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue. For now, suppose you are a dominator of m 0s and n 1s respectively. On the other hand, there is an array with strings consisting of only 0s and 1s. Now your task is to find the maximum number of strin...
["class Solution:\n def getMax(self, arr, m, n):\n res = 0\n \n for e in arr:\n if m >= e[0] and n >= e[1]:\n res += 1\n m -= e[0]\n n -= e[1]\n \n return res\n \n def findMaxForm(self, strs, m, n):\n \"\"\"\n :t...
{"fn_name": "findMaxForm", "inputs": [[["\"10\"", "\"0001\"", "\"111001\"", "\"1\"", "\"0\""], 5, 3]], "outputs": [4]}
interview
https://leetcode.com/problems/ones-and-zeroes/
class Solution: def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
[ "class Solution:\n def getMax(self, arr, m, n):\n res = 0\n \n for e in arr:\n if m >= e[0] and n >= e[1]:\n res += 1\n m -= e[0]\n n -= e[1]\n \n return res\n \n def findMaxForm(self, strs, m, n):\n \"\"\"\n ...
4
[ 0, 1, 2, 3 ]
[ null, null, null, null ]
false
206
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. Th...
["class Solution:\n def PredictTheWinner(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n if not nums: return True\n n = len(nums)\n if n & 1 == 0: return True\n \n dp = [0] * n\n for i in range(n-1, -1, -1):\n ...
{"fn_name": "PredictTheWinner", "inputs": [[[1, 5, 2]]], "outputs": [false]}
interview
https://leetcode.com/problems/predict-the-winner/
class Solution: def PredictTheWinner(self, nums: List[int]) -> bool:
[ "class Solution:\n def PredictTheWinner(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n if not nums: return True\n n = len(nums)\n if n & 1 == 0: return True\n \n dp = [0] * n\n for i in range(n-1, -1, -1):\n...
13
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
207
Given a list of non negative integers, arrange them such that they form the largest number. Example 1: Input: [10,2] Output: "210" Example 2: Input: [3,30,34,5,9] Output: "9534330" Note: The result may be very large, so you need to return a string instead of an integer.
["class Solution:\n def largestNumber(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: str\n \"\"\"\n nums = [str(n) for n in nums]\n \n nums.sort(reverse=True)\n \n for i in range(1, len(nums)):\n if len(nums[i-1]) > len(num...
{"fn_name": "largestNumber", "inputs": [[[10, 2]]], "outputs": ["210"]}
interview
https://leetcode.com/problems/largest-number/
class Solution: def largestNumber(self, nums: List[int]) -> str:
[ "class Solution:\n def largestNumber(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: str\n \"\"\"\n nums = [str(n) for n in nums]\n \n nums.sort(reverse=True)\n \n for i in range(1, len(nums)):\n if len(nums[i-1]) > len(...
11
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ null, null, null, null, null, null, null, null, null, null, null ]
false
208
In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the t...
["class Solution:\n def predictPartyVictory(self, senate):\n \"\"\"\n :type senate: str\n :rtype: str\n \"\"\"\n num = 0 # num of Reeding R\n while ('R' in senate and 'D' in senate):\n res = []\n for i in senate:\n if i=='R':\n...
{"fn_name": "predictPartyVictory", "inputs": [["\"RD\""]], "outputs": ["Dire"]}
interview
https://leetcode.com/problems/dota2-senate/
class Solution: def predictPartyVictory(self, senate: str) -> str:
[ "class Solution:\n def predictPartyVictory(self, senate):\n \"\"\"\n :type senate: str\n :rtype: str\n \"\"\"\n num = 0 # num of Reeding R\n while ('R' in senate and 'D' in senate):\n res = []\n for i in senate:\n if i=='R'...
6
[ 0, 1, 2, 3, 4, 5 ]
[ null, null, null, null, null, null ]
false
209
There are N piles of stones arranged in a row.  The i-th pile has stones[i] stones. A move consists of merging exactly K consecutive piles into one pile, and the cost of this move is equal to the total number of stones in these K piles. Find the minimum cost to merge all piles of stones into one pile.  If it is impossi...
["class Solution:\n def mergeStones(self, stones: List[int], K: int) -> int:\n n = len(stones)\n if (n - 1) % (K - 1) != 0:\n return -1\n prefix = [0]\n for s in stones:\n prefix.append(prefix[-1] + s)\n @lru_cache(None)\n def dp(i, j):\n if ...
{"fn_name": "mergeStones", "inputs": [[[3, 2, 4, 1], 2]], "outputs": [20]}
interview
https://leetcode.com/problems/minimum-cost-to-merge-stones/
class Solution: def mergeStones(self, stones: List[int], K: int) -> int:
[ "class Solution:\n def mergeStones(self, stones: List[int], K: int) -> int:\n n = len(stones)\n if (n - 1) % (K - 1) != 0:\n return -1\n prefix = [0]\n for s in stones:\n prefix.append(prefix[-1] + s)\n @lru_cache(None)\n def dp(i, j):\n ...
28
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
210
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true Example 2: Input: n...
["class Solution:\n def containsNearbyAlmostDuplicate(self, nums, k, t):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type t: int\n :rtype: bool\n \"\"\"\n if len(nums) < 2 or k <= 0 or t < 0: return False\n if t == 0:\n visited = set...
{"fn_name": "containsNearbyAlmostDuplicate", "inputs": [[[1, 2, 3, 1], 3, 0]], "outputs": [true]}
interview
https://leetcode.com/problems/contains-duplicate-iii/
class Solution: def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
[ "class Solution:\n def containsNearbyAlmostDuplicate(self, nums, k, t):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type t: int\n :rtype: bool\n \"\"\"\n if len(nums) < 2 or k <= 0 or t < 0: return False\n if t == 0:\n visited = ...
11
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ null, null, null, null, null, null, null, null, null, null, null ]
false
212
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modu...
["class Solution:\n def numFactoredBinaryTrees(self, A: List[int]) -> int:\n \n mod = 10**9 + 7\n\n nums_set = set(A)\n nums = A.copy()\n nums.sort()\n counts = {}\n total = 0\n\n for n in nums:\n n_count = 1\n for d in nums:\n ...
{"fn_name": "numFactoredBinaryTrees", "inputs": [[[2, 4]]], "outputs": [3]}
interview
https://leetcode.com/problems/binary-trees-with-factors/
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int:
[ "class Solution:\n def numFactoredBinaryTrees(self, A: List[int]) -> int:\n \n mod = 10**9 + 7\n\n nums_set = set(A)\n nums = A.copy()\n nums.sort()\n counts = {}\n total = 0\n\n for n in nums:\n n_count = 1\n for d in nums:\n ...
83
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
213
Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 Example 3: Input: 2.00000, -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/4 = 0.25 Note: -100.0 < x < 100.0 n is a 32-bit signed ...
["class Solution:\n def myPow(self, x, n):\n \"\"\"\n :type x: float\n :type n: int\n :rtype: float\n \"\"\"\n if n == 0:\n return 1\n if abs(n) == 1:\n if n == 1:\n return x\n else:\n return ...
{"fn_name": "myPow", "inputs": [[2.0, 10]], "outputs": [1024.0]}
interview
https://leetcode.com/problems/powx-n/
class Solution: def myPow(self, x: float, n: int) -> float:
[ "class Solution:\n def myPow(self, x, n):\n \"\"\"\n :type x: float\n :type n: int\n :rtype: float\n \"\"\"\n if n == 0:\n return 1\n if abs(n) == 1:\n if n == 1:\n return x\n else:\n retu...
17
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
214
Given an array nums of integers, a move consists of choosing any element and decreasing it by 1. An array A is a zigzag array if either: Every even-indexed element is greater than adjacent elements, ie. A[0] > A[1] < A[2] > A[3] < A[4] > ... OR, every odd-indexed element is greater than adjacent elements, ie. A[0] < A...
["class Solution:\n def movesToMakeZigzag(self, nums):\n n = len(nums)\n res0 = 0\n for i in range(0, n, 2):\n nei = min(nums[j] for j in [i - 1, i + 1] if 0 <= j <= n-1)\n if nums[i] >= nei:\n res0 += nums[i] - nei + 1\n res1 = 0\n for i in ran...
{"fn_name": "movesToMakeZigzag", "inputs": [[[1, 2, 3]]], "outputs": [2]}
interview
https://leetcode.com/problems/decrease-elements-to-make-array-zigzag/
class Solution: def movesToMakeZigzag(self, nums: List[int]) -> int:
[ "class Solution:\n def movesToMakeZigzag(self, nums):\n n = len(nums)\n res0 = 0\n for i in range(0, n, 2):\n nei = min(nums[j] for j in [i - 1, i + 1] if 0 <= j <= n-1)\n if nums[i] >= nei:\n res0 += nums[i] - nei + 1\n res1 = 0\n for i in ...
6
[ 0, 1, 2, 3, 4, 5 ]
[ null, null, null, null, null, null ]
false
215
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False. ...
["class Solution:\n def isGoodArray(self, nums: List[int]) -> bool:\n n = nums[0]\n \n for i in nums:\n n = gcd(i,n)\n \n if n==1:\n return True\n return False\n", "class Solution:\n def isGoodArray(self, nums: List[int]) -> bool:\n ...
{"fn_name": "isGoodArray", "inputs": [[[12, 5, 7, 23]]], "outputs": [true]}
interview
https://leetcode.com/problems/check-if-it-is-a-good-array/
class Solution: def isGoodArray(self, nums: List[int]) -> bool:
[ "class Solution:\n def isGoodArray(self, nums: List[int]) -> bool:\n n = nums[0]\n \n for i in nums:\n n = gcd(i,n)\n \n if n==1:\n return True\n return False\n", "class Solution:\n def isGoodArray(self, nums: List[int]) -> bool:\n ...
39
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
216
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing ...
["class Solution:\n def minNumberOfFrogs(self, croakOfFrogs: str) -> int:\n # valid string? can be seperated into full croaks:\n ### dict of letters. c, r, o, a, k should all be equal, nothing else in\n if len(croakOfFrogs)%5!=0 or croakOfFrogs[0]!='c' or croakOfFrogs[-1]!='k':\n retu...
{"fn_name": "minNumberOfFrogs", "inputs": [["\"croakcroak\""]], "outputs": [-1]}
interview
https://leetcode.com/problems/minimum-number-of-frogs-croaking/
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int:
[ "class Solution:\n def minNumberOfFrogs(self, croakOfFrogs: str) -> int:\n # valid string? can be seperated into full croaks:\n ### dict of letters. c, r, o, a, k should all be equal, nothing else in\n if len(croakOfFrogs)%5!=0 or croakOfFrogs[0]!='c' or croakOfFrogs[-1]!='k':\n r...
29
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
217
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in th...
["class Solution:\n def subarrayBitwiseORs(self, A: List[int]) -> int:\n res = set()\n cur = set()\n for a in A:\n cur = {a | i for i in cur}\n cur |= {a}\n res |= cur\n return len(res)", "class Solution:\n def subarrayBitwiseORs(self, A: List[int]) -> ...
{"fn_name": "subarrayBitwiseORs", "inputs": [[[0]]], "outputs": [1]}
interview
https://leetcode.com/problems/bitwise-ors-of-subarrays/
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int:
[ "class Solution:\n def subarrayBitwiseORs(self, A: List[int]) -> int:\n res = set()\n cur = set()\n for a in A:\n cur = {a | i for i in cur}\n cur |= {a}\n res |= cur\n return len(res)", "class Solution:\n def subarrayBitwiseORs(self, A: List[int]...
99
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
219
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number o...
["class Solution:\n def longestWPI(self, hours: List[int]) -> int:\n ans, count, seen = 0, 0, {}\n for i, hour in enumerate(hours):\n count = count + 1 if hour > 8 else count - 1\n if count > 0:\n ans = i + 1\n else:\n if count not in seen:...
{"fn_name": "longestWPI", "inputs": [[[9, 9, 6, 0, 6, 6, 9]]], "outputs": [3]}
interview
https://leetcode.com/problems/longest-well-performing-interval/
class Solution: def longestWPI(self, hours: List[int]) -> int:
[ "class Solution:\n def longestWPI(self, hours: List[int]) -> int:\n ans, count, seen = 0, 0, {}\n for i, hour in enumerate(hours):\n count = count + 1 if hour > 8 else count - 1\n if count > 0:\n ans = i + 1\n else:\n if count not in se...
45
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
220
Today, the bookstore owner has a store open for customers.length minutes.  Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute. On some minutes, the bookstore owner is grumpy.  If the bookstore owner is grumpy on the i-th minute, grumpy[i] = ...
["class Solution:\n def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:\n # feel like its sliding window max\n \n window, max_window = 0, 0\n \n # init first window\n for i in range(X):\n if grumpy[i]: window += customers[i]\n ma...
{"fn_name": "maxSatisfied", "inputs": [[[1, 0, 1, 2, 1, 1, 7, 5], [0, 1, 0, 1, 0, 1, 0, 1], 3]], "outputs": [16]}
interview
https://leetcode.com/problems/grumpy-bookstore-owner/
class Solution: def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
[ "class Solution:\n def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:\n # feel like its sliding window max\n \n window, max_window = 0, 0\n \n # init first window\n for i in range(X):\n if grumpy[i]: window += customers[i]\n ...
88
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
221
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" ...
["class Solution:\n def longestDupSubstring(self, S):\n nums, N = [ord(c) - ord('a') for c in S], len(S)\n BASE, MOD = 26, 2**32\n def check(L):\n cur_hash, seen = 0, set()\n for val in nums[:L]:\n cur_hash = (cur_hash * BASE + val) % MOD\n seen.ad...
{"fn_name": "longestDupSubstring", "inputs": [["\"banana\""]], "outputs": ["ana"]}
interview
https://leetcode.com/problems/longest-duplicate-substring/
class Solution: def longestDupSubstring(self, S: str) -> str:
[ "class Solution:\n def longestDupSubstring(self, S):\n nums, N = [ord(c) - ord('a') for c in S], len(S)\n BASE, MOD = 26, 2**32\n def check(L):\n cur_hash, seen = 0, set()\n for val in nums[:L]:\n cur_hash = (cur_hash * BASE + val) % MOD\n seen...
70
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
223
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index. According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N −...
["class Solution(object):\n def hIndex(self, citations):\n \"\"\"\n :type citations: List[int]\n :rtype: int\n \"\"\"\n n = len(citations)\n l = 0\n r = n-1\n while l <= r:\n m = (l + r) // 2\n if m == 0 and citations[m] >= n - ...
{"fn_name": "hIndex", "inputs": [[[0, 1, 3, 5, 6]]], "outputs": [3]}
interview
https://leetcode.com/problems/h-index-ii/
class Solution: def hIndex(self, citations: List[int]) -> int:
[ "class Solution(object):\n def hIndex(self, citations):\n \"\"\"\n :type citations: List[int]\n :rtype: int\n \"\"\"\n n = len(citations)\n l = 0\n r = n-1\n while l <= r:\n m = (l + r) // 2\n if m == 0 and citations[m] >= n...
10
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
[ null, null, null, null, null, null, null, null, null, null ]
false
224
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subs...
["class Solution:\n def numDistinct(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: int\n \"\"\"\n setOft=set(t)\n news=\"\"\n for ch in s:\n if ch in setOft:\n news+=ch\n dp=[[1 for i in range(len(news...
{"fn_name": "numDistinct", "inputs": [["\"rabbbit\"", "\"rabbit\""]], "outputs": [3]}
interview
https://leetcode.com/problems/distinct-subsequences/
class Solution: def numDistinct(self, s: str, t: str) -> int:
[ "class Solution:\n def numDistinct(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: int\n \"\"\"\n setOft=set(t)\n news=\"\"\n for ch in s:\n if ch in setOft:\n news+=ch\n dp=[[1 for i in range(len(n...
5
[ 0, 1, 2, 3, 4 ]
[ null, null, null, null, null ]
false
225
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right. After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right ...
["INF = float('inf')\nclass Solution:\n def pushDominoes(self, dominoes: str) -> str:\n n = len(dominoes)\n d1 = [-1] * n\n d2 = [-1] * n\n \n cnt = INF\n for i in range(n - 1, -1, -1):\n if dominoes[i] == 'L':\n cnt = 0\n elif dominoes[i...
{"fn_name": "pushDominoes", "inputs": [["\".L.R...LR..L..\""]], "outputs": ["LLL.RR.LLRRLL..."]}
interview
https://leetcode.com/problems/push-dominoes/
class Solution: def pushDominoes(self, dominoes: str) -> str:
[ "INF = float('inf')\nclass Solution:\n def pushDominoes(self, dominoes: str) -> str:\n n = len(dominoes)\n d1 = [-1] * n\n d2 = [-1] * n\n \n cnt = INF\n for i in range(n - 1, -1, -1):\n if dominoes[i] == 'L':\n cnt = 0\n elif dominoe...
6
[ 0, 1, 2, 3, 4, 5 ]
[ null, null, null, null, null, null ]
false
226
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square. Return the number of permutations of A that are squareful.  Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].   Example 1: Input: [1,17,...
["\nclass Solution:\n def numSquarefulPerms(self, A: List[int]) -> int:\n\n A.sort()\n self.ans = 0\n\n def check(A, i, path):\n return int((A[i] + path[-1])**0.5 + 0.0)**2 == A[i] + path[-1]\n\n def dfs(A, path):\n if not A:\n self.ans += 1\n ...
{"fn_name": "numSquarefulPerms", "inputs": [[[1, 17, 8]]], "outputs": [2]}
interview
https://leetcode.com/problems/number-of-squareful-arrays/
class Solution: def numSquarefulPerms(self, A: List[int]) -> int:
[ "\nclass Solution:\n def numSquarefulPerms(self, A: List[int]) -> int:\n\n A.sort()\n self.ans = 0\n\n def check(A, i, path):\n return int((A[i] + path[-1])**0.5 + 0.0)**2 == A[i] + path[-1]\n\n def dfs(A, path):\n if not A:\n self.ans += 1\n ...
33
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
227
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray i...
["class Solution:\n def longestOnes(self, A: List[int], K: int) -> int:\n hulu = []\n cnt = 0\n num = A[0]\n for x in A:\n if x == num:\n cnt += 1\n else:\n hulu.append([num,cnt])\n cnt = 1\n num = x\n ...
{"fn_name": "longestOnes", "inputs": [[[1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], 2]], "outputs": [6]}
interview
https://leetcode.com/problems/max-consecutive-ones-iii/
class Solution: def longestOnes(self, A: List[int], K: int) -> int:
[ "class Solution:\n def longestOnes(self, A: List[int], K: int) -> int:\n hulu = []\n cnt = 0\n num = A[0]\n for x in A:\n if x == num:\n cnt += 1\n else:\n hulu.append([num,cnt])\n cnt = 1\n num = x\n ...
137
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
229
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: tr...
["class Solution:\n def canReorderDoubled(self, A: List[int]) -> bool:\n cache=Counter(A)\n c_list=sorted(list(cache),key=abs)\n for x in c_list:\n if cache[x]>cache[2*x]:\n return False\n cache[2*x]-=cache[x]\n return True", "class Solution:\n def ...
{"fn_name": "canReorderDoubled", "inputs": [[[3, 1, 3, 6]]], "outputs": [false]}
interview
https://leetcode.com/problems/array-of-doubled-pairs/
class Solution: def canReorderDoubled(self, A: List[int]) -> bool:
[ "class Solution:\n def canReorderDoubled(self, A: List[int]) -> bool:\n cache=Counter(A)\n c_list=sorted(list(cache),key=abs)\n for x in c_list:\n if cache[x]>cache[2*x]:\n return False\n cache[2*x]-=cache[x]\n return True", "class Solution:\n ...
114
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false
230
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. Note: The length of num is less than 10002 and will be ≥ k. The given num does not contain any leading zero. Example 1: Input: num = "1432219", k = 3 Output: "1219" Explanat...
["class Solution:\n def removeKdigits(self, num, k):\n \"\"\"\n :type num: str\n :type k: int\n :rtype: str\n \"\"\"\n out=[]\n for digit in num:\n while k and out and out[-1] > digit:\n out.pop()\n k-=1\n ...
{"fn_name": "removeKdigits", "inputs": [["\"1432219\"", 3]], "outputs": ["\"1219\""]}
interview
https://leetcode.com/problems/remove-k-digits/
class Solution: def removeKdigits(self, num: str, k: int) -> str:
[ "class Solution:\n def removeKdigits(self, num, k):\n \"\"\"\n :type num: str\n :type k: int\n :rtype: str\n \"\"\"\n out=[]\n for digit in num:\n while k and out and out[-1] > digit:\n out.pop()\n k-=1\n ...
5
[ 0, 1, 2, 3, 4 ]
[ null, null, null, null, null ]
false
231
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1] Output: 2 Example 3: Input: [7,8,9,11,12] Output: 1 Note: Your algorithm should run in O(n) time and uses constant extra space.
["class Solution:\n def firstMissingPositive(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n nums = sorted(set(nums), key=lambda x: x)\n result = 0\n for i in range(len(nums)):\n if nums[i] <= 0:\n continue\n ...
{"fn_name": "firstMissingPositive", "inputs": [[[0, 1, 2, 0]]], "outputs": [3]}
interview
https://leetcode.com/problems/first-missing-positive/
class Solution: def firstMissingPositive(self, nums: List[int]) -> int:
[ "class Solution:\n def firstMissingPositive(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n nums = sorted(set(nums), key=lambda x: x)\n result = 0\n for i in range(len(nums)):\n if nums[i] <= 0:\n continue...
16
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
232
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition. You may assume...
["class Solution:\n def findPoisonedDuration(self, timeSeries, duration):\n \"\"\"\n :type timeSeries: List[int]\n :type duration: int\n :rtype: int\n \"\"\"\n if not timeSeries:\n return 0\n prev = timeSeries[0]\n ret = 0\n count =...
{"fn_name": "findPoisonedDuration", "inputs": [[[1, 4], 2]], "outputs": [4]}
interview
https://leetcode.com/problems/teemo-attacking/
class Solution: def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
[ "class Solution:\n def findPoisonedDuration(self, timeSeries, duration):\n \"\"\"\n :type timeSeries: List[int]\n :type duration: int\n :rtype: int\n \"\"\"\n if not timeSeries:\n return 0\n prev = timeSeries[0]\n ret = 0\n coun...
10
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
[ null, null, null, null, null, null, null, null, null, null ]
false
233
In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space.  These characters divide the square into contiguous regions. (Note that backslash characters are escaped, so a \ is represented as "\\".) Return the number of regions.   Example 1: Input: [   " /",   "/ " ] Out...
["from itertools import chain\nclass Solution:\n def regionsBySlashes(self, grid):\n grid = self.convert_grid(grid)\n print(*(list(map(str, x)) for x in grid), sep='\\\n')\n return len([self.destroy_island(x, y, grid) for y in range(len(grid)) for x,v in enumerate(grid[y]) if v == 0])\n\n @st...
{"fn_name": "regionsBySlashes", "inputs": [[["\" /\"", "\"/ \""]]], "outputs": [2]}
interview
https://leetcode.com/problems/regions-cut-by-slashes/
class Solution: def regionsBySlashes(self, grid: List[str]) -> int:
[ "from itertools import chain\nclass Solution:\n def regionsBySlashes(self, grid):\n grid = self.convert_grid(grid)\n print(*(list(map(str, x)) for x in grid), sep='\\\n')\n return len([self.destroy_island(x, y, grid) for y in range(len(grid)) for x,v in enumerate(grid[y]) if v == 0])\n\n ...
30
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 ]
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]
false
234
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', and in any positions ) so that the resulting parentheses string is valid. Formally, a parentheses string is valid if and only if: It is the empty string, or It can be written as AB (A concatenated with B), where A and B...
["class Solution:\n def minAddToMakeValid(self, S: str) -> int:\n if not S:\n return 0\n \n stack = []\n \n add = 0\n for c in S:\n if c == '(':\n stack.append(c)\n elif c == ')':\n if stack:\n ...
{"fn_name": "minAddToMakeValid", "inputs": [["\"())\""]], "outputs": [1]}
interview
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
class Solution: def minAddToMakeValid(self, S: str) -> int:
[ "class Solution:\n def minAddToMakeValid(self, S: str) -> int:\n if not S:\n return 0\n \n stack = []\n \n add = 0\n for c in S:\n if c == '(':\n stack.append(c)\n elif c == ')':\n if stack:\n ...
3
[ 0, 1, 2 ]
[ null, null, null ]
false
236
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.) We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Return the minimum number of flips to make S monotone increasing.   Examp...
["class Solution:\n def minFlipsMonoIncr(self, S: str) -> int:\n onesSoFar = 0\n partial = 0\n \n for n in S:\n if n == '0':\n partial = min(onesSoFar, partial+1) \n else:\n onesSoFar += 1\n \n return partial\n", "clas...
{"fn_name": "minFlipsMonoIncr", "inputs": [["\"00110\""]], "outputs": [2]}
interview
https://leetcode.com/problems/flip-string-to-monotone-increasing/
class Solution: def minFlipsMonoIncr(self, S: str) -> int:
[ "class Solution:\n def minFlipsMonoIncr(self, S: str) -> int:\n onesSoFar = 0\n partial = 0\n \n for n in S:\n if n == '0':\n partial = min(onesSoFar, partial+1) \n else:\n onesSoFar += 1\n \n return partial\n", ...
7
[ 0, 1, 2, 3, 4, 5, 6 ]
[ null, null, null, null, null, null, null ]
false
237
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
["class Solution:\n def numSubarraysWithSum(self, pl, S):\n ans = 0\n \n if(S == 0):\n c = 0\n for i in range(len(pl)):\n if(pl[i] == 0):\n c+=1\n else:\n c = 0\n ans +=c\n return ...
{"fn_name": "numSubarraysWithSum", "inputs": [[[1, 0, 1, 0, 1], 2]], "outputs": [4]}
interview
https://leetcode.com/problems/binary-subarrays-with-sum/
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int:
[ "class Solution:\n def numSubarraysWithSum(self, pl, S):\n ans = 0\n \n if(S == 0):\n c = 0\n for i in range(len(pl)):\n if(pl[i] == 0):\n c+=1\n else:\n c = 0\n ans +=c\n retu...
55
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54...
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null...
false