description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | def m(l1, l2):
l1 = [i for i in l1]
l2 = [i for i in l2]
dp1 = [0] * len(l1)
dp2 = [0] * len(l2)
for i in range(len(l1)):
if l1[i] == "#" and l2[i] == "#":
return ["No"]
if l1[0] == "#":
dp1[0] = 10**9
if l2[0] == "#":
dp2[0] = 10**9
if l1[-1] == "#":
dp1[-1] = 10**9
if l2[-1] == "#":
dp2[-1] = 10**9
for i in range(1, len(l1)):
if l1[i] == "#":
dp1[i] = 10**9
if l2[i] == "#":
dp2[i] = 10**9
if l1[i] != "#":
dp1[i] = min(int(dp1[i - 1]), int(dp2[i - 1] + 1))
if l2[i] != "#":
dp2[i] = min(int(dp2[i - 1]), int(dp1[i - 1] + 1))
return ["Yes", min(dp1[-1], dp2[-1])]
t = int(input())
while t:
l1 = input()
l2 = input()
li = m(l1, l2)
if len(li) == 1:
print("No")
else:
print(li[0])
print(li[1])
t -= 1 | FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING RETURN LIST STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN LIST STRING FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | for _ in range(int(input())):
s1 = input()
s2 = input()
s1 = [i for i in s1]
s2 = [i for i in s2]
k = len(s1)
dp1 = [0] * k
dp2 = [0] * k
f = 0
for i in range(k):
if s1[i] == "#" and s2[i] == "#":
print("No")
f = 1
break
if f:
continue
if s1[0] == "#":
dp1[0] = 10**9
if s2[0] == "#":
dp2[0] = 10**9
if s2[-1] == "#":
dp2[-1] = 10**9
if s1[-1] == "#":
dp1[-1] = 10**9
for i in range(1, k):
if s1[i] == "#":
dp1[i] = 10**9
if s2[i] == "#":
dp2[i] = 10**9
if s2[i] != "#":
dp2[i] = min(int(dp1[i - 1] + 1), int(dp2[i - 1]))
if s1[i] != "#":
dp1[i] = min(int(dp2[i - 1] + 1), int(dp1[i - 1]))
print("Yes")
print(min(dp1[-1], dp2[-1])) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | cases = int(input())
for _ in range(cases):
top = "." + input()
bottom = "." + input()
array = [top, bottom]
tfind = top.find("#")
bfind = bottom.find("#")
if (tfind > bfind and bfind != -1) | (tfind == -1):
start = 0
else:
start = 1
count = 0
ok = True
for index in range(len(top) - 1):
if array[start][index + 1] == ".":
continue
start = (start + 1) % 2
count += 1
if array[start][index + 1] == ".":
continue
else:
ok = False
break
if ok:
print("Yes")
print(count)
else:
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | val = 10**7
t = int(input().strip())
for i in range(t):
str1 = input().strip()
str2 = input().strip()
n = len(str1)
dp = [[val for i in range(2)] for j in range(n)]
if str1[0] == "#" and str2[0] == "#":
print("No")
else:
if str1[0] == ".":
dp[0][0] = 0
if str2[0] == ".":
dp[0][1] = 0
for i in range(1, n):
if str1[i] == ".":
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1] + 1)
else:
dp[i][0] = val
if str2[i] == ".":
dp[i][1] = min(dp[i - 1][0] + 1, dp[i - 1][1])
min1 = min(dp[n - 1][0], dp[n - 1][1])
if min1 < val:
print("Yes")
print(min1)
else:
print("No") | ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | T = int(input())
for _ in range(T):
l1 = list(input())
l2 = list(input())
for i in range(len(l1)):
if l1[i] == l2[i] == "#":
print("No")
break
else:
print("Yes")
dp1 = [0] * len(l1)
dp2 = [0] * len(l1)
if l1[0] == "#":
dp1[0] = 1000000
else:
dp1[0] = 0
if l2[0] == "#":
dp2[0] = 1000000
else:
dp2[0] = 0
for i in range(1, len(l1)):
if l1[i] == "#":
dp1[i] = 1000000
else:
dp1[i] = min(dp1[i - 1], dp2[i - 1] + 1)
if l2[i] == "#":
dp2[i] = 1000000
else:
dp2[i] = min(dp2[i - 1], dp1[i - 1] + 1)
val = min(dp1[-1], dp2[-1])
if val < 1000000:
print(val) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | import sys
for t in range(int(input())):
s1 = input()
s2 = input()
leng = len(s1)
f = 0
for i in range(leng):
if s1[i] == "#" and s2[i] == "#":
print("No")
f = 1
break
if f != 1:
l1 = []
l2 = []
for i in range(leng):
l1.append(0)
l2.append(0)
if s1[0] == "#":
l1[0] = sys.maxsize
if s2[0] == "#":
l2[0] = sys.maxsize
for i in range(1, leng):
if s1[i] == "#":
l1[i] = sys.maxsize
else:
l1[i] = min(l1[i - 1], l2[i - 1] + 1)
if s2[i] == "#":
l2[i] = sys.maxsize
else:
l2[i] = min(l2[i - 1], l1[i - 1] + 1)
print("Yes")
print(min(l1[-1], l2[-1])) | IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | for _ in range(int(input())):
s1 = input()
s2 = input()
n = len(s1)
ls1 = [0] * n
ls2 = [0] * n
ls1[0] = 0 if s1[0] == "." else int(10000000.0)
ls2[0] = 0 if s2[0] == "." else int(10000000.0)
for i in range(1, n):
ls1[i] = min(ls1[i - 1], ls2[i - 1] + 1) if s1[i] == "." else int(10000000.0)
ls2[i] = min(ls2[i - 1], ls1[i - 1] + 1) if s2[i] == "." else int(10000000.0)
ans = min(ls1[n - 1], ls2[n - 1])
print("{}\n{}".format("Yes", ans)) if ans < int(10000000.0) else print("No") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER STRING NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER STRING NUMBER FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING STRING VAR FUNC_CALL VAR STRING |
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | t = int(input())
for i in range(t):
a = input()
b = input()
c = 0
arr = [a, b]
z = 0
yon = True
for k in range(len(a)):
if arr[z][k] == "#":
c += 1
z = (z + 1) % 2
if arr[z][k] == "#":
yon = False
c = 2 * 10**5
break
c2 = 0
z = 1
yon2 = True
for k in range(len(a)):
if arr[z][k] == "#":
c2 += 1
z = (z + 1) % 2
if arr[z][k] == "#":
yon2 = False
c2 = 2 * 10**5
break
if yon or yon2:
print("Yes")
print(min(c, c2))
else:
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | for _ in range(int(input())):
l = input()
s = input()
flag = "Yes"
bad = 10000000000
for i in range(len(l)):
if l[i] == "#" and s[i] == "#":
flag = "No"
print("No")
break
if flag == "Yes":
n = len(l)
dp1 = [0] * n
dp2 = [0] * n
if l[0] == "#":
dp1[0] = bad
if s[0] == "#":
dp2[0] = bad
for i in range(1, n):
if l[i] == "#":
dp1[i] = bad
else:
dp1[i] = min(dp1[i - 1], dp2[i - 1] + 1)
if s[i] == "#":
dp2[i] = bad
else:
dp2[i] = min(dp2[i - 1], dp1[i - 1] + 1)
if min(dp1[-1], dp2[-1]) < 10**6:
print("Yes")
print(min(dp1[-1], dp2[-1]))
else:
print("No") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING |
Read problems statements in Mandarin Chinese and Russian.
Chef likes to play games a lot. Gravity Guy is one such interesting game.
"Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over."
Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'.
Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes.
Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane.
He can jump to x+1^{th} block of the same lane.
He can switch gravity quickly and jump to x^{th} block of the other lane.
He can switch gravity and jump to x+1^{th} block of the other lane.
You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination.
------ Input ------
First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}.
------ Output ------
For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ |L_{1}| β€ 2 Γ 10^{5}, where |S| denotes the length of string S$
$|L_{1}| = |L_{2}|$
------ Subtasks ------
Subtask 1 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 2 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 200.$
Subtask 3 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
$Only "Yes"/"No" response will be evaluated.$
Subtask 4 (25 points)
$Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$
----- Sample Input 1 ------
3
#...#
.###.
#.#.#.
.#.#.#
#...
#...
----- Sample Output 1 ------
Yes
2
Yes
5
No
----- explanation 1 ------
Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination.
Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination.
Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them. | for _ in range(int(input())):
l1 = input()
l2 = input()
curr = 0
numb = 1
n = len(l1)
u = False
if l1[0] == "." and l2[0] == "#":
curr = 1
elif l2[0] == "." and l1[0] == "#":
curr = 2
elif l1[0] == "." and l2[0] == ".":
i = 0
while l1[i] == "." and l2[i] == ".":
i += 1
if i == n:
print("Yes")
print(0)
u = True
break
numb = i
if not u:
if l1[i] == "." and l2[i] == "#":
curr = 1
elif l2[i] == "." and l1[i] == "#":
curr = 2
else:
print("No")
u = True
else:
print("No")
u = True
ans = 0
if not u:
for i in range(numb, n):
if l1[i] == "#" and l2[i] == "#":
print("No")
u = True
break
if curr == 1:
if l1[i] == "#":
curr = 2
ans += 1
elif l2[i] == "#":
curr = 1
ans += 1
if not u:
print("Yes")
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def f(self, ind, target, arr, dp):
if ind == 0:
if target % arr[ind] == 0:
return target // arr[ind]
else:
return float("inf")
if dp[ind][target] != -1:
return dp[ind][target]
notTake = 0 + self.f(ind - 1, target, arr, dp)
take = float("inf")
if arr[ind] <= target:
take = 1 + self.f(ind, target - arr[ind], arr, dp)
dp[ind][target] = min(take, notTake)
return min(take, notTake)
def MinCoin(self, nums, amount):
dp = [[(-1) for _ in range(amount + 1)] for _ in range(len(nums))]
res = self.f(len(nums) - 1, amount, nums, dp)
if res == float("inf"):
return -1
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
def find(nums, amount, dp):
if amount == 0:
return 0
if amount < 0:
return 9999999
if dp[amount] != -1:
return dp[amount]
ans = 9999999
for el in nums:
par_ans = 1 + find(nums, amount - el, dp)
ans = min(par_ans, ans)
dp[amount] = ans
return dp[amount]
dp = [-1] * (amount + 1)
ans = find(nums, amount, dp)
if ans == 9999999:
return -1
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | import sys
class Solution:
def MinCoin(self, nums, W):
import sys
sys.setrecursionlimit(10**9 + 7)
n = len(nums)
prev = [sys.maxsize for j in range(W + 1)]
curr = [sys.maxsize for j in range(W + 1)]
for j in range(W + 1):
if j % nums[0] == 0:
prev[j] = j // nums[0]
for i in range(1, n):
for j in range(W + 1):
pick = sys.maxsize
npick = prev[j]
if nums[i] <= j:
pick = 1 + curr[j - nums[i]]
curr[j] = min(pick, npick)
prev = curr
if prev[amount] != sys.maxsize:
return prev[amount]
return -1
def solve(self, n, W, nums, dp):
import sys
if n == 0:
if W % nums[n] == 0:
dp[n][W] = W // nums[n]
return dp[n][W]
dp[n][W] = sys.maxsize
return sys.maxsize
if dp[n][W] != sys.maxsize:
return dp[n][W]
npick = self.solve(n - 1, W, nums, dp)
pick = sys.maxsize
if nums[n] <= W:
pick = 1 + self.solve(n, W - nums[n], nums, dp)
dp[n][W] = min(dp[n][W], min(pick, npick))
return dp[n][W] | IMPORT CLASS_DEF FUNC_DEF IMPORT EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR VAR RETURN NUMBER FUNC_DEF IMPORT IF VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
n = len(nums)
k = [[(0) for i in range(amount + 1)] for i in range(n + 1)]
for j in range(amount + 1):
k[0][j] = float("inf")
for i in range(1, n + 1):
k[i][0] = 0
for i in range(1, n + 1):
if amount % nums[i - 1] == 0:
k[i][amount] = amount // nums[i - 1]
else:
k[i][amount] = float("inf")
for i in range(1, n + 1):
for j in range(1, amount + 1):
if nums[i - 1] <= j:
k[i][j] = min(k[i - 1][j], 1 + k[i][j - nums[i - 1]])
else:
k[i][j] = k[i - 1][j]
return k[n][amount] if k[n][amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_CALL VAR STRING VAR VAR VAR NUMBER |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, coins, sum):
dp = [([0] * (sum + 1)) for i in range(len(coins) + 1)]
for i in range(1, sum + 1):
dp[0][i] = 10**5
for i in range(1, len(coins) + 1):
for j in range(sum + 1):
if j >= coins[i - 1]:
dp[i][j] = min(dp[i - 1][j], 1 + dp[i][j - coins[i - 1]])
else:
dp[i][j] = dp[i - 1][j]
if dp[-1][-1] > 10**4:
return -1
else:
return dp[-1][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR NUMBER NUMBER |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
n = len(nums)
dp = [([0] * (amount + 1)) for tt in range(n + 1)]
dp[0] = [0] + [amount + 1] * amount
for i in range(1, amount + 1):
for j in range(1, n + 1):
if nums[j - 1] <= i:
dp[j][i] = min(1 + dp[j][i - nums[j - 1]], dp[j - 1][i])
else:
dp[j][i] = dp[j - 1][i]
if dp[-1][-1] < amount + 1:
return dp[-1][-1]
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP LIST NUMBER BIN_OP LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER RETURN NUMBER |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, n, a):
dp = [0] * (a + 1)
for i in range(1, a + 1):
dp[i] = 100000000
for j in range(len(n)):
if i - n[j] >= 0:
su = dp[i - n[j]]
dp[i] = min(dp[i], su + 1)
if dp[a] == 0 or dp[a] == 100000000:
return -1
else:
return dp[a] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, coins, target):
dp = [[(0) for i in range(target + 1)] for i in range(len(coins))]
for i in range(target + 1):
if i % coins[0] == 0:
dp[0][i] = i // coins[0]
else:
dp[0][i] = 1000000000.0
for i in range(1, n):
for j in range(target + 1):
nottake = dp[i - 1][j]
take = 1000000000.0
if coins[i] <= j:
take = 1 + dp[i][j - coins[i]]
dp[i][j] = min(take, nottake)
if dp[n - 1][target] >= 1000000000.0:
return -1
return dp[n - 1][target] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
dp = [0] * (amount + 1)
nums.sort()
for amt in range(1, amount + 1):
ans = float("inf")
for coin in nums:
if coin <= amt:
ans = min(ans, 1 + dp[amt - coin])
else:
break
dp[amt] = ans
if dp[amt] >= 10**7:
return -1
else:
return dp[amt] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
N = len(nums)
nums.sort(reverse=True)
dp = {}
def solve(n, cap):
if cap == 0:
return 0
elif n == 0:
return float("INF")
elif (n, cap) in dp:
return dp[n, cap]
else:
cv = nums[n - 1]
if cv <= cap:
c1 = 1 + solve(n, cap - cv)
c2 = solve(n - 1, cap)
c = min(c1, c2)
else:
c = float("INf")
dp[n, cap] = c
return c
val = solve(N, amount)
if val >= 10**9 + 7:
return -1
else:
return val | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, arr, amount):
dp = {}
arr.sort()
def solve(amt):
if amt == 0:
return 0
elif amt in dp:
return dp[amt]
else:
ans = float("INF")
for val in arr:
if val <= amt:
ans = min(ans, 1 + solve(amt - val))
else:
break
dp[amt] = ans
return ans
val = solve(amount)
if val >= 10**9 + 7:
return -1
else:
return val | CLASS_DEF FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
t = [[(0) for i in range(amount + 1)] for j in range(len(nums) + 1)]
for i in range(1, len(nums) + 1):
t[i][0] = 0
for j in range(amount + 1):
t[0][j] = amount + 2
for k in range(1, amount + 1):
if k % nums[0] == 0:
t[1][k] = k // nums[0]
else:
t[1][k] = amount + 2
for i in range(2, len(nums) + 1):
for j in range(1, amount + 1):
if nums[i - 1] <= j:
t[i][j] = min(t[i][j - nums[i - 1]] + 1, t[i - 1][j])
else:
t[i][j] = t[i - 1][j]
if t[len(nums)][amount] == amount + 2:
return -1
return t[len(nums)][amount] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR FUNC_CALL VAR VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
dp = [amount + 1] * 100001
dp[0] = 0
for i in range(len(nums)):
for j in range(1, amount + 1):
if nums[i] <= j:
dp[j] = min(dp[j], dp[j - nums[i]] + 1)
if dp[amount] > amount:
return -1
return dp[amount] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | import sys
class Solution:
def MinCoin(self, nums, amount):
prev = [0] * (amount + 1)
for i in range(amount + 1):
if i % nums[0] == 0:
prev[i] = i // nums[0]
else:
prev[i] = sys.maxsize
for index in range(1, len(nums)):
cur = []
for target in range(amount + 1):
notTake, take = prev[target], sys.maxsize
if nums[index] <= target:
take = 1 + cur[target - nums[index]]
cur.append(min(notTake, take))
prev = cur
t = prev[-1]
if t == sys.maxsize:
return -1
return t | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | import sys
class Solution:
def solve(self, nums, amount):
dp = [float("inf")] * (amount + 1)
dp[0] = 0
mini = float("inf")
for i in range(1, amount + 1):
for j in range(len(nums)):
if i - nums[j] >= 0 and dp[i - nums[j]] != float("inf"):
dp[i] = min(dp[i], 1 + dp[i - nums[j]])
if dp[amount] == float("inf"):
return -1
return dp[amount]
def MinCoin(self, nums, amount):
return self.solve(nums, amount) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | def dynamic(arr, amount):
n = len(arr)
arr.sort()
arr = arr[::-1]
dp = [[(10**5) for _ in range(amount + 1)] for _ in range(n + 1)]
for i in range(n + 1):
dp[i][0] = 1
for i in range(1, n + 1):
for j in range(1, amount + 1):
if j % arr[i - 1] == 0:
dp[i][j] = j // arr[i - 1]
for i in range(1, n + 1):
for j in range(1, amount + 1):
if j >= arr[i - 1]:
dp[i][j] = min(
dp[i][j], dp[i][j - arr[i - 1]] + dp[i][arr[i - 1]], dp[i - 1][j]
)
else:
dp[i][j] = dp[i - 1][j]
if dp[n][amount] == 10**5:
return -1
return dp[n][amount]
class Solution:
def MinCoin(self, nums, amount):
return dynamic(nums, amount) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR VAR VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
dp = [amount + 1] * (amount + 1)
dp[0] = 0
for a in range(1, amount + 1):
for c in nums:
if a - c >= 0:
dp[a] = min((dp[a], 1 + dp[a - c]))
return dp[amount] if dp[amount] != amount + 1 else -1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
n = len(nums)
dp = [([-1] * (amount + 1)) for _ in range(n)]
def rec(i, val):
if i >= n:
return 10001
if val == 0:
dp[i][val] = 0
return 0
if dp[i][val] != -1:
return dp[i][val]
coins = rec(i + 1, val)
if nums[i] <= val:
coins = min(1 + rec(i, val - nums[i]), coins)
dp[i][val] = coins
return dp[i][val]
res = rec(0, amount)
if res < 10001:
return res
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER RETURN VAR RETURN NUMBER |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
dp = [[(-1) for i in range(amount + 1)] for i in range(len(nums))]
def f(ind, arr, target, dp):
if ind == 0:
if target % arr[ind] == 0:
return target // arr[ind]
else:
return 1000000000.0
if dp[ind][target] != -1:
return dp[ind][target]
not_pic = 0 + f(ind - 1, arr, target, dp)
pic = 1000000000.0
if arr[ind] <= target:
pic = 1 + f(ind, arr, target - arr[ind], dp)
dp[ind][target] = min(pic, not_pic)
return dp[ind][target]
ans = f(len(nums) - 1, nums, amount, dp)
if ans == 1000000000.0:
return -1
else:
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
n = len(nums)
dp = [[(10000000000) for i in range(amount + 1)] for i in range(n)]
for i in range(n):
dp[i][0] = 0
for i in range(1, amount + 1):
if nums[0] <= i:
dp[0][i] = dp[0][i - nums[0]] + 1
for i in range(1, n):
for j in range(1, amount + 1):
dp[i][j] = dp[i - 1][j]
if nums[i] <= j:
dp[i][j] = min(dp[i][j], dp[i][j - nums[i]] + 1)
ans = dp[n - 1][amount]
if ans >= 10000000000:
return -1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
def func(coins, ind, tar, dp):
if ind == 0:
if tar % coins[ind] == 0:
return tar // coins[ind]
else:
return 10**9
if dp[ind][tar] != -1:
return dp[ind][tar]
nottake = 0 + func(coins, ind - 1, tar, dp)
take = 10**9
if coins[ind] <= tar:
take = 1 + func(coins, ind, tar - coins[ind], dp)
dp[ind][tar] = min(nottake, take)
return dp[ind][tar]
n = len(nums)
dp = [[(-1) for i in range(amount + 1)] for j in range(n)]
kk = func(nums, n - 1, amount, dp)
if kk == 10**9:
return -1
return kk | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR RETURN BIN_OP NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
coins = nums
V = amount
m = max(coins)
m = max(m, V)
dp = [-1] * (m + 1)
for el in coins:
dp[el] = 1
for i in range(V + 1):
choices = []
for el in coins:
if i == el:
choices.append(1)
elif i - el >= 0 and dp[i - el] != -1:
choices.append(dp[i - el] + 1)
if len(choices) > 0:
dp[i] = min(choices)
return dp[V] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | def f(A, amt, dp):
if amt == 0:
return 0
elif amt < 0:
return int(100000.0)
else:
if dp[amt] != -1:
return dp[amt]
ans = int(100000.0)
for i in A:
ans = min(ans, 1 + f(A, amt - i, dp))
if ans >= int(100000.0):
ans = int(100000.0)
dp[amt] = ans
return ans
class Solution:
def MinCoin(self, nums, amount):
dp = [-1] * (amount + 1)
ans = f(nums, amount, dp)
if ans == int(100000.0):
return -1
return ans | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR NUMBER RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, a, n):
dp = [float("inf")] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
for j in a:
if i - j >= 0:
dp[i] = min(dp[i - j] + 1, dp[i])
if dp[n] == float("inf"):
return -1
return dp[n] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, coins, Sum):
N = len(coins)
MAX = pow(10, 7)
dp = [[MAX for j in range(Sum + 1)] for i in range(N)]
for i in range(N):
for s in range(Sum + 1):
if i == 0 and s % coins[0] == 0:
dp[0][s] = int(s / coins[0])
else:
notTake = dp[i - 1][s]
take = MAX
if coins[i] <= s:
take = 1 + dp[i][s - coins[i]]
dp[i][s] = min(take, notTake)
return -1 if dp[N - 1][Sum] == MAX else dp[N - 1][Sum] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
d = dict()
res = self.MinCoiN(nums, amount, d)
if res == float("inf"):
return -1
return res
def MinCoiN(self, nums, amount, d):
if amount in d:
return d[amount]
if amount == 0:
return 0
if amount < 0:
return -1
else:
Max = float("inf")
for i in nums:
x = self.MinCoiN(nums, amount - i, d)
if x != -1 and x != float("inf"):
x = x + 1
if x < Max:
Max = x
d[amount] = Max
return Max | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | import sys
class Solution:
def mcoins(self, coins, x, n, dp):
if x == 0:
return 0
if x < 0:
return sys.maxsize
if dp[x] != -1:
return dp[x]
mini = sys.maxsize
for i in range(n):
ans = self.mcoins(coins, x - coins[i], n, dp)
if ans != sys.maxsize:
mini = min(ans + 1, mini)
dp[x] = mini
return mini
def solve(self, coins, x):
dp = [sys.maxsize for _ in range(x + 1)]
dp[0] = 0
for i in range(1, x + 1):
for j in range(len(coins)):
if i - coins[j] >= 0 and dp[i - coins[j]] != sys.maxsize:
dp[i] = min(dp[i], 1 + dp[i - coins[j]])
if dp[x] == sys.maxsize:
return -1
return dp[x]
def MinCoin(self, nums, amount):
a = self.solve(nums, amount)
return a | IMPORT CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, coins, amount):
dp = [(amount + 1) for i in range(0, amount + 1)]
dp[0] = 0
for i in range(1, len(dp)):
for coin in coins:
if coin <= i:
dp[i] = min(1 + dp[i - coin], dp[i])
return -1 if dp[amount] == amount + 1 else dp[amount] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, arr, amount):
N = len(arr)
dp = [([0] * (amount + 1)) for _ in range(N + 1)]
for i in range(N + 1):
for j in range(amount + 1):
amt = j
n = i
if amt == 0:
dp[i][j] = 0
elif n == 0:
dp[i][j] = float("INF")
else:
val = arr[n - 1]
if val <= amt:
c1 = 1 + dp[n][amt - val]
c2 = dp[n - 1][amt]
dp[i][j] = min(c1, c2)
else:
dp[i][j] = dp[n - 1][amt]
val = dp[N][amount]
if val >= 10**9 + 7:
return -1
else:
return val | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
prev = [0] * (amount + 1)
cur = [0] * (amount + 1)
for T in range(amount + 1):
if T % nums[0] == 0:
prev[T] = T // nums[0]
else:
prev[T] = float("inf")
for ind in range(1, len(nums)):
for target in range(amount + 1):
notTake = 0 + prev[target]
take = float("inf")
if nums[ind] <= target:
take = 1 + cur[target - nums[ind]]
cur[target] = min(take, notTake)
prev = cur
res = prev[amount]
if res >= float("inf"):
return -1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | import sys
class Solution:
def MinCoin(self, nums, amount):
dp = [-1] * (amount + 1)
dp[0] = 0
for i in nums:
if i <= amount:
dp[i] = 1
for j in range(amount + 1):
if dp[j] != -1:
for k in nums:
if j + k <= amount:
if dp[j + k] == -1:
dp[j + k] = dp[j] + 1
else:
dp[j + k] = min(dp[j + k], dp[j] + 1)
return dp[amount] | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, coins, Sum):
N = len(nums)
MAX = pow(10, 7)
dp = [[(-1) for j in range(Sum + 1)] for i in range(N)]
def f(i, s):
if i == 0:
if s % coins[0] == 0:
return int(s / coins[0])
return MAX
if dp[i][s] == -1:
t = MAX
nt = f(i - 1, s)
if coins[i] <= s:
t = 1 + f(i, s - coins[i])
dp[i][s] = min(nt, t)
return dp[i][s]
ans = f(N - 1, Sum)
return -1 if ans == MAX else ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR NUMBER VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
m = {}
n = len(nums)
nums.sort()
def mincoin(i, s):
if s == 0:
return 0
if i < 0:
return 999999
if s % nums[i] == 0:
return s // nums[i]
if (i, s) in m:
return m[i, s]
t = 999999
h = 999999
if nums[i] <= s:
t = 1 + mincoin(i, s - nums[i])
h = mincoin(i - 1, s)
m[i, s] = min(t, h)
return m[i, s]
d = mincoin(n - 1, amount)
if d == 999999:
return -1
return d | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | import sys
class Solution:
def MinCoin(self, nums, amount):
ans = [([-1] * (amount + 1)) for _ in range(len(nums) + 1)]
def selectCoin(index, T):
if index == 0:
if T % nums[0] == 0:
return T // nums[0]
return sys.maxsize
if ans[index][T] != -1:
return ans[index][T]
notTake = selectCoin(index - 1, T)
take = sys.maxsize
if nums[index] <= T:
take = 1 + selectCoin(index, T - nums[index])
ans[index][T] = min(notTake, take)
return ans[index][T]
t = selectCoin(len(nums) - 1, amount)
if t == sys.maxsize:
return -1
return t | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
dp = [-1] * (amount + 1)
dp[0] = 0
for i in range(1, amount + 1):
ans = 9999999
for el in nums:
f = i - el
if f >= 0:
par_ans = 1 + dp[f]
ans = min(par_ans, ans)
dp[i] = ans
if dp[amount] == 9999999:
return -1
return dp[amount] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | import sys
class Solution:
def MinCoin(self, nums, amount):
ans = [([0] * (amount + 1)) for _ in range(len(nums))]
for i in range(amount + 1):
if i % nums[0] == 0:
ans[0][i] = i // nums[0]
else:
ans[0][i] = sys.maxsize
for index in range(1, len(nums)):
for target in range(amount + 1):
notTake, take = ans[index - 1][target], sys.maxsize
if nums[index] <= target:
take = 1 + ans[index][target - nums[index]]
ans[index][target] = min(notTake, take)
t = ans[-1][-1]
if t == sys.maxsize:
return -1
return t | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | def cc(arr, n, dp):
if n == 0:
return 0
if dp.get(n):
return dp[n]
ans = float("inf")
for i in range(len(arr)):
if n - arr[i] >= 0:
sub = 0
if dp.get(n - arr[i]):
sub = dp[n - arr[i]]
else:
sub = cc(arr, n - arr[i], dp)
if sub != float("inf") and sub + 1 < ans:
ans = sub + 1
dp[n] = ans
return dp[n]
class Solution:
def MinCoin(self, nums, amount):
return (
cc(nums, amount, {(0): 0})
if cc(nums, amount, {(0): 0}) != float("inf")
else -1
) | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR DICT NUMBER NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR DICT NUMBER NUMBER NUMBER |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, coins, total_amount):
min_coin_results = [-2] * total_amount
for coin in coins:
if coin <= total_amount:
min_coin_results[coin - 1] = 1
return self.MinCoinHelper(coins, total_amount, min_coin_results)
def MinCoinHelper(self, coins, total_amount, min_coin_results):
if total_amount == 0:
return 0
if min_coin_results[total_amount - 1] != -2:
return min_coin_results[total_amount - 1]
result = -1
for coin in coins:
if total_amount >= coin:
temp_result = self.MinCoinHelper(
coins, total_amount - coin, min_coin_results
)
if temp_result >= 0 and (result == -1 or result >= temp_result + 1):
result = temp_result + 1
min_coin_results[total_amount - 1] = result
return result | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
n, x = len(nums), amount
c = nums
a = [(-1) for i in range(x + 1)]
a[0] = 0
for i in range(x):
if a[i] != -1:
for j in c:
if i + j <= x:
if a[i + j] == -1:
a[i + j] = a[i] + 1
a[i + j] = min(a[i + j], a[i] + 1)
return a[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def noofways(self, amt, coins, n, dp):
if amt == 0:
return 0
if n == 0:
return float("inf")
if n == 1 and amt > 1:
if amt % coins[n - 1] == 0:
return amt // coins[n - 1]
else:
return float("inf")
if dp[n][amt] != -1:
return dp[n][amt]
if coins[n - 1] > amt:
dp[n][amt] = self.noofways(amt, coins, n - 1, dp)
return dp[n][amt]
else:
dp[n][amt] = min(
1 + self.noofways(amt - coins[n - 1], coins, n, dp),
self.noofways(amt, coins, n - 1, dp),
)
return dp[n][amt]
def MinCoin(self, nums, amount):
coins = nums
dp = [[(-1) for _ in range(amount + 1)] for _ in range(len(coins) + 1)]
if self.noofways(amount, coins, len(coins), dp) == float("inf"):
return -1
else:
return self.noofways(amount, coins, len(coins), dp) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def help(self, nums, amt, n):
dp = [[(0) for j in range(amt + 1)] for i in range(n + 1)]
for i in range(amt + 1):
dp[0][i] = float("inf")
for i in range(1, n + 1):
for j in range(1, amt + 1):
if nums[i - 1] <= j:
dp[i][j] = min(1 + dp[i][j - nums[i - 1]], dp[i - 1][j])
else:
dp[i][j] = dp[i - 1][j]
return dp[n][amt]
def MinCoin(self, nums, amount):
n = len(nums)
val = self.help(nums, amount, n)
if val == float("inf"):
return -1
else:
return val | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | INV = 10**9
class Solution:
def MinCoin(self, A, K):
dp = [INV] * (K + 1)
dp[0] = 0
for amt in range(1, K + 1):
for v in A:
if v > amt:
continue
dp[amt] = min(1 + dp[amt - v], dp[amt])
return dp[K] if dp[K] < INV else -1 | ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR NUMBER |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, coins, amount):
n = len(coins)
dp = [[(0) for j in range(0, amount + 1)] for i in range(0, n + 1)]
for i in range(0, n + 1):
dp[i][0] = 0
for j in range(1, amount + 1):
if j % coins[0] == 0:
dp[1][j] = j // coins[0]
else:
dp[1][j] = 1000000000.0
for i in range(2, n + 1):
for j in range(1, amount + 1):
if coins[i - 1] > j:
dp[i][j] = dp[i - 1][j]
else:
include = 1 + dp[i][j - coins[i - 1]]
exclude = dp[i - 1][j]
dp[i][j] = min(include, exclude)
if dp[n][amount] >= 1000000000.0:
return -1
return dp[n][amount] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
dp = [float("inf") for _ in range(amount + 1)]
dp[0] = 0
for i in range(len(dp)):
for j in nums:
if i - j >= 0:
dp[i] = min(dp[i], dp[i - j] + 1)
return -1 if dp[-1] == float("inf") else dp[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR NUMBER |
Given a list of coins of distinct denominations and total amount of money. Find the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins.
You may assume that there are infinite numbers of coins of each type.
Example 1:
Input: arr = [1, 2, 5], amount = 11
Output: 3
Explanation: 2*5 + 1 = 11. So taking 2
denominations of 5 and 1 denomination of
1, one can make 11.
Example 2:
Input: arr = [2, 6], amount = 7
Output: -1
Explanation: Not possible to make 7 using
denominations 2 and 6.
Your Task:
You don't need to read or print anything. Your task is to complete the function MinCoin() which takes list of denominations and amount as input parameter and returns miimum number of coins to make up amount. If not possible returns -1.
Expected Time Complexity: O(n*amount)
Expected Space Complexity: O(amount)
Contstraints:
1 <= number of distinct denominations <= 100
1 <= amount <= 10^{4} | class Solution:
def MinCoin(self, nums, amount):
n = len(nums)
nums.sort()
dp = [1000000000000.0] * (amount + 1)
dp[0] = 0
for i in range(1, amount + 1):
for j in range(n):
if nums[j] > i:
break
dp[i] = min(dp[i], 1 + dp[i - nums[j]])
if dp[amount] == 1000000000000.0:
return -1
return dp[amount] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR |
[INSPION FullBand Master - INSPION](https://www.youtube.com/watch?v=kwsciXm_7sA)
[INSPION - IOLITE-SUNSTONE](https://www.youtube.com/watch?v=kwsciXm_7sA)
On another floor of the A.R.C. Markland-N, the young man Simon "Xenon" Jackson, takes a break after finishing his project early (as always). Having a lot of free time, he decides to put on his legendary hacker "X" instinct and fight against the gangs of the cyber world.
His target is a network of n small gangs. This network contains exactly n - 1 direct links, each of them connecting two gangs together. The links are placed in such a way that every pair of gangs is connected through a sequence of direct links.
By mining data, Xenon figured out that the gangs used a form of cross-encryption to avoid being busted: every link was assigned an integer from 0 to n - 2 such that all assigned integers are distinct and every integer was assigned to some link. If an intruder tries to access the encrypted data, they will have to surpass S password layers, with S being defined by the following formula:
$$$S = β_{1 β€ u < v β€ n} mex(u, v)$$$
Here, mex(u, v) denotes the smallest non-negative integer that does not appear on any link on the unique simple path from gang u to gang v.
Xenon doesn't know the way the integers are assigned, but it's not a problem. He decides to let his AI's instances try all the passwords on his behalf, but before that, he needs to know the maximum possible value of S, so that the AIs can be deployed efficiently.
Now, Xenon is out to write the AI scripts, and he is expected to finish them in two hours. Can you find the maximum possible S before he returns?
Input
The first line contains an integer n (2 β€ n β€ 3000), the number of gangs in the network.
Each of the next n - 1 lines contains integers u_i and v_i (1 β€ u_i, v_i β€ n; u_i β v_i), indicating there's a direct link between gangs u_i and v_i.
It's guaranteed that links are placed in such a way that each pair of gangs will be connected by exactly one simple path.
Output
Print the maximum possible value of S β the number of password layers in the gangs' network.
Examples
Input
3
1 2
2 3
Output
3
Input
5
1 2
1 3
1 4
3 5
Output
10
Note
In the first example, one can achieve the maximum S with the following assignment:
<image>
With this assignment, mex(1, 2) = 0, mex(1, 3) = 2 and mex(2, 3) = 1. Therefore, S = 0 + 2 + 1 = 3.
In the second example, one can achieve the maximum S with the following assignment:
<image>
With this assignment, all non-zero mex value are listed below:
* mex(1, 3) = 1
* mex(1, 5) = 2
* mex(2, 3) = 1
* mex(2, 5) = 2
* mex(3, 4) = 1
* mex(4, 5) = 3
Therefore, S = 1 + 2 + 1 + 2 + 1 + 3 = 10. | import sys
inp = [int(x) for x in sys.stdin.read().split()]
ii = 0
n = inp[ii]
ii += 1
coupl = [[] for _ in range(n)]
for _ in range(n - 1):
u = inp[ii] - 1
ii += 1
v = inp[ii] - 1
ii += 1
coupl[u].append(v)
coupl[v].append(u)
bfs = [0]
found = [0] * n
found[0] = 1
for node in bfs:
for nei in coupl[node]:
if not found[nei]:
found[nei] = 1
bfs.append(nei)
naming = [0] * n
for i in range(n):
naming[bfs[i]] = i
coupl = [coupl[i] for i in bfs]
for c in coupl:
c[:] = [naming[x] for x in c]
subsizes = []
Ps = []
for root in range(n):
P = [-1] * n
P[root] = root
bfs = [root]
for node in bfs:
for nei in coupl[node]:
if P[nei] == -1:
P[nei] = node
bfs.append(nei)
Ps += P
subsize = [1] * n
for node in reversed(bfs[1:]):
subsize[P[node]] += subsize[node]
other = [0] * n
for nei in coupl[root]:
other[nei] = subsize[root] - subsize[nei]
for node in bfs:
if P[node] != root:
other[node] = other[P[node]]
subsize[node] *= other[node]
subsizes += subsize
DP = [0] * (n * n)
bfs = [(n * root + root) for root in range(n)]
for ind in bfs:
root, node = divmod(ind, n)
for nei in coupl[node]:
ind2 = ind - node + nei
if Ps[ind2] == node:
DP[n * nei + root] = DP[ind2] = max(DP[ind2], DP[ind] + subsizes[ind2])
bfs.append(ind2)
print(max(DP)) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
[INSPION FullBand Master - INSPION](https://www.youtube.com/watch?v=kwsciXm_7sA)
[INSPION - IOLITE-SUNSTONE](https://www.youtube.com/watch?v=kwsciXm_7sA)
On another floor of the A.R.C. Markland-N, the young man Simon "Xenon" Jackson, takes a break after finishing his project early (as always). Having a lot of free time, he decides to put on his legendary hacker "X" instinct and fight against the gangs of the cyber world.
His target is a network of n small gangs. This network contains exactly n - 1 direct links, each of them connecting two gangs together. The links are placed in such a way that every pair of gangs is connected through a sequence of direct links.
By mining data, Xenon figured out that the gangs used a form of cross-encryption to avoid being busted: every link was assigned an integer from 0 to n - 2 such that all assigned integers are distinct and every integer was assigned to some link. If an intruder tries to access the encrypted data, they will have to surpass S password layers, with S being defined by the following formula:
$$$S = β_{1 β€ u < v β€ n} mex(u, v)$$$
Here, mex(u, v) denotes the smallest non-negative integer that does not appear on any link on the unique simple path from gang u to gang v.
Xenon doesn't know the way the integers are assigned, but it's not a problem. He decides to let his AI's instances try all the passwords on his behalf, but before that, he needs to know the maximum possible value of S, so that the AIs can be deployed efficiently.
Now, Xenon is out to write the AI scripts, and he is expected to finish them in two hours. Can you find the maximum possible S before he returns?
Input
The first line contains an integer n (2 β€ n β€ 3000), the number of gangs in the network.
Each of the next n - 1 lines contains integers u_i and v_i (1 β€ u_i, v_i β€ n; u_i β v_i), indicating there's a direct link between gangs u_i and v_i.
It's guaranteed that links are placed in such a way that each pair of gangs will be connected by exactly one simple path.
Output
Print the maximum possible value of S β the number of password layers in the gangs' network.
Examples
Input
3
1 2
2 3
Output
3
Input
5
1 2
1 3
1 4
3 5
Output
10
Note
In the first example, one can achieve the maximum S with the following assignment:
<image>
With this assignment, mex(1, 2) = 0, mex(1, 3) = 2 and mex(2, 3) = 1. Therefore, S = 0 + 2 + 1 = 3.
In the second example, one can achieve the maximum S with the following assignment:
<image>
With this assignment, all non-zero mex value are listed below:
* mex(1, 3) = 1
* mex(1, 5) = 2
* mex(2, 3) = 1
* mex(2, 5) = 2
* mex(3, 4) = 1
* mex(4, 5) = 3
Therefore, S = 1 + 2 + 1 + 2 + 1 + 3 = 10. | import sys
inp = [int(x) for x in sys.stdin.buffer.read().split()]
ii = 0
n = inp[ii]
ii += 1
coupl = [[] for _ in range(n)]
for _ in range(n - 1):
u = inp[ii] - 1
ii += 1
v = inp[ii] - 1
ii += 1
coupl[u].append(v)
coupl[v].append(u)
bfs = [0]
found = [0] * n
found[0] = 1
for node in bfs:
for nei in coupl[node]:
if not found[nei]:
found[nei] = 1
bfs.append(nei)
new_label = [0] * n
for i in range(n):
new_label[bfs[i]] = i
coupl = [coupl[i] for i in bfs]
for c in coupl:
c[:] = [new_label[x] for x in c]
DP = [0] * (n * n)
size = [1] * (n * n)
P = [-1] * (n * n)
bfs = [(root * n + root) for root in range(n)]
for ind in bfs:
P[ind] = ind
for ind in bfs:
node, root = divmod(ind, n)
for nei in coupl[node]:
ind2 = nei * n + root
if P[ind2] == -1:
bfs.append(ind2)
P[ind2] = ind
del bfs[:n]
for ind in reversed(bfs):
node, root = divmod(ind, n)
ind2 = root * n + node
pind = P[ind]
parent = pind // n
size[pind] += size[ind]
DP[pind] = max(DP[pind], max(DP[ind], DP[ind2]) + size[ind] * size[ind2])
print(max(DP[root * n + root] for root in range(n))) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR |
[INSPION FullBand Master - INSPION](https://www.youtube.com/watch?v=kwsciXm_7sA)
[INSPION - IOLITE-SUNSTONE](https://www.youtube.com/watch?v=kwsciXm_7sA)
On another floor of the A.R.C. Markland-N, the young man Simon "Xenon" Jackson, takes a break after finishing his project early (as always). Having a lot of free time, he decides to put on his legendary hacker "X" instinct and fight against the gangs of the cyber world.
His target is a network of n small gangs. This network contains exactly n - 1 direct links, each of them connecting two gangs together. The links are placed in such a way that every pair of gangs is connected through a sequence of direct links.
By mining data, Xenon figured out that the gangs used a form of cross-encryption to avoid being busted: every link was assigned an integer from 0 to n - 2 such that all assigned integers are distinct and every integer was assigned to some link. If an intruder tries to access the encrypted data, they will have to surpass S password layers, with S being defined by the following formula:
$$$S = β_{1 β€ u < v β€ n} mex(u, v)$$$
Here, mex(u, v) denotes the smallest non-negative integer that does not appear on any link on the unique simple path from gang u to gang v.
Xenon doesn't know the way the integers are assigned, but it's not a problem. He decides to let his AI's instances try all the passwords on his behalf, but before that, he needs to know the maximum possible value of S, so that the AIs can be deployed efficiently.
Now, Xenon is out to write the AI scripts, and he is expected to finish them in two hours. Can you find the maximum possible S before he returns?
Input
The first line contains an integer n (2 β€ n β€ 3000), the number of gangs in the network.
Each of the next n - 1 lines contains integers u_i and v_i (1 β€ u_i, v_i β€ n; u_i β v_i), indicating there's a direct link between gangs u_i and v_i.
It's guaranteed that links are placed in such a way that each pair of gangs will be connected by exactly one simple path.
Output
Print the maximum possible value of S β the number of password layers in the gangs' network.
Examples
Input
3
1 2
2 3
Output
3
Input
5
1 2
1 3
1 4
3 5
Output
10
Note
In the first example, one can achieve the maximum S with the following assignment:
<image>
With this assignment, mex(1, 2) = 0, mex(1, 3) = 2 and mex(2, 3) = 1. Therefore, S = 0 + 2 + 1 = 3.
In the second example, one can achieve the maximum S with the following assignment:
<image>
With this assignment, all non-zero mex value are listed below:
* mex(1, 3) = 1
* mex(1, 5) = 2
* mex(2, 3) = 1
* mex(2, 5) = 2
* mex(3, 4) = 1
* mex(4, 5) = 3
Therefore, S = 1 + 2 + 1 + 2 + 1 + 3 = 10. | import sys
INF = 10**10
def main():
n = get_int()
coupl = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = get_list()
coupl[u - 1].append(v - 1)
coupl[v - 1].append(u - 1)
bfs = [0]
found = [0] * n
found[0] = 1
for node in bfs:
for nei in coupl[node]:
if not found[nei]:
found[nei] = 1
bfs.append(nei)
new_label = [0] * n
for i in range(n):
new_label[bfs[i]] = i
coupl = [coupl[i] for i in bfs]
for c in coupl:
c[:] = [new_label[x] for x in c]
DP = [0] * (n * n)
size = [1] * (n * n)
P = [-1] * (n * n)
bfs = [(root * n + root) for root in range(n)]
for ind in bfs:
P[ind] = ind
for ind in bfs:
node, root = divmod(ind, n)
for nei in coupl[node]:
ind2 = nei * n + root
if P[ind2] == -1:
bfs.append(ind2)
P[ind2] = ind
del bfs[:n]
for ind in reversed(bfs):
node, root = divmod(ind, n)
ind2 = root * n + node
pind = P[ind]
parent = pind // n
size[pind] += size[ind]
DP[pind] = max(DP[pind], max(DP[ind], DP[ind2]) + size[ind] * size[ind2])
print(max(DP[root * n + root] for root in range(n)))
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda: int(input())
get_list = lambda: list(map(int, input().split()))
main()
print(*out, sep="\n") | IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | import sys
n, *x = map(int, sys.stdin.read().split())
x.sort()
def main():
res1 = set()
for i in x:
if not i - 1 in res1:
res1.add(i - 1)
elif not i in res1:
res1.add(i)
elif not i + 1 in res1:
res1.add(i + 1)
else:
continue
res2 = set()
for i in x:
if i - 1 in res2:
res2.add(i - 1)
elif i in res2:
res2.add(i)
elif i + 1 in res2:
res2.add(i + 1)
else:
res2.add(i + 1)
return len(res2), len(res1)
ans = main()
print(*ans, sep=" ") | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
a = list(map(int, input().split()))
a.sort()
dp1 = [0] * (n + 2)
dp2 = [0] * (n + 2)
for i in range(n):
if dp1[a[i] - 1] == 0:
dp1[a[i] - 1] = 1
elif dp1[a[i]] == 0:
dp1[a[i]] = 1
else:
dp1[a[i] + 1] = 1
if dp2[a[i] - 1] == 1 or dp2[a[i] + 1] == 1 or dp2[a[i]] == 1:
continue
dp2[a[i] + 1] = 1
print(sum(dp2), sum(dp1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 2)
c = [0] * (n + 2)
for i in range(n):
c[a[i]] += 1
b[a[i]] = 1
for i in range(len(c) - 1):
if c[i] == 0 and c[i + 1] > 0:
c[i] = 1
c[i + 1] -= 1
for i in range(len(c) - 1, 0, -1):
if c[i] == 0 and c[i - 1] > 0:
c[i] = 1
c[i - 1] -= 1
ans = 0
x = 1
dp = [([0] * 3) for i in range(n + 2)]
dp[1][0], dp[1][1], dp[1][2] = b[1], b[1], b[1]
for i in range(2, len(dp)):
x = 0
if b[i - 1] + b[i - 2] == 0:
x = 1
if b[i] == 1:
dp[i][0] = min(dp[i - 1]) + 1
dp[i][1] = min(dp[i - 1][0] + (b[i - 1] + 1) % 2, dp[i - 1][2] + x)
dp[i][2] = dp[i - 1][0] - b[i - 1] + 1
else:
dp[i][0] = min(dp[i - 1])
dp[i][1] = min(dp[i - 1])
dp[i][2] = dp[i - 1][0]
print(min(dp[-1]), len(c) - c.count(0)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
ar = list(map(int, input().split()))
ar = sorted(ar)
ar2 = sorted(list(set(ar)))
a = set()
for x in ar:
if x - 1 not in a:
a.add(x - 1)
elif x not in a:
a.add(x)
else:
a.add(x + 1)
k = len(ar2)
i = 0
while i < len(ar2):
if i + 1 < len(ar2):
if ar2[i] == ar2[i + 1] - 2:
k -= 1
i += 1
elif ar2[i] == ar2[i + 1] - 1:
if i + 2 < len(ar2):
if ar2[i] == ar2[i + 2] - 2:
k -= 2
i += 2
else:
k -= 1
i += 1
else:
k -= 1
i += 1
i += 1
print(k, len(a)) | 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 VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | def maxfind(arr):
covered = set()
i = 0
while i < len(arr):
if (
arr[i] not in covered
and arr[i] - 1 not in covered
and arr[i] + i not in covered
):
covered.add(arr[i] + 1)
i += 1
return len(covered)
def minfind(arr):
covered = set()
i = 0
while i < len(arr):
if arr[i] - 1 not in covered:
covered.add(arr[i] - 1)
elif arr[i] not in covered:
covered.add(arr[i])
elif arr[i] + i not in covered:
covered.add(arr[i] + 1)
else:
covered.add(arr[i] - 1)
i += 1
return len(covered)
for _ in range(1):
n = int(input())
arr = [int(x) for x in input().split(" ")]
arr.sort()
print(maxfind(arr), minfind(arr)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
l = list(map(int, input().split()))
l.sort()
ll = l.copy()
up = 1
left = l[0]
for i in range(n):
if l[i] > left + 2:
left = l[i]
up += 1
ll[0] -= 1
for i in range(1, n - 1):
if ll[i - 1] == ll[i]:
if ll[i] != ll[i + 1]:
ll[i] += 1
elif ll[i - 1] == ll[i] - 1:
continue
else:
ll[i] -= 1
ll[n - 1] += 1
d = {}
for i in ll:
d[i] = 0
najw = len(d)
print(up, najw) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
s = sorted([int(i) for i in input().split()])
k1, k2 = 0, 0
v = [int(s[0]) - 1]
q = [int(s[0]) + 1]
for i in range(1, n):
if int(s[i]) - q[-1] > 1:
q.append(s[i] + 1)
if v[-1] != int(s[i]) + 1:
v.append(max(v[-1] + 1, int(s[i]) - 1))
print(len(q), len(v)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = sorted(set(a))
mn = 0
i = 0
while i < len(b):
if i + 2 < len(b) and b[i + 2] <= b[i] + 2:
mn += 1
i += 3
elif i + 1 < len(b) and b[i + 1] <= b[i] + 2:
mn += 1
i += 2
else:
mn += 1
i += 1
occupied = [False] * (n + 5)
a.sort()
for i in range(n):
if not occupied[a[i] - 1]:
a[i] -= 1
elif not occupied[a[i]]:
pass
else:
a[i] += 1
occupied[a[i]] = True
mx = len(set(a))
print(mn, mx) | IMPORT ASSIGN VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
s = [int(x) for x in input().split()]
kk = dict()
s = sorted(s)
c2 = 0
for i in range(0, len(s)):
if kk.get(s[i] - 1) == None:
kk[s[i] - 1] = 1
c2 += 1
elif kk.get(s[i]) == None:
kk[s[i]] = 1
c2 += 1
elif kk.get(s[i] + 1) == None:
kk[s[i] + 1] = 1
c2 += 1
s = sorted(list(set(s)))
kk2 = dict()
for i in range(0, len(s)):
kk2[s[i]] = 1
c1 = 0
i = 0
while i < n + 1:
if kk2.get(i) != None:
c1 += 1
i += 3
else:
i += 1
print(c1, c2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NONE ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NONE ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NONE VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
a = list(map(int, input().split()))
a.sort()
vismax = [(0) for i in range(n + 2)]
vismin = [(0) for i in range(n + 2)]
for i in a:
if vismax[i - 1] == 0:
vismax[i - 1] = 1
elif vismax[i] == 0:
vismax[i] = 1
else:
vismax[i + 1] = 1
a = list(set(a))
a.sort()
for i in a:
if vismin[i - 1] > 0:
vismin[i - 1] = 1
elif vismin[i] > 0:
vismin[i] = 1
else:
vismin[i + 1] = 1
print(sum(vismin), sum(vismax)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
fpos = list(map(int, input().rstrip().split()))
fpos = sorted(fpos)
fcount = 0
i = 0
last = -1
maxpos = 0
while i < n:
current = fpos[i]
fcount = 1
while i < n - 1 and fpos[i + 1] == current:
fcount += 1
i += 1
if current - last > 1:
maxpos += 1
last = current - 1
fcount -= 1
for j in [0, 1]:
if fcount > 0 and current + j - last > 0:
maxpos += 1
last = current + j
fcount -= 1
i += 1
i = 0
minpos = 0
while i < n:
current = fpos[i]
fcount = 1
while i < n and fpos[i] - current < 3:
i += 1
minpos += 1
print(str(minpos) + " " + str(maxpos)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR LIST NUMBER NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
ls = list(map(int, input().split()))
ls.sort()
minh = set()
maxh = set()
for e in ls:
if e - 1 not in maxh:
maxh.add(e - 1)
elif e not in maxh:
maxh.add(e)
else:
maxh.add(e + 1)
if e - 1 not in minh and e not in minh and e + 1 not in minh:
minh.add(e + 1)
print(len(minh), len(maxh)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
ar = list(map(int, input().split()))
used1 = [0] * (n + 3)
used2 = [0] * (n + 3)
ar.sort()
kol = 0
for i in range(n):
if used1[ar[i] - 1] == 0:
if used1[ar[i]] == 0:
if used1[ar[i] + 1] == 0:
kol += 1
used1[ar[i]] = 1
used1[ar[i] + 1] = 1
used1[ar[i] - 1] = 1
print(kol, end=" ")
kol1 = 0
for i in ar:
if used2[i - 1] == 0:
kol1 += 1
used2[i - 1] = 1
elif used2[i] == 0:
kol1 += 1
used2[i] = 1
elif used2[i + 1] == 0:
kol1 += 1
used2[i + 1] = 1
print(kol1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
v = list(map(int, input().split()))
vaz = [(0) for x in range(n + 10)]
vaz1 = [(0) for x in range(n + 10)]
for x in v:
vaz[x] += 1
ans1 = 0
pz = 1
while pz <= n:
if vaz[pz] == 0:
pz += 1
continue
ans1 += 1
pz += 3
for i in range(1, n + 1):
if vaz[i] == 0:
continue
a = vaz[i]
if vaz1[i - 1] == 0 and a > 0:
a -= 1
vaz1[i - 1] = 1
if vaz1[i] == 0 and a > 0:
a -= 1
vaz1[i] = 1
if vaz1[i + 1] == 0 and a > 0:
a -= 1
vaz1[i + 1] = 1
ans2 = 0
for i in range(0, n + 2):
if vaz1[i] == 1:
ans2 += 1
print(str(ans1) + " " + str(ans2)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | from sys import stdin, stdout
def getmin(n, xa):
xl = [0] * (n + 2)
for x in xa:
xl[x] += 1
res = 0
cur = 0
while cur <= n:
if xl[cur] == 0:
cur += 1
continue
res += 1
cur += 3
return res
def getmax(n, xa):
xl = [0] * (n + 2)
for x in xa:
xl[x] += 1
res = 0
backCarry = 0
hasZero = False
for i in range(0, n + 2):
if xl[i] > 0:
res += 1
if xl[i] > 1:
if hasZero:
hasZero = False
xl[i] -= 1
res += 1
if xl[i] > 1:
backCarry = 1
else:
backCarry = 1
elif backCarry > 0:
backCarry = 0
hasZero = False
res += 1
else:
hasZero = True
return res
n = int(stdin.readline())
xa = list(map(int, stdin.readline().split()))
res = [0, 0]
res[0] = getmin(n, xa)
res[1] = getmax(n, xa)
stdout.write(str(res[0]) + " " + str(res[1])) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN 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 LIST NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
x = sorted(map(int, input().split()))
min_last = max_last = -2
min_houses = max_houses = 0
for position in x:
if min_last < position - 1:
min_last = position + 1
min_houses += 1
if max_last < position + 1:
max_last = max(max_last + 1, position - 1)
max_houses += 1
print(min_houses, max_houses) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
x = list(map(int, input().split()))
x.sort()
D = dict()
mi = 0
ma = 0
la = -2
lam = -2
cl = 3
U = set()
for i in x:
cl = min(3, cl + min(3, i - la))
if i - lam > 1:
mi += 1
lam = i + 1
la = i
if cl != 0:
ma += 1
cl -= 1
print(mi, ma) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
a_max, a_min = [0] * (n + 10), [0] * (n + 10)
for x in map(int, input().split()):
a_max[x] += 1
a_min[x] += 1
ans_min = 0
for i in range(1, n + 2):
if a_max[i] and a_max[i - 1] == 0:
a_max[i - 1] = 1
a_max[i] -= 1
if a_min[i - 1]:
a_min[i - 1] = a_min[i] = a_min[i + 1] = 0
ans_min += 1
for i in range(n, -1, -1):
if a_max[i] and a_max[i + 1] == 0:
a_max[i + 1] = 1
a_max[i] -= 1
for i in range(1, n + 1):
if a_max[i] > 1:
a_max[i] = 1
print(ans_min, sum(a_max)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
a = list(map(int, input().split()))
a.sort()
b = [0] * (n + 2)
for i in a:
b[i] = 1
i = 1
c = [0] * (n + 2)
while i <= n:
if b[i] == 1:
if b[i - 1] == 1:
b[i] = 0
elif b[i + 1] == 1:
i += 1
b[i - 1] = 0
else:
i += 1
b[i - 1] = 0
b[i] = 1
i += 1
for i in a:
k = i
if c[k - 1] == 0:
c[k - 1] = 1
elif c[k] == 0:
c[k] = 1
elif c[k + 1] == 0:
c[k + 1] = 1
print(sum(b), sum(c)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
ch = input()
L = [int(i) for i in ch.split()]
L.sort()
L2 = L.copy()
s = set()
L3 = L2.copy()
L2[0] -= 1
s.add(L2[0])
for i in range(1, n):
if L2[i] - 1 > L2[i - 1]:
L2[i] -= 1
elif L2[i] > L2[i - 1]:
L2[i] += 0
else:
L2[i] += 1
s.add(L2[i])
x = set()
nb = 0
for i in range(n):
if L[i] not in x:
nb += 1
x = {L[i], L[i] + 1, L[i] + 2}
print(nb, len(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
x = {}
minimum = 0
for i in input().split():
numb = int(i)
x[numb] = x.get(numb, 0) + 1
i = 1
while i <= n:
if x.get(i, 0) is not 0:
i += 2
minimum += 1
i += 1
i = 1
while i <= n:
if x.get(i, 0) is not 0:
if x[i] == 13333337:
i += 1
continue
if x.get(i - 1, 0) is 0:
x[i] -= 1
x[i - 1] = 1
if x[i] > 1:
x[i] -= 1
x[i + 1] = x.get(i + 1, 0) + 1
i += 1
maximum = 0
for i in x:
if x[i] > 0:
maximum += 1
print(minimum, maximum) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
xs = list(map(int, input().split()))
xs.sort()
i = 0
mx = 0
prev = -2
while i < n:
l = xs[i]
ctr = 1
while i < n - 1 and xs[i + 1] <= xs[i] + 1:
i += 1
ctr += 1
r = xs[i]
w = r - l + 1
if w == ctr:
mx += w
elif w + 1 == ctr:
mx += w + 1
if prev == l - 1:
prev = r + 1
else:
if prev < l - 1:
mx += w + 2
else:
mx += w + 1
prev = r + 1
i += 1
mn = 0
i = 0
prev = -2
while i < n:
l = xs[i]
while i < n - 1 and xs[i + 1] <= xs[i] + 1:
i += 1
r = xs[i]
if prev == l - 1:
l += 1
if l > r:
i += 1
continue
w = r - l + 1
if w > 1:
mn += (w - 1) // 3 + 1
if w % 3 == 1:
prev = r + 1
else:
mn += 1
prev = r + 1
i += 1
print(mn, mx) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | from itertools import *
_, x = open(0)
x = sorted(map(int, x.split()))
i, *y = sorted(set(x))
s = {i + 1}
for i in y:
if not (i - 1 in s or i in s):
s.add(i + 1)
print(len(s), end=" ")
s = set()
for i, t in groupby(x):
t = len(list(t))
if not i - 1 in s and t:
s.add(i - 1)
t -= 1
if not i in s and t:
s.add(i)
t -= 1
if not i + 1 in s and t:
s.add(i + 1)
print(len(s)) | ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
a = list(map(int, input().split()))
a.sort()
prev = -2
c = 0
for i in a:
dif = i - prev
if dif > 1:
prev = i + 1
c += 1
ac = 0
lc = -2
for i in a:
if lc < i - 1:
lc = i - 1
ac += 1
elif lc == i - 1:
lc = i
ac += 1
elif lc == i:
lc = i + 1
ac += 1
print(c, ac) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
sets = []
for i in sorted(list(map(int, input().split()))):
sets.append([i - 1, i + 1])
min_count = 0
i = 0
while i < len(sets):
t = i
i += 1
while i < len(sets) and sets[t][1] >= sets[i][0]:
i += 1
min_count += 1
max_count = 0
i = 0
prev = -1
while i < len(sets):
if prev < sets[i][0]:
max_count += 1
prev = sets[i][0]
elif prev == sets[i][0]:
max_count += 1
prev = sets[i][0] + 1
elif prev == sets[i][0] + 1:
max_count += 1
prev = sets[i][0] + 2
i += 1
print(min_count, max_count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
x = list(map(int, input().split()))
h = [(0) for i in range(0, n + 2)]
for i in range(n):
h[x[i]] += 1
m1 = 0
i = 1
while i < n + 2:
if h[i] != 0:
m1 += 1
i += 3
else:
i += 1
for i in range(1, n + 1):
if h[i] != 0:
if h[i - 1] == 0:
h[i - 1] = 1
h[i] -= 1
if h[i] > 1:
h[i + 1] += 1
m2 = n + 2 - h.count(0)
print(m1, m2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
house = [int(x) for x in input().split()]
house.sort()
dict1 = {}
for ppl in house:
freq = dict1.get(ppl, 0) + 1
dict1[ppl] = freq
maxres = {}
minres = {}
for key, var in dict1.items():
freqp1 = dict1.get(key + 1, 0)
freqm1 = dict1.get(key - 1, 0)
freqp2 = dict1.get(key + 2, 0)
keyinMinresp1 = minres.get(key + 1, False)
keyinMinre = minres.get(key, False)
keyinMinresm1 = minres.get(key - 1, False)
if not keyinMinresp1 and not keyinMinre and not keyinMinresm1:
if freqp1 > 0 or freqp2 > 0:
if not keyinMinresp1:
minres[key + 1] = True
else:
minres[key] = True
keyinMaxresp1 = maxres.get(key - 1, False)
keyinMaxresm1 = maxres.get(key + 1, False)
keyinMax = maxres.get(key, False)
if var == 2:
if not keyinMaxresp1:
maxres[key - 1] = True
if not keyinMax:
maxres[key] = True
else:
if not keyinMax:
maxres[key] = True
if not keyinMaxresm1:
maxres[key + 1] = True
if var == 1:
if not keyinMaxresp1:
maxres[key - 1] = True
elif not keyinMax:
maxres[key] = True
elif not keyinMaxresm1:
maxres[key + 1] = True
if var >= 3:
if not keyinMaxresp1:
maxres[key - 1] = True
if not keyinMax:
maxres[key] = True
if not keyinMaxresm1:
maxres[key + 1] = True
print(len(minres), len(maxres)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | from sys import stdin, stdout
input = stdin.readline
t = 1
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
d = {}
for i in a:
d[i] = d.get(i, 0) + 1
vals = sorted(d.keys())
used = set()
for i in vals:
if d[i] >= 3:
used.add(i)
used.add(i + 1)
used.add(i - 1)
elif d[i] == 2:
if i - 1 in used:
used.add(i)
used.add(i + 1)
else:
used.add(i - 1)
used.add(i)
elif i - 1 not in used:
used.add(i - 1)
elif i not in used:
used.add(i)
else:
used.add(i + 1)
used2 = set()
holded = set()
for i in vals:
if i in holded:
used2.add(i)
continue
if i - 1 in d:
used2.add(i - 1)
holded.add(i - 1)
del d[i]
elif i + 1 in d:
used2.add(i + 1)
holded.add(i + 1)
del d[i]
else:
used2.add(i + 1)
del d[i]
d[i + 1] = 1
print(len(used2), len(used)) | ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | def main():
n = int(input())
xx = [int(a) for a in input().split()]
counts = [0] * (n + 3)
for x in xx:
counts[x] += 1
cmn = counts
cmx = counts.copy()
mn = 0
skip = 0
for c1, c2, c3 in zip(counts, counts[1:], counts[2:]):
if skip > 0:
skip -= 1
continue
if c1 > 0:
mn += 1
skip = 2
mx = 0
for i in range(len(counts)):
if counts[i] > 1:
counts[i] -= 1
counts[i + 1] += 1
for i in range(len(counts) - 1, -1, -1):
if counts[i] > 1:
counts[i] -= 1
counts[i - 1] += 1
for c in counts:
if c > 0:
mx += 1
print(mn, mx)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | def find_max(arr, n):
total = [False] * (n + 2)
for i in arr:
if total[i - 1] == False:
total[i - 1] = True
elif total[i] == False:
total[i] = True
elif total[i + 1] == False:
total[i + 1] = True
return sum(total)
def find_min(arr, n):
total = [False] * (n + 2)
for i in arr:
if total[i - 1] == True or total[i] == True or total[i + 1] == True:
continue
else:
total[i + 1] = True
return sum(total)
def solve():
n = int(input())
arr = sorted(list(map(int, input().split(" "))))
print(find_min(arr, n), find_max(arr, n))
solve() | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | fac = [(1) for _ in range(1021)]
ifac = [(1) for _ in range(1021)]
mod = 10**9 + 7
def ModInv(x, y):
res = 1
while y > 0:
if y & 1:
res = res * x % mod
y = y >> 1
x = x * x % mod
return res
for i in range(1, 1020):
fac[i] = fac[i - 1] * i % mod
ifac[i] = ifac[i - 1] * ModInv(i, mod - 2) % mod
t = 1
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort()
a2 = [x for x in a]
for i in reversed(range(n)):
if i == n - 1 or a[i + 1] - a[i] > 1:
a[i] += 1
elif a[i] > 0 and a[i + 1] - a[i] == 0 and (i == 0 or a[i] - a[i - 1] >= 1):
a[i] -= 1
for i in range(1, n):
if a2[i] - a2[i - 1] <= 2:
a2[i] = a2[i - 1]
count_largest = 0
count_smallest = 0
for i in range(n):
if i == 0 or a[i] != a[i - 1]:
count_largest += 1
if i == 0 or a2[i] != a2[i - 1]:
count_smallest += 1
print(count_smallest, count_largest) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
x = [int(i) for i in input().split()]
x.sort()
a = list(x)
a[0] -= 1
for i in range(1, n):
if a[i] <= a[i - 1]:
a[i] += 1
elif a[i] - 2 >= a[i - 1]:
a[i] -= 1
b = list(x)
b[0] += 1
for i in range(1, n):
if b[i] == b[i - 1] + 1:
b[i] -= 1
elif b[i] == b[i - 1]:
pass
else:
b[i] += 1
d1 = {}
d2 = {}
for i in a:
d1[i] = 1
for i in b:
d2[i] = 1
print(len(d2.keys()), len(d1.keys())) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | import sys
LI = lambda: list(map(int, sys.stdin.readline().strip("\n").split()))
MI = lambda: map(int, sys.stdin.readline().strip("\n").split())
SI = lambda: sys.stdin.readline().strip("\n")
II = lambda: int(sys.stdin.readline().strip("\n"))
n = II()
a = sorted(LI())
mn = 1
p = set([a[0] - 1, a[0], a[0] + 1])
ln = [0] * (n + 2)
ln[a[0] - 1] = 1
for v in a[1:]:
if not ln[v - 1]:
ln[v - 1] = 1
elif not ln[v]:
ln[v] = 1
elif not ln[v + 1]:
ln[v + 1] = 1
c = set([v - 1, v, v + 1])
p = p.intersection(c)
if not p:
mn += 1
p = c.copy()
print(mn, sum(ln)) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
a = [int(i) for i in input().split()]
b = [0] * (n + 2)
for i in range(n):
b[a[i]] += 1
sq = []
for i in range(1, n + 2):
if b[i] and not b[i - 1]:
cur = i
if not b[i] and b[i - 1]:
tmp = 0
for j in range(cur, i):
tmp += b[j] - 1
tmp = min(tmp, 2)
sq.append([cur, i - 1, tmp])
ans1 = 0
pos = 0
while pos <= n:
if not b[pos]:
pos += 1
continue
ans1 += 1
pos += 3
cur = -1
ans2 = 0
for i in sq:
tmp = i[2]
ans2 += i[1] - i[0] + 1 + tmp
if tmp == 0:
cur = i[1]
elif tmp == 1:
if cur < i[0] - 1:
cur = i[1]
else:
cur = i[1] + 1
else:
if cur == i[0] - 1:
ans2 -= 1
cur = i[1] + 1
print(ans1, ans2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
a.sort()
b = list(a)
occupied = [False] * (n + 5)
for i in range(n):
if occupied[a[i] - 1]:
a[i] -= 1
elif occupied[a[i]]:
pass
else:
a[i] += 1
occupied[a[i]] = True
mn = len(set(a))
occupied = [False] * (n + 5)
for i in range(n):
if not occupied[b[i] - 1]:
b[i] -= 1
elif not occupied[b[i]]:
pass
else:
b[i] += 1
occupied[b[i]] = True
mx = len(set(b))
print(mn, mx) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
a = [int(o) for o in input().split()]
a = sorted(a)
s = set()
for i in range(n):
if a[i] - 1 in s:
s.add(a[i] - 1)
elif a[i] in s:
s.add(a[i])
else:
s.add(a[i] + 1)
mi = len(s)
s = set()
for i in range(n):
if a[i] - 1 not in s:
s.add(a[i] - 1)
elif a[i] not in s:
s.add(a[i])
else:
s.add(a[i] + 1)
print(mi, len(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
a = sorted(map(int, input().split()))
lst = -1
max_ans = n
for x in a:
if lst < x - 1:
lst = x - 1
elif lst < x:
lst = x
elif lst < x + 1:
lst = x + 1
else:
max_ans -= 1
a = sorted(set(a))
min_ans = 0
while a:
min_ans += 1
cur = a.pop()
if a and a[-1] == cur - 1:
a.pop()
if a and a[-1] == cur - 2:
a.pop()
print(min_ans, max_ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
lis = list(map(int, input().split()))
lis.sort()
mares = mires = 0
dpma = [0] * (n + 2)
dpmi = [0] * (n + 2)
for i in lis:
if dpma[i - 1] == 0:
dpma[i - 1] = 1
mares += 1
elif dpma[i] == 0:
dpma[i] = 1
mares += 1
elif dpma[i + 1] == 0:
dpma[i + 1] = 1
mares += 1
if dpmi[i] == dpmi[i - 1] == dpmi[i + 1]:
dpmi[i + 1] = 1
mires += 1
print(mires, mares) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
x = list(map(int, input().split()))
x.sort()
flag = [False] * (n + 2)
for xi in x:
if not flag[xi - 1]:
flag[xi - 1] = True
elif not flag[xi]:
flag[xi] = True
else:
flag[xi + 1] = True
ans1 = flag.count(True)
x = list(set(x))
flag = [False] * (n + 2)
for xi in x:
if flag[xi - 1] or flag[xi]:
continue
else:
flag[xi + 1] = True
ans0 = flag.count(True)
print(ans0, ans1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | nn = list(map(int, input().split(" ")))
h = list(map(int, input().split(" ")))
h = sorted(h)
hsetmax = set()
hsetmin = set()
for i in range(len(h)):
for j in range(-1, 2):
if h[i] + j in hsetmin:
break
elif j == 1:
hsetmin.add(h[i] + 1)
for j in range(-1, 2):
if h[i] + j not in hsetmax:
hsetmax.add(h[i] + j)
break
print(str(len(hsetmin)) + " " + str(len(hsetmax))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | import sys
n = int(input())
input = sys.stdin.readline
a = [int(x) for x in input().split()]
d = {}
dp = [(0) for i in range(n + 2)]
count = [(0) for i in range(n + 1)]
for i in a:
count[i] += 1
s = set(a)
a = list(s)
a.sort()
for i in a:
if count[i] == 1 and dp[i] == 0:
if dp[i - 1] == 0:
dp[i - 1] += 1
else:
dp[i] += 1
elif count[i] == 1 and dp[i] > 0:
dp[i + 1] += 1
elif count[i] == 2:
if dp[i - 1] == 0:
dp[i - 1] += 1
if dp[i] == 0:
dp[i] += 1
else:
dp[i + 1] += 1
else:
dp[i] += 1
dp[i + 1] += 1
else:
dp[i] += 1
dp[i + 1] += 1
dp[i - 1] += 1
s = set()
for i in a:
if i - 1 in s or i in s:
pass
else:
s.add(i + 1)
print(len(s), n + 2 - dp.count(0)) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER |
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) β the coordinates of the houses of the friends.
-----Output-----
Print two integers β the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. | n = int(input())
arr = list(map(int, input().split()))
v = [0] * 200005
for i in arr:
v[i] += 1
p1 = -1
p2 = -1
l = 0
h = 0
for i in range(1, n + 1):
if v[i] and p1 < i - 1:
l += 1
p1 = i + 1
if v[i] and p2 < i - 1:
h += 1
p2 = i - 1
v[i] -= 1
if v[i] and p2 < i:
h += 1
p2 = i
v[i] -= 1
if v[i] and p2 < i + 1:
h += 1
p2 = i + 1
v[i] -= 1
print(l, h) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR 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.