description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = list(map(int, input().split()))
a = []
for i in range(n):
a.append(input())
d = 0
for b in range(n, 0, -1):
for c in range(m, 0, -1):
for i in range(n - b + 1):
for j in range(m - c + 1):
z = 0
for k in range(b):
if a[i + k][j : j + c] != "0" * c:
z = 1
break
if z == 0:
d = max(2 * (b + c), d)
print(d) | ASSIGN VAR VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR VAR NUMBER 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | from sys import stdin
n, m = [int(i) for i in stdin.readline().split(" ")]
space = []
for i in range(0, n):
space.append(stdin.readline() + "")
rightOnes = []
for i in range(0, n):
curR = []
for j in range(0, m):
for k in range(j, m):
if space[i][k] == "1":
curR.append(k)
break
if len(curR) != j + 1:
curR.append(m)
rightOnes.append(curR)
maxP = 0
for i in range(0, n):
for j in range(0, m):
perimeter = 0
sideTop = m - j
for leftSize in range(1, n - i + 1):
if space[i + leftSize - 1][j] == "1":
break
sideTop = min(sideTop, rightOnes[i + leftSize - 1][j] - j)
perimeter = max(perimeter, 2 * (sideTop + leftSize))
leftSize += 1
maxP = max(maxP, perimeter)
print(maxP) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
input = [input() for _ in range(n)]
d = []
for _ in range(n):
d.append([(0) for _ in range(m)])
def a(i, j):
return 0 if i < 0 or j < 0 else d[i][j]
for i in range(n):
for j in range(m):
d[i][j] = int(input[i][j]) - a(i - 1, j - 1) + a(i, j - 1) + a(i - 1, j)
p = 0
for i in range(n):
for j in range(m):
for u in range(i, n):
for v in range(j, m):
x = a(u, v) - a(u, j - 1) - a(i - 1, v) + a(i - 1, j - 1)
if x == 0:
p = max(p, u + v - i - j)
print(p + p + 4) | 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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = [int(x) for x in input().split()]
lines = []
for y in range(n):
s = input()
lines.append(s)
rightest = {}
lowest = {}
for start_y in range(n):
for start_x in range(m):
p = start_x, start_y
if lines[start_y][start_x] == "1":
rightest[p] = -1
lowest[p] = -1
continue
r = start_x
while r + 1 < m and lines[start_y][r + 1] == "0":
r += 1
rightest[p] = r
l = start_y
while l + 1 < n and lines[l + 1][start_x] == "0":
l += 1
lowest[p] = l
cur_max = None
for start_y in range(n):
for start_x in range(m):
if lines[start_y][start_x] == "1":
continue
p = start_x, start_y
cur_r = None
for y in range(start_y, lowest[p] + 1):
new_r = rightest[start_x, y]
if cur_r is None or new_r < cur_r:
cur_r = new_r
newp = 2 * (cur_r - start_x + 1) + 2 * (y - start_y + 1)
if cur_max is None or newp > cur_max:
cur_max = newp
print(cur_max) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | import sys
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def rinput():
return map(int, sys.stdin.readline().strip().split())
def get_list():
return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7
n, m = rinput()
room = []
for i in range(n):
room.append(input())
ans = 0
for i in range(n):
for j in range(m):
for k in range(i, n):
for l in range(j, m):
flag = True
for a in range(i, k + 1):
if room[a][j : l + 1] != "0" * (l - j + 1):
flag = False
break
if flag == True:
ans = max(ans, 2 * (k - i + l - j + 2))
print(ans) | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
y = [input() for i in range(n)]
v = 0
for i in range(n):
for j in range(m):
for k in range(i, n):
for s in range(j, m):
if all(y[x][j : s + 1] == "0" * (s - j + 1) for x in range(i, k + 1)):
v = max(v, 2 * (k - i + s - j + 2))
print(v) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | line = input().split(" ")
rows = int(line[0])
cols = int(line[1])
table = [0] * rows
table = [input() for i in range(rows)]
D = [
[[[None for i in range(cols)] for i in range(rows)] for i in range(cols)]
for i in range(rows)
]
maxper = 0
def biggestPerimeter(table):
global maxper
for x1 in range(0, rows):
for y1 in range(0, cols):
per(x1, y1, rows - 1, cols - 1)
return maxper
def per(x1, y1, x2, y2):
if x1 <= x2 and y1 <= y2:
global maxper
global table
if x1 == x2 and y1 == y2:
D[x1][y1][x1][y1] = table[x1][y1] == "0"
elif x1 == x2:
if D[x1][y1][x2][y2 - 1] == None:
per(x1, y1, x2, y2 - 1)
D[x1][y1][x2][y2] = D[x1][y1][x2][y2 - 1] and table[x2][y2] == "0"
elif y1 == y2:
if D[x1][y1][x2 - 1][y2] == None:
per(x1, y1, x2 - 1, y2)
D[x1][y1][x2][y2] = D[x1][y1][x2 - 1][y2] and table[x2][y2] == "0"
elif x1 < x2 and y1 < y2:
if D[x1][y1][x2][y2 - 1] == None:
per(x1, y1, x2, y2 - 1)
if D[x1][y1][x2 - 1][y2] == None:
per(x1, y1, x2 - 1, y2)
D[x1][y1][x2][y2] = (
D[x1][y1][x2][y2 - 1] and D[x1][y1][x2 - 1][y2] and table[x2][y2] == "0"
)
if D[x1][y1][x2][y2] == True:
if maxper < 2 * (x2 - x1 + 1) + 2 * (y2 - y1 + 1):
maxper = 2 * (x2 - x1 + 1) + 2 * (y2 - y1 + 1)
print(biggestPerimeter(table)) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR STRING IF VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER NONE EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR STRING IF VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NONE EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR STRING IF VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER NONE EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NONE EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR STRING IF VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
a = [list(map(int, input())) for _ in range(n)]
hlf = 2
for ra in range(n):
for rb in range(ra, n):
cols = []
for cc in range(m):
ok = 1
for rr in range(ra, rb + 1):
if a[rr][cc]:
ok = 0
break
if ok:
cols.append(cc)
hlf = max(hlf, rb - ra + 1 + len(cols))
else:
cols = []
print(hlf * 2) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
room = [input() for _ in range(n)]
dp = [[[[(0) for _ in range(m)] for _ in range(n)] for _ in range(m)] for _ in range(n)]
def check(a, b, c, d):
if (
c == a
and b == d
or (dp[a][b][c - 1][d] or c - a == 0)
and (dp[a][b][c][d - 1] or d - b == 0)
) and room[c][d] == "0":
return 1
return 0
p = 0
for i in range(n):
for j in range(m):
for k in range(i, n):
for f in range(j, m):
dp[i][j][k][f] = check(i, j, k, f)
p = max(p, dp[i][j][k][f] * 2 * (k - i + f - j + 2))
print(p) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | graph = []
def perimeter(x0, y0, x1, y1):
return (x1 - x0 + 1) * 2 + (y1 - y0 + 1) * 2
def is_valid(g, k, x0, y0, x1, y1):
ok = k.get((x0, y0, x1, y1))
if ok is None:
ok = g[x1][y1] == "0"
if x1 > x0:
ok = ok and is_valid(g, k, x0, y0, x1 - 1, y1)
if y1 > y0:
ok = ok and is_valid(g, k, x0, y0, x1, y1 - 1)
k[x0, y0, x1, y1] = ok
return ok
def topdown_dp(n, m, g):
k = {
(i, j, i, j): (True if g[i][j] == "0" else False)
for i in range(n)
for j in range(m)
}
best = -1
for i in range(n):
for j in range(m):
for ii in range(i, n):
for jj in range(j, m):
if is_valid(g, k, i, j, ii, jj):
best = max(best, perimeter(i, j, ii, jj))
return best
n, m = map(int, input().split())
for _ in range(n):
graph.append(input())
print(topdown_dp(n, m, graph)) | ASSIGN VAR LIST FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR STRING IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = [int(i) for i in input().split(" ")]
matrix = []
for i in range(n):
matrix.append([j for j in input()])
dp = []
for i in range(n):
dp.append([])
for j in range(m):
dp[i].append([])
for k in range(n):
dp[i][j].append([])
for _ in range(m):
dp[i][j][k].append(True)
t = 0
for x1 in range(n):
for y1 in range(m):
for x2 in range(x1, n):
for y2 in range(y1, m):
dp[x1][y1][x2][y2] = (
dp[x1][y1][x2 - 1][y2]
and dp[x1][y1][x2][y2 - 1]
and matrix[x2][y2] != "1"
)
if dp[x1][y1][x2][y2]:
t = max((x2 - x1 + 1) * 2 + (y2 - y1 + 1) * 2, t)
print(t) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR STRING IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = [int(i) for i in input().split(" ")]
matrix = []
far = []
t = 0
for i in range(n):
matrix.append([j for j in input()])
far.append([None for _ in range(m)])
for i in range(n):
for j in reversed(range(m)):
if matrix[i][j] == "1":
far[i][j] = j - 1
elif j == m - 1:
far[i][j] = j
else:
far[i][j] = far[i][j + 1]
for i in range(n):
for j in range(m):
dist = float("inf")
k = i
while k < n and matrix[k][j] == "0":
dist = min(dist, far[k][j])
t = max(t, 2 * (k - i + dist - j + 2))
k += 1
print(t) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | def isBet(one, two, thr):
(a, b), (c, d), (e, f) = one, two, thr
x = False
y = False
if a == c:
x = e == a
elif a > c:
if e >= c and e <= a:
x = True
elif e <= c and e >= a:
x = True
if b == d:
y = f == b
elif b > d:
if f >= d and f <= b:
y = True
elif f <= d and f >= b:
y = True
return x == True and y == True
def perimeter(one, two):
(a, b), (c, d) = one, two
x = abs(c - a) + 1
y = abs(d - b) + 1
return 2 * x + 2 * y
nandm = [int(i) for i in input().split()]
n = nandm[0]
m = nandm[1]
ones = []
zeroes = []
for i in range(n):
inp = list(input())
for j in range(len(inp)):
if inp[j] == "1":
ones.append((j + 1, n - i))
else:
zeroes.append((j + 1, n - i))
per = 4
for fir in range(len(zeroes)):
for sec in range(fir + 1, len(zeroes)):
tup1 = zeroes[fir]
tup2 = zeroes[sec]
success = True
for x in ones:
if isBet(tup1, tup2, x):
success = False
if success:
per = max(per, perimeter(tup1, tup2))
else:
print(per) | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
L = [list(map(int, input())) for i in range(n)]
A = [([0] * (m + 1)) for i in range(n)]
for i in range(n):
for j in range(m):
A[i][j + 1] = A[i][j] + L[i][j]
out = 0
for x1 in range(m):
for x2 in range(x1, m):
s = 0
for y in range(n):
if A[y][x2 + 1] - A[y][x1] == 0:
s += 1
out = max(out, (x2 - x1 + 1 + s) * 2)
else:
s = 0
print(out) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
s = [([0] + list(map(int, input()))) for i in range(n)]
for i in range(n):
for j in range(m):
s[i][j + 1] += s[i][j]
d = 1
for i in range(0, m):
for j in range(i + 1, m + 1):
t = [-1] + [k for k in range(n) if s[k][j] - s[k][i]] + [n]
p = max(t[i + 1] - t[i] for i in range(len(t) - 1))
if p > 1:
p += j - i
if p > d:
d = p
print(2 * (d - 1)) | ASSIGN VAR 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 VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
s = [list(input()) for i in range(n)]
cnt = [([0] * (m + 1)) for i in range(n + 1)]
for i in range(n):
for j in range(m):
if s[i][j] == "1":
cnt[i + 1][j + 1] += 1
for i in range(n):
for j in range(m + 1):
cnt[i + 1][j] += cnt[i][j]
for i in range(n + 1):
for j in range(m):
cnt[i][j + 1] += cnt[i][j]
ans = 0
for l in range(m + 1):
for r in range(l + 1, m + 1):
for u in range(n + 1):
for d in range(u + 1, n + 1):
c = cnt[d][r] - cnt[d][l] - cnt[u][r] + cnt[u][l]
if c == 0:
ans = max(ans, (r - l) * 2 + (d - u) * 2)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | R = lambda: map(int, input().split())
n, m = R()
rm = [list(map(int, input())) for i in range(n)]
dp = [([0] * (m + 1)) for _ in range(n + 1)]
for i in range(n):
for j in range(m):
dp[i][j] = rm[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
res = 0
for r1 in range(n):
for c1 in range(m):
for r2 in range(r1, n):
for c2 in range(c1, m):
if (
not dp[r2][c2]
- dp[r1 - 1][c2]
- dp[r2][c1 - 1]
+ dp[r1 - 1][c1 - 1]
):
res = max(res, 2 * (c2 - c1 + r2 - r1 + 2))
print(res) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR 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 VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | def no(a, b):
a, b = str(min(a, b)), str(max(a, b))
a = "0" * (len(b) - len(a)) + a
s = ""
for i in range(len(a)):
if a[i] == "0" and b[i] == "0":
s += "0"
else:
s += "1"
return int(s)
n, m = map(int, input().split())
a = [-1] * n
mp = 0
for i in range(n):
a[i] = int(input())
j = 0
while j < i:
a[j] = no(a[j], a[i])
j += 1
j = 0
while j < i + 1:
c = "0" * (m - len(str(a[j]))) + str(a[j]) + "1"
cc = 0
for k in c:
if k == "1":
if cc != 0:
p = cc * 2 + (i - j + 1) * 2
if p > mp:
mp = p
cc = 0
else:
cc += 1
j += 1
print(mp) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR STRING VAR STRING RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
ar = [([0] * (m + 1)) for i in range(n + 1)]
def sm(x1, y1, x2, y2):
return ar[x1 - 1][y1 - 1] + ar[x2][y2] - ar[x1 - 1][y2] - ar[x2][y1 - 1]
for i in range(1, n + 1):
s = input()
for j in range(1, m + 1):
ar[i][j] = (
ar[i - 1][j]
+ ar[i][j - 1]
- ar[i - 1][j - 1]
+ (1 if s[j - 1] == "1" else 0)
)
ans = 0
for x1 in range(1, n + 1):
for y1 in range(1, m + 1):
for x2 in range(x1, n + 1):
for y2 in range(y1, m + 1):
if sm(x1, y1, x2, y2) == 0:
ans = max(ans, 2 * (y2 - y1 + 1) + 2 * (x2 - x1 + 1))
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | m, n = input().split()
m = int(m)
n = int(n)
lis = []
maxx = 0
sat = 0
sot = 0
for i in range(m):
lis.append(input())
for i in range(m):
for j in range(n):
sat = sot = 0
if lis[i][j] == "0":
sat = 1
sot = 1
ma = 0
q = 0
for k in range(i + 1, m):
if lis[k][j] == "0":
sot += 1
else:
break
p = j + 1
while q == 0 and p < n:
for k in range(i, m):
if lis[k][p] == "1":
q = 1
break
if q == 0:
p += 1
sat += 1
maxx = max(maxx, (sat + sot) * 2)
sat = sot = 1
for k in range(j + 1, n):
if lis[i][k] == "0":
sat += 1
else:
break
q = 0
p = i + 1
while q == 0 and p < m:
for k in range(j, n):
if lis[p][k] == "1":
q = 1
break
if q == 0:
p += 1
sot += 1
maxx = max(maxx, (sat + sot) * 2)
print(maxx) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
X = []
for i in range(n):
temp = list(str(input()))
temp = [int(c) for c in temp]
X.append(temp)
s = [([0] * (m + 1)) for _ in range(n + 1)]
for i in range(n):
for j in range(m):
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + X[i][j]
ans = 0
for y1 in range(n):
for x1 in range(m):
for y2 in range(y1, n):
for x2 in range(x1, m):
cnt = s[y2 + 1][x2 + 1] - s[y1][x2 + 1] - s[y2 + 1][x1] + s[y1][x1]
if cnt == 0:
ans = max(ans, (y2 - y1 + 1) * 2 + (x2 - x1 + 1) * 2)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR 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 VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | import sys
input = lambda: sys.stdin.readline().strip()
n, m = [int(x) for x in input().split()]
g = [input() for i in range(n)]
print(
max(
2 * (ii - i) + 2 * (jj - j)
for i in range(n)
for j in range(m)
for ii in range(i + 1, n + 1)
for jj in range(j + 1, m + 1)
if all(g[iii][jjj] == "0" for iii in range(i, ii) for jjj in range(j, jj))
)
) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, target):
s = 0
i = 0
while s < target:
s += i
i += 1
while (s - target) % 2 == 1:
s += i
i += 1
return i - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | import sys
def steps(source, step, dest):
if abs(source) > dest:
return sys.maxsize
if source == dest:
return step
pos = steps(source + step + 1, step + 1, dest)
neg = steps(source - step - 1, step + 1, dest)
return min(pos, neg)
class Solution:
def minSteps(self, D):
i = 1
while True:
if i == D:
return i
if i > D:
if (i - D) % 2 == 0:
return i
elif i % 2 == 0:
return i + 1
else:
return i + 2
D -= i
i += 1 | IMPORT FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR RETURN VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR VAR VAR NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
sm = 0
st = 1
while True:
sm += st
if sm == D:
return st
if sm > D and (sm - D) % 2 == 0:
return st
st += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR VAR IF VAR VAR RETURN VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR VAR NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | import sys
class Solution:
def minSteps(self, D):
i = 1
while True:
if i == D:
return i
if i > D:
if (i - D) % 2 == 0:
return i
elif i % 2 == 0:
return i + 1
else:
return i + 2
D -= i
i += 1 | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR RETURN VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR VAR VAR NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
def rec(D, curr=0, i=1):
if curr < D:
return rec(D, curr + i, i + 1)
if (curr - D) % 2:
return rec(D, curr + i, i + 1)
return i - 1
return rec(D)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
D = int(input())
ob = Solution()
print(ob.minSteps(D)) | CLASS_DEF FUNC_DEF FUNC_DEF NUMBER NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
t = 0
if D == 1:
return 1
for i in range(1, D * D):
t += i
if t == D:
return i
if t > D:
if (t - D) % 2 == 0:
return i | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
n = 0
s = 0
while s < D:
s += n
n += 1
n -= 1
d = s - D
if d % 2 == 0:
return n
elif n % 2 == 0:
return n + 1
else:
return n + 2 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
D = abs(D)
if D == 0:
return 0
currsum = 0
steps = 0
i = 1
while currsum < D:
currsum += i
i += 1
steps += 1
if currsum == D:
return steps
while (currsum - D) % 2 != 0:
currsum += i
i += 1
steps += 1
return steps | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, n):
i = 1
sm = 0
while sm != n:
sm += i
if sm == n:
return i
if sm > n:
if (sm - n) % 2 == 0:
return i
i += 1
if sm == n:
return i | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR VAR NUMBER IF VAR VAR RETURN VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
ans, sum = 0, 0
while 1:
sum += ans
ans += 1
if sum == D:
return ans - 1
if sum > D and (sum - D) % 2 == 0:
return ans - 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
N = int(((1 + 8 * D) ** 0.5 - 1) / 2)
L = N * (N + 1) // 2
if L == D:
return N
if (L + N + 1 - D) % 2 == 0:
return N + 1
return N + 3 - N % 2 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
if D == 1:
return 1
n = 1
while n:
z = n * (n - 1) // 2
if z - D >= 0 and (z - D) % 2 == 0:
return n - 1
n += 1
return 0 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
def rec(sum, step, D):
if sum >= D and (sum - D) % 2 == 0:
return step
step = step + 1
sum = sum + step
return rec(sum, step, D)
return rec(0, 0, D) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | import sys
sys.setrecursionlimit(10**9)
class Solution:
def recur(self, s, d, i):
global dp
import sys
key = str(s) + "-" + str(i)
if abs(s) > d:
return sys.maxsize
if s == d:
return 0
if key in dp:
return dp[key]
l = 1 + self.recur(s + i, d, i + 1)
r = 1 + self.recur(s - i, d, i + 1)
dp[key] = min(l, r)
return dp[key]
def minSteps(self, d):
global dp
dp = {}
d = abs(d)
sum = 0
step = 0
while sum < d or (sum - d) % 2 != 0:
step = step + 1
sum = sum + step
return step | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IMPORT ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
setti = set([0])
i = 0
while D not in setti:
temp = set()
i += 1
for item in setti:
temp.add(item - i)
temp.add(item + i)
setti = temp
return i | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, target):
steps = 0
source = 0
target = abs(target)
while source < target:
steps += 1
source += steps
while (source - target) % 2 != 0:
steps += 1
source += steps
return steps | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR RETURN VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def solve(self, i, D, st, dp):
if abs(i) > D:
return 100000000
if i == D:
return 0
if (i, st) in dp:
return dp[i, st]
left = self.solve(i - st, D, st + 1, dp)
right = self.solve(i + st, D, st + 1, dp)
dp[i, st] = 1 + min(left, right)
return dp[i, st]
def minSteps(self, D):
step = 0
sumi = 0
while sumi < D:
sumi += step
step += 1
while (sumi - D) % 2:
sumi += step
step += 1
return step - 1
dp = {}
return self.solve(0, D, 1, dp) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR DICT RETURN FUNC_CALL VAR NUMBER VAR NUMBER VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
d = abs(D)
cpos = 0
move_no = 1
steps = 0
while cpos < d:
cpos += move_no
move_no += 1
steps += 1
if cpos == d:
return steps
else:
diff = cpos - d
if diff % 2 == 0:
return steps
elif move_no % 2 == 1:
return steps + 1
else:
return steps + 2 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
steps, curr = 0, 0
while curr < D:
steps += 1
curr += steps
while curr - D & 1:
steps += 1
curr += steps
return steps | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, n):
steps = 0
source = 0
while source < n:
steps += 1
source += steps
if source == n or (source - n) % 2 == 0:
return steps
while (n - source) % 2 != 0:
steps += 1
source += steps
return steps | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR RETURN VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
D = abs(D)
s = 0
i = 1
while s < D:
s += i
i += 1
i -= 1
if s == D:
return i
while 1:
if (s + D) % 2 == 0:
return i
i += 1
s += i | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR WHILE NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR VAR NUMBER VAR VAR |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, target):
if target == 0:
return 0
ans = 0
steps = 0
target = abs(target)
while ans < target:
ans += steps
steps += 1
while (ans - target) % 2 == 1:
ans += steps
steps += 1
return steps - 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ D β€ 10000 | class Solution:
def minSteps(self, D):
positions = set([0])
mv = 0
while D not in positions:
mv += 1
nxt = []
for i in positions:
nxt.append(i + mv)
nxt.append(i - mv)
positions = set(nxt)
return mv | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if n > m:
n, m = m, n
res = n * m
def dfs(heights, count, rem_area):
nonlocal res
if count >= res:
return
lowest_idx, lowest_height, width, prev = -1, math.inf, 0, -1
for i in range(m):
if heights[i] < lowest_height:
lowest_height, lowest_idx, width, prev = heights[i], i, 1, i
elif heights[i] == lowest_height and prev == i - 1:
width, prev = width + 1, i
if rem_area == 0:
res = min(res, count)
return
width = min(width, n - lowest_height)
for w in range(width, 0, -1):
temp = heights.copy()
for j in range(lowest_idx, lowest_idx + w):
temp[j] += w
dfs(temp, count + 1, rem_area - w * w)
dfs([0] * m, 0, n * m)
return res | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
ans = math.inf
matrix = [([0] * m) for _ in range(n)]
def dfs(r, c, count):
nonlocal ans
if count >= ans:
return
if r >= n:
ans = count
return
if c >= m:
dfs(r + 1, 0, count)
return
if matrix[r][c]:
dfs(r, c + 1, count)
return
for i in range(min(n - r, m - c), 0, -1):
if not check(r, c, i):
break
cover(r, c, i)
dfs(r, c + i, count + 1)
uncover(r, c, i)
def check(r, c, k):
return all(
matrix[i][j] == 0 for i in range(r, r + k) for j in range(c, c + k)
)
def cover(r, c, k):
for i in range(r, r + k):
for j in range(c, c + k):
matrix[i][j] = 1
def uncover(r, c, k):
for i in range(r, r + k):
for j in range(c, c + k):
matrix[i][j] = 0
dfs(0, 0, 0)
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, m: int, n: int) -> int:
cur = [0] * (n * m)
q = [cur]
step = 0
seen = {tuple(cur)}
while q and step < n * m + 1:
step += 1
nex = []
for cur in q:
found = True
for i in range(m):
for j in range(n):
if cur[i * n + j] == 0:
start = [i, j]
found = False
break
if not found:
break
if found:
return step - 1
while j + 1 < n and cur[i * n + j + 1] == 0:
j += 1
k = j - start[1] + 1
for sz in range(1, k + 1):
cc = cur.copy()
flag = False
for i in range(start[0], start[0] + sz):
for j in range(start[1], start[1] + sz):
if not (0 <= i < m and 0 <= j < n and cur[i * n + j] == 0):
flag = True
break
cc[i * n + j] = 1
if flag:
break
if flag:
break
C = tuple(cc)
if C not in seen:
seen.add(C)
nex.append(cc)
q = nex | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR RETURN BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
self.best = m * n
def dfs(heights, count):
if min(heights) == n:
self.best = min(self.best, count)
return
if count >= self.best:
return
min_height = min(heights)
idx = heights.index(min_height)
right_end = idx + 1
while right_end < m and heights[right_end] == min_height:
right_end += 1
max_possible_box = min(right_end - idx, n - min_height)
for box_size in range(max_possible_box, 0, -1):
new_heights = heights[:]
for i in range(box_size):
new_heights[idx + i] += box_size
dfs(new_heights, count + 1)
dfs([0] * m, 0)
return self.best | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, m, n):
self.best = n * m
def dfs(heights, mvs):
if mvs >= self.best:
return
if all(h == n for h in heights):
self.best = min(self.best, mvs)
return
i = j = min(list(range(m)), key=lambda i: heights[i])
while j < m and heights[j] == heights[i]:
j += 1
for x in range(min(j - i, n - heights[i]), 0, -1):
dfs(heights[:i] + [heights[i] + x] * x + heights[i + x :], mvs + 1)
heights = [0] * m
dfs(heights, 0)
return self.best | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
def dp(state):
if state in cache:
return cache[state]
if state[::-1] in cache:
return cache[state[::-1]]
tmp = state
if min(state) == n:
return 0
state = list(state)
min_s = min(state)
start = state.index(min_s)
res = n
for end in range(start, m):
if state[end] != min_s:
break
side = end - start + 1
if min_s + side > n:
break
state[start : end + 1] = [min_s + side] * side
res = min(res, dp(tuple(state)))
cache[tmp] = res + 1
return res + 1
if m > n:
m, n = n, m
cache = dict()
return dp(tuple([0] * m)) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER VAR RETURN VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
memo = {}
return self.dfs(n, m, memo)
def dfs(self, x, y, memo):
if x == 0 or y == 0:
return 0
if x == y:
return 1
if x == 1:
return y
if y == 1:
return x
if (x, y) in memo:
return memo[x, y]
ans = float("inf")
for i in range(1, x):
ans = min(ans, self.dfs(i, y, memo) + self.dfs(x - i, y, memo))
for j in range(1, y):
ans = min(ans, self.dfs(x, j, memo) + self.dfs(x, y - j, memo))
for side in range(1, min(x, y)):
for i in range(x - side):
for j in range(y - side):
ans1 = self.dfs(i, j + side, memo)
ans2 = self.dfs(x - i, j, memo)
ans3 = self.dfs(x - i - side, y - j, memo)
ans4 = self.dfs(i + side, y - j - side, memo)
ans = min(ans, ans1 + ans2 + ans3 + ans4 + 1)
memo[x, y] = ans
return memo[x, y] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if m > n:
n, m = m, n
res, state = m * n, [0] * n
def dfs(count):
nonlocal res
if count > res:
return
min_h = min(state)
if min_h == m:
res = min(res, count)
return
e = s = state.index(min_h)
while e < n and state[e] == min_h:
e += 1
max_len = min(e - s, m - min_h)
for l in range(max_len, 0, -1):
for i in range(s, s + l):
state[i] += l
dfs(count + 1)
for i in range(s, s + l):
state[i] -= l
dfs(0)
return res | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
res = 0
dq = deque([[0] * m])
seen = {tuple([0] * m)}
while dq:
length = len(dq)
for _ in range(length):
curr = dq.popleft()
minh = min(curr)
s = curr.index(minh)
e = s
while e + 1 < m and curr[e + 1] == minh:
e += 1
for i in range(min(e - s + 1, n - minh), 0, -1):
nxt = curr[:]
for j in range(s, s + i):
nxt[j] += i
nxt_state = tuple(nxt)
if nxt_state in seen:
continue
if all(j == n for j in nxt):
return res + 1
seen.add(nxt_state)
dq.append(nxt)
res += 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
dp = [([sys.maxsize] * (m + 1)) for _ in range(n + 1)]
def getdp(n, m):
if dp[n][m] != sys.maxsize:
return dp[n][m]
if n == 0 or m == 0:
return 0
if m == n:
return 1
if m == 1:
return n
if n == 1:
return m
ans = n * m
for i in range(1, n // 2 + 1):
ans = min(ans, getdp(i, m) + getdp(n - i, m))
for i in range(1, m // 2 + 1):
ans = min(ans, getdp(n, i) + getdp(n, m - i))
for l in range(1, min(n, m) - 2):
for i in range(2, n + 1 - l):
for j in range(2, m + 1 - l):
ans = min(
ans,
getdp(i + l - 1, j - 1)
+ getdp(i - 1, m - j + 1)
+ getdp(n - (i + l - 1), j + l - 1)
+ getdp(n - i + 1, m - (j + l - 1))
+ 1,
)
dp[n][m] = ans
return ans
return getdp(n, m) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
@lru_cache(None)
def helper(heights):
mh = min(heights)
if mh == n:
return 0
ret = float("inf")
j = heights.index(mh)
w = 1
while mh + w <= n and j + w - 1 < m and heights[j + w - 1] == mh:
ret = min(
ret, 1 + helper(heights[:j] + (mh + w,) * w + heights[j + w :])
)
w += 1
return ret
return helper((0,) * m) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR BIN_OP NUMBER VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def findSum(self, n, m, k):
res = []
s = m * n
hi = min(n, m)
border = max(n, m)
path = [1] * k
while path[0] <= hi:
i = k - 1
while i >= 0 and path[i] >= hi:
i -= 1
if i == -1 or path[i] >= hi:
return res
path[i] += 1
path[i + 1 :] = [path[i]] * (k - i - 1)
if path[k - 1] + path[k - 2] > border:
path[i:] = [hi] * (k - i)
continue
if sum(x * x for x in path) == s:
x = path[:]
x.reverse()
res.append(x)
return res
def hasNoOverLap(self, x1, y1, s1, x2, y2, s2):
if x1 + s1 - 1 < x2 or y1 + s1 - 1 < y2 or x2 + s2 - 1 < x1 or y2 + s2 - 1 < y1:
return True
else:
return False
def nextPos(self, placement, n, m, size):
if not placement:
return 0, 0
for i in range(n - size + 1):
for j in range(m - size + 1):
if all(self.hasNoOverLap(i, j, size, x, y, z) for x, y, z in placement):
return i, j
return -1, -1
def canPlace(self, sizes, n, m, placement, memo):
if len(sizes) == 0:
return True
h = tuple(placement)
if h in memo:
return memo[h]
for k, s in enumerate(sizes):
i, j = self.nextPos(placement, n, m, s)
if i == -1:
continue
placement.append((i, j, s))
if self.canPlace(sizes[:k] + sizes[k + 1 :], n, m, placement, memo):
memo[h] = True
return True
placement.pop()
memo[h] = False
return False
def tilingRectangle(self, n: int, m: int) -> int:
if n % m == 0:
return n // m
if m % n == 0:
return m // n
for i in range(3, 10):
res = self.findSum(n, m, i)
if any(self.canPlace(sizes, n, m, [], {}) for sizes in res):
return i | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP LIST VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR RETURN 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 FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR RETURN NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR LIST DICT VAR VAR RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
self.result = n * m
heights = [0] * n
def dfs(cur):
if cur >= self.result:
return
curMinHeight = min(heights)
if curMinHeight == m:
self.result = cur
return
end = start = heights.index(curMinHeight)
while (
end < n
and heights[start] == heights[end]
and end - start + 1 + curMinHeight <= m
):
end += 1
for i in range(end - 1, start - 1, -1):
size = i - start + 1
for j in range(start, i + 1):
heights[j] += size
dfs(cur + 1)
for j in range(start, i + 1):
heights[j] -= size
dfs(0)
return self.result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR 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 FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if n > m:
n, m = m, n
if m == n:
return 1
res = [m * n]
dp = {}
def dfs(cnt, hs):
if cnt > res[0]:
return
key = tuple(hs)
if key in dp and dp[key] <= cnt:
return
dp[key] = cnt
if all(h == n for h in hs):
res[0] = min(res[0], cnt)
return
min_h = min(hs)
min_i = hs.index(min_h)
r = m
for i in range(min_i, m):
if hs[i] > min_h:
r = i
break
for side in range(min(r - min_i, n - min_h), 0, -1):
dfs(cnt + 1, hs[:min_i] + [side + min_h] * side + hs[min_i + side :])
dfs(0, [0] * m)
return res[0] | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR LIST BIN_OP VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP LIST NUMBER VAR RETURN VAR NUMBER VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if n > m:
return self.tilingRectangle(m, n)
ans = n * m
h = [0] * n
def getmin(l):
minv = 100000
mini = -1
for i in range(len(l)):
if l[i] < minv:
minv = l[i]
mini = i
return minv, mini
def dfs(cur):
nonlocal ans
if cur >= ans:
return
it, iti = getmin(h)
if it == m:
ans = cur
return
low = it
s = iti
e = s
while e < n and h[e] == h[s] and e - s + 1 <= m - low:
e += 1
e -= 1
for i in range(e, s - 1, -1):
size = i - s + 1
for j in range(s, i + 1):
h[j] += size
dfs(cur + 1)
for j in range(s, i + 1):
h[j] -= size
dfs(0)
return ans | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
self.result = n * m
def dfs(heights, moves):
if moves > self.result:
return
if all(h == n for h in heights):
self.result = min(self.result, moves)
return
min_height = min(heights)
idx = heights.index(min_height)
right_boundary = idx + 1
while right_boundary < m and heights[right_boundary] == min_height:
right_boundary += 1
for i in range(min(right_boundary - idx, n - min_height), 0, -1):
dfs(
heights[:idx] + [min_height + i] * i + heights[idx + i :], moves + 1
)
dfs([0] * m, 0)
return self.result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
res = float("inf")
def tilingRectangle(self, n: int, m: int) -> int:
s = {}
def dfs(n, m, h, cnt):
if cnt > self.res:
return
is_full = True
pos = -1
minh = float("inf")
for i in range(1, n + 1):
if h[i] < m:
is_full = False
if h[i] < minh:
pos = i
minh = h[i]
if is_full:
self.res = min(cnt, self.res)
return
key = 0
base = m + 1
for i in range(1, n + 1):
key += h[i] * base
base *= m + 1
if key in s and s[key] <= cnt:
return
s[key] = cnt
end = pos
while end < n and h[end + 1] == h[pos] and end + 1 - pos + 1 + minh <= m:
end += 1
for j in range(end, pos - 1, -1):
curh = j - pos + 1
nex = h[:]
for k in range(pos, j + 1):
nex[k] += curh
dfs(n, m, nex, cnt + 1)
if n == m:
return 1
if n > m:
n, m = m, n
dfs(n, m, [0] * (n + 1), 0)
return self.res | CLASS_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int):
cache = [[(-1) for i in range(m + 1)] for j in range(n + 1)]
return self.helper(n, m, cache)
def helper(self, n: int, m: int, cache) -> int:
if n <= 0 or m <= 0:
return 0
if n == 11 and m == 13 or n == 13 and m == 11:
return 6
if n == m:
return 1
if cache[n][m] != -1:
return cache[n][m]
rr1 = 10000
rr2 = 10000
_min = 10000
for x in range(1, min(n, m) + 1):
rr1 = self.helper(n, m - x, cache) + self.helper(n - x, x, cache)
rr2 = self.helper(n - x, m, cache) + self.helper(x, m - x, cache)
_min = min(rr1, min(rr2, _min))
cache[n][m] = _min + 1
return _min + 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if n == m:
return 1
if n > m:
n, m = m, n
layers0 = [(0) for _ in range(n)]
visited = {}
ans = [n * m]
def dfs(layers, stepsNow):
if stepsNow > ans[0]:
return
key = tuple(layers)
if key in visited and stepsNow >= visited[key]:
return
visited[key] = stepsNow
minH = m + 1
i0 = -1
for i, h in enumerate(layers):
if h < minH:
minH = h
i0 = i
if minH == m:
ans[0] = min(ans[0], stepsNow)
return
minHWidth = 0
maxL = m - minH
for i in range(i0, n):
if layers[i] == minH:
minHWidth += 1
else:
break
maxL = min(maxL, minHWidth)
for l in range(maxL, 0, -1):
nextLayers = list(key)
for i in range(i0, i0 + l):
nextLayers[i] += l
dfs(nextLayers, stepsNow + 1)
return
dfs(layers0, 0)
return ans[0] | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST BIN_OP VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if n < m:
n, m = m, n
if m == n:
return 1
heights = [0] * n
dp = {}
final = [m * n]
def helper(heights, counts):
key = tuple(heights)
if counts >= final[0]:
return
if all(h == m for h in heights):
final[0] = min(final[0], counts)
return
if key in dp and dp[key] <= counts:
return
dp[key] = counts
min_val = min(heights)
idx = heights.index(min_val)
d = 0
for i in range(idx, n):
if heights[i] == min_val:
d += 1
else:
break
d = min(m - min_val, d)
for i in range(d, 0, -1):
if heights[idx] + i <= m:
helper(
heights[:idx] + [heights[idx] + i] * i + heights[idx + i :],
counts + 1,
)
return
helper(heights, 0)
return final[0] | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR LIST BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
memo = {}
def helper(w, h):
if w > h:
w, h = h, w
if (w, h) not in memo:
if w == h:
ans = 1
elif w == 1:
ans = h
elif w == 0:
ans = 0
else:
ans = w * h
for iw in range(1, w // 2 + 1):
ans = min(ans, helper(iw, h) + helper(w - iw, h))
for ih in range(1, h // 2 + 1):
ans = min(ans, helper(w, ih) + helper(w, h - ih))
for iw in range(1, (w + 1) // 2):
for ih in range(1, (h + 1) // 2):
for s in range(1, min(w - 2 * iw, h - 2 * ih) + 1):
ans = min(
ans,
1
+ helper(iw + s, ih)
+ helper(w - iw - s, ih + s)
+ helper(w - iw, h - ih - s)
+ helper(iw, h - ih),
)
memo[w, h] = ans
return memo[w, h]
return helper(n, m) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
h = [0] * m
self.ans = m * n
def dfs(h: List[int], cur: int) -> None:
min_h = min(h)
if min_h == n:
self.ans = min(self.ans, cur)
return
if cur > self.ans:
return
idx = h.index(min_h)
j = idx
while j < len(h) and h[j] == min_h:
j += 1
fill_width = j - idx
fill_height = n - min_h
for fill in range(min(fill_width, fill_height), 0, -1):
for k in range(idx, idx + fill):
h[k] += fill
dfs(h, cur + 1)
for k in range(idx, idx + fill):
h[k] -= fill
dfs(h, 0)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
m, n = min(m, n), max(m, n)
self.res = n * m
self.H = n
self.search(0, [0] * m)
return self.res
def search(self, count, heights):
if count >= self.res:
return
min_h = min(heights)
if min_h == self.H:
self.res = count
return
l = heights.index(min_h)
width = 1
while (
width <= self.H - min_h
and l + width - 1 < len(heights)
and heights[l + width - 1] == heights[l]
):
width += 1
width -= 1
for w in range(width, 0, -1):
self.search(count + 1, heights[:l] + [min_h + w] * w + heights[l + w :]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP LIST NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if n < m:
return self.tilingRectangle(m, n)
h = [0] * m
self.ans = 100000
self.dfs(n, m, h, 0)
return self.ans
def dfs(self, m, n, h, cur):
if cur >= self.ans:
return
min_h = min(h)
if min_h == m:
self.ans = min(self.ans, cur)
return
for j in range(n):
if h[j] == min_h:
break
start = j
while j + 1 < n and h[j] == h[j + 1] and j + 1 - start + 1 + h[j] <= m:
j += 1
side = j - start + 1
for k in reversed(range(1, side + 1)):
temp = list(h)
for j in range(start, start + k):
temp[j] += k
self.dfs(m, n, temp, cur + 1) | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if m == n:
return 1
if n > m:
m, n = n, m
h_i = [0] * n
visited = {}
res = [m * n]
def dfs(height, cnt):
if cnt > res[0]:
return
status = tuple(height)
if status in visited and visited[status] <= cnt:
return
visited[status] = cnt
complete = True
start_j = -1
lowest_h = m + 1
for j in range(n):
if height[j] < m:
complete = False
if height[j] < lowest_h:
start_j = j
lowest_h = height[j]
if complete:
res[0] = min(res[0], cnt)
return
j = start_j
while j < n and height[j] == lowest_h:
j += 1
max_l = min(j - start_j, m - lowest_h)
for l in range(max_l, 0, -1):
next_h = list(height)
for k in range(l):
next_h[start_j + k] += l
dfs(next_h, cnt + 1)
dfs(h_i, 0)
return res[0] | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR LIST BIN_OP VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, R, C):
A = [([0] * C) for _ in range(R)]
self.ans = R * C
def search(r, c, steps):
if steps >= self.ans:
return
if r == R:
self.ans = steps
return
if c == C:
return search(r + 1, 0, steps)
if A[r][c]:
return search(r, c + 1, steps)
for k in range(min(R - r, C - c), 0, -1):
bad = False
for r0 in range(r, r + k):
for c0 in range(c, c + k):
if A[r0][c0]:
bad = True
break
if bad:
break
if not bad:
for r0 in range(r, r + k):
for c0 in range(c, c + k):
A[r0][c0] = 1
search(r, c + 1, steps + 1)
for r0 in range(r, r + k):
for c0 in range(c, c + k):
A[r0][c0] = 0
search(0, 0, 0)
return self.ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR RETURN IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
self.res = math.inf
grid = [([False] * n) for _ in range(m)]
def dfs(i, j, s):
if s >= self.res:
return
elif i == m:
self.res = s
elif j == n:
dfs(i + 1, 0, s)
elif grid[i][j]:
dfs(i, j + 1, s)
else:
side = min(m - i, n - j)
while side:
if not any(
grid[ni][nj]
for ni in range(i, i + side)
for nj in range(j, j + side)
):
for ni in range(i, i + side):
for nj in range(j, j + side):
grid[ni][nj] = True
dfs(i, j + side, s + 1)
for ni in range(i, i + side):
for nj in range(j, j + side):
grid[ni][nj] = False
side -= 1
dfs(0, 0, 0)
return self.res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
self.best = m * n
height = [0] * m
def dfs(moves):
if all(h == n for h in height):
self.best = min(self.best, moves)
return
if moves >= self.best:
return
idx = height.index(min(height))
for i in range(min(m - idx, n - height[idx]), 0, -1):
for j in range(i):
height[idx + j] += i
dfs(moves + 1)
for j in range(i):
height[idx + j] -= i
dfs(0)
return self.best | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
self.best = n * m
def dfs(hts, mvs):
if mvs >= self.best:
return
if all(h == n for h in hts):
self.best = min(self.best, mvs)
return
i = min(list(range(m)), key=lambda i: hts[i])
j = i + ([(hts[k] != hts[i]) for k in range(i, m)] + [True]).index(True)
for x in range(min(j - i, n - hts[i]), 0, -1):
for k in range(x):
hts[i + k] += x
dfs(hts, mvs + 1)
for k in range(x):
hts[i + k] -= x
dfs([0] * m, 0)
return self.best | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if n == m:
return 1
if m > n:
m, n = n, m
@lru_cache(None)
def helper(skyline):
if all(h == n for h in skyline):
return 0
ans = float("inf")
minh = min(skyline)
l = skyline.index(minh)
for r in range(l, m):
if skyline[r] != minh:
break
if r - l + 1 > n - minh:
break
newsl = list(skyline)
for i in range(l, r + 1):
newsl[i] += r - l + 1
ans = min(ans, helper(tuple(newsl)))
return ans + 1
ans = helper(tuple([0] * m))
return ans | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
d = gcd(n, m)
if d != 1:
return self.tilingRectangle(n // d, m // d)
if n > m:
n, m = m, n
if m % n == 0:
return m // n
heights = [0] * n
recs = [[0, 1]]
CAP = max(n, m)
ans = CAP
def is_valid():
if len(recs) > ans - 1:
return False
i, side = recs[-1]
if heights[i] + 1 > m:
return False
if i + side > n:
return False
if heights[i + side - 1] > heights[i]:
return False
return True
while recs:
if is_valid():
i, side = recs[-1]
for k in range(i, i + side - 1):
heights[k] += 1
heights[i + side - 1] += side
_, i = min((heights[k], k) for k in range(n))
if heights == [m] * n:
ans = min(ans, len(recs))
recs.append([i, 1])
else:
i, side = recs.pop()
for k in range(i, i + side - 1):
heights[k] -= side - 1
if recs:
recs[-1][-1] += 1
return ans
def gcd(a, b):
while a:
a, b = b % a, a
return b | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR IF FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
self.n = n
self.m = m
board = [([0] * n) for _ in range(m)]
self.res = float("inf")
self.dfs(board, 0)
return self.res
def dfs(self, board, count):
if count >= self.res:
return
i, j = self.find_next(board)
if i == -1 and j == -1:
self.res = min(self.res, count)
return
max_length = self.find_max_length(board, i, j)
for k in range(1, max_length + 1)[::-1]:
self.assign(board, i, j, k, 1)
self.dfs(board, count + 1)
self.assign(board, i, j, k, 0)
def assign(self, board, i, j, length, val):
for row in range(i, i + length):
for col in range(j, j + length):
board[row][col] = val
def find_max_length(self, board, i, j):
max_length = 1
while i + max_length - 1 < self.m and j + max_length - 1 < self.n:
for row in range(i, i + max_length):
if board[row][j + max_length - 1] != 0:
return max_length - 1
for col in range(j, j + max_length):
if board[i + max_length - 1][col] != 0:
return max_length - 1
max_length += 1
return max_length - 1
def find_next(self, board):
for i in range(self.m):
for j in range(self.n):
if board[i][j] == 0:
return i, j
return -1, -1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR RETURN NUMBER NUMBER |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if n > m:
self.tilingRectangle(m, n)
self.ans = n * m
h = [0] * n
self.dfs(h, 0, n, m)
return self.ans
def dfs(self, h, cur, n, m):
if cur >= self.ans:
return
min_h = min(h)
if min_h == m:
self.ans = cur
return
for i in range(n):
if h[i] == min_h:
break
start, end = i, i
while end < n and h[start] == h[end] and end - start + 1 + min_h <= m:
end += 1
for i in reversed(list(range(start, end))):
size = i - start + 1
for j in range(start, i + 1):
h[j] += size
self.dfs(h, cur + 1, n, m)
for j in range(start, i + 1):
h[j] -= size | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
dp = {}
if n < m:
m, n = n, m
if m == n:
return 1
res = [m * n + 1]
def dfs(ys, cnt):
key = tuple(ys)
if key not in dp or cnt < dp[key]:
dp[key] = cnt
if dp[key] < cnt or cnt > res[0]:
return
if all(i == m for i in ys):
res[0] = min(res[0], cnt)
return
if any(i > m for i in ys):
return
ymin = min(ys)
idx = ys.index(ymin)
ymax = 0
for i in range(idx, n):
if ys[i] > ymin:
break
else:
ymax += 1
ymax = min(ymax, m)
for i in range(ymax, 0, -1):
dfs(ys[:idx] + [ys[idx] + i] * i + ys[idx + i :], cnt + 1)
dfs([0] * n, 0)
return res[0] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR LIST BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR NUMBER RETURN VAR NUMBER VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
if m == n:
return 1
if n < m:
m, n = n, m
if m == 1:
return n
if m == 2:
k, rem = divmod(n, 2)
return k + 2 * rem
def recurse(h, ct):
nonlocal Min
if ct >= Min:
return
if all(x == m for x in h):
Min = min(Min, ct)
return
i = j = min(list(range(n)), key=lambda c: h[c])
hi = h[i]
bound = min(n, i + m - hi)
while j < bound and h[j] == hi:
j += 1
for x in range(j - i, 0, -1):
recurse(h[:i] + [hi + x] * x + h[i + x :], ct + 1)
Min = m * n
recurse([0] * n, 0)
return Min | CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF IF VAR VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR NUMBER RETURN VAR VAR |
Given a rectangle of sizeΒ nΒ x m, find the minimum number of integer-sided squares that tile the rectangle.
Β
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Β
Constraints:
1 <= n <= 13
1 <= mΒ <=Β 13 | class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
self.best = n * m
def dp(heights, n_square):
if n_square > self.best:
return
if heights == [n] * m:
self.best = min(self.best, n_square)
min_index = min(range(m), key=lambda x: heights[x])
i, j = min_index, min_index
while j < m and heights[i] == heights[j]:
j += 1
max_line = min(j - i + int(j == m), n - heights[i])
result = float("inf")
for x in range(max_line, 0, -1):
cur = dp(
heights[:i] + [heights[i] + x] * x + heights[i + x :], n_square + 1
)
if cur:
result = min(result, cur)
return result
dp([0] * m, 0)
return self.best | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN IF VAR BIN_OP LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR NUMBER RETURN VAR VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
def helper(n, x, l, temp):
if x == n:
temp.append(l)
return
if x > n:
return
for i in range(n, 0, -1):
if len(l) == 0 or l[-1] >= i:
helper(n, x + i, l + [i], temp)
ans = []
helper(n, 0, [], ans)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER LIST VAR RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
arr = []
for i in range(n, 0, -1):
arr.append(i)
res = []
def finder(arr, target, ind, path):
if target == 0:
res.append(path)
return
if ind == len(arr):
return
if target < 0:
return
finder(arr, target - arr[ind], ind, path + [arr[ind]])
finder(arr, target, ind + 1, path)
finder(arr, n, 0, [])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR FUNC_CALL VAR VAR RETURN IF VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER LIST RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
li = []
def solve(i, target, li, temparr):
if i <= 0:
if target == 0:
li.append(temparr[:])
return
if target > 0:
temparr.append(i)
solve(i, target - i, li, temparr)
temparr.pop()
solve(i - 1, target, li, temparr)
solve(n, n, li, [])
return li | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR LIST RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | import sys
sys.setrecursionlimit(1000000)
def f(t, temp, res, last, N):
if t == 0:
c = temp[:]
c.sort(reverse=True)
res.append(c)
return
for i in range(last, n + 1):
if i <= t:
temp.append(i)
f(t - i, temp, res, i, n)
temp.pop()
class Solution:
def UniquePartitions(self, n):
res = []
f(n, [], res, 1, n)
res.sort(reverse=True)
return res | IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
arr = [i for i in range(n, 0, -1)]
li = []
def solve(i, target, arr, li, temparr):
if i >= len(arr):
if target == 0:
li.append(temparr[:])
return
if target > 0:
temparr.append(arr[i])
solve(i, target - arr[i], arr, li, temparr)
temparr.pop()
solve(i + 1, target, arr, li, temparr)
solve(0, n, arr, li, [])
return li | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR LIST RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | def backtracking(n):
total = n
stack = []
res = []
helper(n, total, stack, res)
return res
def helper(n, total, stack, res):
if not total:
res.append(tuple(stack))
for i in range(n, 0, -1):
if total - i >= 0 and not stack:
stack.append(i)
helper(n, total - i, stack, res)
stack.remove(i)
elif total - i >= 0 and stack[-1] >= i:
stack.append(i)
helper(n, total - i, stack, res)
stack.remove(i)
class Solution:
def UniquePartitions(self, n):
return backtracking(n) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def __init__(self):
self.ans = []
def f(self, W, n, arr, temp):
if W == 0:
self.ans.append(temp[0:])
return
if n < 0:
return
if arr[n] <= W:
temp.append(arr[n])
Solution.f(self, W - arr[n], n, arr, temp)
temp.pop()
Solution.f(self, W, n - 1, arr, temp)
def UniquePartitions(self, n):
arr = []
for i in range(n):
arr.append(i + 1)
Solution.f(self, n, n - 1, arr, [])
return self.ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN IF VAR NUMBER RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR LIST RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
dp = [(0) for i in range(n)]
count = 0
ar = []
def solve(remSum, maxVal, idx, count):
if remSum == 0:
a = []
for i in range(idx):
a.append(dp[i])
ar.append(a)
count += 1
return
i = maxVal
while i >= 1:
if i > remSum:
i -= 1
continue
elif i <= remSum:
dp[idx] = i
solve(remSum - i, i, idx + 1, count)
i -= 1
solve(n, n, 0, 0)
return ar | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def firstApproach(self, n):
p = [0] * n
k = 0
p[k] = n
res = []
while True:
res.append(p[0 : k + 1])
rem_val = 0
while k >= 0 and p[k] == 1:
rem_val += p[k]
k -= 1
if k < 0:
return res
p[k] -= 1
rem_val += 1
while rem_val > p[k]:
p[k + 1] = p[k]
rem_val = rem_val - p[k]
k += 1
p[k + 1] = rem_val
k += 1
def UniquePartitions(self, n):
return self.firstApproach(n) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST WHILE NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def duplicate(self, lt):
k = []
for i in lt:
if i not in k:
k.append(i)
return k
def lexsort(self, lt):
lt = sorted(lt, reverse=True)
return lt
def UniquePartitions(self, n):
dict1 = {(1): [[1]]}
for i in range(2, n + 1):
dict1[i] = []
dict1[i].append([i])
for j in range(i - 1, i // 2 - 1, -1):
for k in dict1[j]:
for h in dict1[i - j]:
po = k + h
dict1[i].append(sorted(po, reverse=True))
dict1[i] = self.lexsort(self.duplicate(dict1[i]))
ans = []
for i in dict1[n]:
ans.append(i)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT NUMBER LIST LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR FOR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def helper(self, n, prev, current):
if n == 0:
self.finalAns.append(current[:])
return
for i in range(min(prev, n), 0, -1):
current.append(i)
self.helper(n - i, i, current)
current.pop()
def UniquePartitions(self, n):
self.finalAns = []
self.helper(n, float("inf"), [])
return self.finalAns | CLASS_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING LIST RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
table = [None] * (n + 1)
table[0] = []
table[1] = [[1]]
for i in range(2, n + 1):
res = [[i]]
for j in range(i, 0, -1):
for k in table[i - j]:
if j >= k[0]:
res.append([j] + k)
table[i] = res
return table[n] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR NUMBER LIST ASSIGN VAR NUMBER LIST LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST LIST VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def solve(self, remSum, maxVal, idx, count, ans, dp):
if remSum == 0:
part = []
for i in range(1, idx, 1):
part.append(dp[i])
ans.append(part)
count += 1
return
i = maxVal
while i >= 1:
if i > remSum:
i -= 1
continue
elif i <= remSum:
dp[idx] = i
self.solve(remSum - i, i, idx + 1, count, ans, dp)
i -= 1
def UniquePartitions(self, n):
ans = []
dp = [(0) for i in range(200)]
self.solve(n, n, 1, 0, ans, dp)
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
dp = [[[1 + i]] for i in range(n)]
for i in range(1, n):
for j in range(i):
dp[i] += [
([i - j] + partition)
for partition in dp[j]
if partition[0] <= i - j
]
return dp[n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR BIN_OP VAR NUMBER |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
seq = [i for i in range(1, n + 1)]
ans = []
cur = []
def partition(num, seq, n):
nonlocal ans, cur
if num == 0:
for i in cur:
ans.append([i])
return
elif n == 0:
return
if seq[n - 1] <= num:
cur.append(seq[n - 1])
partition(num - seq[n - 1], seq, n)
cur.pop()
partition(num, seq, n - 1)
return
partition(n, seq, n)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR RETURN IF VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
res = []
nums = [i for i in range(1, n + 1)]
def solve(target, n, arr):
if target == 0:
if arr not in res:
res.append(arr)
return
if n < 0:
return
if nums[n - 1] <= target:
arr1 = arr[:]
arr1.append(nums[n - 1])
solve(target - nums[n - 1], n, arr1)
solve(target, n - 1, arr)
else:
solve(target, n - 1, arr)
solve(n, n, [])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR LIST RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
all_p = []
def partition(k, m, parts):
if k == 0:
return parts
return_parts = []
for j in range(min(k, m), 0, -1):
next_parts = parts.copy()
next_parts.append(j)
return_parts = return_parts + partition(k - j, j, next_parts)
return return_parts
for i in range(n, 0, -1):
all_p.append(partition(n - i, i, [i]))
return all_p | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR LIST VAR RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | class Solution:
def UniquePartitions(self, n):
def fun(l, i, n):
if n == 0:
l1.append(l.copy())
return l1
if i < 1:
return l1
if i <= n:
l.append(i)
fun(l, i, n - i)
l.pop()
fun(l, i - 1, n)
else:
fun(l, i - 1, n)
l1 = []
l = []
i = n
fun(l, i, n)
return l1 | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR IF VAR NUMBER RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.
Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.
Example 1:
Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.
Example 2:
Input: n = 4
Output: 4 3 1 2 2 2 1 1 1 1 1 1
Explanation: For n = 4,
{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}.
Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.
Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)
Constraints:
1 <= n <= 25 | def rec(n, j):
if n == 0:
return [[]]
c = []
for i in range(1, j + 1):
if n >= i:
ans = rec(n - i, i)
for k in ans:
c.append([i] + k)
return c
class Solution:
def UniquePartitions(self, n):
res = rec(n, n)
res.sort(reverse=True)
return res | FUNC_DEF IF VAR NUMBER RETURN LIST LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.