description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$.
Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks.
Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$.
You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.
That is the minimum total cost to make a fault-tolerant network?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow.
The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row.
It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.
-----Examples-----
Input
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
Output
31
1999999998
-----Note-----
In the first test case, it's optimal to connect four pairs of computers:
computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$;
computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$;
In total, $3 + 3 + 10 + 15 = 31$.
In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row. | t = int(input())
while t:
t -= 1
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s00 = abs(a[0] - b[0])
s11 = abs(a[-1] - b[-1])
s01 = abs(a[0] - b[-1])
s10 = abs(a[-1] - b[0])
sa0 = min(abs(i - b[0]) for i in a)
sa1 = min(abs(i - b[-1]) for i in a)
s0b = min(abs(a[0] - i) for i in b)
s1b = min(abs(a[-1] - i) for i in b)
ans = s00 + s11
ans = min(ans, s01 + s10)
ans = min(ans, s00 + sa1 + s1b)
ans = min(ans, s11 + sa0 + s0b)
ans = min(ans, s01 + s1b + sa0)
ans = min(ans, s10 + s0b + sa1)
ans = min(ans, s0b + s1b + sa0 + sa1)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$.
Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks.
Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$.
You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.
That is the minimum total cost to make a fault-tolerant network?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow.
The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row.
It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.
-----Examples-----
Input
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
Output
31
1999999998
-----Note-----
In the first test case, it's optimal to connect four pairs of computers:
computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$;
computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$;
In total, $3 + 3 + 10 + 15 = 31$.
In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
cost_00 = min([abs(arr[0] - i) for i in arr2])
cost_01 = min([abs(arr[-1] - i) for i in arr2])
cost_10 = min([abs(arr2[0] - i) for i in arr])
cost_11 = min([abs(arr2[-1] - i) for i in arr])
cst = (
cost_00 + cost_01 + cost_10 + cost_11,
cost_01 + abs(arr[0] - arr2[0]) + cost_11,
cost_00 + cost_10 + abs(arr[-1] - arr2[-1]),
cost_00 + abs(arr[-1] - arr2[0]) + cost_11,
cost_01 + abs(arr[0] - arr2[-1]) + cost_10,
abs(arr[0] - arr2[0]) + abs(arr[-1] - arr2[-1]),
abs(arr[0] - arr2[-1]) + abs(arr[-1] - arr2[0]),
abs(arr[-1] - arr2[0]) + abs(arr[0] - arr2[-1]),
)
print(min(cst)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$.
Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks.
Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$.
You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.
That is the minimum total cost to make a fault-tolerant network?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow.
The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row.
It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.
-----Examples-----
Input
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
Output
31
1999999998
-----Note-----
In the first test case, it's optimal to connect four pairs of computers:
computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$;
computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$;
In total, $3 + 3 + 10 + 15 = 31$.
In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
z1 = abs(a[-1] - b[0])
z2 = abs(a[0] - b[-1])
a1 = abs(a[-1] - b[-1])
a2 = abs(a[0] - b[0])
m3 = m1 = a1
m4 = m2 = a2
for i in range(n):
d1 = abs(a[i] - b[-1])
if d1 < m1:
m1 = d1
d2 = abs(a[i] - b[0])
if d2 < m2:
m2 = d2
d3 = abs(b[i] - a[-1])
if d3 < m3:
m3 = d3
d4 = abs(b[i] - a[0])
if d4 < m4:
m4 = d4
print(
min(
m1 + m2 + m3 + m4,
a1 + a2,
a1 + m2 + m4,
a2 + m1 + m3,
z1 + z2,
z1 + m1 + m4,
z2 + m2 + m3,
)
) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR |
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$.
Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks.
Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$.
You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.
That is the minimum total cost to make a fault-tolerant network?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow.
The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row.
It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.
-----Examples-----
Input
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
Output
31
1999999998
-----Note-----
In the first test case, it's optimal to connect four pairs of computers:
computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$;
computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$;
In total, $3 + 3 + 10 + 15 = 31$.
In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row. | for _ in range(int(input())):
n = int(input())
a = (*map(int, input().split()),)
b = (*map(int, input().split()),)
ans1 = abs(a[0] - b[0]) + abs(a[-1] - b[-1])
ans2 = abs(a[0] - b[-1]) + abs(a[-1] - b[0])
ans3 = (
abs(a[0] - b[0])
+ min(abs(a[-1] - b[i]) for i in range(n))
+ min(abs(a[i] - b[-1]) for i in range(n))
)
ans4 = (
abs(a[-1] - b[-1])
+ min(abs(a[0] - b[i]) for i in range(n))
+ min(abs(a[i] - b[0]) for i in range(n))
)
ans5 = (
abs(a[0] - b[-1])
+ min(abs(a[-1] - b[i]) for i in range(n))
+ min(abs(a[i] - b[0]) for i in range(n))
)
ans6 = (
abs(a[-1] - b[0])
+ min(abs(a[0] - b[i]) for i in range(n))
+ min(abs(a[i] - b[-1]) for i in range(n))
)
ans7 = (
min(abs(a[0] - b[i]) for i in range(n))
+ min(abs(a[i] - b[0]) for i in range(n))
+ min(abs(a[-1] - b[i]) for i in range(n))
+ min(abs(a[i] - b[-1]) for i in range(n))
)
print(min(ans1, ans2, ans3, ans4, ans5, ans6, ans7)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR |
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$.
Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks.
Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$.
You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.
That is the minimum total cost to make a fault-tolerant network?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow.
The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row.
It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.
-----Examples-----
Input
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
Output
31
1999999998
-----Note-----
In the first test case, it's optimal to connect four pairs of computers:
computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;
computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$;
computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$;
In total, $3 + 3 + 10 + 15 = 31$.
In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row. | def find_smallest(va, vb, id):
ret = 1000000000
for b in vb:
n = abs(va[id] - b)
if n < ret:
ret = n
return ret
for i in range(int(input())):
n = int(input())
a = []
b = []
line = input().split()
for j in range(n):
a.append(int(line[j]))
line = input().split()
for j in range(n):
b.append(int(line[j]))
tl = find_smallest(a, b, 0)
tr = find_smallest(a, b, n - 1)
bl = find_smallest(b, a, 0)
br = find_smallest(b, a, n - 1)
tlbr_diag = abs(a[0] - b[n - 1])
trbl_diag = abs(b[0] - a[n - 1])
cycle = abs(a[0] - b[0]) + abs(a[n - 1] - b[n - 1])
cycle_l = abs(a[0] - b[0]) + tr + br
cycle_r = abs(a[n - 1] - b[n - 1]) + tl + bl
accum = min(tl + br, tlbr_diag) + min(tr + bl, trbl_diag)
print(min(min(cycle, accum), min(cycle_r, cycle_l))) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
Under a grammar given below, strings can represent a set of lowercase words. Let's use R(expr) to denote the set of words the expression represents.
Grammar can best be understood through simple examples:
Single letters represent a singleton set containing that word.
R("a") = {"a"}
R("w") = {"w"}
When we take a comma delimited list of 2 or more expressions, we take the union of possibilities.
R("{a,b,c}") = {"a","b","c"}
R("{{a,b},{b,c}}") = {"a","b","c"} (notice the final set only contains each word at most once)
When we concatenate two expressions, we take the set of possible concatenations between two words where the first word comes from the first expression and the second word comes from the second expression.
R("{a,b}{c,d}") = {"ac","ad","bc","bd"}
R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"}
Formally, the 3 rules for our grammar:
For every lowercase letter x, we have R(x) = {x}
For expressions e_1, e_2, ... , e_k with k >= 2, we have R({e_1,e_2,...}) = R(e_1) ∪ R(e_2) ∪ ...
For expressions e_1 and e_2, we have R(e_1 + e_2) = {a + b for (a, b) in R(e_1) × R(e_2)}, where + denotes concatenation, and × denotes the cartesian product.
Given an expression representing a set of words under the given grammar, return the sorted list of words that the expression represents.
Example 1:
Input: "{a,b}{c,{d,e}}"
Output: ["ac","ad","ae","bc","bd","be"]
Example 2:
Input: "{{a,z},a{b,c},{ab,z}}"
Output: ["a","ab","ac","z"]
Explanation: Each distinct word is written only once in the final answer.
Constraints:
1 <= expression.length <= 60
expression[i] consists of '{', '}', ','or lowercase English letters.
The given expression represents a set of words based on the grammar given in the description. | class Solution:
def braceExpansionII(self, expression: str) -> List[str]:
stack, res, cur = [], [], [""]
for v in expression:
if v.isalpha():
cur = [(c + v) for c in cur]
elif v == ",":
res += cur
cur = [""]
elif v == "{":
stack.append(res)
stack.append(cur)
res, cur = [], [""]
elif v == "}":
preCur = stack.pop()
preRes = stack.pop()
cur = [(p + c) for p in preCur for c in res + cur]
res = preRes
return sorted(set(res + cur)) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR VAR LIST LIST LIST STRING FOR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR STRING VAR VAR ASSIGN VAR LIST STRING IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR |
Under a grammar given below, strings can represent a set of lowercase words. Let's use R(expr) to denote the set of words the expression represents.
Grammar can best be understood through simple examples:
Single letters represent a singleton set containing that word.
R("a") = {"a"}
R("w") = {"w"}
When we take a comma delimited list of 2 or more expressions, we take the union of possibilities.
R("{a,b,c}") = {"a","b","c"}
R("{{a,b},{b,c}}") = {"a","b","c"} (notice the final set only contains each word at most once)
When we concatenate two expressions, we take the set of possible concatenations between two words where the first word comes from the first expression and the second word comes from the second expression.
R("{a,b}{c,d}") = {"ac","ad","bc","bd"}
R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"}
Formally, the 3 rules for our grammar:
For every lowercase letter x, we have R(x) = {x}
For expressions e_1, e_2, ... , e_k with k >= 2, we have R({e_1,e_2,...}) = R(e_1) ∪ R(e_2) ∪ ...
For expressions e_1 and e_2, we have R(e_1 + e_2) = {a + b for (a, b) in R(e_1) × R(e_2)}, where + denotes concatenation, and × denotes the cartesian product.
Given an expression representing a set of words under the given grammar, return the sorted list of words that the expression represents.
Example 1:
Input: "{a,b}{c,{d,e}}"
Output: ["ac","ad","ae","bc","bd","be"]
Example 2:
Input: "{{a,z},a{b,c},{ab,z}}"
Output: ["a","ab","ac","z"]
Explanation: Each distinct word is written only once in the final answer.
Constraints:
1 <= expression.length <= 60
expression[i] consists of '{', '}', ','or lowercase English letters.
The given expression represents a set of words based on the grammar given in the description. | class Solution:
def braceExpansionII(self, expression: str) -> List[str]:
groups = [[]]
level = 0
for i, c in enumerate(expression):
if c == "{":
if level == 0:
start = i + 1
level += 1
elif c == "}":
level -= 1
if level == 0:
groups[-1].append(self.braceExpansionII(expression[start:i]))
elif c == "," and level == 0:
groups.append([])
elif level == 0:
groups[-1].append([c])
word_set = set()
for group in groups:
word_set |= set(map("".join, itertools.product(*group)))
return sorted(word_set) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Under a grammar given below, strings can represent a set of lowercase words. Let's use R(expr) to denote the set of words the expression represents.
Grammar can best be understood through simple examples:
Single letters represent a singleton set containing that word.
R("a") = {"a"}
R("w") = {"w"}
When we take a comma delimited list of 2 or more expressions, we take the union of possibilities.
R("{a,b,c}") = {"a","b","c"}
R("{{a,b},{b,c}}") = {"a","b","c"} (notice the final set only contains each word at most once)
When we concatenate two expressions, we take the set of possible concatenations between two words where the first word comes from the first expression and the second word comes from the second expression.
R("{a,b}{c,d}") = {"ac","ad","bc","bd"}
R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"}
Formally, the 3 rules for our grammar:
For every lowercase letter x, we have R(x) = {x}
For expressions e_1, e_2, ... , e_k with k >= 2, we have R({e_1,e_2,...}) = R(e_1) ∪ R(e_2) ∪ ...
For expressions e_1 and e_2, we have R(e_1 + e_2) = {a + b for (a, b) in R(e_1) × R(e_2)}, where + denotes concatenation, and × denotes the cartesian product.
Given an expression representing a set of words under the given grammar, return the sorted list of words that the expression represents.
Example 1:
Input: "{a,b}{c,{d,e}}"
Output: ["ac","ad","ae","bc","bd","be"]
Example 2:
Input: "{{a,z},a{b,c},{ab,z}}"
Output: ["a","ab","ac","z"]
Explanation: Each distinct word is written only once in the final answer.
Constraints:
1 <= expression.length <= 60
expression[i] consists of '{', '}', ','or lowercase English letters.
The given expression represents a set of words based on the grammar given in the description. | class Solution:
def braceExpansionII(self, expression: str) -> List[str]:
stack, res, cur = [], [], [""]
for i, v in enumerate(expression):
print(v)
print(stack)
print(res, cur)
if v.isalpha():
cur = [(c + v) for c in cur]
elif v == "{":
stack.append(res)
stack.append(cur)
res, cur = [], [""]
elif v == "}":
precur = stack.pop()
preres = stack.pop()
cur = [(p + c) for p in precur for c in res + cur]
res = preres
elif v == ",":
res += cur
cur = [""]
return sorted(set(res + cur)) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR VAR LIST LIST LIST STRING FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR STRING VAR VAR ASSIGN VAR LIST STRING RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR |
Under a grammar given below, strings can represent a set of lowercase words. Let's use R(expr) to denote the set of words the expression represents.
Grammar can best be understood through simple examples:
Single letters represent a singleton set containing that word.
R("a") = {"a"}
R("w") = {"w"}
When we take a comma delimited list of 2 or more expressions, we take the union of possibilities.
R("{a,b,c}") = {"a","b","c"}
R("{{a,b},{b,c}}") = {"a","b","c"} (notice the final set only contains each word at most once)
When we concatenate two expressions, we take the set of possible concatenations between two words where the first word comes from the first expression and the second word comes from the second expression.
R("{a,b}{c,d}") = {"ac","ad","bc","bd"}
R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"}
Formally, the 3 rules for our grammar:
For every lowercase letter x, we have R(x) = {x}
For expressions e_1, e_2, ... , e_k with k >= 2, we have R({e_1,e_2,...}) = R(e_1) ∪ R(e_2) ∪ ...
For expressions e_1 and e_2, we have R(e_1 + e_2) = {a + b for (a, b) in R(e_1) × R(e_2)}, where + denotes concatenation, and × denotes the cartesian product.
Given an expression representing a set of words under the given grammar, return the sorted list of words that the expression represents.
Example 1:
Input: "{a,b}{c,{d,e}}"
Output: ["ac","ad","ae","bc","bd","be"]
Example 2:
Input: "{{a,z},a{b,c},{ab,z}}"
Output: ["a","ab","ac","z"]
Explanation: Each distinct word is written only once in the final answer.
Constraints:
1 <= expression.length <= 60
expression[i] consists of '{', '}', ','or lowercase English letters.
The given expression represents a set of words based on the grammar given in the description. | class Solution:
def braceExpansionII(self, expression: str) -> List[str]:
stack = []
for letter in expression:
if letter.isalpha():
stack.append(set(letter))
elif letter == "{":
stack.append("{")
elif letter == ",":
stack.append(",")
elif letter == "}":
while stack[-2] == ",":
set2 = stack.pop()
stack.pop()
stack[-1].update(set2)
tail = stack.pop()
stack[-1] = tail
while (
len(stack) > 1
and isinstance(stack[-1], set)
and isinstance(stack[-2], set)
):
set2 = stack.pop()
set1 = stack.pop()
stack.append(set(w1 + w2 for w1 in set1 for w2 in set2))
return list(sorted(stack[-1])) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR |
Under a grammar given below, strings can represent a set of lowercase words. Let's use R(expr) to denote the set of words the expression represents.
Grammar can best be understood through simple examples:
Single letters represent a singleton set containing that word.
R("a") = {"a"}
R("w") = {"w"}
When we take a comma delimited list of 2 or more expressions, we take the union of possibilities.
R("{a,b,c}") = {"a","b","c"}
R("{{a,b},{b,c}}") = {"a","b","c"} (notice the final set only contains each word at most once)
When we concatenate two expressions, we take the set of possible concatenations between two words where the first word comes from the first expression and the second word comes from the second expression.
R("{a,b}{c,d}") = {"ac","ad","bc","bd"}
R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"}
Formally, the 3 rules for our grammar:
For every lowercase letter x, we have R(x) = {x}
For expressions e_1, e_2, ... , e_k with k >= 2, we have R({e_1,e_2,...}) = R(e_1) ∪ R(e_2) ∪ ...
For expressions e_1 and e_2, we have R(e_1 + e_2) = {a + b for (a, b) in R(e_1) × R(e_2)}, where + denotes concatenation, and × denotes the cartesian product.
Given an expression representing a set of words under the given grammar, return the sorted list of words that the expression represents.
Example 1:
Input: "{a,b}{c,{d,e}}"
Output: ["ac","ad","ae","bc","bd","be"]
Example 2:
Input: "{{a,z},a{b,c},{ab,z}}"
Output: ["a","ab","ac","z"]
Explanation: Each distinct word is written only once in the final answer.
Constraints:
1 <= expression.length <= 60
expression[i] consists of '{', '}', ','or lowercase English letters.
The given expression represents a set of words based on the grammar given in the description. | class Solution:
def braceExpansionII(self, expression: str) -> List[str]:
stack = []
prev = set()
curr = set()
for s in expression:
if s == "{":
stack.append(prev)
stack.append(curr)
curr = set()
prev = set()
elif s == "}":
k = stack.pop()
p = set()
for i in k or [""]:
for j in prev | curr:
p.add(i + j)
prev = stack.pop()
curr = p
elif s == ",":
prev |= curr
curr = set()
else:
p = set()
for c in curr or [""]:
p.add(c + s)
curr = p
return list(sorted(curr)) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR LIST STRING FOR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR LIST STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
A Hamiltonian path, is a path in an undirected graph that visits each vertex exactly once. Given an undirected graph, the task is to check if a Hamiltonian path is present in it or not.
Example 1:
Input:
N = 4, M = 4
Edges[][]= { {1,2}, {2,3}, {3,4}, {2,4} }
Output:
1
Explanation:
There is a hamiltonian path:
1 -> 2 -> 3 -> 4
Example 2:
Input:
N = 4, M = 3
Edges[][] = { {1,2}, {2,3}, {2,4} }
Output:
0
Explanation:
It can be proved that there is no
hamiltonian path in the given graph
Your task:
You don't need to read input or print anything. Your task is to complete the function check() which takes the N (the number of vertices), M (Number of edges) and the list of Edges[][] (where Edges[i] denotes the undirected Edge between vertices Edge[i][0] and Edges[i][1] ) as input parameter and returns true (boolean value) if the graph contains Hamiltonian path,otherwise returns false.
Expected Time Complexity: O(N!).
Expected Auxiliary Space: O(N+M).
Constraints:
1 ≤ N ≤ 10
1 ≤ M ≤ 15
Size of Edges[i] is 2
1 ≤ Edges[i][0],Edges[i][1] ≤ N | class Solution:
def check(self, N, M, Edges):
adj = [[] for i in range(N)]
for edge in Edges:
adj[edge[0] - 1].append(edge[1] - 1)
adj[edge[1] - 1].append(edge[0] - 1)
for node in range(N):
visited = set()
if self.check_util(N, M, adj, node, visited):
return True
return False
def check_util(self, N, M, adj, node, visited):
visited.add(node)
for child in adj[node]:
if child in visited:
if len(visited) == N:
return True
else:
continue
if self.check_util(N, M, adj, child, visited):
return True
if len(visited) == N:
return True
visited.remove(node)
return False | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER |
A Hamiltonian path, is a path in an undirected graph that visits each vertex exactly once. Given an undirected graph, the task is to check if a Hamiltonian path is present in it or not.
Example 1:
Input:
N = 4, M = 4
Edges[][]= { {1,2}, {2,3}, {3,4}, {2,4} }
Output:
1
Explanation:
There is a hamiltonian path:
1 -> 2 -> 3 -> 4
Example 2:
Input:
N = 4, M = 3
Edges[][] = { {1,2}, {2,3}, {2,4} }
Output:
0
Explanation:
It can be proved that there is no
hamiltonian path in the given graph
Your task:
You don't need to read input or print anything. Your task is to complete the function check() which takes the N (the number of vertices), M (Number of edges) and the list of Edges[][] (where Edges[i] denotes the undirected Edge between vertices Edge[i][0] and Edges[i][1] ) as input parameter and returns true (boolean value) if the graph contains Hamiltonian path,otherwise returns false.
Expected Time Complexity: O(N!).
Expected Auxiliary Space: O(N+M).
Constraints:
1 ≤ N ≤ 10
1 ≤ M ≤ 15
Size of Edges[i] is 2
1 ≤ Edges[i][0],Edges[i][1] ≤ N | class Solution:
def check(self, N, M, Edges):
adj = [[] for _ in range(N + 1)]
for i in range(M):
adj[Edges[i][0]].append(Edges[i][1])
adj[Edges[i][1]].append(Edges[i][0])
visit = [0] * (N + 1)
ans = [False] * 1
def fun(i, c):
if c == N - 1:
ans[0] = True
return
if ans[0] == True:
return
visit[i] = 1
for j in adj[i]:
if visit[j] == 0:
fun(j, c + 1)
visit[i] = 0
for i in range(1, N + 1):
fun(i, 0)
if ans[0] == True:
return True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER RETURN IF VAR NUMBER NUMBER RETURN ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Node:
def __init__(self, val):
self.data = val
self.left = None
self.right = None
class Solution:
def checktree(self, preorder, inorder, postorde, N):
def postorder(root):
if root is None:
return []
return postorder(root.left) + postorder(root.right) + [root.data]
def recursive(inorder, preorder, N, idx=0, left=0, right=N - 1):
if idx == N:
return None, idx
cur = preorder[idx]
root = None
for i in range(left, right + 1):
if inorder[i] == cur:
root = Node(cur)
break
if root:
root.left, idx = recursive(inorder, preorder, N, idx + 1, left, i - 1)
root.right, idx = recursive(inorder, preorder, N, idx, i + 1, right)
return root, idx
root, idx = recursive(inorder, preorder, N)
post = postorder(root)
if post == postorde:
return True
else:
return False | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST VAR FUNC_DEF NUMBER NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN NONE VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
class Solution:
def checktree(self, preorder, inorder, postorder, N):
def postOrder(root):
if root:
postOrder(root.left)
postOrder(root.right)
post.append(root.data)
def buildTree(inStart, inEnd, preStart, preEnd):
if inStart > inEnd or preStart > preEnd:
return None
root = Node(preorder[preStart])
inRoot = inMap[root.data]
inLeft = inRoot - inStart
root.left = buildTree(inStart, inRoot - 1, preStart + 1, preStart + inLeft)
root.right = buildTree(inRoot + 1, inEnd, preStart + inLeft + 1, preEnd)
return root
inMap = {}
for i in range(N):
inMap[inorder[i]] = i
try:
root = buildTree(0, N - 1, 0, N - 1)
except:
return False
post = []
postOrder(root)
for i in range(N):
if postorder[i] != post[i]:
return False
return True | CLASS_DEF FUNC_DEF NONE NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Node:
def __init__(self, d):
self.data = d
self.left = None
self.right = None
class Solution:
def checktree(self, preorder, inorder, postorder, N):
m = {}
for i in range(N):
m[inorder[i]] = i
l = [0]
def construct(st, end):
if st > end:
return None
node = Node(preorder[l[0]])
pos = m[preorder[l[0]]]
l[0] += 1
try:
node.left = construct(st, pos - 1)
node.right = construct(pos + 1, end)
except:
return None
return node
root = construct(0, N - 1)
l = []
def post(r):
if r:
post(r.left)
post(r.right)
l.append(r.data)
post(root)
return l == postorder | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Node:
def __init__(self, val):
self.val = val
self.left = self.right = None
class Solution:
def build(self, pre, ino):
tem = [True]
n = len(pre)
def rec(st, en, n):
if st > en or ind[0] >= n or tem[0] == None:
return
nod = Node(pre[ind[0]])
ind[0] += 1
idx = st - 1
for i in range(st, en + 1):
if ino[i] == nod.val:
idx = i
break
if idx == st - 1:
tem[0] = None
return
nod.left = rec(st, idx - 1, n)
nod.right = rec(idx + 1, en, n)
return nod
ind = [0]
root = Node(pre[0])
ind[0] += 1
idx = inorder.index(root.val)
root.left = rec(0, idx - 1, n)
root.right = rec(idx + 1, n - 1, n)
if tem[0] == None:
return tem[0]
return root
def checktree(self, preorder, inorder, postorder, N):
root = self.build(preorder, inorder)
if root is None:
return False
ind = [0, True]
def rec(nod, n):
if nod is None or ind[1] == False:
return
if nod is not None and ind[0] >= n:
ind[1] = False
return
rec(nod.left, n)
rec(nod.right, n)
if postorder[ind[0]] != nod.val:
ind[1] = False
return
ind[0] += 1
rec(root, N)
return ind[1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR NUMBER NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER NONE RETURN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER FUNC_DEF IF VAR NONE VAR NUMBER NUMBER RETURN IF VAR NONE VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Solution:
def checktree(self, preorder, inorder, postorder, N):
if N == 0:
return True
if N == 1:
return preorder[0] == postorder[0] and postorder[0] == inorder[0]
root = preorder[0]
if root != postorder[N - 1]:
return False
if root not in set(inorder):
return False
else:
index = inorder.index(root)
left = index
right = N - index - 1
return self.checktree(
preorder[1 : left + 1], inorder[:index], postorder[:left], left
) and self.checktree(
preorder[left + 1 :],
inorder[index + 1 :],
postorder[left : N - 1],
right,
) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Solution:
def checktree(self, preorder, inorder, postorder, N):
if N == 0:
return 1
if N == 1:
return preorder[0] == inorder[0] and inorder[0] == postorder[0]
idx = -1
for i in range(N):
if inorder[i] == preorder[0]:
idx = i
break
if idx == -1:
return 0
if preorder[0] != postorder[N - 1]:
return 0
ret1 = self.checktree(preorder[1:], inorder, postorder, idx)
ret2 = self.checktree(
preorder[idx + 1 :], inorder[idx + 1 :], postorder[idx:], N - idx - 1
)
return ret1 and ret2 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Node:
def __init__(self, val):
self.data = val
self.left = None
self.right = None
class Solution:
def checktree(self, preorder, inorder, postorder, N):
inorder_map = {inorder[i]: i for i in range(len(inorder))}
ind = [0]
root = build(preorder, ind, 0, len(inorder) - 1, inorder_map)
res = [True]
ind = [0]
check_same(root, postorder, ind, res)
traverse_to_end = ind[0] == len(postorder)
return res[0] and traverse_to_end
def build(preorder, ind, inorder_l, inorder_h, inorder_map):
if ind[0] == len(preorder):
return None
val = preorder[ind[0]]
inorder_i = inorder_map[val]
if inorder_i < inorder_l or inorder_i > inorder_h:
return None
cur = Node(val)
ind[0] += 1
cur.left = build(preorder, ind, inorder_l, inorder_i - 1, inorder_map)
cur.right = build(preorder, ind, inorder_i + 1, inorder_h, inorder_map)
return cur
def check_same(cur, postorder, ind, res):
if not cur:
return
check_same(cur.left, postorder, ind, res)
check_same(cur.right, postorder, ind, res)
if ind[0] == len(postorder):
res[0] = False
return
val = postorder[ind[0]]
ind[0] += 1
if cur.data != val:
res[0] = False
return
return True | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR RETURN VAR NUMBER VAR FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER RETURN ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER NUMBER RETURN RETURN NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Solution:
def checktree(self, preorder, inorder, postorder, N):
if N == 0:
return True
if preorder[0] != postorder[-1]:
return False
root = preorder[0]
if root not in inorder:
return False
else:
p = inorder.index(preorder[0])
lt = self.checktree(preorder[1 : p + 1], inorder[:p], postorder[:p], p)
rt = self.checktree(
preorder[p + 1 :], inorder[p + 1 :], postorder[p : N - 1], N - p - 1
)
return lt & rt | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Solution:
def checktreeutil(self, preorder, inorder, postorder):
if len(preorder) >= 1 and len(postorder) >= 1 and len(inorder) >= 1:
if len(preorder) != len(postorder):
return False
temp = -10000000
for i in range(len(inorder)):
if inorder[i] == preorder[0]:
temp = i
if temp == -10000000:
return False
if len(preorder) != 0 and len(postorder) != 0:
if preorder[0] != postorder[len(postorder) - 1]:
return False
return self.checktreeutil(
preorder[1 : 1 + temp], inorder[0:temp], postorder[0:temp]
) and self.checktreeutil(
preorder[temp + 1 :],
inorder[temp + 1 :],
postorder[temp : len(postorder) - 1],
)
return True
def checktree(self, preorder, inorder, postorder, N):
return self.checktreeutil(preorder, inorder, postorder) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class Solution:
def post(self, root, crr):
if root == None:
return
self.post(root.left, crr)
self.post(root.right, crr)
crr.append(root.data)
return crr
def solve(self, pre, ino, N, arr, s, e, hm):
if arr[0] >= N or s > e:
return None
el = pre[arr[0]]
root = Node(el)
arr[0] += 1
pos = hm[el] - 1
root.left = self.solve(pre, ino, N, arr, s, pos - 1, hm)
root.right = self.solve(pre, ino, N, arr, pos + 1, e, hm)
return root
def checktree(self, preorder, inorder, postorder, N):
hm = {}
for i in range(N):
hm[inorder[i]] = i + 1
arr = [0]
s = 0
e = N - 1
root = self.solve(preorder, inorder, N, arr, s, e, hm)
crr = []
crr = self.post(root, crr)
if crr == postorder:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR RETURN NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Solution:
def checktree(self, preorder, inorder, postorder, N):
def f(inord, pre, st, en, i, n, d, arr):
if st > en or i[0] >= n:
return
pos = d[pre[i[0]]]
val = pre[i[0]]
i[0] += 1
f(inord, pre, st, pos - 1, i, n, d, arr)
f(inord, pre, pos + 1, en, i, n, d, arr)
arr += [val]
ind = [0]
d = {}
for i in range(N):
d[inorder[i]] = i
arr = []
f(inorder, preorder, 0, N - 1, ind, N, d, arr)
for i in range(N):
if arr[i] != postorder[i]:
return False
return True | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR NUMBER VAR RETURN ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR LIST VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Solution:
def checktree(self, preorder, inorder, postorder, N):
if not preorder and not inorder and not postorder:
return True
if (
len(preorder) != len(inorder)
or len(inorder) != len(postorder)
or len(postorder) != len(preorder)
):
return False
preorderRoot = preorder[0]
postorderRoot = postorder[-1]
if preorderRoot != postorderRoot:
return False
try:
rootIndex = inorder.index(preorderRoot)
except ValueError:
return False
leftsubTree = self.checktree(
preorder[1 : rootIndex + 1], inorder[:rootIndex], postorder[:rootIndex], N
)
rightsubTree = self.checktree(
preorder[rootIndex + 1 :],
inorder[rootIndex + 1 :],
postorder[rootIndex:-1],
N,
)
return leftsubTree and rightsubTree | CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR RETURN VAR VAR |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Node:
def __init__(self, data):
self.left = None
self.data = data
self.right = None
class Solution:
def Search(self, start, end, inorder, val):
for i in range(start, end + 1):
if inorder[i] == val:
return i
return -1
def ConstructTree(self, start, end, preindx, N, preorder, inorder, ans):
if start > end:
return None
if preindx[0] >= N:
return None
root = Node(preorder[preindx[0]])
indx = self.Search(start, end, inorder, preorder[preindx[0]])
if indx == -1:
ans[0] = False
preindx[0] += 1
root.left = self.ConstructTree(
start, indx - 1, preindx, N, preorder, inorder, ans
)
root.right = self.ConstructTree(
indx + 1, end, preindx, N, preorder, inorder, ans
)
return root
def Postorder(self, root, postindx, N, postorder, ans):
if root == None:
return
if postindx[0] >= N:
return
self.Postorder(root.left, postindx, N, postorder, ans)
self.Postorder(root.right, postindx, N, postorder, ans)
if root.data != postorder[postindx[0]]:
ans[0] = False
postindx[0] += 1
def checktree(self, preorder, inorder, postorder, N):
preindx = [0]
ans = [True]
root = self.ConstructTree(0, N - 1, preindx, N, preorder, inorder, ans)
if ans[0] == False:
return False
postindx = [0]
self.Postorder(root, postindx, N, postorder, ans)
return ans[0] | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NONE IF VAR NUMBER VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN IF VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Tree:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class Solution:
def checktree(self, preorder, inorder, postorder, N):
flag = [False]
def rec1(a, b):
if flag[0]:
return None
if not a:
return None
ele = b.pop(0)
if ele not in a:
flag[0] = True
return None
ind = a.index(ele)
node = Tree(ele)
node.left = rec1(a[:ind], b)
node.right = rec1(a[ind + 1 :], b)
return node
def rec2(a, b):
if flag[0]:
return None
if not a:
return None
ele = b.pop(0)
if ele not in a:
flag[0] = True
return None
ind = a.index(ele)
node = Tree(ele)
node.right = rec2(a[ind + 1 :], b)
node.left = rec2(a[:ind], b)
return node
root1 = rec1(inorder, preorder)
root2 = rec2(inorder, postorder[::-1])
def identicalTrees(a, b):
if a is None and b is None:
return True
if a is not None and b is not None:
return (
a.data == b.data
and identicalTrees(a.left, b.left)
and identicalTrees(a.right, b.right)
)
return False
if identicalTrees(root1, root2) and not flag[0]:
return True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NUMBER RETURN NONE IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NONE IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR NONE VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class Solution:
def check(self, pre, l1, r1, ino, l2, r2, post, l3, r3):
if l2 > r2:
return True
if pre[l1] != post[r3]:
return False
try:
idx = ino.index(pre[l1])
except ValueError:
return False
return self.check(
pre, l1 + 1, idx - l2 + l1, ino, l2, idx - 1, post, l3, l3 + idx - l2 - 1
) and self.check(
pre, r1 + 1 - r2 + idx, r1, ino, idx + 1, r2, post, r3 + idx - r2, r3 - 1
)
def checktree(self, preorder, inorder, postorder, N):
return self.check(preorder, 0, N - 1, inorder, 0, N - 1, postorder, 0, N - 1) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER |
Given Preorder, Inorder and Postorder traversals of some tree of size N. The task is to check if they are all of the same tree or not.
Example 1:
Input:
N = 5
preorder[] = {1, 2, 4, 5, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 5, 2, 3, 1}
Output: Yes
Explanation:
All of the above three traversals
are of the same tree.
1
/ \
2 3
/ \
4 5
Example 2:
Input:
N = 5
preorder[] = {1, 5, 4, 2, 3}
inorder[] = {4, 2, 5, 1, 3}
postorder[] = {4, 1, 2, 3, 5}
Output: No
Explanation: The three traversals can
not be of the same tree.
Your Task:
You don't need to read input or print anything. Complete the function checktree() which takes the array preorder[ ], inorder[ ], postorder[ ] and integer N as input parameters and returns true if the three traversals are of the same tree or not.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{3}
Node values are unique. | class node:
def __init__(self, v):
self.val = v
self.left = None
self.right = None
class Solution:
def checktree(self, pr, io, po, n):
valid = True
def f(pr, io):
nonlocal valid
if not valid:
return
if len(pr) == 0 and len(io) == 0:
return
if len(pr) != len(io) or pr[0] not in io:
valid = False
return
if len(pr) == 1:
z.append(pr[0])
return
i = io.index(pr[0])
if len(pr) > 1:
f(pr[1 : i + 1], io[:i])
f(pr[i + 1 :], io[i + 1 :])
z.append(pr[0])
z = []
f(pr, io)
return z == po | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER RETURN IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | matches = 0
def match_counter(hour, mins, matches):
for m in range(mins):
if m == hour and str(m)[0] == str(hour)[-1] or m * 11 == hour:
matches += 1
elif m == hour * 11:
matches += 1
break
return matches
def hours_counters(hours, mins, matches):
for h in range(hours):
matches = match_counter(h, mins, matches)
return matches
for test in range(int(input())):
hours, mins = input().split(" ")
print(hours_counters(int(hours), int(mins), matches)) | ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
return input().strip()
def invr():
return map(int, input().split())
def outp(n):
sys.stdout.write(str(n) + "\n")
def outlt(lst):
sys.stdout.write(" ".join(map(str, lst)) + "\n")
def outplt(lst):
sys.stdout.write("\n".join(map(str, lst)))
def outpltlt(lst):
sys.stdout.write("\n".join(map(str, (" ".join(map(str, a)) for a in lst))))
ans = []
for _ in range(inp()):
H, M = inlt()
an = 0
for i in range(10):
if i < H and i < M:
an += 1
else:
break
for i in range(1, 10):
if i < H and 11 * i < M:
an += 1
else:
break
for i in range(1, 10):
if 11 * i < H and i < M:
an += 1
else:
break
for i in range(1, 10):
if 11 * i < H and 11 * i < M:
an += 1
else:
break
ans.append(an)
outplt(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
for i in range(t):
time = list(map(int, input().split()))
hours = time[0]
minutes = time[1]
clock = 0
for i in range(hours):
for j in range(minutes):
x = str(i) + str(j)
if x.count(x[0]) == len(x):
clock += 1
print(clock) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
for i in range(t):
n, m = map(int, input().split())
s = min(n, m)
ans = 0
if s > 9:
ans += 10
else:
ans += s
for j in range(1, n):
if j * 11 < m:
ans += 1
for k in range(1, m):
if k * 11 < n:
ans += 1
a = int((n - 1) / 11)
b = int((m - 1) / 11)
g = min(a, b)
ans += g
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for _ in range(int(input())):
hour, minute = map(int, input().split())
count = 0
for h in range(hour):
for m in range(minute):
time = str(h) + str(m)
if time.count(time[0]) == len(time):
count = count + 1
print(count) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for _ in range(int(input())):
h, m = map(int, input().split())
coun = 0
for i in range(h):
for j in range(m):
a, b = [], []
a.extend(str(i))
b.extend(str(j))
if set(a) == set(b) and len(set(a)) == 1:
coun += 1
print(coun) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for _ in range(int(input())):
h, m = map(int, input().split())
ele_cnt = 0
for i in range(0, h):
for j in range(0, m):
x = str(i)
y = str(j)
time = x + ":" + y
if time.count(time[0]) == len(time) - 1:
ele_cnt += 1
print(ele_cnt) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | def identical(h, m):
ans = 0
for hr in range(h):
for min in range(m):
if hr < 10 and min < 10:
if hr == min:
ans += 1
elif hr < 10 and min >= 10:
if hr * 11 == min:
ans += 1
elif hr >= 10 and min < 10:
if hr == min * 11:
ans += 1
elif hr % 11 == 0 and min % 11 == 0:
if hr == min:
ans += 1
return ans
T = int(input())
while T != 0:
H, M = map(int, input().split())
print(identical(H, M))
T -= 1 | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | fine = [11, 22, 33, 44, 55, 66, 77, 88, 99]
t = int(input())
for _ in range(t):
l = input().split()
h = int(l[0])
m = int(l[1])
th = list()
tm = list()
for i in range(h):
if i <= 9:
th.append(i)
elif i in fine:
th.append(i)
for i in range(m):
if i <= 9:
tm.append(i)
elif i in fine:
tm.append(i)
c = 0
f = True
for i in th:
f = True
for j in tm:
f = True
s = str(i) + str(j)
for k in range(len(s) - 1):
if not s[k] == s[k + 1]:
f = False
break
if f == True:
c = c + 1
print(c) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | def reloj(hora=0, minuto=0):
arr_str = []
if hora > 100:
hora = 100
if minuto > 100:
minuto = 100
for i in range(hora):
for j in range(minuto):
arr_str.append(f"{i}:{j}")
c = 0
arr_2 = []
for i in arr_str:
if len(set(i)) == 2:
c += 1
return c
tam = int(input())
arr = []
for i in range(tam):
arr_1 = input().split()
mapeo = map(int, arr_1)
arr.append(list(mapeo))
for i in arr:
print(reloj(i[0], i[1])) | FUNC_DEF NUMBER NUMBER ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | tests = int(input())
for i in range(tests):
h, m = map(int, input().split())
ans = 1
for j in range(1, h):
if j < 10 or j % 11 == 0:
num = j % 10
if num < m:
ans += 1
count = 1
while num * (10**count + 1) < m:
ans += 1
count += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | def getCount(h, m, i):
h = int(h)
m = int(m)
h1 = 0
lst = [11, 22, 33, 44, 55, 66, 77, 88, 99]
while h1 < h:
for m1 in range(0, m):
if h1 < 10:
if m1 < 10 and h1 == m1:
count[i] += 1
if m1 in lst and m1 % 10 == h1:
count[i] += 1
else:
if m1 in lst and h1 == m1:
count[i] += 1
if h1 in lst and h1 % 10 == m1:
count[i] += 1
h1 += 1
t = int(input())
count = [0] * t
for i in range(0, t):
h, m = input().split()
getCount(h, m, i)
for i in count:
print(i) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | test = int(input())
for _ in range(test):
a, b = map(int, input().split())
c = 0
for j in range(0, a):
for k in range(0, b):
if len(list(set(str(j)))) == 1 and len(list(set(str(k)))) == 1:
l = str(j) + str(k)
if l == l[::-1]:
c = c + 1
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
h = []
m = []
for i in range(t):
value = input()
inputs = value.split(" ")
h.append(int(inputs[0]))
m.append(int(inputs[1]))
def dig(h, m):
count = 0
hou = ""
mins = ""
for i in range(0, h):
hou = str(i)
for j in range(0, m):
mins = str(j)
temp = hou + mins
if len(set(temp)) == 1:
count += 1
return count
for i in range(t):
print(dig(h[i], m[i])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
for i in range(t):
hours, minutes = map(int, input().split())
count = 1
i = 1
while i <= 9 and i < hours:
if i < minutes:
count += 1
if i * 10 + i < minutes:
count += 1
i += 1
i = 11
while i <= 99 and i < hours:
if i < minutes:
count += 1
if i % 10 < minutes:
count += 1
i += 11
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
for _ in range(t):
h, m = map(int, input().split())
cnt = 0
for i in range(h):
for j in range(m):
cnt += 1
s = str(i) + str(j)
for k in s:
if k != s[0]:
cnt -= 1
break
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
for _ in range(t):
h, m = map(int, input().split())
count = 0
for i in range(0, h):
for j in range(0, m):
if len(str(i)) < 2 and len(str(j)) < 2:
if i == j:
count += 1
elif len(str(i)) < 2 and len(str(j)) == 2:
j = str(j)
if j[0] == j[1] and int(j[0]) == i:
count += 1
elif len(str(i)) == 2 and len(str(j)) < 2:
i = str(i)
if i[0] == i[1] and int(i[0]) == j:
count += 1
elif len(str(i)) == 2 and len(str(j)) == 2:
j = str(j)
i = str(i)
if j[0] == j[1] and i[0] == i[1] and int(j[0]) == int(i[0]):
count += 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for _ in range(int(input())):
n, m = map(int, input().split())
count = 0
for i in range(n):
i = str(i)
if len(i) == 1 or i[0] == i[1]:
for j in range(m):
j = str(j)
if len(j) == 1 or j[0] == j[1]:
if i[0] == j[0]:
count = count + 1
print(count) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
for _ in range(t):
h, m = [int(i) for i in input().split()]
iden_time = 0
for i in range(h):
if i < 10:
min = i
if min < m:
iden_time += 1
if i > 0:
min = i * 10 + i
if min < m:
iden_time += 1
if i > 10 and i % 10 == i // 10:
min = i
if min < m:
iden_time += 1
min = i % 10
if min < m:
iden_time += 1
print(iden_time) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
while t > 0:
h, m = map(int, input().split())
a = 0
for i in range(h):
if i <= 9 or i % 10 == i // 10:
for j in range(m):
if j <= 9 or j % 10 == j // 10:
if j % 10 == i % 10:
a += 1
print(a)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | times = [(0, 0)]
h = m = 1
while h < 10:
times.append((h, m))
times.append((h * 11, m))
times.append((h, m * 11))
times.append((h * 11, m * 11))
h = h + 1
m = h
t = int(input())
while t:
h, m = [int(i) for i in input().split()]
num = 0
for i in times:
if i[0] < h and i[1] < m:
num = num + 1
print(num)
t = t - 1 | ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for _ in range(int(input())):
h, m = map(int, input().split())
H = h - 1
M = m - 1
print(min(M, H, 9) + 1 + min(H, M // 11) + min(M, H // 11) + min(H // 11, M // 11)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for _ in range(int(input())):
count = 0
HM = list(map(int, input().split()))
for i in range(HM[0]):
if i <= 9 or i % 11 == 0:
for j in range(HM[1]):
if j <= 9 and i % 11 == j != 0:
count += 1
elif j / 11 == i != 0 or i / 11 == j != 0:
count += 1
elif j == i:
count += 1
print(count) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | def is_ident(hr, min):
quer = str(hr) + str(min)
for i in quer:
if i == quer[0]:
pass
else:
return False
return True
def main():
t = int(input())
while t > 0:
hr, min = map(int, input().split())
count = 0
for x in range(hr):
for i in range(min):
if is_ident(x, i):
count += 1
print(count)
t -= 1
main() | FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
while t:
h, m = map(int, input().strip().split())
i = 0
j = 0
f = 0
c = 0
for i in range(h):
if i < 10 or i % 11 == 0:
for j in range(m):
if (j < 10 or j % 11 == 0) and str(i)[0] == str(j)[0]:
f = 1
c += f
f = 0
print(c)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
if 1 <= t <= 50:
for _ in range(t):
h, m = map(int, input().split())
if 1 <= h <= 100 and 1 <= m <= 100:
i = 1
c = 1
while i <= 9:
if 0 <= i <= h - 1 and 0 <= 11 * i <= m - 1:
c = c + 1
if 0 <= 11 * i <= h - 1 and 0 <= i <= m - 1:
c = c + 1
if 0 <= i <= h - 1 and 0 <= i <= m - 1:
c = c + 1
if 0 <= 11 * i <= h - 1 and 0 <= 11 * i <= m - 1:
c = c + 1
i = i + 1
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
while t:
h, m = map(int, input().split())
i = 1
count = 0
while i <= 9 and i <= h - 1:
if i <= m - 1:
count = count + 1
if i * 11 <= m - 1:
count = count + 1
if i * 11 <= h - 1 and i <= m - 1:
count = count + 1
if i * 11 <= h - 1 and i * 11 <= m - 1:
count = count + 1
i = i + 1
t = t - 1
count = count + 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | T = int(input())
while T:
hour, mins = [int(x) for x in input().split()]
count = 0
for i in range(hour):
for j in range(mins):
if len(set(str(i) + str(j))) == 1:
count += 1
print(count)
T -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | t = int(input())
for inh in range(0, t):
h, m = map(int, input().split())
ans = 1
uni, bi = 1, 11
while uni <= 9:
a, b = 0, 0
if uni < h:
a += 1
if bi < h:
a += 1
if uni < m:
b += 1
if bi < m:
b += 1
if a == 0 and b == 0:
break
else:
z = a * b
ans += z
uni += 1
bi += 11
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for _ in range(int(input())):
h, m = map(int, input().split())
def fun(v):
v = str(v)
d = []
if len(v) == 1:
d.append(int(v[0]))
else:
if int(v[0]) <= int(v[1]):
d.append(int(v[0]))
else:
d.append(int(v[0]) - 1)
for i in range(1, len(v)):
d.append(9)
return d
H = fun(h - 1)
M = fun(m - 1)
total = (
min(H[0], M[0])
+ M[0] * (len(H) - 1)
+ H[0] * (len(M) - 1)
+ (len(H) - 1) * (len(M) - 1) * 9
)
print(total + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | n = int(input())
while n > 0:
h, m = map(int, input().split())
count = 0
for i in range(h):
for j in range(m):
x = set(list(str(i)))
y = set(list(str(j)))
if len(x) == 1 and len(y) == 1 and x == y:
count += 1
print(count)
n -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for t in range(int(input())):
h, m = [int(x) for x in input().split(" ")]
count = 1
for i in range(1, h):
if i % 11 == 0 or i < 10:
temp = i % 10
while temp < m:
count += 1
temp = temp * 10 + i % 10
print(count) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for _ in range(int(input())):
h, m = map(int, input().split())
ans = min(10, min(h, m))
if m % 11 == 0:
ans += min(m // 11 - 1, h - 1)
else:
ans += min(m // 11, h - 1)
if h % 11 == 0:
for i in range(1, h // 11):
if 11 * i < m:
ans += 2
elif i < m:
ans += 1
else:
break
else:
for i in range(1, h // 11 + 1):
if 11 * i < m:
ans += 2
elif i < m:
ans += 1
else:
break
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | for _ in range(int(input())):
H, M = map(int, input().split())
count = 0
for i in range(H):
for j in range(M):
if i <= 9 and j <= 9 and i == j:
count += 1
elif i <= 9 and j > 10:
x = j % 10
y = j // 10
if i == x and i == y:
count += 1
elif i > 10 and j <= 9:
x = i % 10
y = i // 10
if x == j and y == j:
count += 1
elif i > 10 and j > 10:
a = i % 10
b = i // 10
c = j % 10
d = j // 10
if a == b == c == d:
count += 1
print(count) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
3:33
It's possible for all the digits displayed on a digital clock in the hours:minutes format to be identical. The time shown above (3:33) is an example of such a situation. Other examples are 2:2 and 1:11. Note that the digits of 33:33 are identical, but it is not a valid time on a usual digital clock.
The above example was for a usual 24-hour format digital clock. Let's consider a more general clock, where an hour lasts M minutes and a day lasts H hours (therefore, the clock can show any number of hours between 0 and H-1, inclusive, and any number of minutes between 0 and M-1, inclusive). Both the hours and the minutes are shown without leading zeroes in decimal notation and their separator (e.g., ':') doesn't matter.
Can you tell how many minutes during a day will the digital clock have identical digits displayed on it?
------ Input ------
The first line of the input contains an integer T - the number of test cases.
Each of the next T lines contains two space-separated integers H and M for one test case.
------ Output ------
For each test case, output a single line corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 50$
$1 ≤ H, M ≤ 100$
----- Sample Input 1 ------
6
24 60
34 50
10 11
10 12
11 11
1 1
----- Sample Output 1 ------
19
20
10
11
10
1
----- explanation 1 ------
Example case 1. A clock shows two identical digits at times 0:0, 1:1, .., 9:9, three identical digits at times 11:1, 22:2, 1:11, 2:22, 3:33, 4:44, 5:55, and four identical digits at times 11:11 and 22:22. So, there are 19 minutes during which the time displayed by the clock will have identical digits.
Example case 2. Compared to the previous case, the clock doesn't show 5:55, but can show 33:3 and 33:33.
Example case 6. In this example, our day consists of one hour and one hour consists of one minute. Hence, the entire day is just 1 minute - the only time the digital clock will display is 0:0 during the entire day, (i.e. the entire hour, i.e. entire minute). And 0:0 has all digits identical, so the answer is 1. | case = int(input())
for i in range(case):
h, m = map(int, input().split(" "))
h = h - 1
m = m - 1
hl = len(str(h))
ml = len(str(m))
final_sum = 1
for i in range(1, 10):
ans = ""
n = hl
while n > 0:
ans = ans + str(i)
n -= 1
if int(ans) <= h:
hrs = len(ans)
else:
hrs = len(ans) - 1
n = ml
ans = ""
while n > 0:
ans = ans + str(i)
n -= 1
if int(ans) <= m:
minu = len(ans)
else:
minu = len(ans) - 1
final_sum += hrs * minu
print(final_sum) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
Empty cells are indicated by the character '.'.
A sudoku puzzle...
...and its solution numbers marked in red.
Note:
The given board contain only digits 1-9 and the character '.'.
You may assume that the given Sudoku puzzle will have a single unique solution.
The given board size is always 9x9. | class Solution:
full_values = set("123456789")
def findValues(self, board, row, col):
row_start = row // 3 * 3
col_start = col // 3 * 3
values = self.full_values - (
set(board[row])
| {board[i][col] for i in range(9)}
| {
board[i][j]
for i in range(row_start, row_start + 3)
for j in range(col_start, col_start + 3)
}
)
return values
def solver(self, board):
done = True
for row in board:
if "." in row:
done = False
if done:
return True
row_num = len(board)
col_num = len(board[0])
candidates = [[(0) for x in range(row_num)] for y in range(col_num)]
NumOfCandidatesDict = [[] for i in range(9)]
for row in range(row_num):
for col in range(col_num):
if board[row][col] == ".":
candidates[row][col] = self.findValues(board, row, col)
if len(candidates[row][col]) == 0:
return False
elif len(candidates[row][col]) == 1:
board[row][col] = candidates[row][col].pop()
ret = self.solver(board)
if ret:
return True
else:
board[row][col] = "."
return False
else:
NumOfCandidatesDict[len(candidates[row][col])].append(
[row, col]
)
for i in range(2, 10):
if len(NumOfCandidatesDict[i]) > 0:
row, col = NumOfCandidatesDict[i][0]
for value in candidates[row][col]:
board[row][col] = value
if self.solver(board):
return True
board[row][col] = "."
return False
return False
def solveSudoku(self, board):
self.solver(board) | CLASS_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF STRING VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR STRING RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR |
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
Empty cells are indicated by the character '.'.
A sudoku puzzle...
...and its solution numbers marked in red.
Note:
The given board contain only digits 1-9 and the character '.'.
You may assume that the given Sudoku puzzle will have a single unique solution.
The given board size is always 9x9. | class Solution(object):
def __init__(self):
self.filled = []
def solveSudoku(self, board):
pln = []
for i in range(9):
pln.append([])
self.filled.append([])
for j in range(9):
if board[i][j].isdigit():
self.filled[-1].append(True)
pln[i].append([])
else:
self.filled[-1].append(False)
pln[i].append([k for k in range(10)])
pln[i][-1].remove(0)
for i in range(9):
for j in range(9):
if board[i][j].isdigit():
self.updatePln(pln, int(board[i][j]), i, j)
result = self.loopSolution(pln, board)
return
def updatePln(self, pln, temp, i, j):
for k in range(9):
a = pln[i][k]
if temp in pln[i][k]:
pln[i][k].remove(temp)
if temp in pln[k][j]:
pln[k][j].remove(temp)
num = int(i / 3) * 3 + int(j / 3)
if temp in pln[int(num / 3) * 3 + int(k / 3)][num % 3 * 3 + k % 3]:
pln[int(num / 3) * 3 + int(k / 3)][num % 3 * 3 + k % 3].remove(temp)
def loopSolution(self, pln, board):
while True:
row, column, minSize = 10, 10, 10
for i in range(9):
for j in range(9):
if not self.filled[i][j] and len(pln[i][j]) == 0:
return False
if len(pln[i][j]) > 0 and len(pln[i][j]) < minSize:
row, column = i, j
minSize = len(pln[i][j])
if minSize == 10:
return True
else:
for i in range(minSize):
self.filled[row][column] = True
temp_pln = []
for ele in pln:
temp_pln.append([])
for ele2 in ele:
temp_pln[-1].append(ele2[:])
self.updatePln(temp_pln, temp_pln[row][column][i], row, column)
temp_pln[row][column] = []
if self.loopSolution(temp_pln, board):
board[row][column] = str(pln[row][column][i])
return True
self.filled[row][column] = False
return False | CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF WHILE NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER |
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
Empty cells are indicated by the character '.'.
A sudoku puzzle...
...and its solution numbers marked in red.
Note:
The given board contain only digits 1-9 and the character '.'.
You may assume that the given Sudoku puzzle will have a single unique solution.
The given board size is always 9x9. | class Solution:
def reduceSingleOption(self, board, emptyGrid, valid):
m = 1
while m == 1:
m = 10
for k in range(len(emptyGrid) - 1, -1, -1):
i, j = emptyGrid[k]
option = set.intersection(
valid[i], valid[j + 9], valid[i // 3 * 3 + j // 3 + 18]
)
if not option:
return
if len(option) < m:
m = len(option)
if len(option) == 1:
option = option.pop()
board[i][j] = str(option)
emptyGrid.pop(k)
valid[i].remove(option)
valid[j + 9].remove(option)
valid[i // 3 * 3 + j // 3 + 18].remove(option)
def dfs(self, board, emptyGrid, valid, ans):
if len(ans) == len(emptyGrid):
return
i, j = emptyGrid[len(ans)]
options = set.intersection(
valid[i], valid[j + 9], valid[i // 3 * 3 + j // 3 + 18]
)
if len(options) == 0:
return
else:
for temp in options:
print(i, j, temp)
ans.append(temp)
valid[i].remove(temp)
valid[j + 9].remove(temp)
valid[i // 3 * 3 + j // 3 + 18].remove(temp)
self.dfs(board, emptyGrid, valid, ans)
if len(ans) == len(emptyGrid):
break
temp = ans.pop()
valid[i].add(temp)
valid[j + 9].add(temp)
valid[i // 3 * 3 + j // 3 + 18].add(temp)
def solveSudoku(self, board):
valid = [set(range(1, 10)) for _ in range(27)]
emptyGrid = []
for i in range(9):
for j in range(9):
c = board[i][j]
if c != ".":
n = int(c)
valid[i].remove(n)
valid[j + 9].remove(n)
valid[i // 3 * 3 + j // 3 + 18].remove(n)
else:
emptyGrid.append((i, j))
self.reduceSingleOption(board, emptyGrid, valid)
ans = []
self.dfs(board, emptyGrid, valid, ans)
print(ans)
for k in range(len(emptyGrid)):
i, j = emptyGrid[k]
board[i][j] = str(ans[k]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR RETURN IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR |
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
Empty cells are indicated by the character '.'.
A sudoku puzzle...
...and its solution numbers marked in red.
Note:
The given board contain only digits 1-9 and the character '.'.
You may assume that the given Sudoku puzzle will have a single unique solution.
The given board size is always 9x9. | class Solution:
def FindValid(self):
a = "123456789"
d, val = {}, {}
for i in range(9):
for j in range(9):
temp = self.board[i][j]
if temp != ".":
d["r", i] = d.get(("r", i), []) + [temp]
d["c", j] = d.get(("c", j), []) + [temp]
d[i // 3, j // 3] = d.get((i // 3, j // 3), []) + [temp]
else:
val[i, j] = []
for i, j in list(val.keys()):
invalid = (
d.get(("r", i), []) + d.get(("c", j), []) + d.get((i // 3, j // 3), [])
)
val[i, j] = [ele for ele in a if ele not in invalid]
return val
def CheckingSolution(self, ele, Pos, Updata):
self.board[Pos[0]][Pos[1]] = ele
del self.val[Pos]
i, j = Pos
for invalid in list(self.val.keys()):
if ele in self.val[invalid]:
if (
invalid[0] == i
or invalid[1] == j
or (invalid[0] // 3, invalid[1] // 3) == (i // 3, j // 3)
):
Updata[invalid] = ele
self.val[invalid].remove(ele)
if len(self.val[invalid]) == 0:
return False
return True
def Sudo(self, Pos, Updata):
self.board[Pos[0]][Pos[1]] = "."
for i in Updata:
if i not in self.val:
self.val[i] = Updata[i]
else:
self.val[i].append(Updata[i])
def FindSolution(self):
if len(self.val) == 0:
return True
Pos = min(list(self.val.keys()), key=lambda x: len(self.val[x]))
nums = self.val[Pos]
for ele in nums:
updata = {Pos: self.val[Pos]}
if self.CheckingSolution(ele, Pos, updata):
if self.FindSolution():
return True
self.Sudo(Pos, updata)
return False
def solveSudoku(self, board):
self.board = board
self.val = self.FindValid()
self.FindSolution() | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR STRING VAR BIN_OP FUNC_CALL VAR STRING VAR LIST LIST VAR ASSIGN VAR STRING VAR BIN_OP FUNC_CALL VAR STRING VAR LIST LIST VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST LIST VAR ASSIGN VAR VAR VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR LIST FUNC_CALL VAR STRING VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR DICT VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
Empty cells are indicated by the character '.'.
A sudoku puzzle...
...and its solution numbers marked in red.
Note:
The given board contain only digits 1-9 and the character '.'.
You may assume that the given Sudoku puzzle will have a single unique solution.
The given board size is always 9x9. | class Solution:
def solveSudoku(self, board):
self.board = board
self.val = self.PossibleValue()
self.Solver()
def PossibleValue(self):
nums = "123456789"
d, val = {}, {}
for i in range(9):
for j in range(9):
element = self.board[i][j]
if element != ".":
d["r", i] = d.get(("r", i), []) + [element]
d["c", j] = d.get(("c", j), []) + [element]
d[i // 3, j // 3] = d.get((i // 3, j // 3), []) + [element]
else:
val[i, j] = []
for i, j in val.keys():
temp = (
d.get(("r", i), []) + d.get(("c", j), []) + d.get((i // 3, j // 3), [])
)
val[i, j] = [x for x in nums if x not in temp]
return val
def Solver(self):
if len(self.val) == 0:
return True
kee = min(self.val.keys(), key=lambda x: len(self.val[x]))
possible_nums = self.val[kee]
for n in possible_nums:
update = {kee: self.val[kee]}
if self.ValidOne(n, kee, update):
if self.Solver():
return True
self.undo(kee, update)
return False
def ValidOne(self, n, kee, update):
self.board[kee[0]][kee[1]] = n
del self.val[kee]
i, j = kee
for index in self.val:
if (
i == index[0]
or j == index[1]
or (i // 3, j // 3) == (index[0] // 3, index[1] // 3)
) and n in self.val[index]:
update[index] = n
self.val[index].remove(n)
if len(self.val[index]) == 0:
return False
return True
def undo(self, kee, update):
self.board[kee[0]][kee[1]] = "."
for x in update:
if x not in self.val:
self.val[x] = update[x]
else:
self.val[x].append(update[x])
return None | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR STRING VAR BIN_OP FUNC_CALL VAR STRING VAR LIST LIST VAR ASSIGN VAR STRING VAR BIN_OP FUNC_CALL VAR STRING VAR LIST LIST VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST LIST VAR ASSIGN VAR VAR VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR LIST FUNC_CALL VAR STRING VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR DICT VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN NONE |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def dfs(self, row, col, ans, image, newColor, delRow, delCol, iniColor):
ans[row][col] = newColor
n, m = len(image), len(image[0])
for i in range(4):
nrow, ncol = row + delRow[i], col + delCol[i]
if (
nrow >= 0
and nrow < n
and ncol >= 0
and ncol < m
and image[nrow][ncol] == iniColor
and ans[nrow][ncol] != newColor
):
self.dfs(nrow, ncol, ans, image, newColor, delRow, delCol, iniColor)
def floodFill(self, image, sr, sc, newColor):
iniColor = image[sr][sc]
ans = image
delRow = [-1, 0, 1, 0]
delCol = [0, 1, 0, -1]
self.dfs(sr, sc, ans, image, newColor, delRow, delCol, iniColor)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
m = len(image)
n = len(image[0])
startcolor = image[sr][sc]
visited = []
for i in range(m):
visited.append([0] * n)
self.dfs(sr, sc, visited, image, newColor, startcolor)
return image
def dfs(self, i, j, visited, image, color, startcol):
m = len(image)
n = len(image[0])
if i < 0 or i > m - 1 or j < 0 or j > n - 1 or image[i][j] != startcol:
return
if visited[i][j]:
return
visited[i][j] = 1
image[i][j] = color
return (
self.dfs(i - 1, j, visited, image, color, startcol)
or self.dfs(i, j + 1, visited, image, color, startcol)
or self.dfs(i + 1, j, visited, image, color, startcol)
or self.dfs(i, j - 1, visited, image, color, startcol)
) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN IF VAR VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
original_color = image[sr][sc]
if original_color == newColor:
return image
def fill(image, row, col):
if (
row < 0
or row >= len(image)
or col < 0
or col >= len(image[0])
or image[row][col] != original_color
):
return
image[row][col] = newColor
fill(image, row + 1, col)
fill(image, row, col + 1)
fill(image, row - 1, col)
fill(image, row, col - 1)
fill(image, sr, sc)
return image | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
def dfs(ans, image, inicolor, newcolor, nrow, ncol, sr, sc):
n = len(image)
m = len(image[0])
ans[sr][sc] = newcolor
for i in range(4):
nsr = sr + nrow[i]
nsc = sc + ncol[i]
if (
nsr >= 0
and nsr < n
and nsc >= 0
and nsc < m
and ans[nsr][nsc] != newColor
and image[nsr][nsc] == inicolor
):
dfs(ans, image, inicolor, newcolor, nrow, ncol, nsr, nsc)
ans = image
nrow = [-1, 0, 1, 0]
ncol = [0, 1, 0, -1]
inicolor = image[sr][sc]
dfs(ans, image, inicolor, newColor, nrow, ncol, sr, sc)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, color):
r = len(image)
c = len(image[0])
def dfs(i, j):
if (
i < 0
or j < 0
or i >= r
or j >= c
or image[i][j] == color
or image[i][j] != pre
):
return
image[i][j] = color
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
pre = image[sr][sc]
dfs(sr, sc)
return image | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | import itertools
class Solution:
def floodFill(self, image, sr, sc, newColor):
ans = image
iniColor = image[sr][sc]
n = len(image)
m = len(image[0])
delRow = [-1, 0, +1, 0]
delCol = [0, +1, 0, -1]
visited = [[None for _ in range(m)] for _ in range(n)]
def dfs(row, col):
ans[row][col] = newColor
visited[row][col] = 1
for i in range(4):
nRow = row + delRow[i]
nCol = col + delCol[i]
if (
nRow >= 0
and nRow < n
and nCol >= 0
and nCol < m
and image[nRow][nCol] == iniColor
and visited[nRow][nCol] is None
):
dfs(nRow, nCol)
dfs(sr, sc)
return ans | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
def perform(a, b, visited=set()):
if (a, b) in visited:
return
if a < 0 or b < 0:
return
if a >= len(image) or b >= len(image[0]):
return
if image[a][b] == color:
visited.add((a, b))
image[a][b] = newColor
perform(a, b + 1, visited)
perform(a, b - 1, visited)
perform(a + 1, b, visited)
perform(a - 1, b, visited)
color = image[sr][sc]
perform(sr, sc)
return image | CLASS_DEF FUNC_DEF FUNC_DEF FUNC_CALL VAR IF VAR VAR VAR RETURN IF VAR NUMBER VAR NUMBER RETURN IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR 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 BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
initclr = image[sr][sc]
new_image = image
srn = len(image)
scn = len(image[0])
delsr = [-1, +1, 0, 0]
delsc = [0, 0, +1, -1]
def dfs(image, sr, sc, new_image, newcolor, initclr):
new_image[sr][sc] = newcolor
for i in range(4):
nsr = sr + delsr[i]
nsc = sc + delsc[i]
if (
nsr < srn
and nsc < scn
and nsr > -1
and nsc > -1
and image[nsr][nsc] == initclr
and new_image[nsr][nsc] != newColor
):
new_image[nsr][nsc] = newcolor
dfs(image, nsr, nsc, new_image, newColor, initclr)
dfs(image, sr, sc, new_image, newColor, initclr)
return new_image | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def dfs(self, grid, i, j, newColor, val):
if (
i >= 0
and i < len(grid)
and j >= 0
and j < len(grid[i])
and grid[i][j] == val
):
grid[i][j] = newColor
self.dfs(grid, i - 1, j, newColor, val)
self.dfs(grid, i + 1, j, newColor, val)
self.dfs(grid, i, j + 1, newColor, val)
self.dfs(grid, i, j - 1, newColor, val)
def floodFill(self, image, sr, sc, newColor):
if image[sr][sc] != newColor:
self.dfs(image, sr, sc, newColor, image[sr][sc])
return image | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
def dfs(image, sr, sc, newColor, srl, scl, source):
if sr < 0 or sr >= srl or sc < 0 or sc >= scl:
return
if image[sr][sc] != source:
return
image[sr][sc] = newColor
dfs(image, sr - 1, sc, newColor, srl, scl, source)
dfs(image, sr + 1, sc, newColor, srl, scl, source)
dfs(image, sr, sc - 1, newColor, srl, scl, source)
dfs(image, sr, sc + 1, newColor, srl, scl, source)
srl = len(image)
scl = len(image[0])
source = image[sr][sc]
if image[sr][sc] == newColor:
return image
else:
dfs(image, sr, sc, newColor, srl, scl, source)
return image | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
mycolor = image[sr][sc]
def helper(sr, sc):
if sr < 0 or sr >= len(image) or sc < 0 or sc >= len(image[0]):
return
if image[sr][sc] != mycolor:
return
if image[sr][sc] == newColor:
return
image[sr][sc] = newColor
helper(sr - 1, sc)
helper(sr + 1, sc)
helper(sr, sc - 1)
helper(sr, sc + 1)
helper(sr, sc)
return image | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
sourceColor = image[sr][sc]
if newColor == sourceColor:
return image
self.dfs(image, sr, sc, sourceColor, newColor)
return image
def dfs(self, image, sr, sc, sourceColor, newColor):
if (
0 <= sr < len(image)
and 0 <= sc < len(image[0])
and image[sr][sc] != newColor
and image[sr][sc] == sourceColor
):
image[sr][sc] = newColor
self.dfs(image, sr + 1, sc, sourceColor, newColor)
self.dfs(image, sr - 1, sc, sourceColor, newColor)
self.dfs(image, sr, sc + 1, sourceColor, newColor)
self.dfs(image, sr, sc - 1, sourceColor, newColor) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
row = len(image)
col = len(image[0])
directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
visited = [[(-1) for _ in range(col)] for _ in range(row)]
queue = [(sr, sc, image[sr][sc])]
visited[sr][sc] = 1
while queue:
x, y, color = queue.pop(0)
for i, j in directions:
new_x = x + i
new_y = y + j
if (
new_x >= 0
and new_y >= 0
and new_x < row
and new_y < col
and visited[new_x][new_y] == -1
and image[new_x][new_y] == color
):
visited[new_x][new_y] = 1
queue.append((new_x, new_y, image[new_x][new_y]))
for i in range(row):
for j in range(col):
if visited[i][j] == 1:
image[i][j] = newColor
return image | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, color):
def dfs(sr, sc, image, color, row, cols, source):
if sr >= row or sr < 0 or sc >= cols or sc < 0:
return
if image[sr][sc] != source:
return
image[sr][sc] = color
dfs(sr - 1, sc, image, color, row, cols, source)
dfs(sr + 1, sc, image, color, row, cols, source)
dfs(sr, sc - 1, image, color, row, cols, source)
dfs(sr, sc + 1, image, color, row, cols, source)
if image[sr][sc] == color:
return image
source = image[sr][sc]
rows = len(image)
cols = len(image[0])
dfs(sr, sc, image, color, rows, cols, source)
return image | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def dfs(self, image, i, j, m, n, oldColor, newColor, visited):
if i < 0 or j < 0 or i >= m or j >= n:
return
if visited[i][j] == True:
return
visited[i][j] = True
if image[i][j] != oldColor:
return
image[i][j] = newColor
self.dfs(image, i - 1, j, m, n, oldColor, newColor, visited)
self.dfs(image, i, j - 1, m, n, oldColor, newColor, visited)
self.dfs(image, i + 1, j, m, n, oldColor, newColor, visited)
self.dfs(image, i, j + 1, m, n, oldColor, newColor, visited)
def floodFill(self, image, sr, sc, newColor):
visited = []
m, n = len(image), len(image[0])
for i in range(m):
temp = []
for j in range(n):
temp.append(False)
visited.append(temp)
oldColor = image[sr][sc]
self.dfs(image, sr, sc, m, n, oldColor, newColor, visited)
return image | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN IF VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
drow = [-1, 0, 1, 0]
dcol = [0, 1, 0, -1]
vis = [[(0) for i in range(len(image[0]))] for j in range(len(image))]
m, n = len(image), len(image[0])
def bfs(vis, image, sr, sc, newColor, help):
image[sr][sc] = newColor
vis[sr][sc] = 1
queue = []
queue.append([sr, sc])
while queue:
curr = queue.pop(0)
row, col = curr[0], curr[1]
for i in range(4):
nrow, ncol = row + drow[i], col + dcol[i]
if (
nrow >= 0
and ncol >= 0
and nrow < m
and ncol < n
and vis[nrow][ncol] == 0
and image[nrow][ncol] == help
):
vis[nrow][ncol] = 1
image[nrow][ncol] = newColor
queue.append([nrow, ncol])
bfs(vis, image, sr, sc, newColor, image[sr][sc])
return image | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, a, sr, sc, newColor):
color = a[sr][sc]
q = [(sr, sc)]
a[sr][sc] = newColor
vis = [[(0) for i in range(len(a[0]))] for i in range(len(a))]
vis[sr][sc] = 1
while len(q) != 0:
row, col = q.pop(0)
for i in [-1, 1]:
if row + i >= 0 and row + i < len(a):
if a[row + i][col] == color and vis[row + i][col] == 0:
vis[row + i][col] = 1
a[row + i][col] = newColor
q.append((row + i, col))
if col + i >= 0 and col + i < len(a[0]):
if a[row][col + i] == color and vis[row][col + i] == 0:
vis[row][col + i] = 1
a[row][col + i] = newColor
q.append((row, col + i))
return a | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR LIST NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
m, n = len(image), len(image[0])
origColor = image[sr][sc]
if origColor == newColor:
return image
def dfs(row, col):
if row < 0 or row >= m or col < 0 or col >= n:
return
if image[row][col] == origColor:
image[row][col] = newColor
dfs(row + 1, col)
dfs(row, col + 1)
dfs(row - 1, col)
dfs(row, col - 1)
dfs(sr, sc)
return image | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, adj, sr, sc, newColor):
x = len(adj[0])
y = len(adj)
visited = [([0] * x) for i in range(y)]
q = [[sr, sc]]
ptr = 0
curr = adj[sr][sc]
adj[sr][sc] = newColor
visited[sr][sc] = 1
while ptr < len(q):
node = q[ptr]
for a in range(-1, 2):
for b in range(-1, 2):
try:
if (
abs(a) != abs(b)
and not (a == b and a != 0)
and node[0] - a >= 0
and node[1] - b >= 0
and adj[node[0] - a][node[1] - b] == curr
and visited[node[0] - a][node[1] - b] == 0
):
visited[node[0] - a][node[1] - b] = 1
adj[node[0] - a][node[1] - b] = newColor
q.append([node[0] - a, node[1] - b])
except:
pass
ptr += 1
return adj | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
src_pixel = image[sr][sc]
self.helper(image, sr, sc, newColor, len(image), len(image[0]), src_pixel)
return image
def helper(self, image, cur_i, cur_j, newColor, rows, cols, src_pixel):
if cur_i < 0 or cur_i >= rows:
return
if cur_j < 0 or cur_j >= cols:
return
if image[cur_i][cur_j] != src_pixel:
return
if image[cur_i][cur_j] == newColor:
return
image[cur_i][cur_j] = newColor
self.helper(image, cur_i + 1, cur_j, newColor, rows, cols, src_pixel)
self.helper(image, cur_i - 1, cur_j, newColor, rows, cols, src_pixel)
self.helper(image, cur_i, cur_j + 1, newColor, rows, cols, src_pixel)
self.helper(image, cur_i, cur_j - 1, newColor, rows, cols, src_pixel) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN IF VAR NUMBER VAR VAR RETURN IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
def dfs(row, col, newColor, sr, sc, sourse, image):
if sr < 0 or sr >= row or sc < 0 or sc >= col:
return
if sourse != image[sr][sc]:
return
else:
image[sr][sc] = newColor
dfs(row, col, newColor, sr - 1, sc, sourse, image)
dfs(row, col, newColor, sr + 1, sc, sourse, image)
dfs(row, col, newColor, sr, sc - 1, sourse, image)
dfs(row, col, newColor, sr, sc + 1, sourse, image)
if image[sr][sc] == newColor:
return image
else:
dfs(len(image), len(image[0]), newColor, sr, sc, image[sr][sc], image)
return image | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
n = len(image)
m = len(image[0])
ini = image[sr][sc]
vis = [[(0) for j in range(m)] for i in range(n)]
queue = []
queue.append((sr, sc))
vis[sr][sc] = 1
delrow = [-1, 0, 1, 0]
delcol = [0, 1, 0, -1]
while len(queue) > 0:
e = queue.pop(0)
r = e[0]
c = e[1]
image[r][c] = newColor
for i in range(4):
nrow = r + delrow[i]
ncol = c + delcol[i]
if (
nrow >= 0
and nrow < n
and ncol >= 0
and ncol < m
and image[nrow][ncol] == ini
and vis[nrow][ncol] == 0
):
queue.append((nrow, ncol))
vis[nrow][ncol] = 1
return image | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def util(self, image, i, j, n, m, nc, c):
if i < 0 or j < 0 or i >= n or j >= m or image[i][j] == nc:
return
if image[i][j] == c:
image[i][j] = nc
self.util(image, i + 1, j, n, m, nc, c)
self.util(image, i, j + 1, n, m, nc, c)
self.util(image, i - 1, j, n, m, nc, c)
self.util(image, i, j - 1, n, m, nc, c)
def floodFill(self, image, sr, sc, newColor):
color = image[sr][sc]
n = len(image)
m = len(image[0])
self.util(image, sr, sc, n, m, newColor, color)
return image | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, new):
color = image[sr][sc]
if color == new:
return image
def dfs(i, j):
queue = [(i, j)]
while len(queue):
i, j = queue.pop()
image[i][j] = new
dirx = [-1, 1, 0, 0]
diry = [0, 0, -1, 1]
for ele in range(4):
newx = i + dirx[ele]
newy = j + diry[ele]
if (
newx in range(len(image))
and newy in range(len(image[0]))
and image[newx][newy] == color
):
queue.append((newx, newy))
dfs(sr, sc)
return image | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
inicol = image[sr][sc]
ans = [row[:] for row in image]
delrow = [-1, 0, 1, 0]
delcol = [0, 1, 0, -1]
self.dfs(image, sr, sc, newColor, delrow, delcol, inicol, ans)
return ans
def dfs(self, image, row, col, newColor, delrow, delcol, inicol, ans):
ans[row][col] = newColor
r = len(image)
c = len(image[0])
for i in range(4):
nrow = row + delrow[i]
ncol = col + delcol[i]
if (
nrow >= 0
and nrow < r
and ncol >= 0
and ncol < c
and ans[nrow][ncol] == inicol
and ans[nrow][ncol] != newColor
):
self.dfs(image, nrow, ncol, newColor, delrow, delcol, inicol, ans) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def isValid(self, i, j, image, mapp, old, new):
if (
i < 0
or j < 0
or i == len(image)
or j == len(image[0])
or image[i][j] == new
or (i, j) in mapp
or image[i][j] not in [old, new]
):
return False
return True
def floodFill(self, image, sr, sc, new):
old = image[sr][sc]
mapp = {}
mapp[sr, sc] = True
image[sr][sc] = new
queue = [[sr, sc]]
while queue:
x = queue.pop(0)
i, j = x[0], x[1]
if self.isValid(i + 1, j, image, mapp, old, new):
queue.append([i + 1, j])
mapp[i + 1, j] = True
image[i + 1][j] = new
if self.isValid(i, j - 1, image, mapp, old, new):
queue.append([i, j - 1])
mapp[i, j - 1] = True
image[i][j - 1] = new
if self.isValid(i, j + 1, image, mapp, old, new):
queue.append([i, j + 1])
mapp[i, j + 1] = True
image[i][j + 1] = new
if self.isValid(i - 1, j, image, mapp, old, new):
queue.append([i - 1, j])
mapp[i - 1, j] = True
image[i - 1][j] = new
return image | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST LIST VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
seen = set()
ROWS = len(image)
COLS = len(image[0])
oldColor = image[sr][sc]
def recursive(r, c, seen):
if image[r][c] != oldColor:
return
image[r][c] = newColor
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for dr, dc in directions:
new_r = r + dr
new_c = c + dc
if (
new_r in range(ROWS)
and new_c in range(COLS)
and (new_r, new_c) not in seen
):
seen.add((new_r, new_c))
recursive(new_r, new_c, seen)
recursive(sr, sc, seen)
return image | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image
(with position (sr, sc) = (1, 1)), all
pixels connected by a path of the same color
as the starting pixel are colored with the new
color.Note the bottom corner is not colored 2,
because it is not 4-directionally connected to
the starting pixel.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
Constraints:
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1) | class Solution:
def floodFill(self, image, sr, sc, newColor):
def dfs(i, j, val, newColor):
if i < 0 or i >= rows or j < 0 or j >= cols or image[i][j] != val:
return
image[i][j] = newColor
dfs(i - 1, j, val, newColor)
dfs(i + 1, j, val, newColor)
dfs(i, j - 1, val, newColor)
dfs(i, j + 1, val, newColor)
rows, cols = len(image), len(image[0])
val = image[sr][sc]
if val != newColor:
dfs(sr, sc, val, newColor)
return image | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types: Application x generates a notification (this new notification is unread). Thor reads all notifications generated so far by application x (he may re-read some notifications). Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
-----Input-----
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer type_{i} — type of the i-th event. If type_{i} = 1 or type_{i} = 2 then it is followed by an integer x_{i}. Otherwise it is followed by an integer t_{i} (1 ≤ type_{i} ≤ 3, 1 ≤ x_{i} ≤ n, 1 ≤ t_{i} ≤ q).
-----Output-----
Print the number of unread notifications after each event.
-----Examples-----
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
-----Note-----
In the first sample: Application 3 generates a notification (there is 1 unread notification). Application 1 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test: Application 2 generates a notification (there is 1 unread notification). Application 4 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. Application 3 generates a notification (there is 1 unread notification). Application 3 generates a notification (there are 2 unread notifications). | from sys import stdin
input = stdin.readline
n, q = list(map(int, input().split()))
arr = [tuple(map(int, input().split())) for _ in range(q)]
adj = [[] for _ in range(n + 1)]
curr, cnt, res, vis = 0, 0, [], []
for t, v in arr:
if t == 1:
adj[v].append(len(vis))
vis.append(0)
cnt += 1
elif t == 2:
for u in adj[v]:
if not vis[u]:
vis[u] = 1
cnt -= 1
adj[v] = []
else:
while v > curr:
if not vis[curr]:
vis[curr] = 1
cnt -= 1
curr += 1
res.append(cnt)
print("\n".join(map(str, res))) | ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER LIST LIST FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types: Application x generates a notification (this new notification is unread). Thor reads all notifications generated so far by application x (he may re-read some notifications). Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
-----Input-----
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer type_{i} — type of the i-th event. If type_{i} = 1 or type_{i} = 2 then it is followed by an integer x_{i}. Otherwise it is followed by an integer t_{i} (1 ≤ type_{i} ≤ 3, 1 ≤ x_{i} ≤ n, 1 ≤ t_{i} ≤ q).
-----Output-----
Print the number of unread notifications after each event.
-----Examples-----
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
-----Note-----
In the first sample: Application 3 generates a notification (there is 1 unread notification). Application 1 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test: Application 2 generates a notification (there is 1 unread notification). Application 4 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. Application 3 generates a notification (there is 1 unread notification). Application 3 generates a notification (there are 2 unread notifications). | from sys import stdin
input = stdin.readline
n, q = map(int, input().split())
is_read_index = 0
is_read = []
l = [[] for i in range(n)]
ans = 0
prev = 0
for _ in range(q):
t, v = map(int, input().split())
if t == 1:
l[v - 1].append(is_read_index)
is_read_index += 1
is_read.append(False)
ans += 1
elif t == 2:
for idx in l[v - 1]:
if not is_read[idx]:
is_read[idx] = True
ans -= 1
l[v - 1] = []
elif v > prev:
for idx in range(prev, v):
if not is_read[idx]:
is_read[idx] = True
ans -= 1
prev = v
print(ans) | ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types: Application x generates a notification (this new notification is unread). Thor reads all notifications generated so far by application x (he may re-read some notifications). Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
-----Input-----
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer type_{i} — type of the i-th event. If type_{i} = 1 or type_{i} = 2 then it is followed by an integer x_{i}. Otherwise it is followed by an integer t_{i} (1 ≤ type_{i} ≤ 3, 1 ≤ x_{i} ≤ n, 1 ≤ t_{i} ≤ q).
-----Output-----
Print the number of unread notifications after each event.
-----Examples-----
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
-----Note-----
In the first sample: Application 3 generates a notification (there is 1 unread notification). Application 1 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test: Application 2 generates a notification (there is 1 unread notification). Application 4 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. Application 3 generates a notification (there is 1 unread notification). Application 3 generates a notification (there are 2 unread notifications). | n, q = map(int, input().split())
xi = [([0] * (n + 1)) for i in range(2)]
noti = []
ans = ""
num = 0
num2 = 0
num3 = 0
while q > 0:
typ, xt = map(int, input().split())
if typ == 1:
xi[0][xt] += 1
noti += [xt]
num += 1
num2 += 1
elif typ == 3:
for i in range(num3, xt):
if i + 1 > xi[1][noti[i]]:
xi[0][noti[i]] -= 1
num -= 1
num3 = max(num3, xt)
else:
num -= xi[0][xt]
xi[0][xt] = 0
xi[1][xt] = num2
ans += str(num)
ans += "\n"
q -= 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 NUMBER ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR LIST VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR |
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types: Application x generates a notification (this new notification is unread). Thor reads all notifications generated so far by application x (he may re-read some notifications). Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
-----Input-----
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer type_{i} — type of the i-th event. If type_{i} = 1 or type_{i} = 2 then it is followed by an integer x_{i}. Otherwise it is followed by an integer t_{i} (1 ≤ type_{i} ≤ 3, 1 ≤ x_{i} ≤ n, 1 ≤ t_{i} ≤ q).
-----Output-----
Print the number of unread notifications after each event.
-----Examples-----
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
-----Note-----
In the first sample: Application 3 generates a notification (there is 1 unread notification). Application 1 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test: Application 2 generates a notification (there is 1 unread notification). Application 4 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. Application 3 generates a notification (there is 1 unread notification). Application 3 generates a notification (there are 2 unread notifications). | n, q = list(map(int, input().split()))
count = [(0) for i in range(n + 1)]
queue = []
read = set()
unread = 0
ans = []
last_q_idx = 0
last_app_idx = [(1) for i in range(n + 1)]
for i in range(q):
action, num = list(map(int, input().split()))
if action == 1:
queue.append((num, count[num] + 1))
count[num] += 1
unread += 1
elif action == 2:
for number in range(last_app_idx[num], count[num] + 1):
if (num, number) not in read:
read.add((num, number))
unread -= 1
last_app_idx[num] = max(last_app_idx[num], count[num])
else:
for idx in range(last_q_idx, num):
app, number = queue[idx]
if (app, number) not in read:
read.add((app, number))
last_app_idx[app] = max(last_app_idx[app], number)
unread -= 1
last_q_idx = max(last_q_idx, num)
ans.append(unread)
print("\n".join(map(str, ans))) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.