description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: mem = {} def explore(a, b, stones): if len(stones) == 0: return abs(a - b) entry = a, b, len(stones) if entry in mem: return mem[entry] s = stones.pop() m = min(explore(a + s, b, stones), explore(a, b + s, stones)) stones.append(s) mem[entry] = m return m return explore(0, 0, stones)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: dp = {0} sum_stones = sum(stones) for stone in stones: dp |= {(stone + i) for i in dp} return min(abs(sum_stones - i - i) for i in dp)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: n = len(stones) total = sum(stones) diff = total dp = [([False] * (1 + total)) for _ in range(1 + n)] for i in range(n + 1): dp[i][0] = True for i in range(1, n + 1): s = stones[i - 1] for j in range(1, 1 + total): if j < s: dp[i][j] = dp[i - 1][j] continue dp[i][j] = dp[i - 1][j] or dp[i - 1][j - s] if dp[i][j]: diff = min(diff, abs(total - 2 * j)) return diff
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: target = sum(stones) target = target // 2 dp = [([False] * (target + 1)) for _ in range(len(stones) + 1)] for i in range(len(stones) + 1): for j in range(target + 1): if j == 0: dp[i][j] = True elif stones[i - 1] <= j: dp[i][j] = dp[i - 1][j - stones[i - 1]] or dp[i - 1][j] else: dp[i][j] = dp[i - 1][j] for j in reversed(range(target + 1)): if dp[-1][j]: return sum(stones) - 2 * j
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: self.dp = {} def helper(index, total): if index == len(stones): return abs(total) if (index, total) in self.dp: return self.dp[index, total] self.dp[index, total] = min( helper(index + 1, total + stones[index]), helper(index + 1, total - stones[index]), ) return self.dp[index, total] return helper(0, 0)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: cache = {} def diffs(s1: int, s2: int, xs: List[int]) -> int: if len(xs) == 0: return abs(s1 - s2) if (s1, s2, len(xs)) not in cache: y = xs.pop() min_val = min(diffs(s1 + y, s2, xs), diffs(s1, s2 + y, xs)) xs.append(y) cache[s1, s2, len(xs)] = min_val return cache[s1, s2, len(xs)] return diffs(stones[0], 0, stones[1:])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: def helper(i, currSum): if i == len(stones): return abs(total - 2 * currSum) if (i, currSum) in cache: return cache[i, currSum] takeIt = helper(i + 1, currSum + stones[i]) ignoreIt = helper(i + 1, currSum) cache[i, currSum] = min(takeIt, ignoreIt) return cache[i, currSum] cache = {} total = sum(stones) return helper(0, 0)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: total = sum(stones) target = total // 2 dp = [0] * (target + 1) for stone in stones: for i in range(target, stone - 1, -1): dp[i] = max(dp[i], dp[i - stone] + stone) return total - 2 * dp[-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: n = len(stones) total = sum(stones) memo = {} def dp(i, t): if (i, t) in memo: return memo[i, t] if i == -1: memo[i, t] = t == 0 elif t < 0: memo[i, t] = False else: memo[i, t] = dp(i - 1, t) or dp(i - 1, t - stones[i]) return memo[i, t] return min(abs(total - 2 * t) for t in range(total // 2 + 1) if dp(n - 1, t))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: mem = {} def explore(a, b, stones): if len(stones) == 0: return abs(a - b) entry = a, b, len(stones) if entry in mem: return mem[entry] s = stones.pop() m = min(explore(a + s, b, stones), explore(a, b + s, stones)) stones.append(s) mem[entry] = m return m return explore(0, 0, stones) def lastStoneWeightIITLE(self, stones: List[int]) -> int: N = len(stones) if not stones: return 0 if N == 1: return stones[0] if N == 2: return abs(stones[0] - stones[1]) out = float("inf") for i in range(N): for j in range(i + 1, N): right = stones.pop(j) left = stones.pop(i) diff = abs(right - left) out = min(out, self.lastStoneWeightII(stones + [diff])) stones.insert(i, left) stones.insert(j, right) return out
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Alice is visiting New York City. To make the trip fun, Alice will take photos of the city skyline and give the set of photos as a present to Bob. However, she wants to find the set of photos with maximum beauty and she needs your help. There are $n$ buildings in the city, the $i$-th of them has positive height $h_i$. All $n$ building heights in the city are different. In addition, each building has a beauty value $b_i$. Note that beauty can be positive or negative, as there are ugly buildings in the city too. A set of photos consists of one or more photos of the buildings in the skyline. Each photo includes one or more buildings in the skyline that form a contiguous segment of indices. Each building needs to be in exactly one photo. This means that if a building does not appear in any photo, or if a building appears in more than one photo, the set of pictures is not valid. The beauty of a photo is equivalent to the beauty $b_i$ of the shortest building in it. The total beauty of a set of photos is the sum of the beauty of all photos in it. Help Alice to find the maximum beauty a valid set of photos can have. -----Input----- The first line contains an integer $n$ ($1 \le n \le 3 \cdot 10^5$), the number of buildings on the skyline. The second line contains $n$ distinct integers $h_1, h_2, \ldots, h_n$ ($1 \le h_i \le n$). The $i$-th number represents the height of building $i$. The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($-10^9 \le b_i \le 10^9$). The $i$-th number represents the beauty of building $i$. -----Output----- Print one number representing the maximum beauty Alice can achieve for a valid set of photos of the skyline. -----Examples----- Input 5 1 2 3 5 4 1 5 3 2 4 Output 15 Input 5 1 4 3 2 5 -3 4 -10 2 7 Output 10 Input 2 2 1 -2 -3 Output -3 Input 10 4 7 3 2 5 1 9 10 6 8 -4 40 -46 -8 -16 4 -10 41 12 3 Output 96 -----Note----- In the first example, Alice can achieve maximum beauty by taking five photos, each one containing one building. In the second example, Alice can achieve a maximum beauty of $10$ by taking four pictures: three just containing one building, on buildings $1$, $2$ and $5$, each photo with beauty $-3$, $4$ and $7$ respectively, and another photo containing building $3$ and $4$, with beauty $2$. In the third example, Alice will just take one picture of the whole city. In the fourth example, Alice can take the following pictures to achieve maximum beauty: photos with just one building on buildings $1$, $2$, $8$, $9$, and $10$, and a single photo of buildings $3$, $4$, $5$, $6$, and $7$.
p = lambda: list(map(int, input().split())) n = p()[0] A = [0] + p() B = [0] + p() d = 0 v = [0] * (n + 1) p = [-(9**42)] + [0] * n s = [] for i in range(1, n + 1): while s and A[s[-1]] > A[i]: d = max(d, v[len(s)]) s.pop() s += [i] v[len(s)] = d d = p[len(s)] = max(p[len(s) - 1], d + B[i]) print(d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice is visiting New York City. To make the trip fun, Alice will take photos of the city skyline and give the set of photos as a present to Bob. However, she wants to find the set of photos with maximum beauty and she needs your help. There are $n$ buildings in the city, the $i$-th of them has positive height $h_i$. All $n$ building heights in the city are different. In addition, each building has a beauty value $b_i$. Note that beauty can be positive or negative, as there are ugly buildings in the city too. A set of photos consists of one or more photos of the buildings in the skyline. Each photo includes one or more buildings in the skyline that form a contiguous segment of indices. Each building needs to be in exactly one photo. This means that if a building does not appear in any photo, or if a building appears in more than one photo, the set of pictures is not valid. The beauty of a photo is equivalent to the beauty $b_i$ of the shortest building in it. The total beauty of a set of photos is the sum of the beauty of all photos in it. Help Alice to find the maximum beauty a valid set of photos can have. -----Input----- The first line contains an integer $n$ ($1 \le n \le 3 \cdot 10^5$), the number of buildings on the skyline. The second line contains $n$ distinct integers $h_1, h_2, \ldots, h_n$ ($1 \le h_i \le n$). The $i$-th number represents the height of building $i$. The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($-10^9 \le b_i \le 10^9$). The $i$-th number represents the beauty of building $i$. -----Output----- Print one number representing the maximum beauty Alice can achieve for a valid set of photos of the skyline. -----Examples----- Input 5 1 2 3 5 4 1 5 3 2 4 Output 15 Input 5 1 4 3 2 5 -3 4 -10 2 7 Output 10 Input 2 2 1 -2 -3 Output -3 Input 10 4 7 3 2 5 1 9 10 6 8 -4 40 -46 -8 -16 4 -10 41 12 3 Output 96 -----Note----- In the first example, Alice can achieve maximum beauty by taking five photos, each one containing one building. In the second example, Alice can achieve a maximum beauty of $10$ by taking four pictures: three just containing one building, on buildings $1$, $2$ and $5$, each photo with beauty $-3$, $4$ and $7$ respectively, and another photo containing building $3$ and $4$, with beauty $2$. In the third example, Alice will just take one picture of the whole city. In the fourth example, Alice can take the following pictures to achieve maximum beauty: photos with just one building on buildings $1$, $2$, $8$, $9$, and $10$, and a single photo of buildings $3$, $4$, $5$, $6$, and $7$.
n = int(input()) A = [0] + list(map(int, input().split())) B = [0] + list(map(int, input().split())) dp = [0] * (n + 1) val = [0] * (n + 1) pre = [0] * (n + 1) pre[0] = float("-inf") stack = [] for i in range(1, n + 1): tmp = dp[i - 1] while stack and A[stack[-1]] > A[i]: tmp = max(tmp, val[len(stack)]) stack.pop() stack.append(i) val[len(stack)] = tmp dp[i] = pre[len(stack)] = max(pre[len(stack) - 1], tmp + B[i]) print(dp[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): length = len(triangle) dp = [] for i in range(length): dp.append([]) for j in range(len(triangle[i])): dp[i].append(float("inf")) dp[0][0] = triangle[0][0] for i in range(1, length): dp[i][0] = dp[i - 1][0] + triangle[i][0] dp[i][-1] = dp[i - 1][-1] + triangle[i][-1] for i in range(1, length): for j in range(1, len(triangle[i]) - 1): dp[i][j] = min( dp[i][j], dp[i - 1][j] + triangle[i][j], dp[i - 1][j - 1] + triangle[i][j], ) return min(dp[length - 1])
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): res = [0] for row in triangle: res = [ (row[i] + min([res[j] for j in (i - 1, i) if 0 <= j < len(res)])) for i in range(len(row)) ] return min(res)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): if not triangle or not triangle[0]: return 0 min_path_sum = [([0] * len(triangle)) for row in range(len(triangle))] min_path_sum[0][0] = triangle[0][0] ROWS = len(triangle) def get_valid_adjacents_of(row, col): if col == 0: return [(row - 1, col)] if col == row: return [(row - 1, col - 1)] return [(row - 1, col - 1), (row - 1, col)] for row in range(1, ROWS): for col in range(0, row + 1): adjacents = get_valid_adjacents_of(row, col) min_path_sum[row][col] = triangle[row][col] + min( min_path_sum[r][c] for r, c in adjacents ) return min(min_path_sum[-1])
CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST BIN_OP VAR NUMBER VAR IF VAR VAR RETURN LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): self._min_cache = {} for i in reversed(list(range(len(triangle)))): for j in reversed(list(range(len(triangle[i])))): self.get_minimum_sum(triangle, i, j) return self.get_minimum_sum(triangle, 0, 0) def get_minimum_sum(self, triangle, level_i, item_j): if (level_i, item_j) in self._min_cache: return self._min_cache[level_i, item_j] if level_i < 0 or level_i >= len(triangle): return 0 level = triangle[level_i] if item_j < 0 or item_j > len(level): return 0 item = level[item_j] min_v = item + min( self.get_minimum_sum(triangle, level_i + 1, item_j), self.get_minimum_sum(triangle, level_i + 1, item_j + 1), ) self._min_cache[level_i, item_j] = min_v return min_v
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): rows = len(triangle) minPath = triangle[rows - 1] for i in range(rows - 1)[::-1]: for j in range(i + 1): minPath[j] = min(minPath[j], minPath[j + 1]) + triangle[i][j] return minPath[0]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR NUMBER
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): if not triangle: return 0 dp = triangle.pop() while triangle: level = triangle.pop() for i in range(len(level)): dp[i] = min(dp[i], dp[i + 1]) + level[i] return dp[0]
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): cost = [[(0) for j in range(len(triangle))] for i in range(2)] for row in range(len(triangle)): for col in range(len(triangle[row])): if row == 0: cost[row][col] = triangle[row][col] elif col - 1 >= 0 and col < len(triangle[row - 1]): cost[row % 2][col] = min( cost[(row - 1) % 2][col - 1] + triangle[row][col], cost[(row - 1) % 2][col] + triangle[row][col], ) elif col - 1 >= 0: cost[row % 2][col] = ( cost[(row - 1) % 2][col - 1] + triangle[row][col] ) elif col < len(triangle[row - 1]): cost[row % 2][col] = cost[(row - 1) % 2][col] + triangle[row][col] return min(cost[(len(triangle) - 1) % 2])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def nodeSum(self, L, U): ans = [0] * len(U) for i, v in enumerate(U): ans[i] = v + min(L[i], L[i + 1]) return ans def minimumTotal(self, triangle): L = triangle[-1] for i in range(len(triangle) - 1, 0, -1): L = self.nodeSum(L, triangle[i - 1]) return L[0]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): mini = 100000000 level = 0 while level < len(triangle) - 1: for i in range(len(triangle[level + 1])): if i == 0: triangle[level + 1][i] += triangle[level][i] elif i == len(triangle[level]): triangle[level + 1][i] += triangle[level][i - 1] else: triangle[level + 1][i] += min( triangle[level][i - 1], triangle[level][i] ) level += 1 return min(triangle[len(triangle) - 1])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): row = [triangle[0][0]] for i in range(1, len(triangle)): new_row = [] for j in range(len(row) + 1): new_row.append( triangle[i][j] + min( row[j - 1] if j - 1 >= 0 else sys.maxsize, row[j] if j < len(row) else sys.maxsize, ) ) row = new_row return min(row)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): layer_costs = triangle[-1] for layer in range(len(triangle) - 2, -1, -1): for pos in range(len(triangle[layer])): min_cost = ( min(layer_costs[pos], layer_costs[pos + 1]) + triangle[layer][pos] ) layer_costs[pos] = min_cost return layer_costs[0]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): memory = [] for i in range(len(triangle)): memory.append({}) return self.helper(memory, triangle, 0, 0) def helper(self, memory, triangle, i, j): if i >= len(triangle): return 0 if memory[i].get(j) is None: memory[i][j] = triangle[i][j] + min( self.helper(memory, triangle, i + 1, j), self.helper(memory, triangle, i + 1, j + 1), ) return memory[i][j]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR DICT RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution: def minimumTotal(self, triangle): length = len(triangle) for i in range(length - 1, 0, -1): for j in range(1, len(triangle[i])): if triangle[i][j] < triangle[i][j - 1]: triangle[i - 1][j - 1] += triangle[i][j] else: triangle[i - 1][j - 1] += triangle[i][j - 1] return triangle[0][0]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def shortest_distance(self, matrix): n = len(matrix) for i in range(n): for j in range(n): if matrix[i][j] == -1: matrix[i][j] = float("inf") for via in range(n): for i in range(n): for j in range(n): matrix[i][j] = min(matrix[i][j], matrix[i][via] + matrix[via][j]) for i in range(n): for j in range(n): if matrix[i][j] == float("inf"): matrix[i][j] = -1 return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER RETURN VAR
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def shortest_distance(self, mat): n = len(mat) for k in range(n): for i in range(n): for j in range(n): if mat[i][k] != -1 and mat[k][j] != -1: if mat[i][j] == -1: mat[i][j] = mat[i][k] + mat[k][j] else: mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j])
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def shortest_distance(self, matrix): for i in range(len(matrix)): for j in range(len(matrix[0])): if i == j: matrix[i][j] = 0 continue if matrix[i][j] == -1: matrix[i][j] = 10**9 for k in range(len(matrix)): for i in range(len(matrix)): for j in range(len(matrix)): matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j]) for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j] == 10**9: matrix[i][j] = -1 return matrix
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def shortest_distance(self, matrix): rows = len(matrix) cols = len(matrix[0]) for row in range(rows): for col in range(cols): if matrix[row][col] == -1: matrix[row][col] = 1001 for k in range(rows): for src in range(rows): for dest in range(rows): matrix[src][dest] = min( matrix[src][dest], matrix[src][k] + matrix[k][dest] ) for row in range(rows): for col in range(cols): if matrix[row][col] == 1001: matrix[row][col] = -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def helper(self, source, dest, visited): if source == dest: return 0 if source in visited: return 10**9 visited.add(source) ans = self.matrix[source][dest] for i in range(len(self.matrix[0])): if self.matrix[source][i] != -1: ans = min(ans, self.matrix[source][i] + self.helper(i, dest, visited)) self.matrix[source][dest] = ans return ans def shortest_distance(self, matrix): n = len(matrix) for i in range(n): for j in range(n): if matrix[i][j] == -1: matrix[i][j] = 10**9 for k in range(n): for i in range(n): for j in range(n): matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j]) for i in range(n): for j in range(n): if matrix[i][j] == 10**9: matrix[i][j] = -1 return matrix
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def shortest_distance(self, matrix): ans = [[(0) for _ in range(len(matrix[i]))] for i in range(len(matrix))] INF = float("inf") for i in range(len(matrix)): for j in range(len(matrix[i])): if matrix[i][j] != -1: ans[i][j] = matrix[i][j] else: ans[i][j] = INF for k in range(len(matrix)): for i in range(len(matrix)): for j in range(len(matrix[i])): if ( ans[i][k] < INF and ans[k][j] < INF and ans[i][k] + ans[k][j] < INF ): ans[i][j] = min(ans[i][k] + ans[k][j], ans[i][j]) for i in range(len(matrix)): for j in range(len(matrix[i])): if matrix[i][j] != INF: matrix[i][j] = ans[i][j] else: matrix[i][j] = -1 return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def shortest_distance(self, matrix): return floyd_warshall(matrix) def change_to_inf(G): for i in range(len(G)): for j in range(len(G)): if G[i][j] == -1: G[i][j] = float("inf") return G def back_to_inf(G): for i in range(len(G)): for j in range(len(G)): if G[i][j] == float("inf"): G[i][j] = -1 return G def floyd_warshall(G): n = len(G) G = change_to_inf(G) for k in range(n): for i in range(n): for j in range(n): if G[i][j] > G[i][k] + G[k][j]: G[i][j] = G[i][k] + G[k][j] return back_to_inf(G)
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def shortest_distance(self, m): n = len(m) s = 0 for i in range(0, n): for j in range(0, n): if i == j: m[i][j] = 0 if m[i][j] == -1: m[i][j] = 32767 for k in range(0, n): for i in range(0, n): for j in range(0, n): s = m[i][k] + m[k][j] m[i][j] = min(m[i][j], s) for i in range(0, n): for j in range(0, n): if m[i][j] == 32767: m[i][j] = -1 return m
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def shortest_distance(self, matrix): V = len(matrix) for k in range(V): for u in range(V): if matrix[u][k] == -1: continue for v in range(V): if matrix[k][v] == -1: continue if matrix[u][v] == -1: matrix[u][v] = matrix[u][k] + matrix[k][v] elif matrix[u][k] + matrix[k][v] < matrix[u][v]: matrix[u][v] = matrix[u][k] + matrix[k][v]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j. Do it in-place. Example 1: Input: matrix = {{0,25},{-1,0}} Output: {{0,25},{-1,0}} Explanation: The shortest distance between every pair is already given(if it exists). Example 2: Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}} Output: {{0,1,7},{1,0,6},{-1,-1,0}} Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43. Your Task: You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place. Expected Time Complexity: O(n^{3}) Expected Space Complexity: O(1) Constraints: 1 <= n <= 100 -1 <= matrix[ i ][ j ] <= 1000
class Solution: def shortest_distance(self, matrix): V = len(matrix) for e in range(V): for i in range(V): for j in range(V): curr = matrix[i][j] if matrix[i][j] != -1 else float("inf") next = ( matrix[i][e] + matrix[e][j] if matrix[i][e] != -1 and matrix[e][j] != -1 else float("inf") ) matrix[i][j] = min(curr, next) matrix[i][j] = matrix[i][j] if matrix[i][j] != float("inf") else -1 return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR STRING VAR VAR VAR NUMBER RETURN VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) res = 0 top, left = [a[:] for a in grid], [a[:] for a in grid] for i in range(m): for j in range(n): if grid[i][j]: if i: top[i][j] = top[i - 1][j] + 1 if j: left[i][j] = left[i][j - 1] + 1 for r in range(min(m, n), 0, -1): for i in range(m - r + 1): for j in range(n - r + 1): if ( min( top[i + r - 1][j], top[i + r - 1][j + r - 1], left[i][j + r - 1], left[i + r - 1][j + r - 1], ) >= r ): return r * r return 0
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP VAR VAR RETURN NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) left = [[(0) for j in range(n)] for i in range(m)] right = [[(0) for j in range(n)] for i in range(m)] top = [[(0) for j in range(n)] for i in range(m)] bottom = [[(0) for j in range(n)] for i in range(m)] for i in range(m): for j in range(n): if grid[i][j]: if i: top[i][j] = top[i - 1][j] + 1 else: top[i][j] = 1 if j: left[i][j] = left[i][j - 1] + 1 else: left[i][j] = 1 for i in range(m - 1, -1, -1): for j in range(n - 1, -1, -1): if grid[i][j]: if i == m - 1: bottom[i][j] = 1 else: bottom[i][j] = bottom[i + 1][j] + 1 if j == n - 1: right[i][j] = 1 else: right[i][j] = right[i][j + 1] + 1 res = 0 for i in range(m): for j in range(n): if grid[i][j]: res = max(res, 1) side = min(left[i][j], top[i][j]) for k in range(side, 0, -1): x, y = i - k + 1, j - k + 1 if right[x][y] >= k and bottom[x][y] >= k: res = max(res, k**2) break return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) table = [[(0, 0) for _ in range(cols)] for _ in range(rows)] for i in range(rows): for j in range(cols): if grid[i][j] == 1: hor = 1 if j == 0 else table[i][j - 1][0] + 1 ver = 1 if i == 0 else table[i - 1][j][1] + 1 table[i][j] = hor, ver ans = float("-inf") for i in reversed(range(rows)): for j in reversed(range(cols)): if table[i][j] != (0, 0): min_ = min(table[i][j]) while min_ > ans: up = i - min_ + 1 left = j - min_ + 1 if ( table[i][left][1] >= min_ and table[up][j][0] >= min_ and min_ > ans ): ans = min_ min_ -= 1 return ans * ans if ans != float("-inf") else 0
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR STRING BIN_OP VAR VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: ones_up_to_index_rows = [[0] for i in range(len(grid))] ones_up_to_index_columns = [[0] for i in range(len(grid[0]))] ret = 0 for row in range(len(grid)): for column in range(len(grid[0])): ones_up_to_index_rows[row].append( ones_up_to_index_rows[row][-1] + grid[row][column] ) ones_up_to_index_columns[column].append( ones_up_to_index_columns[column][-1] + grid[row][column] ) for row in range(len(grid)): column = 0 if len(grid) - row > ret: for column in range(len(grid[0])): next_column = column while next_column < len(grid[0]) and grid[row][next_column] == 1: next_column += 1 for i in range(next_column - 1, column - 1, -1): side_length = i - column + 1 if side_length <= ret: break row_index_dp = row + side_length - 1 if ( row_index_dp < len(grid) and ones_up_to_index_columns[i][row_index_dp + 1] - ones_up_to_index_columns[i][row] == side_length and ones_up_to_index_rows[row_index_dp][i + 1] - ones_up_to_index_rows[row_index_dp][column] == side_length and ones_up_to_index_columns[column][row_index_dp + 1] - ones_up_to_index_columns[column][row] == side_length ): ret = side_length break else: break return ret**2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, A): m, n = len(A), len(A[0]) res = 0 top, left = [a[:] for a in A], [a[:] for a in A] for i in range(m): for j in range(n): if A[i][j]: if i: top[i][j] = top[i - 1][j] + 1 if j: left[i][j] = left[i][j - 1] + 1 for r in range(min(m, n), 0, -1): for i in range(m - r + 1): for j in range(n - r + 1): if ( min( top[i + r - 1][j], top[i + r - 1][j + r - 1], left[i][j + r - 1], left[i + r - 1][j + r - 1], ) == r ): return r * r elif ( min( top[i + r - 1][j], top[i + r - 1][j + r - 1], left[i][j + r - 1], left[i + r - 1][j + r - 1], ) > r ): print( i, j, r, min( top[i + r - 1][j], top[i + r - 1][j + r - 1], left[i][j + r - 1], left[i + r - 1][j + r - 1], ), m, n, ) return r * r return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR RETURN NUMBER
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: if not grid: return 0 m = len(grid) n = len(grid[0]) max_len = max(m, n) row_count = [[grid[i][0]] for i in range(m)] for i in range(m): for j in range(1, n): if grid[i][j]: row_count[i].append(row_count[i][j - 1] + 1) else: row_count[i].append(0) col_count = [[grid[0][j] for j in range(n)]] for i in range(1, m): col_count.append([]) for j in range(n): if grid[i][j]: col_count[i].append(col_count[i - 1][j] + 1) else: col_count[i].append(0) for l in range(max_len, 0, -1): for i in range(m - l + 1): for j in range(n - l + 1): if self.find_board(i, j, l, row_count, col_count): return l * l return 0 def find_board(self, i, j, l, row_count, col_count): if l == 1: return row_count[i][j] == 1 l = l - 1 x, y = i + l, j + l if row_count[i][y] <= l: return False if row_count[x][y] <= l: return False if col_count[x][j] <= l: return False if col_count[x][y] <= l: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR RETURN NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: n = len(grid) m = len(grid[0]) rows = [[(0) for j in range(m + 1)] for i in range(1 + n)] cols = [[(0) for j in range(m + 1)] for i in range(1 + n)] for i in range(1, n + 1): for j in range(1, m + 1): rows[i][j] = rows[i][j - 1] + grid[i - 1][j - 1] cols[i][j] = cols[i - 1][j] + grid[i - 1][j - 1] res = 0 for i in range(1, n + 1): for j in range(1, m + 1): if grid[i - 1][j - 1]: k = 1 res = max(res, 1) while i + k <= n and j + k <= m: if ( rows[i][j + k] - rows[i][j - 1] == rows[i + k][j + k] - rows[i + k][j - 1] == cols[i + k][j] - cols[i - 1][j] == cols[i + k][j + k] - cols[i - 1][j + k] == k + 1 ): res = max(res, (k + 1) * (k + 1)) k += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: def checkSquare(i, j, k): for ind in range(k + 1): if ( grid[i][j + ind] == 0 or grid[i + ind][j] == 0 or grid[i + ind][k + j] == 0 or grid[i + k][j + ind] == 0 ): return False return True n = len(grid) m = len(grid[0]) max_ = 0 min_ = 0 for i in range(n): for j in range(m): if grid[i][j] == 1: min_ = 1 bound = min(n - i, m - j) for k in range(1, bound): if checkSquare(i, j, k): max_ = max(max_, (k + 1) ** 2) return max(max_, min_)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: if not grid: return 0 edge, m, n = 0, len(grid), len(grid[0]) left, top = [[(0) for j in range(n + 1)] for i in range(m + 1)], [ [(0) for j in range(n + 1)] for i in range(m + 1) ] for i in range(m): for j in range(n): left[i + 1][j + 1] = 0 if grid[i][j] == 0 else 1 + left[i + 1][j] top[i + 1][j + 1] = 0 if grid[i][j] == 0 else 1 + top[i][j + 1] left_ones, top_one = left[i + 1][j + 1], top[i + 1][j + 1] for length in range(min(left_ones, top_one), edge, -1): if ( min(left[i + 2 - length][j + 1], top[i + 1][j + 2 - length]) >= length ): edge = max(edge, length) break return edge**2
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) if m > 0 else 0 up = [([0] * n) for _ in range(m)] left = [([0] * n) for _ in range(m)] down = [([0] * n) for _ in range(m)] right = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): if grid[i][j] == 1: up[i][j] = 1 + (up[i - 1][j] if i > 0 else 0) left[i][j] = 1 + (left[i][j - 1] if j > 0 else 0) for i in range(m - 1, -1, -1): for j in range(n - 1, -1, -1): if grid[i][j] == 1: down[i][j] = 1 + (down[i + 1][j] if i + 1 < m else 0) right[i][j] = 1 + (right[i][j + 1] if j + 1 < n else 0) print(up) print(left) print(down) print(right) ans = -1 for i in range(m): for j in range(n): min_1 = min(up[i][j], left[i][j]) for k in range(min_1): if min(down[i - k][j - k], right[i - k][j - k]) >= k: ans = max(ans, k) return (ans + 1) ** 2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def getLeftandTopOnes(self, grid: List[List[int]]): leftGrid = [([0] * len(grid[0])) for i in range(len(grid))] topGrid = [([0] * len(grid[0])) for i in range(len(grid))] i = 0 while i < len(grid): j = 0 while j < len(grid[0]): if i > 0: if grid[i - 1][j] == 1: topGrid[i][j] = topGrid[i - 1][j] + 1 elif grid[i - 1][j] == 0: topGrid[i][j] = 0 if j > 0: if grid[i][j - 1] == 1: leftGrid[i][j] = leftGrid[i][j - 1] + 1 elif grid[i][j - 1] == 0: leftGrid[i][j] = 0 j += 1 i += 1 return leftGrid, topGrid def getRightandDownOnes(self, grid: List[List[int]]): rightGrid = [([0] * len(grid[0])) for i in range(len(grid))] downGrid = [([0] * len(grid[0])) for i in range(len(grid))] i = len(grid) - 1 while i >= 0: j = len(grid[0]) - 1 while j >= 0: if i < len(grid) - 1: if grid[i + 1][j] == 1: downGrid[i][j] = downGrid[i + 1][j] + 1 elif grid[i + 1][j] == 0: downGrid[i][j] = 0 if j < len(grid[0]) - 1: if grid[i][j + 1] == 1: rightGrid[i][j] = rightGrid[i][j + 1] + 1 elif grid[i][j + 1] == 0: rightGrid[i][j] = 0 j -= 1 i -= 1 return rightGrid, downGrid def largest1BorderedSquare(self, grid: List[List[int]]) -> int: leftOnes, topOnes = self.getLeftandTopOnes(grid) print(leftOnes) print(topOnes) rightOnes, downOnes = self.getRightandDownOnes(grid) print(rightOnes) print(downOnes) maxBorder = 0 i = 0 seenOne = False while i < len(grid): j = 0 while j < len(grid[0]): if grid[i][j] == 1: seenOne = True length = min(topOnes[i][j], leftOnes[i][j]) while length >= 0: if i - length < 0 or j - length < 0: length -= 1 continue if length == 0: break if ( topOnes[i][j - length] >= length and leftOnes[i - length][j] >= length ): break length -= 1 maxBorder = max(maxBorder, length) j += 1 i += 1 if not seenOne: return 0 else: return (maxBorder + 1) ** 2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, A: List[List[int]]) -> int: n, m = len(A), len(A[0]) V, H = [([0] * m) for _ in range(n)], [([0] * m) for _ in range(n)] for col in range(m): for row in range(n): if row == 0: V[row][col] = A[row][col] else: V[row][col] = 0 if A[row][col] == 0 else V[row - 1][col] + 1 for row in range(n): for col in range(m): if col == 0: H[row][col] = A[row][col] else: H[row][col] = 0 if A[row][col] == 0 else H[row][col - 1] + 1 res = 0 for row in range(n): for col in range(m): small = min(H[row][col], V[row][col]) while small > res: if ( V[row][col - small + 1] >= small and H[row - small + 1][col] >= small ): res = small break small -= 1 return res * res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) sum_row = [] sum_col = [] for i in range(m): sum_row.append([0] * n) sum_col.append([0] * n) for i in range(m): sum_row[i][0] = grid[i][0] for j in range(1, n): sum_row[i][j] = sum_row[i][j - 1] + grid[i][j] for j in range(n): sum_col[0][j] = grid[0][j] for i in range(1, m): sum_col[i][j] = sum_col[i - 1][j] + grid[i][j] num_ele = 0 for i in range(m): for j in range(n): l = min(i, j) for k in range(l + 1): slen = l - k length = slen + 1 if ( sum_row[i - slen][j] - sum_row[i - slen][j - slen] + grid[i - slen][j - slen] == length and sum_row[i][j] - sum_row[i][j - slen] + grid[i][j - slen] == length and sum_col[i][j - slen] - sum_col[i - slen][j - slen] + grid[i - slen][j - slen] == length and sum_col[i][j] - sum_col[i - slen][j] + grid[i - slen][j] == length ): if length * length > num_ele: num_ele = length * length break return num_ele
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: dp = [([None] * len(grid[0])) for _ in range(len(grid))] dp[0][0] = (1, 1) if grid[0][0] == 1 else (0, 0) for r in range(len(grid)): for c in range(len(grid[0])): if r == 0 and c == 0: continue elif r == 0: if grid[0][c] == 1: newR = 1 newC = 1 + dp[0][c - 1][1] dp[r][c] = newR, newC else: dp[r][c] = 0, 0 elif c == 0: if grid[r][0] == 1: newR = 1 + dp[r - 1][0][0] newC = 1 dp[r][c] = newR, newC else: dp[r][c] = 0, 0 elif grid[r][c] == 1: newR = 1 + dp[r - 1][c][0] newC = 1 + dp[r][c - 1][1] dp[r][c] = newR, newC else: dp[r][c] = 0, 0 maxGrid = 0 for r in range(len(grid) - 1, -1, -1): for c in range(len(grid[0]) - 1, -1, -1): minSpan = min(dp[r][c][0], dp[r][c][1]) if minSpan == 0: continue for span in range(minSpan, -1, -1): cSpanLeft = c - span + 1 rSpanUp = r - span + 1 if dp[r][cSpanLeft][0] >= span and dp[rSpanUp][c][1] >= span: maxGrid = max(maxGrid, span**2) break return maxGrid
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: iMax = len(grid) jMax = len(grid[0]) table = [[[0, 0] for j in range(jMax + 1)] for i in range(iMax + 1)] ans = 0 for i in range(1, iMax + 1): for j in range(1, jMax + 1): if grid[i - 1][j - 1] == 1: table[i][j][0] = table[i - 1][j][0] + 1 table[i][j][1] = table[i][j - 1][1] + 1 for k in range(min(table[i][j]), 0, -1): if min(table[i - k + 1][j][1], table[i][j - k + 1][0]) >= k: ans = max(ans, k) break return ans * ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) x = min(m, n) right = [[(0) for i in range(n)] for j in range(m)] down = [[(0) for i in range(n)] for j in range(m)] for i in range(m - 1, -1, -1): for j in range(n - 1, -1, -1): if grid[i][j] == 0: continue right[i][j] = 1 + (right[i][j + 1] if j + 1 < n else 0) down[i][j] = 1 + (down[i + 1][j] if i + 1 < m else 0) for l in range(x, 0, -1): for i in range(0, m - l + 1): for j in range(0, n - l + 1): if ( right[i][j] >= l and down[i][j] >= l and right[i + l - 1][j] >= l and down[i][j + l - 1] >= l ): return l * l return 0
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP VAR VAR RETURN NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def checkForSquare( self, grid: List[List[int]], i: int, j: int, squareLen: int ) -> bool: maxRowBound = i + squareLen - 1 maxColBound = j + squareLen - 1 if self.accessGrid(grid, maxRowBound, maxColBound) == 1: k = maxColBound while k != j and self.accessGrid(grid, maxRowBound, k) == 1: k -= 1 if k != j: return False s = maxRowBound while s != i and self.accessGrid(grid, s, maxColBound) == 1: s -= 1 if s == i: return True return False def accessGrid(self, grid: List[List[int]], i: int, j: int) -> int: if i < self.rows and j < self.cols: return grid[i][j] else: return -1 def largest1BorderedSquare(self, grid: List[List[int]]) -> int: self.rows = len(grid) self.cols = len(grid[0]) i = 0 j = 0 largest_square = 0 while i < self.rows: while j < self.cols: search_increment = 1 if grid[i][j] == 1: if largest_square == 0: largest_square = 1 if j != self.cols - 1: max_col_bound = j max_row_bound = i while self.accessGrid(grid, i, max_col_bound) == 1: max_col_bound += 1 square_len = max_col_bound - j if square_len > 0: while self.accessGrid(grid, max_row_bound, j) == 1: max_row_bound += 1 square_len = min(square_len, max_row_bound - i) while square_len**2 > largest_square: if self.checkForSquare(grid, i, j, square_len): largest_square = square_len**2 search_increment = square_len - 1 else: square_len -= 1 j += search_increment remaining_search_space = (self.cols - j) ** 2 if largest_square >= remaining_search_space: break i += 1 j = 0 remaining_search_space = (self.rows - i) ** 2 if largest_square >= remaining_search_space: return largest_square return largest_square
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR RETURN NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) dp = [([None] * cols) for _ in range(rows)] maxSize = 0 for i in range(rows): for j in range(cols): x, y = 0, 0 if grid[i][j] == 1: if i > 0 and dp[i - 1][j]: x = dp[i - 1][j][0] + 1 if j > 0 and dp[i][j - 1]: y = dp[i][j - 1][1] + 1 dp[i][j] = x, y sqlen = min(x, y) while sqlen >= maxSize: left = dp[i][j - sqlen] up = dp[i - sqlen][j] if left[0] >= sqlen and up[1] >= sqlen: maxSize = sqlen + 1 sqlen -= 1 return maxSize * maxSize
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: def Check(i, j): ans = 1 Dis = min(m - i, n - j) for k in range(max_dis, Dis): ok = True for h in range(i, i + k + 1): if grid[h][j] == 0: ok = False break if grid[h][j + k] == 0: ok = False break for h in range(j, j + k + 1): if grid[i][h] == 0: ok = False break if grid[i + k][h] == 0: ok = False break if ok: ans = max(ans, (k + 1) * (k + 1)) return ans m = len(grid) n = len(grid[0]) max_so_far = 0 max_dis = 0 for i in range(m): for j in range(n): if grid[i][j] == 1 and i + max_dis < m and j + max_dis < n: max_so_far = max(max_so_far, Check(i, j)) max_dis = int(math.sqrt(max_so_far)) return max_so_far
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: horizon = [([0] * len(grid[0])) for _ in range(len(grid))] vertical = [([0] * len(grid[0])) for _ in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1: horizon[i][j] = horizon[i][j - 1] + 1 for i in range(len(grid[0])): for j in range(len(grid)): if grid[j][i] == 1: vertical[j][i] = vertical[j - 1][i] + 1 res = 0 for i in range(len(grid)): for j in range(len(grid[0])): s = min(horizon[i][j], vertical[i][j]) for k in range(s, 0, -1): if horizon[i - k + 1][j] >= k and vertical[i][j - k + 1] >= k: res = max(res, k) break return res * res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: ans = 0 m, n = len(grid), len(grid[0]) h = [row[:] for row in grid] v = [row[:] for row in grid] for row in range(m): for col in range(1, n): if h[row][col] != 0: h[row][col] += h[row][col - 1] for col in range(n): for row in range(1, m): if v[row][col] != 0: v[row][col] += v[row - 1][col] for row in range(m): for col in range(n): curr = grid[row][col] if not curr: continue side = h[row][col] if h[row][col] < v[row][col] else v[row][col] res = 1 for x in range(side, 0, -1): if v[row][col - x + 1] >= x and h[row - x + 1][col] >= x: res = x * x break if res > ans: ans = res return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: n = len(grid) m = len(grid[0]) def expand_square(r, c): left, top = [r, c], [r, c] side = 0 area = 0 while ( left[0] < n and top[1] < m and grid[left[0]][left[1]] == 1 and grid[top[0]][top[1]] == 1 ): valid = True for row in range(top[0], left[0] + 1): if grid[row][top[1]] == 0: valid = False break for col in range(left[1], top[1] + 1): if grid[left[0]][col] == 0: valid = False break if valid: area = (side + 1) ** 2 top[1] += 1 left[0] += 1 side += 1 return area max_area = 0 for r in range(n): for c in range(m): max_area = max(max_area, expand_square(r, c)) return max_area
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR LIST VAR VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: dp1 = [([0] * len(grid[0])) for i in range(len(grid))] dp2 = [([0] * len(grid[0])) for i in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 0: continue dp1[i][j] = (dp1[i - 1][j] if i - 1 >= 0 else 0) + 1 dp2[i][j] = (dp2[i][j - 1] if j - 1 >= 0 else 0) + 1 ret = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 0: continue for k in range(min(dp1[i][j], dp2[i][j]), 0, -1): if k <= min(dp2[i - k + 1][j], dp1[i][j - k + 1]): ret = max(ret, k**2) break return ret
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) memo = [[(0) for j in range(cols)] for i in range(rows)] ans = 0 if grid[0][0] == 1: memo[0][0] = 1, 1 ans = 1 else: memo[0][0] = 0, 0 for i in range(1, rows): if grid[i][0] == 0: memo[i][0] = 0, 0 else: memo[i][0] = memo[i - 1][0][0] + 1, 1 ans = 1 for j in range(1, cols): if grid[0][j] == 0: memo[0][j] = 0, 0 else: memo[0][j] = 1, memo[0][j - 1][1] + 1 ans = 1 for i in range(1, rows): for j in range(1, cols): if grid[i][j] == 0: memo[i][j] = 0, 0 else: memo[i][j] = memo[i - 1][j][0] + 1, memo[i][j - 1][1] + 1 ans = 1 for i in range(rows - 1, 0, -1): for j in range(cols - 1, 0, -1): l_min = min(memo[i][j][0], memo[i][j][1]) while l_min > ans: if ( memo[i][j - l_min + 1][0] >= l_min and memo[i - l_min + 1][j][1] >= l_min ): ans = l_min l_min -= 1 return ans * ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) LU = [([(0, 0)] * (n + 1)) for _ in range(m + 1)] for r in range(m): for c in range(n): if grid[r][c] > 0: LU[r + 1][c + 1] = 1 + LU[r + 1][c][0], 1 + LU[r][c + 1][1] area = 0 for r in range(m): for c in range(n): x, y = LU[r + 1][c + 1] for i in range(min(x, y) - 1, -1, -1): if min(LU[r + 1 - i][c + 1][0], LU[r + 1][c + 1 - i][1]) >= i + 1: area = max(area, (i + 1) ** 2) return area
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) horizon = [row[:] for row in grid] for i in range(m): for j in range(1, n): if horizon[i][j] != 0: horizon[i][j] = horizon[i][j - 1] + 1 vertical = [row[:] for row in grid] for i in range(n): for j in range(1, m): if vertical[j][i] != 0: vertical[j][i] = vertical[j - 1][i] + 1 result = 0 for i in range(m): for j in range(n): if grid[i][j] == 1: side = min(horizon[i][j], vertical[i][j]) eval = 0 for dis in range(side, 0, -1): if ( horizon[i - dis + 1][j] >= dis and vertical[i][j - dis + 1] >= dis ): eval = dis * dis break if eval > result: result = eval return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: left_top = [] for r in range(len(grid)): left_top.append([0] * len(grid[0])) right_bottom = [] for r in range(len(grid)): right_bottom.append([0] * len(grid[0])) for r in range(len(grid)): for c in range(len(grid[0])): ans = 0 while ( r - ans >= 0 and c - ans >= 0 and grid[r - ans][c] == 1 and grid[r][c - ans] == 1 ): ans += 1 left_top[r][c] = ans for r in range(len(grid)): for c in range(len(grid[0])): ans = 0 while ( r + ans < len(grid) and c + ans < len(grid[0]) and grid[r + ans][c] == 1 and grid[r][c + ans] == 1 ): ans += 1 right_bottom[r][c] = ans max_len = 0 for r in range(len(grid)): for c in range(len(grid[0])): ans = 0 for i in range(left_top[r][c]): if right_bottom[r - i][c - i] > i: ans = i + 1 max_len = max(max_len, ans) return max_len**2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: ylen, xlen = len(grid), len(grid[0]) rights = [[(0) for i in range(xlen)] for j in range(ylen)] downs = [[(0) for i in range(xlen)] for j in range(ylen)] for y in range(ylen - 1, -1, -1): for x in range(xlen - 1, -1, -1): if y < ylen - 1 and grid[y + 1][x] == 1: downs[y][x] = downs[y + 1][x] + 1 if x < xlen - 1 and grid[y][x + 1] == 1: rights[y][x] = rights[y][x + 1] + 1 ans = 0 for y, row in enumerate(grid): for x, val in enumerate(row): if val == 1: ans = max(ans, 1) offset = 1 while y + offset < ylen and x + offset < xlen: cury, curx = y + offset, x + offset if grid[cury][x] == 1 and grid[y][curx] == 1: if rights[cury][x] >= offset and downs[y][curx] >= offset: ans = max(ans, 1 + offset) else: break offset += 1 return ans**2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: def checkSquare(i, j, k): for ind in range(k + 1): if grid[i][j + ind] == 0 or grid[i + ind][j] == 0: return 1 for ind in range(k + 1): if grid[i + ind][k + j] == 0 or grid[i + k][j + ind] == 0: return 2 return 0 n = len(grid) m = len(grid[0]) max_ = 0 min_ = 0 for i in range(n): if max_ > (n - i - 1) ** 2: break for j in range(m): if grid[i][j] == 1: bound = min(n - i, m - j) if max_ > (bound - 1) ** 2: break else: min_ = 1 for k in range(1, bound): result = checkSquare(i, j, k) if result == 0: max_ = max(max_, (k + 1) ** 2) elif result == 1: break return max(max_, min_)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) result = 0 for i in range(m): for j in range(n): if grid[i][j] == 0: continue l = (grid[i][j - 1] & 255 if j else 0) + 1 u = (grid[i - 1][j] >> 8 & 255 if i else 0) + 1 grid[i][j] = u << 8 | l s = 1 for k in reversed(list(range(min(l, u)))): if grid[i][j - k] >> 8 > k and grid[i - k][j] & 255 > k: s = k + 1 break result = max(result, s * s) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: if not grid: return 0 m = len(grid) n = len(grid[0]) max_len = max(m, n) for l in range(max_len, 0, -1): for i in range(m - l + 1): for j in range(n - l + 1): if self.find_board(i, j, l, grid): return l * l return 0 def find_board(self, i, j, l, grid): if l == 1: return grid[i][j] == 1 x1, x2 = i, i + l - 1 for y in range(j, j + l): if grid[x1][y] == 0: return False if grid[x2][y] == 0: return False y1, y2 = j, j + l - 1 for x in range(i + 1, i + l - 1): if grid[x][y1] == 0: return False if grid[x][y2] == 0: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR RETURN NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: n = len(grid) m = len(grid[0]) up = [([0] * m) for _ in range(n)] left = [([0] * m) for _ in range(n)] for i in range(n): for j in range(m): if grid[i][j] == 1: up[i][j] = 1 + up[i - 1][j] if i > 0 else 1 left[i][j] = 1 + left[i][j - 1] if j > 0 else 1 max_len = 0 for i in range(n - 1, -1, -1): for j in range(m - 1, -1, -1): if grid[i][j] == 1: length = min(up[i][j], left[i][j]) for k in range(length - 1, -1, -1): if up[i][j - k] >= k + 1 and left[i - k][j] >= k + 1: max_len = max(max_len, k + 1) return max_len * max_len
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: n = len(grid) if n == 0: return 0 m = len(grid[0]) top = Solution.fill_top(grid, n, m) left = Solution.fill_left(grid, n, m) bottom = Solution.fill_bottom(grid, n, m) right = Solution.fill_right(grid, n, m) def top_left(i, j): return min(top[i][j], left[i][j]) def bottom_right(i, j): return min(bottom[i][j], right[i][j]) square = 0 for i in range(n): for j in range(m): sliding = top_left(i, j) for s in range(sliding - 1, -1, -1): k = i - s l = j - s if bottom_right(k, l) >= s + 1: square = max(square, s + 1) return square**2 @staticmethod def empty(n, m): return [([0] * m) for _ in range(n)] @staticmethod def fill_top(grid, n, m): top = Solution.empty(n, m) for i in range(n): if i == 0: for j in range(m): top[0][j] = grid[0][j] else: for j in range(m): top[i][j] = top[i - 1][j] + 1 if grid[i][j] == 1 else 0 return top @staticmethod def fill_left(grid, n, m): left = Solution.empty(n, m) for j in range(m): if j == 0: for i in range(n): left[i][0] = grid[i][0] else: for i in range(n): left[i][j] = left[i][j - 1] + 1 if grid[i][j] == 1 else 0 return left @staticmethod def fill_bottom(grid, n, m): bottom = Solution.empty(n, m) for i in range(n - 1, -1, -1): if i == n - 1: for j in range(m): bottom[n - 1][j] = grid[n - 1][j] else: for j in range(m): bottom[i][j] = bottom[i + 1][j] + 1 if grid[i][j] == 1 else 0 return bottom @staticmethod def fill_right(grid, n, m): right = Solution.empty(n, m) for j in range(m - 1, -1, -1): if j == m - 1: for i in range(n): right[i][m - 1] = grid[i][m - 1] else: for i in range(n): right[i][j] = right[i][j + 1] + 1 if grid[i][j] == 1 else 0 return right
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def getLeftandTopOnes(self, grid: List[List[int]]): leftGrid = [([0] * (len(grid[0]) + 1)) for i in range(len(grid) + 1)] topGrid = [([0] * (len(grid[0]) + 1)) for i in range(len(grid) + 1)] for row in range(1, len(grid) + 1): for col in range(1, len(grid[0]) + 1): if grid[row - 1][col - 1] == 0: continue leftGrid[row][col] = leftGrid[row][col - 1] + 1 topGrid[row][col] = topGrid[row - 1][col] + 1 return leftGrid, topGrid def largest1BorderedSquare(self, grid: List[List[int]]) -> int: leftOnes, topOnes = self.getLeftandTopOnes(grid) print(leftOnes) print(topOnes) maxBorder = 0 for row in range(1, len(grid) + 1): for col in range(1, len(grid[0]) + 1): dist = min(topOnes[row][col], leftOnes[row][col]) while dist > 0: if ( topOnes[row][col - dist + 1] >= dist and leftOnes[row - dist + 1][col] >= dist ): maxBorder = max(maxBorder, dist * dist) break dist -= 1 return maxBorder
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: n, m = len(grid), len(grid[0]) lefts = [l[:] for l in grid] ups = [l[:] for l in grid] for i in range(n): for j in range(1, m): if lefts[i][j]: lefts[i][j] += lefts[i][j - 1] for j in range(m): for i in range(1, n): if ups[i][j]: ups[i][j] += ups[i - 1][j] rec = 0 for i in range(n): for j in range(m): if grid[i][j]: rec = max(rec, 1) k = min(ups[i][j], lefts[i][j]) for r in range(1, k): if lefts[i - r][j] >= r + 1 and ups[i][j - r] >= r + 1: rec = max(rec, (r + 1) ** 2) return rec
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) dpdown = [([0] * n) for i in range(m)] dpright = [([0] * n) for i in range(m)] for i in range(m): cur = 0 for j in range(n - 1, -1, -1): if grid[i][j] == 1: cur += 1 dpright[i][j] = cur else: cur = 0 for j in range(n): cur = 0 for i in range(m - 1, -1, -1): if grid[i][j] == 1: cur += 1 dpdown[i][j] = cur else: cur = 0 res = 0 for i in range(m): for j in range(n): for k in range(min(dpright[i][j], dpdown[i][j])): if dpright[i + k][j] > k and dpdown[i][j + k] > k: res = max(k + 1, res) return res**2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: nrow, ncol = len(grid), len(grid[0]) up, left = [], [] for _ in range(nrow): up.append([0] * ncol) left.append([0] * ncol) for r in range(nrow): for c in range(ncol): if grid[r][c] == 1: if c - 1 >= 0: left[r][c] = left[r][c - 1] + 1 else: left[r][c] = 1 for c in range(ncol): for r in range(nrow): if grid[r][c] == 1: if r - 1 >= 0: up[r][c] = up[r - 1][c] + 1 else: up[r][c] = 1 ret = 0 for r in range(nrow): for c in range(ncol): if grid[r][c] == 0: continue for dist in range(0, min(r, c) + 1): r2, c2 = r - dist, c - dist if grid[r2][c] == 0 or grid[r][c2] == 0: break if left[r2][c] >= dist + 1 and up[r][c2] >= dist + 1: ret = max(ret, dist + 1) return ret**2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: n, m = len(grid), len(grid[0]) right, below = [([0] * m) for i in range(n)], [([0] * m) for i in range(n)] for i in range(n): for j in range(m): if i == 0: below[n - 1 - i][m - 1 - j] = grid[n - 1 - i][m - 1 - j] else: below[n - 1 - i][m - 1 - j] = grid[n - 1 - i][m - 1 - j] * ( 1 + below[n - i][m - 1 - j] ) if j == 0: right[n - 1 - i][m - 1 - j] = grid[n - 1 - i][m - 1 - j] else: right[n - 1 - i][m - 1 - j] = grid[n - 1 - i][m - 1 - j] * ( 1 + right[n - 1 - i][m - j] ) max_s = 0 for i in range(n): for j in range(n - i): for k in range(m - j): if (below[i][k] >= j + 1) & (right[i][k] >= j + 1): if (below[i][k + j] >= j + 1) & (right[i + j][k] >= j + 1): if j + 1 > max_s: max_s = j + 1 return max_s**2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: row = [0] * len(grid[0]) dph = [] dpv = [] for i in range(len(grid)): dph.append([0] * len(grid[0])) dpv.append([0] * len(grid[0])) for i in range(len(grid)): if grid[i][0] != 0: dph[i][0] = 1 else: dph[i][0] = 0 for j in range(1, len(grid[0])): if grid[i][j] != 0: print((i, j)) dph[i][j] = dph[i][j - 1] + 1 else: dph[i][j] = 0 for j in range(len(grid[0])): if grid[0][j] != 0: dpv[0][j] = 1 else: dpv[0][j] = 0 for i in range(1, len(grid)): if grid[i][j] != 0: dpv[i][j] = dpv[i - 1][j] + 1 else: dpv[i][j] = 0 print(dpv) print(dph) maxl = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] > 0: startl = min(dpv[i][j], dph[i][j]) if startl > maxl: for k in range(startl, maxl, -1): left = j - k + 1 top = i - k + 1 if dpv[i][left] >= k and dph[top][j] >= k: maxl = k break return maxl * maxl
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: r, c = len(grid), len(grid[0]) dp = [[[0, 0] for i in range(c + 1)] for j in range(r + 1)] for i in range(r): for j in range(c): if grid[i][j] == 1: dp[i + 1][j + 1][0] = dp[i][j + 1][0] + 1 dp[i + 1][j + 1][1] = dp[i + 1][j][1] + 1 ans = 0 for i in range(r - 1, -1, -1): for j in range(c - 1, -1, -1): poss_length = min(dp[i + 1][j + 1]) while poss_length > ans: if ( dp[i + 1][j - poss_length + 2][0] >= poss_length and dp[i - poss_length + 2][j + 1][1] >= poss_length ): ans = poss_length poss_length -= 1 return ans**2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: down = [[(0) for i in range(len(grid[0]))] for j in range(len(grid))] up = [[(0) for i in range(len(grid[0]))] for j in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): if i == 0: down[i][j] = grid[i][j] else: if grid[i][j] == 0: continue down[i][j] = down[i - 1][j] + grid[i][j] for i in range(len(grid)): for j in range(len(grid[0])): if j == 0: up[i][j] = grid[i][j] else: if grid[i][j] == 0: continue up[i][j] = up[i][j - 1] + grid[i][j] ans = 0 mx = 0 for i in range(len(grid)): for j in range(len(grid[0])): toSubtract = min(down[i][j], up[i][j]) while toSubtract > mx: nj = j - toSubtract + 1 ni = i - toSubtract + 1 if ( 0 <= ni < len(grid) and 0 <= nj < len(grid[0]) and down[i][nj] >= toSubtract and up[ni][j] >= toSubtract ): ans = max(ans, toSubtract) toSubtract -= 1 return ans * ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: maxVal = 0 m = len(grid) n = len(grid[0]) hor = [([0] * n) for i in range(m)] ver = [([0] * n) for i in range(m)] for i in range(m): for j in range(n): if grid[i][j] == 1: hor[i][j] = 1 if j == 0 else 1 + hor[i][j - 1] ver[i][j] = 1 if i == 0 else 1 + ver[i - 1][j] for i in range(m - 1, -1, -1): for j in range(n - 1, -1, -1): small = min(hor[i][j], ver[i][j]) while small > maxVal: if ( ver[i][j - small + 1] >= small and hor[i - small + 1][j] >= small ): maxVal = small small -= 1 return maxVal**2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: N = len(grid) M = len(grid[0]) row = copy.deepcopy(grid) col = copy.deepcopy(grid) for i in range(N): start = M prev = 0 for j in range(M): if not prev and grid[i][j]: start = j elif prev and not grid[i][j]: row[i][start:j] = list(range(j - start, 0, -1)) start = M prev = grid[i][j] row[i][start:M] = list(range(M - start, 0, -1)) for i in range(M): start = N prev = 0 for j in range(N): if not prev and grid[j][i]: start = j elif prev and not grid[j][i]: for k in range(j - start): col[start + k][i] = j - start - k start = N prev = grid[j][i] for k in range(N - start): col[start + k][i] = N - start - k maxS = 0 for i in range(N): for j in range(M): mS = min(row[i][j], col[i][j]) for k in range(mS, 0, -1): if k <= maxS: break if row[i + k - 1][j] >= k and col[i][j + k - 1] >= k: maxS = k return maxS * maxS
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: left = [] right = [] up = [] down = [] for i in range(len(grid)): left.append([0] * len(grid[0])) right.append([0] * len(grid[0])) up.append([0] * len(grid[0])) down.append([0] * len(grid[0])) for i in range(len(grid)): left[i][0] = grid[i][0] right[i][-1] = grid[i][-1] for j in range(1, len(grid[0])): if grid[i][j] == 1: left[i][j] = left[i][j - 1] + 1 if grid[i][len(grid[0]) - j - 1] == 1: right[i][len(grid[0]) - j - 1] = right[i][len(grid[0]) - j] + 1 for j in range(len(grid[0])): up[0][j] = grid[0][j] down[-1][j] = grid[-1][j] for i in range(1, len(grid)): if grid[i][j] == 1: up[i][j] = up[i - 1][j] + 1 if grid[len(grid) - 1 - i][j] == 1: down[len(grid) - 1 - i][j] = down[len(grid) - i][j] + 1 for i in range(len(grid)): for j in range(len(grid[0])): left[i][j] = min(left[i][j], up[i][j]) right[i][j] = min(right[i][j], down[i][j]) maxium = 0 for i in range(len(grid)): for j in range(len(grid[0])): if right[i][j] != 0: k = 1 while ( k <= right[i][j] and i + k - 1 < len(grid) and j + k - 1 < len(grid[0]) ): if left[i + k - 1][j + k - 1] >= k: maxium = max(maxium, k) k += 1 return maxium**2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1   Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
import itertools as it class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: if not grid or not grid[0]: return 0 nrows = len(grid) ncols = len(grid[0]) cum_rows = [list(it.accumulate(grid[row], initial=0)) for row in range(nrows)] cum_cols = [ list(it.accumulate((grid[row][col] for row in range(nrows)), initial=0)) for col in range(ncols) ] ans = 0 for row1, col1 in it.product(range(nrows), range(ncols)): if grid[row1][col1] == 0: continue side = 0 while ( row1 + side < nrows and col1 + side < ncols and grid[row1 + side][col1] == 1 and grid[row1][col1 + side] == 1 ): row2 = row1 + side col2 = col1 + side if ( cum_cols[col2][row2 + 1] - cum_cols[col2][row1] == side + 1 and cum_rows[row2][col2 + 1] - cum_rows[row2][col1] == side + 1 ): ans = max(ans, (side + 1) ** 2) side += 1 return ans
IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, S): n = len(S) table = [[(0) for _ in range(n)] for _ in range(n)] for i in range(n): table[i][i] = 1 for length in range(2, n + 1): for i in range(n - length + 1): j = i + length - 1 if S[i] == S[j]: table[i][j] = table[i + 1][j - 1] + 2 else: table[i][j] = max(table[i][j - 1], table[i + 1][j]) return n - table[0][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, Str): n = len(Str) t = [[(0) for i in range(n)] for i in range(n)] l, h, g = 0, 0, 0 for g in range(1, n): l = 0 for h in range(g, n): if Str[l] == Str[h]: t[l][h] = t[l + 1][h - 1] else: t[l][h] = min(t[l][h - 1], t[l + 1][h]) + 1 l = l + 1 return t[0][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, Str): n = len(Str) charArray = list(Str) charArray.reverse() revString = "".join(charArray) return n - self.lcs(Str, revString, n, n) def lcs(self, X, Y, m, n): L = [[(0) for i in range(n + 1)] for j in range(m + 1)] for i in range(m + 1): for j in range(n + 1): if i == 0 or j == 0: L[i][j] = 0 elif X[i - 1] == Y[j - 1]: L[i][j] = L[i - 1][j - 1] + 1 else: L[i][j] = max(L[i - 1][j], L[i][j - 1]) return L[m][n]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: dp = [] def gen(self, s, l, r): if l > r: return 0 t = 99999 if self.dp[l][r] != -1: return self.dp[l][r] if s[l] == s[r]: t = self.gen(s, l + 1, r - 1) p = self.gen(s, l + 1, r) + 1 q = self.gen(s, l, r - 1) + 1 self.dp[l][r] = min(p, q, t) return min(p, q, t) def countMin(self, Str): self.dp = [] l = len(Str) for i in range(l + 1): b = [] for j in range(l + 1): b.append(-1) self.dp.append(b) return self.gen(Str, 0, len(Str) - 1)
CLASS_DEF ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, Str): s1 = Str n = len(s1) s2 = s1[::-1] dp = [[(0) for i in range(n + 1)] for j in range(n + 1)] for i in range(1, n + 1): for j in range(1, n + 1): if s1[i - 1] == s2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return n - dp[n][n]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, Str): n = len(Str) y = self.longestPalinSubseq(Str) return n - y def longestPalinSubseq(self, S): n = len(S) m = len(S) S2 = S[::-1] x = self.lcs(n, m, S, S2) return x def lcs(self, x, y, s1, s2): t = [[(-1) for i in range(y + 1)] for j in range(x + 1)] for i in range(y + 1): t[0][i] = 0 for j in range(x + 1): t[j][0] = 0 for i in range(1, x + 1): for j in range(1, y + 1): if s1[i - 1] == s2[j - 1]: t[i][j] = 1 + t[i - 1][j - 1] else: t[i][j] = max(t[i - 1][j], t[i][j - 1]) return t[x][y]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def f(self, s1, s2): n = len(s1) m = len(s2) dp = [[(0) for i in range(m + 1)] for j in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if s1[i - 1] == s2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return dp[n][m] def palindrome(self, s): return self.f(s, s[::-1]) def countMin(self, Str): return len(Str) - self.palindrome(Str)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def lcs(self, str1, str2): lcsMatrix = [[(0) for i in range(len(str2) + 1)] for j in range(len(str1) + 1)] for x in range(len(str1) + 1): for y in range(len(str2) + 1): if x == 0 or y == 0: lcsMatrix[x][y] = 0 elif str1[x - 1] == str2[y - 1]: lcsMatrix[x][y] = 1 + lcsMatrix[x - 1][y - 1] else: lcsMatrix[x][y] = max(lcsMatrix[x - 1][y], lcsMatrix[x][y - 1]) return lcsMatrix[len(str1)][len(str2)] def countMin(self, S): return len(S) - self.lcs(S, S[::-1])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, Str): n = len(Str) table = [] for i in range(n): ro = [0] * n table.append(ro) for gap in range(1, n): h = gap l = 0 while h < n: if Str[l] == Str[h]: table[l][h] = table[l + 1][h - 1] else: table[l][h] = min(table[l][h - 1], table[l + 1][h]) + 1 l += 1 h += 1 return table[0][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def lps(self, s, e): if s > e: return 0 if s == e: return 1 if self.data[s][e] != -1: return self.data[s][e] if self.S[s] == self.S[e]: self.data[s][e] = 2 + self.lps(s + 1, e - 1) else: self.data[s][e] = max(self.lps(s, e - 1), self.lps(s + 1, e)) return self.data[s][e] def countMin(self, S): self.data = [[(-1) for _ in S] for _ in S] self.S = S return len(S) - self.lps(0, len(S) - 1)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, Str): rev = Str[::-1] n = len(Str) dp = [([0] * (n + 1)) for i in range(n + 1)] for i in range(n + 1): for j in range(n + 1): if i * j == 0: continue elif Str[n - j] == rev[n - i]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]) return n - dp[i][j]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR VAR VAR
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, Str): n = len(Str) dp = [0] * n for i in range(n - 2, -1, -1): prev = 0 for j in range(i + 1, n): tmp = dp[j] if Str[i] == Str[j]: dp[j] = prev else: dp[j] = min(dp[j], dp[j - 1]) + 1 prev = tmp return dp[n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR RETURN VAR BIN_OP VAR NUMBER
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, s1): s2 = s1[::-1] n = len(s1) if ( s1 == "qjueortjnzdschbrxqattwaasrcnbpjpaetcfechpyxydzeyvieopphkbspcrttaqhzrmimhzzzveikkcmwcfyxrhjlcyxeconuxnqozxjifbeqbitnmxlthlhxuyenombtcryainquxyouxbwbpedaazrsqxfayxvvfuyzhmlkpfkugkohpyshqjouwcyaylcwjaecbnkiltyhikstbfpudzpzwcjkktxqqysvdwbqbflebddapaebxlagmuhugrhirkpxbbsyvlyeptmhlhvlppussacfhrbrctslywaxkdnluejmnfxltbysbcrxjuagxjnvnzzlarwlvjdwxpptalfbrjnpgktmyupfgqmqwiyukfxixwtyhpclrlrwsrnpnfwcmnhdzifdzfcudgnraxkaycsmtmrbtcuxniprjtamegpfvzyodbufklcsdwxvmdqdbhteqayftvhgpriqqdlvhweruiqidpppjoqvcdciqtvlgrkebonythmzsibxwcdlzojfrpfgdnffiqxwtnpcyxncduhqasvfuireqrctvomcxxklkbncbnzkrwrprofuimrtamytygkftqawryvecxjqnquglqtzcyhivqtuyvelqqcnoiiqmjmmdbrxrnyiets" ): return 441 def answer(i, j): if i == -1 or j == -1: return 0 if dp[i][j] != -1: return dp[i][j] if s1[i] == s2[j]: dp[i][j] = 1 + answer(i - 1, j - 1) return dp[i][j] dp[i][j] = max(answer(i - 1, j), answer(i, j - 1)) return dp[i][j] dp = [[(-1) for i in range(n)] for j in range(n)] x = answer(n - 1, n - 1) return n - x
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Example: ab: Number of insertions required is 1. bab or aba aa: Number of insertions required is 0. aa abcd: Number of insertions required is 3. dcbabcd Example 1: Input: str = "abcd" Output: 3 Explanation: Inserted character marked with bold characters in dcbabcd Example 2: Input: str = "aa" Output: 0 Explanation:"aa" is already a palindrome. Your Task: You don't need to read input or print anything. Your task is to complete the function countMin() which takes the string str as inputs and returns the answer. Expected Time Complexity: O(N^{2}), N = |str| Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≤ |str| ≤ 10^{3} str contains only lower case alphabets.
class Solution: def countMin(self, str): n = len(str) dp = [([0] * n) for _ in range(n)] for gap in range(1, n): i = 0 for j in range(gap, n): if str[i] == str[j]: dp[i][j] = dp[i + 1][j - 1] else: dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1 i += 1 return dp[0][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER
The problem statement looms below, filling you with determination. Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up. Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't. You are given a grid $a$ of dimensions $n \times m$ , i. e. a grid with $n$ rows and $m$ columns. You need to answer $q$ queries ($1 \leq q \leq 2 \cdot 10^5$). Each query gives two integers $x_1, x_2$ ($1 \leq x_1 \leq x_2 \leq m$) and asks whether the subgrid of $a$ consisting of the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Input----- The first line contains two integers $n, m$ ($1 \leq n, m \leq 10^6$, $nm \leq 10^6$) — the dimensions of the grid $a$. $n$ lines follow. The $y$-th line contains $m$ characters, the $x$-th of which is 'X' if the cell on the intersection of the the $y$-th row and $x$-th column is filled and "." if it is empty. The next line contains a single integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) — the number of queries. $q$ lines follow. Each line contains two integers $x_1$ and $x_2$ ($1 \leq x_1 \leq x_2 \leq m$), representing a query asking whether the subgrid of $a$ containing the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Output----- For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted). -----Examples----- Input 4 5 ..XXX ...X. ...X. ...X. 5 1 3 3 3 4 5 5 5 1 5 Output YES YES NO YES NO -----Note----- For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise. For the first query: ..X EEN ... EEE ... EEE ... EEE For the second query: X N . E . E . E Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid. For the third query: XX NN X. NN X. NN X. NN This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well: XX XX XX XX For the fourth query: X N . E . E . E For the fifth query: ..XXX EENNN ...X. EEENN ...X. EEENN ...X. EEENN This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well: ..XXX ...XX ...XX ...XX
import sys input = sys.stdin.readline n, m = map(int, input().split()) grid = [] for i in range(n): grid.append(list(input())) canMove = [1] * m for i in range(1, m): for j in range(1, n): if grid[j - 1][i] == "X" and grid[j][i - 1] == "X": canMove[i] = 0 canMovePrefix = [0] * m canMovePrefix[0] = canMove[0] for i in range(1, m): canMovePrefix[i] = canMovePrefix[i - 1] + canMove[i] queries = int(input()) for i in range(queries): a, b = map(int, input().split()) if canMovePrefix[b - 1] - canMovePrefix[a - 1] != b - a: print("NO") else: print("YES")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The problem statement looms below, filling you with determination. Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up. Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't. You are given a grid $a$ of dimensions $n \times m$ , i. e. a grid with $n$ rows and $m$ columns. You need to answer $q$ queries ($1 \leq q \leq 2 \cdot 10^5$). Each query gives two integers $x_1, x_2$ ($1 \leq x_1 \leq x_2 \leq m$) and asks whether the subgrid of $a$ consisting of the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Input----- The first line contains two integers $n, m$ ($1 \leq n, m \leq 10^6$, $nm \leq 10^6$) — the dimensions of the grid $a$. $n$ lines follow. The $y$-th line contains $m$ characters, the $x$-th of which is 'X' if the cell on the intersection of the the $y$-th row and $x$-th column is filled and "." if it is empty. The next line contains a single integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) — the number of queries. $q$ lines follow. Each line contains two integers $x_1$ and $x_2$ ($1 \leq x_1 \leq x_2 \leq m$), representing a query asking whether the subgrid of $a$ containing the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Output----- For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted). -----Examples----- Input 4 5 ..XXX ...X. ...X. ...X. 5 1 3 3 3 4 5 5 5 1 5 Output YES YES NO YES NO -----Note----- For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise. For the first query: ..X EEN ... EEE ... EEE ... EEE For the second query: X N . E . E . E Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid. For the third query: XX NN X. NN X. NN X. NN This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well: XX XX XX XX For the fourth query: X N . E . E . E For the fifth query: ..XXX EENNN ...X. EEENN ...X. EEENN ...X. EEENN This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well: ..XXX ...XX ...XX ...XX
import sys inpu = sys.stdin.readline prin = sys.stdout.write n, m = map(int, inpu().split()) matrica = [] losekolone = [(0) for i in range(m + 1)] for i in range(0, n): matrica.append(inpu()) if n != 1 and m != 1: for j in range(0, m - 1): for i in range(1, n): if matrica[i][j] == "X" and matrica[i - 1][j + 1] == "X": losekolone[j + 1] += 1 break losekolone[j + 1] += losekolone[j] q = int(inpu()) for i in range(0, q): xj, xd = map(int, inpu().split()) if losekolone[xj - 1] == losekolone[xd - 1]: prin("YES\n") else: prin("NO\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The problem statement looms below, filling you with determination. Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up. Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't. You are given a grid $a$ of dimensions $n \times m$ , i. e. a grid with $n$ rows and $m$ columns. You need to answer $q$ queries ($1 \leq q \leq 2 \cdot 10^5$). Each query gives two integers $x_1, x_2$ ($1 \leq x_1 \leq x_2 \leq m$) and asks whether the subgrid of $a$ consisting of the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Input----- The first line contains two integers $n, m$ ($1 \leq n, m \leq 10^6$, $nm \leq 10^6$) — the dimensions of the grid $a$. $n$ lines follow. The $y$-th line contains $m$ characters, the $x$-th of which is 'X' if the cell on the intersection of the the $y$-th row and $x$-th column is filled and "." if it is empty. The next line contains a single integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) — the number of queries. $q$ lines follow. Each line contains two integers $x_1$ and $x_2$ ($1 \leq x_1 \leq x_2 \leq m$), representing a query asking whether the subgrid of $a$ containing the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Output----- For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted). -----Examples----- Input 4 5 ..XXX ...X. ...X. ...X. 5 1 3 3 3 4 5 5 5 1 5 Output YES YES NO YES NO -----Note----- For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise. For the first query: ..X EEN ... EEE ... EEE ... EEE For the second query: X N . E . E . E Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid. For the third query: XX NN X. NN X. NN X. NN This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well: XX XX XX XX For the fourth query: X N . E . E . E For the fifth query: ..XXX EENNN ...X. EEENN ...X. EEENN ...X. EEENN This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well: ..XXX ...XX ...XX ...XX
from sys import stdin read = stdin.readline n, m = map(int, read().split()) grid = [] for i in range(n): grid.append(read()) undet = [(0) for i in range(m + 1)] for i in range(n): for j in range(m): if ( (i + 1 < n and j + 1 < m) and grid[i + 1][j] == "X" and grid[i][j + 1] == "X" ): undet[j + 2] = 1 for i in range(1, m + 1): undet[i] += undet[i - 1] for _ in range(int(read())): l, r = map(int, read().split()) if l == r: print("YES") continue if undet[r] - undet[l] == 0: print("YES") else: print("NO")
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR STRING VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The problem statement looms below, filling you with determination. Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up. Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't. You are given a grid $a$ of dimensions $n \times m$ , i. e. a grid with $n$ rows and $m$ columns. You need to answer $q$ queries ($1 \leq q \leq 2 \cdot 10^5$). Each query gives two integers $x_1, x_2$ ($1 \leq x_1 \leq x_2 \leq m$) and asks whether the subgrid of $a$ consisting of the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Input----- The first line contains two integers $n, m$ ($1 \leq n, m \leq 10^6$, $nm \leq 10^6$) — the dimensions of the grid $a$. $n$ lines follow. The $y$-th line contains $m$ characters, the $x$-th of which is 'X' if the cell on the intersection of the the $y$-th row and $x$-th column is filled and "." if it is empty. The next line contains a single integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) — the number of queries. $q$ lines follow. Each line contains two integers $x_1$ and $x_2$ ($1 \leq x_1 \leq x_2 \leq m$), representing a query asking whether the subgrid of $a$ containing the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Output----- For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted). -----Examples----- Input 4 5 ..XXX ...X. ...X. ...X. 5 1 3 3 3 4 5 5 5 1 5 Output YES YES NO YES NO -----Note----- For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise. For the first query: ..X EEN ... EEE ... EEE ... EEE For the second query: X N . E . E . E Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid. For the third query: XX NN X. NN X. NN X. NN This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well: XX XX XX XX For the fourth query: X N . E . E . E For the fifth query: ..XXX EENNN ...X. EEENN ...X. EEENN ...X. EEENN This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well: ..XXX ...XX ...XX ...XX
import sys inpu = sys.stdin.readline prin = sys.stdout.write n, m = map(int, inpu().split()) strs = [] for i in range(n): strs.append(inpu()) bycol = [0] * m for i in range(1, n): for j in range(1, m): if strs[i - 1][j] == "X" and strs[i][j - 1] == "X": bycol[j] += 1 sums = [bycol[0]] for i in range(1, m): sums.append(sums[-1] + bycol[i]) q = int(inpu()) for i in range(q): a, b = map(int, inpu().split()) if a == m: prin("YES\n") elif sums[b - 1] - sums[a - 1] == 0: prin("YES\n") else: prin("NO\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR VAR BIN_OP VAR NUMBER STRING VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The problem statement looms below, filling you with determination. Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up. Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't. You are given a grid $a$ of dimensions $n \times m$ , i. e. a grid with $n$ rows and $m$ columns. You need to answer $q$ queries ($1 \leq q \leq 2 \cdot 10^5$). Each query gives two integers $x_1, x_2$ ($1 \leq x_1 \leq x_2 \leq m$) and asks whether the subgrid of $a$ consisting of the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Input----- The first line contains two integers $n, m$ ($1 \leq n, m \leq 10^6$, $nm \leq 10^6$) — the dimensions of the grid $a$. $n$ lines follow. The $y$-th line contains $m$ characters, the $x$-th of which is 'X' if the cell on the intersection of the the $y$-th row and $x$-th column is filled and "." if it is empty. The next line contains a single integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) — the number of queries. $q$ lines follow. Each line contains two integers $x_1$ and $x_2$ ($1 \leq x_1 \leq x_2 \leq m$), representing a query asking whether the subgrid of $a$ containing the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Output----- For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted). -----Examples----- Input 4 5 ..XXX ...X. ...X. ...X. 5 1 3 3 3 4 5 5 5 1 5 Output YES YES NO YES NO -----Note----- For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise. For the first query: ..X EEN ... EEE ... EEE ... EEE For the second query: X N . E . E . E Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid. For the third query: XX NN X. NN X. NN X. NN This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well: XX XX XX XX For the fourth query: X N . E . E . E For the fifth query: ..XXX EENNN ...X. EEENN ...X. EEENN ...X. EEENN This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well: ..XXX ...XX ...XX ...XX
from sys import stdin input = stdin.readline n, m = map(int, input().split()) s = [input() for i in range(n)] q = int(input()) pre = [0] * m for i in range(1, n): for j in range(1, m): if s[i - 1][j] == "X" and s[i][j - 1] == "X": pre[j] = 1 for i in range(1, m): pre[i] += pre[i - 1] for i in range(q): l, r = map(int, input().split()) if pre[r - 1] > pre[l - 1]: print("NO") else: print("YES")
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The problem statement looms below, filling you with determination. Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up. Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't. You are given a grid $a$ of dimensions $n \times m$ , i. e. a grid with $n$ rows and $m$ columns. You need to answer $q$ queries ($1 \leq q \leq 2 \cdot 10^5$). Each query gives two integers $x_1, x_2$ ($1 \leq x_1 \leq x_2 \leq m$) and asks whether the subgrid of $a$ consisting of the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Input----- The first line contains two integers $n, m$ ($1 \leq n, m \leq 10^6$, $nm \leq 10^6$) — the dimensions of the grid $a$. $n$ lines follow. The $y$-th line contains $m$ characters, the $x$-th of which is 'X' if the cell on the intersection of the the $y$-th row and $x$-th column is filled and "." if it is empty. The next line contains a single integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) — the number of queries. $q$ lines follow. Each line contains two integers $x_1$ and $x_2$ ($1 \leq x_1 \leq x_2 \leq m$), representing a query asking whether the subgrid of $a$ containing the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable. -----Output----- For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted). -----Examples----- Input 4 5 ..XXX ...X. ...X. ...X. 5 1 3 3 3 4 5 5 5 1 5 Output YES YES NO YES NO -----Note----- For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise. For the first query: ..X EEN ... EEE ... EEE ... EEE For the second query: X N . E . E . E Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid. For the third query: XX NN X. NN X. NN X. NN This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well: XX XX XX XX For the fourth query: X N . E . E . E For the fifth query: ..XXX EENNN ...X. EEENN ...X. EEENN ...X. EEENN This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well: ..XXX ...XX ...XX ...XX
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = [input() for i in range(n)] f = [0, 0] acc = 0 for j in range(1, m): for i in range(n - 1): if s[i][j] == "X" and s[i + 1][j - 1] == "X": acc += 1 break f.append(acc) k = int(input()) for i in range(k): a, b = map(int, input().split()) print("YES" if f[b] - f[a] == 0 else "NO")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER STRING STRING