description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
op = []
def amt_to_be_subtracted(N):
k = 0
curr = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
for note in curr:
if note > N:
k += 1
return curr[k]
while N != 0:
minus = amt_to_be_subtracted(N)
op.append(minus)
N = N - minus
return op
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, amount):
denominations = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
n = len(denominations)
i = n - 1
ans = []
while i >= 0:
if amount < denominations[i]:
i -= 1
elif amount == denominations[i]:
ans.append(denominations[i])
break
else:
ans.append(denominations[i])
amount -= denominations[i]
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
p = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
ans = []
for i in range(N):
for i in p:
if N >= i:
ans.append(i)
N = N - i
break
if N == 0:
break
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
arr = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
count = []
while N != 0:
for coin in arr:
if coin <= N:
n = N // coin
N = N - N // coin * coin
for i in range(n):
count.append(coin)
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST WHILE VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
lt = []
cur = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
x = 0
for i in range(10):
if cur[i] > N:
break
x = i
while N > 0:
if N < cur[x]:
x -= 1
else:
lt.append(cur[x])
N -= cur[x]
return lt
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
coins = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
def minPartition(self, N):
notes = []
coinsIdx = 0
while N and coinsIdx < len(Solution.coins):
noteNums = N // Solution.coins[coinsIdx]
N = N % Solution.coins[coinsIdx]
for i in range(noteNums):
notes.append(Solution.coins[coinsIdx])
coinsIdx += 1
return notes
|
CLASS_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
if N == 1:
return [1]
notes = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
ans = []
for note in notes:
cnt = N // note
if cnt:
for j in range(cnt):
ans.append(note)
N = N % note
return ans
|
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
ans = []
arr = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
while N != 0:
for i in range(0, 10):
a = N // arr[i]
N = N % arr[i]
for j in range(a):
ans.append(arr[i])
if N == 0:
break
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
currency = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
arr = []
for i in currency:
while N - i >= 0:
arr.append(i)
N -= i
return arr
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
ans = []
arr = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
for x in arr:
coin = N // x
ans += [x] * coin
N = N % x
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
l = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
l1 = []
for i in l:
if i <= N:
l1.append(i)
else:
break
l1.reverse()
s = 0
out = []
i = 0
while s <= N and i < len(l1):
if s + l1[i] <= N:
out.append(l1[i])
s += l1[i]
else:
i += 1
return out
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
def dyn(v):
coins = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000][::-1]
result = []
while v:
for val in coins:
if val <= v:
result += [val] * (v // val)
v -= val * (v // val)
return result
class Solution:
def minPartition(self, N):
return dyn(N)
|
FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST WHILE VAR FOR VAR VAR IF VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
result = []
coins = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
coins.sort(reverse=True)
while N:
for coin in coins:
if coin <= N:
result.append(coin)
N -= coin
break
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
money = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
result = []
for i in range(9, -1, -1):
result += [money[i]] * (N // money[i])
N = N % money[i]
if N == 0:
break
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP LIST VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, V):
deno = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
n = len(deno)
ans = []
i = n - 1
while i >= 0:
while V >= deno[i]:
V -= deno[i]
ans.append(deno[i])
i -= 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
coins = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
amount = N
i = len(coins) - 1
while i >= 0:
if coins[i] <= amount:
break
i -= 1
out = []
while amount > 0 and i >= 0:
times = amount // coins[i]
out = out + [coins[i]] * times
amount = amount % coins[i]
while i >= 0:
if coins[i] <= amount:
break
i -= 1
return out
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
set_list = [None] * N
coins = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}
def find_min_set(coins, set_list, n):
min_set = []
min_set_len = float("Inf")
for coin in coins:
if coin == n:
cur_set = [coin]
cur_set_len = 1
elif coin < n:
diff = n - coin
cur_set = set_list[diff - 1] + [coin]
cur_set_len = len(set_list[diff - 1]) + 1
else:
continue
if cur_set_len < min_set_len:
min_set_len = cur_set_len
min_set = cur_set
min_set.sort(reverse=True)
set_list[n - 1] = min_set
for i in range(N):
find_min_set(coins, set_list, i + 1)
return set_list[N - 1]
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER LIST VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
coinsList = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
coinsList = coinsList[::-1]
coins = []
while N > 0:
for coin in coinsList:
if N >= coin:
coins.append(coin)
N -= coin
break
return coins
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
target = N
ar = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
ar = ar[::-1]
ans = []
for coin in ar:
n = target // coin
ans.extend([coin] * n)
target -= n * coin
if target == 0:
break
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
coins_available = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
rem_money = N
ans = []
i = 0
while rem_money != 0 and i < len(coins_available):
if coins_available[i] <= rem_money:
rem_money -= coins_available[i]
ans.append(coins_available[i])
if rem_money >= coins_available[i]:
pass
else:
i += 1
else:
i += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def __init__(self):
self.res = []
def solve(self, arr, N, summ, ct, dp):
if summ == 0:
self.res.append(ct)
return 1
if summ < 0:
return 0
if dp[N][summ] != -1:
return dp[N][summ]
if summ - arr[N - 1] >= 0:
ct.append(arr[N - 1])
dp[N][summ] = self.solve(arr, N, summ - arr[N - 1], ct, dp) or self.solve(
arr, N - 1, summ, ct, dp
)
else:
dp[N][summ] = self.solve(arr, N - 1, summ, ct, dp)
return dp[N][summ]
def minPartition(self, N):
if N <= 0:
return 0
arr = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
dp = [[(-1) for i in range(N + 1)] for j in range(len(arr) + 1)]
a = self.solve(arr, len(arr), N, [], dp)
ind = 0
if len(self.res) > 0:
tot = sum(self.res[0])
for i in range(1, len(self.res)):
a = sum(self.res[i])
if a > tot:
tot = a
ind = i
return self.res[ind]
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR LIST VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
coins = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
ans = []
i = 9
while i >= 0:
change = N // coins[i]
for coin in range(change):
ans.append(coins[i])
if change != 0:
N = N % (coins[i] * change)
i -= 1
if N == 0:
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
changes = []
while N:
if N >= 2000:
changes.append(2000)
N = N - 2000
elif N >= 500:
changes.append(500)
N = N - 500
elif N >= 200:
changes.append(200)
N = N - 200
elif N >= 100:
changes.append(100)
N = N - 100
elif N >= 50:
changes.append(50)
N = N - 50
elif N >= 20:
changes.append(20)
N = N - 20
elif N >= 10:
changes.append(10)
N = N - 10
elif N >= 5:
changes.append(5)
N = N - 5
elif N >= 2:
changes.append(2)
N = N - 2
elif N >= 1:
changes.append(1)
N = N - 1
return changes
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N.
Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required.
Example 1:
Input: N = 43
Output: 20 20 2 1
Explaination:
Minimum number of coins and notes needed
to make 43.
Example 2:
Input: N = 1000
Output: 500 500
Explaination: minimum possible notes
is 2 notes of 500.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
|
class Solution:
def minPartition(self, N):
result = []
denomination = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000]
while N > 0:
for i in range(len(denomination) - 1, -1, -1):
if N >= denomination[i]:
N = N - denomination[i]
result.append(denomination[i])
break
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, jobs, n):
jobs.sort(key=lambda x: (-x.profit, x.deadline))
d = 0
for j in jobs:
d = max(d, j.deadline)
res = 0
slot = [False] * d
for j in jobs:
d, p = j.deadline - 1, j.profit
for i in range(d, -1, -1):
if not slot[i]:
res += p
slot[i] = True
break
return [sum(slot), res]
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN LIST FUNC_CALL VAR VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda j: j.profit, reverse=True)
scheduled_jobs = {}
for job in Jobs:
deadline = job.deadline
for i in range(deadline, 0, -1):
if not i in scheduled_jobs:
scheduled_jobs[i] = job
break
scheduled_jobs_list = list(scheduled_jobs.values())
num_scheduled_jobs = 0
profit = 0
for job in scheduled_jobs_list:
num_scheduled_jobs += 1
profit += job.profit
return [num_scheduled_jobs, profit]
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, jobs, n):
nj = 0
mpft = 0
jobs.sort(key=lambda x: -x.profit)
jobs_done = set()
min_dl = 0
for j in jobs:
dl, p = j.deadline, j.profit
while dl > min_dl:
if dl not in jobs_done:
jobs_done.add(dl)
nj += 1
mpft += p
break
dl -= 1
return nj, mpft
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
res, count = 0, 0
Jobs = sorted(Jobs, key=lambda x: x.profit, reverse=True)
result = [(0) for i in range(n)]
slot = [(False) for i in range(n)]
for i in range(n):
for j in range(Jobs[i].deadline - 1, -1, -1):
if not slot[j]:
result[j] = i
slot[j] = True
break
for i in range(n):
if slot[i]:
res += Jobs[result[i]].profit
count += 1
ans = []
ans.append(count)
ans.append(res)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
joblist = []
check = [False] * n
for i in Jobs:
joblist.append([i.profit, i.deadline])
joblist.sort(reverse=True)
for i in range(n):
j = joblist[i][1] - 1
while j >= 0:
if check[j] == False:
check[j] = joblist[i][0]
break
j -= 1
profit1 = 0
count = 0
for i in check:
if i != False:
profit1 += i
count += 1
return [count, profit1]
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: x.profit, reverse=True)
count = 0
temp = []
nsum = 0
arr = [(False) for _ in range(n)]
for i in range(0, n):
t = Jobs[i].deadline - 1
p = Jobs[i].profit
while t >= 0:
if arr[t] is False:
nsum += p
count += 1
arr[t] = True
t = -1
else:
t -= 1
return count, nsum
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
def get_profit(job):
return job.profit
Jobs = sorted(Jobs, key=get_profit, reverse=True)
slots = [1] * n
jobs = 0
profit = 0
for job in Jobs:
i = min(job.deadline, n)
for i in range(job.deadline - 1, -1, -1):
if slots[i]:
slots[i] = 0
jobs += 1
profit += job.profit
break
return jobs, profit
|
CLASS_DEF FUNC_DEF FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: -x.profit)
vis = [False] * 101
cnt, profit = 0, 0
for i in range(n):
job = Jobs[i]
p, d = job.profit, job.deadline
if not vis[d]:
vis[d] = True
cnt += 1
profit += p
else:
for j in range(d - 1, 0, -1):
if not vis[j]:
vis[j] = True
cnt += 1
profit += p
break
return [cnt, profit]
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
maxDline = 0
l = len(Jobs)
for i in range(l):
maxDline = max(maxDline, Jobs[i].deadline)
boundTime = [(0) for i in range(maxDline + 1)]
Jobs.sort(key=lambda x: x.profit, reverse=True)
cnt = 0
maxprofit = 0
for job in Jobs:
i = job.deadline
while i > 0 and boundTime[i] != 0:
i -= 1
if i <= 0:
continue
boundTime[i] = job.id
cnt += 1
maxprofit += job.profit
return [cnt, maxprofit]
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
d = dict()
profit = 0
j_done = 0
Jobs.sort(key=lambda x: x.profit, reverse=True)
for j in Jobs:
if d.get(j.deadline, 1):
profit += j.profit
j_done += 1
d[j.deadline] = 0
else:
ind = j.deadline - 1
while ind > 0:
if d.get(ind, 1):
profit += j.profit
j_done += 1
d[ind] = 0
break
ind -= 1
return j_done, profit
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs = list(Jobs)
A = [0] * n
Jobs = sorted(Jobs, key=lambda t: t.profit, reverse=True)
c = 0
for i in range(n):
if A[Jobs[i].deadline - 1] == 0:
A[Jobs[i].deadline - 1] = Jobs[i].profit
c += 1
else:
j = Jobs[i].deadline - 1
while A[j] != 0 and j > -1:
j -= 1
if j != -1:
A[j] = Jobs[i].profit
c += 1
return c, sum(A)
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
jobs = []
maxtime = 0
for i in range(n):
maxtime = max(maxtime, Jobs[i].deadline)
jobs.append([Jobs[i].id, Jobs[i].deadline, Jobs[i].profit])
jobs.sort(key=lambda x: x[2], reverse=True)
no_jobs = 0
visited = [False] * (maxtime + 1)
max_profit = 0
for i in jobs:
for j in range(i[1], 0, -1):
if visited[j] == False:
visited[j] = True
no_jobs += 1
max_profit += i[2]
break
return [no_jobs, max_profit]
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, jobs, n):
sorted_jobs = sorted(jobs, key=lambda x: x.profit, reverse=True)
res = [False] * n
job_order = ["-1"] * n
count = 0
profit = 0
for job in sorted_jobs:
for i in range(min(n, job.deadline) - 1, -1, -1):
if res[i] is False:
res[i] = True
job_order[i] = job.id
count += 1
profit += job.profit
break
return count, profit
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: x.profit, reverse=True)
maxTime = max(Jobs, key=lambda x: x.deadline)
indexList = [x for x in range(maxTime.deadline - 1, 0, -1)]
summ = maxTime.profit
count = 1
Jobs.remove(maxTime)
for job in Jobs:
for i in range(len(indexList)):
dline = indexList[i]
if dline <= job.deadline:
summ += job.profit
count += 1
indexList.pop(i)
break
if not indexList:
return count, summ
return count, summ
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR RETURN VAR VAR RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, jobs, n):
jobs.sort(key=lambda x: x.profit, reverse=True)
d = max(jobs, key=lambda x: x.deadline)
ans = [0, 0]
visited = [0] * (d.deadline + 1)
for i in range(len(jobs)):
j = jobs[i].deadline
while j > 0:
if visited[j] == 1:
j -= 1
else:
ans[1] += jobs[i].profit
visited[j] = 1
ans[0] += 1
break
return ans
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: x.profit, reverse=True)
job_done = [False] * n
total_profit = 0
jobs_done = 0
for job in Jobs:
deadline = job.deadline
while deadline > 0:
if not job_done[deadline - 1]:
total_profit += job.profit
jobs_done += 1
job_done[deadline - 1] = True
break
deadline -= 1
return jobs_done, total_profit
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
time = [0] * (n + 1)
profit = 0
c = 0
j = list(sorted(Jobs, key=lambda x: x.profit, reverse=True))
for i in j:
for k in range(i.deadline, 0, -1):
if time[k] == 0:
profit += i.profit
time[k] = 1
c += 1
break
return [c, profit]
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: x.profit, reverse=True)
max_d = max(job.deadline for job in Jobs)
slots = (n + 1) * [0]
res, days = 0, 0
for i in range(n):
dl = Jobs[i].deadline
while dl >= 1:
if slots[dl] == 0:
gain = Jobs[i].profit
res += gain
days += 1
slots[dl] = 1
break
dl -= 1
return [days, res]
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: x.profit, reverse=True)
ans = 0
day = 0
vis = [0] * (n + 1)
for i in range(n):
tmp = Jobs[i].deadline
while tmp >= 1:
if vis[tmp] == 0:
ans += Jobs[i].profit
day += 1
vis[tmp] = 1
break
tmp -= 1
return [day, ans]
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
sorted_job_list = sorted(Jobs, key=lambda k: -k.profit)
profit = 0
jobs_done = 0
job_seq = [(0) for i in range(n)]
for job in sorted_job_list:
deadline = job.deadline - 1
for i in range(deadline, -1, -1):
if job_seq[i] == 0:
job_seq[i] = 1
jobs_done += 1
profit += job.profit
break
return jobs_done, profit
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: x.profit, reverse=True)
slot = [False] * n
count, maxp = 0, 0
for i in range(n):
for j in range(min(n, Jobs[i].deadline) - 1, -1, -1):
if slot[j] == False:
slot[j] = True
count += 1
maxp += Jobs[i].profit
break
return [count, maxp]
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
p = 0
t = 0
a = [0] * 1000
Jobs = sorted(Jobs, reverse=True, key=lambda v: v.profit)
for i in Jobs:
d = i.deadline
pr = i.profit
while d >= 1 and a[d] == 1:
d -= 1
if d >= 1:
a[d] = 1
t += 1
p += pr
return [t, p]
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: x.profit, reverse=True)
profit = 0
visited = [False] * (n + 1)
c = 0
for i in range(n):
x = Jobs[i].deadline
for j in range(x - 1, -1, -1):
if visited[j] == False:
visited[j] = True
profit += Jobs[i].profit
c += 1
break
return c, profit
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
jobs = []
maxDeadline = -1
for i in Jobs:
curr = [i.deadline, i.profit]
jobs.append(curr)
maxDeadline = max(maxDeadline, i.deadline)
jobs = sorted(jobs, key=lambda x: (-x[1], -x[0]))
jobsDone = [(-1) for i in range(maxDeadline + 1)]
jobsCompleted = 0
totalProfit = 0
for i in range(len(jobs)):
deadline, profit = jobs[i][0], jobs[i][1]
if jobsDone[deadline - 1] == -1:
jobsDone[deadline - 1] = profit
totalProfit += profit
jobsCompleted += 1
else:
while deadline > 1:
deadline -= 1
if jobsDone[deadline - 1] == -1:
jobsDone[deadline - 1] = profit
totalProfit += profit
jobsCompleted += 1
break
return [jobsCompleted, totalProfit]
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
def profit(job):
return job.profit
count = 0
pro = 0
dead = [0] * 100
Jobs.sort(key=profit, reverse=True)
for i in Jobs:
j = i.deadline - 1
while j >= 0:
if not dead[j]:
dead[j] = 1
count += 1
pro += i.profit
break
j -= 1
return [count, pro]
|
CLASS_DEF FUNC_DEF FUNC_DEF RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: (-x.profit, x.deadline))
maxx, count, ans = 0, 0, 0
for i in Jobs:
maxx = max(maxx, i.deadline)
check = [False] * maxx
for i in Jobs:
d = i.deadline
d -= 1
for j in range(d, -1, -1):
if check[j] != True:
check[j] = True
ans += i.profit
count += 1
break
return [count, ans]
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
Jobs.sort(key=lambda x: x.profit, reverse=True)
time = 0
profit = 0
vis = [0] * n
for i in Jobs:
j = min(n, i.deadline - 1)
for k in range(j, -1, -1):
if vis[k] == 0:
vis[k] = 1
profit += i.profit
time += 1
break
return time, profit
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
ans = [0] * 100
Jobs = sorted(Jobs, key=lambda x: x.profit, reverse=True)
profit1 = 0
profit2 = 0
slots = [(0) for i in range(n)]
for i in range(n):
for j in range(Jobs[i].deadline - 1, -1, -1):
if slots[j] == 0:
slots[j] = 1
profit1 += Jobs[i].profit
profit2 += 1
break
return [profit2, profit1]
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
profit = 0
count = 0
l = []
for i in range(n):
l.append([Jobs[i].profit, Jobs[i].deadline])
l.sort(reverse=True)
time = [0] * n
for i in range(n):
for j in range(min(n - 1, l[i][1] - 1), -1, -1):
if time[j] == 0:
time[j] = 1
profit += l[i][0]
count += 1
break
return [count, profit]
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
jobsDone = [False] * n
Jobs.sort(key=lambda x: -x.profit)
cnt, profit = 0, 0
for job in Jobs:
for i in range(min(job.deadline - 1, n - 1), -1, -1):
if jobsDone[i] == False:
cnt += 1
profit += job.profit
jobsDone[i] = True
break
return [cnt, profit]
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
def tt(Jobs):
return Jobs.profit
Jobs = sorted(Jobs, key=tt, reverse=True)
l = [0] * n
for i in range(n):
x = Jobs[i].deadline
y = Jobs[i].profit
for i in range(x - 1, -1, -1):
if l[i] == 0 or l[i] < y:
l[i] = y
break
count = 0
maxprofit = 0
for i in range(len(l)):
if l[i] != 0:
count += 1
maxprofit += l[i]
return [count, maxprofit]
|
CLASS_DEF FUNC_DEF FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
temp = [0] * n
def fn(ele):
return ele.profit
Jobs.sort(key=fn, reverse=True)
for i in range(n):
if temp[Jobs[i].deadline - 1] == 0:
temp[Jobs[i].deadline - 1] = Jobs[i].profit
elif Jobs[i].profit > temp[Jobs[i].deadline - 1]:
temp[Jobs[i].deadline - 1] = Jobs[i].profit
else:
for j in range(Jobs[i].deadline - 1, -1, -1):
if Jobs[i].profit > temp[j]:
temp[j] = Jobs[i].profit
break
count = 0
ans = 0
for i in range(len(temp)):
if temp[i] != 0:
count += 1
ans += temp[i]
return [count, ans]
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR RETURN LIST VAR VAR
|
Given a set of N jobs where each job_{i} has a deadline and profit associated with it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Job_{id}, Deadline, Profit) associated with that Job.
Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Since at deadline 1 Job3 can give the maximum profit and for deadline 4 we left with only Job1 hence Job_{1} and Job_{3 }can be done with maximum profit of 60 (20+40).
Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
(4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).
Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns an array ans[ ] in which ans[0] contains the count of jobs and ans[1] contains maximum profit.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= Deadline <= N
1 <= Profit <= 500
|
class Solution:
def JobScheduling(self, Jobs, n):
cnt = 0
profit = 0
Jobs.sort(key=lambda x: -x.profit)
result = [False] * n
for i in Jobs:
for j in range(min(i.deadline - 1, n - 1), -1, -1):
if result[j] is False:
cnt += 1
result[j] = True
profit += i.profit
break
return [cnt, profit]
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR RETURN LIST VAR VAR
|
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. [Image]
Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.
A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.
Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 β€ l β€ r β€ |s| and the string s_{l}s_{l} + 1... s_{r} is pretty, where s_{i} is i-th character of s.
Joyce doesn't know anything about bracket sequences, so she asked for your help.
-----Input-----
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 β€ |s| β€ 5000).
-----Output-----
Print the answer to Will's puzzle in the first and only line of output.
-----Examples-----
Input
((?))
Output
4
Input
??()??
Output
7
-----Note-----
For the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
|
s = input()
n = len(s)
res = 0
for i in range(n):
depth1, depth2 = 0, 0
for c in s[i:]:
if c == "(":
depth1 += 1
depth2 += 1
elif c == ")":
depth1 -= 1
depth2 -= 1
else:
depth1 += 1
depth2 -= 1
if depth1 < 0:
break
depth2 = max(depth2, 0)
if depth1 & 1 == 0 and depth2 == 0:
res += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. [Image]
Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.
A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.
Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 β€ l β€ r β€ |s| and the string s_{l}s_{l} + 1... s_{r} is pretty, where s_{i} is i-th character of s.
Joyce doesn't know anything about bracket sequences, so she asked for your help.
-----Input-----
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 β€ |s| β€ 5000).
-----Output-----
Print the answer to Will's puzzle in the first and only line of output.
-----Examples-----
Input
((?))
Output
4
Input
??()??
Output
7
-----Note-----
For the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
|
st = input()
le = len(st)
ans = 0
for i in range(le):
l = 0
w = 0
for j in range(i, le):
if st[j] == "(":
l += 1
elif st[j] == ")":
l -= 1
else:
w += 1
if l + w < 0:
break
elif w > l:
xx = l
l = w
w = xx
elif l == w:
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. [Image]
Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.
A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.
Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 β€ l β€ r β€ |s| and the string s_{l}s_{l} + 1... s_{r} is pretty, where s_{i} is i-th character of s.
Joyce doesn't know anything about bracket sequences, so she asked for your help.
-----Input-----
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 β€ |s| β€ 5000).
-----Output-----
Print the answer to Will's puzzle in the first and only line of output.
-----Examples-----
Input
((?))
Output
4
Input
??()??
Output
7
-----Note-----
For the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
|
s = input()
res, n = 0, len(s)
for i in range(n - 1):
cur, q, ind = 0, 0, i
while ind < n:
if s[ind] == "(":
cur += 1
elif s[ind] == ")":
cur -= 1
else:
q += 1
if cur + q < 0:
break
if q > cur:
cur, q = q, cur
res += cur == q
ind += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR WHILE VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. [Image]
Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.
A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.
Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 β€ l β€ r β€ |s| and the string s_{l}s_{l} + 1... s_{r} is pretty, where s_{i} is i-th character of s.
Joyce doesn't know anything about bracket sequences, so she asked for your help.
-----Input-----
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 β€ |s| β€ 5000).
-----Output-----
Print the answer to Will's puzzle in the first and only line of output.
-----Examples-----
Input
((?))
Output
4
Input
??()??
Output
7
-----Note-----
For the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
|
s = input()
a = 0
n = len(s)
for i in range(n):
l = 0
k = 0
for j in range(i, n):
l += s[j] == "("
l -= s[j] == ")"
k += s[j] == "?"
if l + k < 0:
break
if k > l:
l, k = k, l
if l == k:
a += 1
print(a)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR STRING VAR VAR VAR STRING IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. [Image]
Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.
A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.
Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 β€ l β€ r β€ |s| and the string s_{l}s_{l} + 1... s_{r} is pretty, where s_{i} is i-th character of s.
Joyce doesn't know anything about bracket sequences, so she asked for your help.
-----Input-----
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 β€ |s| β€ 5000).
-----Output-----
Print the answer to Will's puzzle in the first and only line of output.
-----Examples-----
Input
((?))
Output
4
Input
??()??
Output
7
-----Note-----
For the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
|
s = input()
ans = 0
for i in range(0, len(s) - 1):
addQ = 0
subQ = 0
cur = 0
for j in range(i, len(s)):
if s[j] == "(":
cur += 1
elif s[j] == ")":
cur -= 1
if cur < 0 and subQ > 0:
cur += 2
subQ -= 1
addQ += 1
elif cur > 0:
subQ += 1
cur -= 1
else:
addQ += 1
cur += 1
if cur < 0:
break
if cur == 0:
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. [Image]
Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.
A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.
Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 β€ l β€ r β€ |s| and the string s_{l}s_{l} + 1... s_{r} is pretty, where s_{i} is i-th character of s.
Joyce doesn't know anything about bracket sequences, so she asked for your help.
-----Input-----
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 β€ |s| β€ 5000).
-----Output-----
Print the answer to Will's puzzle in the first and only line of output.
-----Examples-----
Input
((?))
Output
4
Input
??()??
Output
7
-----Note-----
For the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
|
s = input()
n = len(s)
res = 0
for st in range(n):
l, r = 0, 0
for e in range(st, n):
if s[e] == "(":
l += 1
r += 1
elif s[e] == ")":
l -= 1
r -= 1
elif s[e] == "?":
l -= 1
r += 1
if r < 0:
break
if l < 0:
l += 2
if (e - st + 1) % 2 == 0 and (l <= 0 and 0 <= r) and abs(l) % 2 == 0:
res += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. [Image]
Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.
A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.
Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 β€ l β€ r β€ |s| and the string s_{l}s_{l} + 1... s_{r} is pretty, where s_{i} is i-th character of s.
Joyce doesn't know anything about bracket sequences, so she asked for your help.
-----Input-----
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 β€ |s| β€ 5000).
-----Output-----
Print the answer to Will's puzzle in the first and only line of output.
-----Examples-----
Input
((?))
Output
4
Input
??()??
Output
7
-----Note-----
For the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
|
s = input()
n = len(s)
f = [[(0) for i in range(n)] for j in range(n)]
g = [[(0) for i in range(n)] for j in range(n)]
for l in range(n):
count = 0
for r in range(l, n):
if s[r] == "(" or s[r] == "?":
count += 1
else:
count -= 1
if count < 0:
break
else:
f[l][r] = 1
for r in range(n):
count = 0
for l in range(r, -1, -1):
if s[l] == ")" or s[l] == "?":
count += 1
else:
count -= 1
if count < 0:
break
else:
g[l][r] = 1
cnt = 0
for l in range(n):
for r in range(l, n):
if f[l][r] == 1 and g[l][r] == 1 and (r - l) % 2 == 1:
cnt += 1
print(cnt)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. [Image]
Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.
A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.
Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 β€ l β€ r β€ |s| and the string s_{l}s_{l} + 1... s_{r} is pretty, where s_{i} is i-th character of s.
Joyce doesn't know anything about bracket sequences, so she asked for your help.
-----Input-----
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 β€ |s| β€ 5000).
-----Output-----
Print the answer to Will's puzzle in the first and only line of output.
-----Examples-----
Input
((?))
Output
4
Input
??()??
Output
7
-----Note-----
For the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
|
def main():
s = input()
l = len(s)
pretty_count = 0
for i in range(l):
left_paren_count = 0
right_paren_count = 0
wild_count = 0
for j in range(i, l):
if s[j] == "(":
left_paren_count += 1
elif s[j] == ")":
right_paren_count += 1
else:
wild_count += 1
if left_paren_count + wild_count < right_paren_count:
break
if left_paren_count < wild_count + right_paren_count:
wild_count -= 1
left_paren_count += 1
if wild_count < 0:
break
if left_paren_count == wild_count + right_paren_count:
pretty_count += 1
print(pretty_count)
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given a string S of length n with each character being one of the first m lowercase English letters.
Calculate how many different strings T of length n composed from the first m lowercase English letters exist such that the length of LCS (longest common subsequence) between S and T is n - 1.
Recall that LCS of two strings S and T is the longest string C such that C both in S and T as a subsequence.
-----Input-----
The first line contains two numbers n and m denoting the length of string S and number of first English lowercase characters forming the character set for strings (1 β€ n β€ 100 000, 2 β€ m β€ 26).
The second line contains string S.
-----Output-----
Print the only line containing the answer.
-----Examples-----
Input
3 3
aaa
Output
6
Input
3 3
aab
Output
11
Input
1 2
a
Output
1
Input
10 9
abacadefgh
Output
789
-----Note-----
For the first sample, the 6 possible strings T are: aab, aac, aba, aca, baa, caa.
For the second sample, the 11 possible strings T are: aaa, aac, aba, abb, abc, aca, acb, baa, bab, caa, cab.
For the third sample, the only possible string T is b.
|
def main():
n, m = map(int, input().split())
s = input()
k = sum(s[i] != s[i - 1] for i in range(1, n)) + 1
x = i = 0
while i < n - 1:
if s[i] != s[i + 1]:
j = i
while i + 2 < n and s[i] == s[i + 2]:
i += 1
j = i - j + 2
x += j * (j - 1) // 2
i += 1
ans = k * (m * n - n) - x
print(ans)
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given a string S of length n with each character being one of the first m lowercase English letters.
Calculate how many different strings T of length n composed from the first m lowercase English letters exist such that the length of LCS (longest common subsequence) between S and T is n - 1.
Recall that LCS of two strings S and T is the longest string C such that C both in S and T as a subsequence.
-----Input-----
The first line contains two numbers n and m denoting the length of string S and number of first English lowercase characters forming the character set for strings (1 β€ n β€ 100 000, 2 β€ m β€ 26).
The second line contains string S.
-----Output-----
Print the only line containing the answer.
-----Examples-----
Input
3 3
aaa
Output
6
Input
3 3
aab
Output
11
Input
1 2
a
Output
1
Input
10 9
abacadefgh
Output
789
-----Note-----
For the first sample, the 6 possible strings T are: aab, aac, aba, aca, baa, caa.
For the second sample, the 11 possible strings T are: aaa, aac, aba, abb, abc, aca, acb, baa, bab, caa, cab.
For the third sample, the only possible string T is b.
|
n, m = map(int, input().split())
s = input()
p = c = 0
for i in range(1, n):
if s[i] == s[i - 1]:
c += n * (m - 1)
p = i
elif s[i] != s[i - 2]:
p = i - 1
c += i - p
ans = n * n * (m - 1) - c
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import sys
sys.setrecursionlimit(10**6)
n = int(input())
a = list(map(int, input().split()))
def painting(left, right, arr, height):
if left >= right:
return 0
min_height = min(arr[left:right])
split = left + arr[left:right].index(min_height)
count_horizontal = min_height - height
count_vertical = right - left
return min(
count_vertical,
count_horizontal
+ painting(left, split, arr, min_height)
+ painting(split + 1, right, arr, min_height),
)
print(painting(0, len(a), a, 0))
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import sys
sys.setrecursionlimit(10000)
def recursion(left, right, painted_height):
if left >= right:
return 0
mini = left + a[left:right].index(min(a[left:right]))
allVerticles = right - left
rec = (
a[mini]
- painted_height
+ recursion(left, mini, a[mini])
+ recursion(mini + 1, right, a[mini])
)
return min(allVerticles, rec)
n = int(input())
a = list(map(int, input().split()))
print(recursion(0, n, 0))
|
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR 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 FUNC_CALL VAR NUMBER VAR NUMBER
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import sys
sys.setrecursionlimit(10000)
def cantidad_de_brochazos(izq, der, altura):
if izq > der:
return 0
solo_vertical = der - izq + 1
altura_minima = min(L[izq : der + 1])
minimo = izq + L[izq : der + 1].index(altura_minima)
dividir_y_conquistar = (
L[minimo]
- altura
+ cantidad_de_brochazos(izq, minimo - 1, L[minimo])
+ cantidad_de_brochazos(minimo + 1, der, L[minimo])
)
return min(solo_vertical, dividir_y_conquistar)
n = int(sys.stdin.readline())
L = list(map(int, input().split()))
sys.stdout.write(str(cantidad_de_brochazos(0, n - 1, 0)))
|
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR 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 FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import sys
sys.setrecursionlimit(10000)
def painting_fence(fence, cut):
n = len(fence)
if n == 0:
return 0
min_i = fence.index(min(fence))
min_h = fence[min_i]
ans = (
min_h
- cut
+ painting_fence(fence[:min_i], min_h)
+ painting_fence(fence[min_i + 1 :], min_h)
)
return min(ans, n)
def main():
_ = int(input())
fence = list(map(int, input().split()))
print(painting_fence(fence, 0))
def __starting_point():
main()
__starting_point()
|
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF 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 FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
n = int(input()) + 1
arr = list(map(int, input().split())) + [0]
res, q = 0, []
for i in arr:
if i != 0:
q.append(i)
continue
dp = []
n = len(q)
for i in range(n):
curr = q[i] + i
curr_min = q[i]
for j in range(i - 1, -1, -1):
curr_min = min(q[j], curr_min)
diff = q[i] - curr_min
curr = min(curr, diff + dp[j] + i - j - 1)
dp.append(curr)
res += min([(n - i + dp[i] - 1) for i in range(n)] + [n])
q = []
print(res)
|
ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR NUMBER LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import sys
ar = []
def solve(l, r, val):
if r < l:
return 0
indx = l + ar[l : r + 1].index(min(ar[l : r + 1]))
tot = r - l + 1
cur = ar[indx] - val + solve(l, indx - 1, ar[indx]) + solve(indx + 1, r, ar[indx])
return min(tot, cur)
sys.setrecursionlimit(10000)
n = int(input())
ar = list(map(int, input().split()))
print(solve(0, n - 1, 0))
|
IMPORT ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER 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 FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import sys
sys.setrecursionlimit(10000)
s, n = 1, int(input())
t = [(q, i) for i, q in enumerate(map(int, input().split()))]
e = 2000000000.0, 0
while s < n:
s <<= 1
h = [e] * s + t + [e] * (s - n)
k = s - 1
while k:
j = k << 1
h[k] = min(h[j], h[j + 1])
k -= 1
def f(a, l, r):
if r - l < 1:
return 0
if r - l < 2:
return int(t[l][0] != a)
p, q = [(s, l, r)], e
while p:
k, u, v = p.pop()
if u < v:
if u & 1:
q = min(q, h[k + u])
u += 1
if v & 1:
q = min(q, h[k + v - 1])
p.append((k >> 1, u >> 1, v >> 1))
b, m = q
d, n = b - a, r - l
return min(n, d + f(b, l, m) + f(b, m + 1, r)) if d < n else n
print(f(0, 0, n))
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import sys
def build(i, l, r):
if l == r:
it[i] = l
return it[i]
mid = (l + r) // 2
p1 = build(i * 2 + 1, l, mid)
p2 = build(i * 2 + 2, mid + 1, r)
if a[p1] > a[p2]:
it[i] = p2
else:
it[i] = p1
return it[i]
def rmq(i, l, r, L, R):
if r < L or R < l:
return -1
if L <= l and r <= R:
return it[i]
mid = (l + r) // 2
p1 = rmq(i * 2 + 1, l, mid, L, R)
p2 = rmq(i * 2 + 2, mid + 1, r, L, R)
if p1 == -1:
return p2
if p2 == -1:
return p1
if a[p1] < a[p2]:
return p1
return p2
def strokesNeeded(left, right, paintedHeight):
if left > right:
return 0
mini = rmq(0, 0, n - 1, left, right)
allVerticle = right - left + 1
recursive = (
a[mini]
- paintedHeight
+ strokesNeeded(left, mini - 1, a[mini])
+ strokesNeeded(mini + 1, right, a[mini])
)
return min(allVerticle, recursive)
sys.setrecursionlimit(10000)
n = int(input())
a = list(map(int, input().split()))
it = [(0) for i in range(4 * 5009)]
build(0, 0, n - 1)
print(strokesNeeded(0, n - 1, 0))
|
IMPORT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR IF VAR VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import sys
n = 0
inp = []
tree = []
def build(node, i, j):
if i > j:
return
if i == j:
tree[node] = int(i)
return
mid = int((i + j) / 2)
build(2 * node, i, mid)
build(2 * node + 1, mid + 1, j)
if inp[tree[2 * node]] < inp[tree[2 * node + 1]]:
tree[node] = tree[2 * node]
else:
tree[node] = tree[2 * node + 1]
def RMQ(node, i, j, l, r):
if i <= l and r <= j:
return tree[node]
if i > r or j < l:
return n
mid = int((l + r) / 2)
a = RMQ(2 * node, i, j, l, mid)
b = RMQ(2 * node + 1, i, j, mid + 1, r)
if inp[a] < inp[b]:
return a
else:
return b
def inputArray():
A = str(input()).split()
return list(map(int, A))
def solve(a, b, ht):
if a > b:
return 0
mn = RMQ(1, a, b, 0, n - 1)
op1 = b - a + 1
op2 = solve(a, mn - 1, inp[mn]) + solve(mn + 1, b, inp[mn]) + inp[mn] - ht
return min(op1, op2)
n = int(input())
inp = inputArray()
inp.append(1000 * 1000 * 1000 + 10)
sys.setrecursionlimit(10000)
tree = [int(n) for x in range(4 * n + 10)]
build(1, 0, n - 1)
print(solve(0, n - 1, 0))
|
IMPORT ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
def checkIncline(a):
for i in range(len(a) - 1):
if a[i] >= a[i + 1]:
return False
return True
def checkDecline(a):
for i in range(len(a) - 1):
if a[i] <= a[i + 1]:
return False
return True
def recursion(a):
if len(a) == 1:
return 1
if checkIncline(a):
return len(a)
if checkDecline(a):
return len(a)
min_height = min(a)
n = len(a)
for i in range(n):
a[i] -= min_height
if max(a) == 0:
return min(min_height, n)
rec = 0
i, j = 0, 0
intervals = []
for k in range(n):
if k == n - 1 and a[k] != 0:
intervals.append((i, k + 1))
if a[k] == 0:
j = k
if i != j:
intervals.append((i, j))
i = k + 1
for i, j in intervals:
rec += recursion(a[i:j])
return min(n, rec + min_height)
n = int(input())
a = list(map(int, input().split()))
a.reverse()
print(recursion(a))
|
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import sys
def Build(l, r, id):
if l == r:
T[id] = l
return
m = (l + r) // 2
Build(l, m, id * 2)
Build(m + 1, r, id * 2 + 1)
if a[T[id * 2]] < a[T[id * 2 + 1]]:
T[id] = T[id * 2]
else:
T[id] = T[id * 2 + 1]
def Search(l, r, le, ri, id):
if r < l:
return n
if r < le or l > ri:
return n
if l >= le and r <= ri:
return T[id]
m = (l + r) // 2
m1 = Search(l, m, le, ri, id * 2)
m2 = Search(m + 1, r, le, ri, id * 2 + 1)
if a[m1] <= a[m2]:
return m1
else:
return m2
def solve(l, r, h):
if r < l:
return 0
m = Search(0, n - 1, l, r, 1)
return min(r - l + 1, solve(l, m - 1, a[m]) + solve(m + 1, r, a[m]) + a[m] - h)
def b5_painting_fence(n, a):
return solve(0, n - 1, 0)
sys.setrecursionlimit(10000)
n = int(input())
a = list(map(int, input().split()))
a.append(2**31 - 1)
T = []
for i in range(4 * n + 1):
T.append(0)
Build(0, n - 1, 1)
result = b5_painting_fence(n, a)
print(result)
|
IMPORT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER 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 BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
import itertools
import sys
sys.setrecursionlimit(10**6)
class RMQ:
def __init__(self, n):
self.sz = 1
self.inf = (1 << 31) - 1
while self.sz <= n:
self.sz = self.sz << 1
self.dat = [[None, self.inf] for i in range(2 * self.sz - 1)]
def update(self, idx, x):
old_idx = idx
idx += self.sz - 1
self.dat[idx] = [old_idx, x]
while idx > 0:
idx = idx - 1 >> 1
self.dat[idx] = min(
self.dat[idx * 2 + 1], self.dat[idx * 2 + 2], key=lambda x: x[1]
)
def query(self, a, b):
return self.query_help(a, b, 0, 0, self.sz)
def query_help(self, a, b, k, l, r):
if r <= a or b <= l:
return 0, sys.maxsize
elif a <= l and r <= b:
return self.dat[k]
else:
return min(
self.query_help(a, b, 2 * k + 1, l, l + r >> 1),
self.query_help(a, b, 2 * k + 2, l + r >> 1, r),
key=lambda x: x[1],
)
def solve_l_r(left, right, height):
if left > right:
return 0
min_right, min_height = rmq.query(left, right + 1)
res = min(
right - left + 1,
min_height
- height
+ solve_l_r(left, min_right - 1, min_height)
+ solve_l_r(min_right + 1, right, min_height),
)
return res
def painting_fences():
for i, j in enumerate(case_n):
rmq.update(i, j)
return solve_l_r(0, len(case_n) - 1, 0)
num = int(sys.stdin.readline())
case_n = list(map(int, sys.stdin.readline().split()))
rmq = RMQ(num)
assert len(case_n) == num
print(painting_fences())
|
IMPORT IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NONE VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER VAR IF VAR VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
-----Input-----
The first line contains integer n (1 β€ n β€ 5000) β the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print a single integer β the minimum number of strokes needed to paint the whole fence.
-----Examples-----
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
-----Note-----
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
|
n = int(input())
t = [int(x) for x in input().split()]
t.insert(0, -1)
dp = [0] * (n + 3)
dp[1] = t[1]
for i in range(2, n + 1):
dp[i] = t[i] + (i - 1)
smallest = t[i]
for j in range(i - 1, 0, -1):
smallest = min(smallest, t[j])
tmp = dp[j] + (i - j - 1) + (t[i] - smallest)
dp[i] = min(dp[i], tmp)
res = n
for i in range(1, n + 1):
res = min(res, dp[i] + (n - i))
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
n = int(input())
R = lambda: [int(i) for i in input().split()]
a, b, c = R(), R(), R()
x, y = a[0], b[0]
for i in range(1, n):
x, y = max(a[i] + y, b[i] + x), max(b[i] + y, c[i] + x)
print(x)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
class Solution:
def max_radiance(n, a, b, c):
dp = [[(0) for i in range(2)] for j in range(n + 1)]
dp[n - 1][0] = a[n - 1]
dp[n - 1][1] = b[n - 1]
for i in range(n - 2, -1, -1):
dp[i][0] = max(a[i] + dp[i + 1][1], b[i] + dp[i + 1][0])
dp[i][1] = max(b[i] + dp[i + 1][1], c[i] + dp[i + 1][0])
return dp[0][0]
n = int(input())
a = list(map(int, input().strip(" ").split(" ")))
b = list(map(int, input().strip(" ").split(" ")))
c = list(map(int, input().strip(" ").split(" ")))
print(Solution.max_radiance(n, a, b, c))
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
dp = [([0] * 2) for i in range(n + 1)]
dp[0][0] = -(10**9)
for i in range(n):
dp[i + 1][0] = max(dp[i + 1][0], dp[i][0] + b[i], dp[i][1] + a[i])
dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + c[i], dp[i][1] + b[i])
print(dp[n][0])
|
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
fed_left = {(0): a[0]}
not_fed_left = {(0): b[0]}
for i in range(1, n):
fed_left[i] = max(fed_left[i - 1] + b[i], not_fed_left[i - 1] + a[i])
not_fed_left[i] = max(fed_left[i - 1] + c[i], not_fed_left[i - 1] + b[i])
print(fed_left[n - 1])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER VAR NUMBER ASSIGN VAR DICT NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
n = int(input())
t = [list(map(int, input().split())) for i in range(3)]
t = list(zip(*t))
F = {}
def g(i, j, k):
if j - i == 1:
return t[i][1]
a, b, c = t[k]
t[k] = b, c, 0
s = f(i, j)
t[k] = a, b, c
return s
def f(i, j):
if (i, j, t[i], t[j - 1]) in F:
return F[i, j, t[i], t[j - 1]]
if j - i == 1:
return t[i][0]
k = (i + j) // 2
F[i, j, t[i], t[j - 1]] = max(f(i, k) + g(k, j, k), g(i, k, k - 1) + f(k, j))
return F[i, j, t[i], t[j - 1]]
print(f(0, n))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
def main():
n = int(input())
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
c = list(map(int, input().split(" ")))
d0 = [0] * n
d0[n - 1] = a[n - 1]
d1 = [0] * n
d1[n - 1] = b[n - 1]
for i in range(n - 2, -1, -1):
d0[i] = max(a[i] + d1[i + 1], b[i] + d0[i + 1])
d1[i] = max(b[i] + d1[i + 1], c[i] + d0[i + 1])
print(d0[0])
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
from sys import setrecursionlimit
setrecursionlimit(10**6)
def dynamic(i, fed):
if d[i][fed] is not None:
return d[i][fed]
if fed:
d[i][fed] = max(b[i] + dynamic(i + 1, True), c[i] + dynamic(i + 1, False))
else:
d[i][fed] = max(a[i] + dynamic(i + 1, True), b[i] + dynamic(i + 1, False))
return d[i][fed]
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
d = [[None, None] for i in range(n)]
d[n - 1] = [a[n - 1], b[n - 1]]
print(dynamic(0, False))
|
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NONE NONE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
DD = a[0]
DP = b[0]
PD = a[0]
PP = b[0]
for i in range(1, n):
tDD = max(DP + a[i], PP + a[i])
tPD = max(PD + b[i], DD + b[i])
tDP = max(DP + b[i], PP + b[i])
tPP = max(PD + c[i], DD + c[i])
DD = tDD
PD = tPD
DP = tDP
PP = tPP
if n == 1:
sol = DD
else:
sol = max(DD, PD)
print(sol)
|
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
import sys
sys.setrecursionlimit(5000)
dp = [[(-1) for i in range(5)] for j in range(3005)]
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
def fun(i, n, idx):
if dp[i][idx] != -1:
return dp[i][idx]
if idx == 0:
if i == n - 1:
return a[n - 1]
dp[i][idx] = max(fun(i + 1, n, 1) + a[i], fun(i + 1, n, 0) + b[i])
else:
if i == n - 1:
return b[n - 1]
dp[i][idx] = max(fun(i + 1, n, 1) + b[i], fun(i + 1, n, 0) + c[i])
return dp[i][idx]
print(fun(0, n, 0))
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 3000) β the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 β€ a_{i}, b_{i}, c_{i} β€ 10^5.
Number a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number Ρ_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
-----Output-----
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
-----Examples-----
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
|
import sys
def solve():
bbCum = lBB[0]
baCum = lBA[0]
abCum = lAB[0]
aaCum = lAA[0]
for i in range(1, len(lBB)):
m1 = max(baCum, aaCum)
m2 = max(bbCum, abCum)
bbCum = lBB[i] + m1
baCum = lBA[i] + m1
abCum = lAB[i] + m2
aaCum = lAA[i] + m2
res = max(bbCum, baCum, abCum, aaCum)
return res
def main():
global lBB
global lBA
global lAB
global lAA
f = sys.stdin
n = int(f.readline())
lBB = list(map(int, f.readline().split()))
lBA = list(map(int, f.readline().split()))
lAB = [elem for elem in lBA]
lAA = list(map(int, f.readline().split()))
lAB[0] = 0
lBA[-1] = 0
lAA[0] = 0
lAA[-1] = 0
res = solve()
print(res)
main()
|
IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
RSJ has a sequence $a$ of $n$ integers $a_1,a_2, \ldots, a_n$ and an integer $s$. For each of $a_2,a_3, \ldots, a_{n-1}$, he chose a pair of non-negative integers $x_i$ and $y_i$ such that $x_i+y_i=a_i$ and $(x_i-s) \cdot (y_i-s) \geq 0$.
Now he is interested in the value $$F = a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot x_4 + \ldots + y_{n - 2} \cdot x_{n-1}+y_{n-1} \cdot a_n.$$
Please help him find the minimum possible value $F$ he can get by choosing $x_i$ and $y_i$ optimally. It can be shown that there is always at least one valid way to choose them.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains two integers $n$, $s$ ($3 \le n \le 2 \cdot 10^5$; $0 \le s \le 2 \cdot 10^5$).
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 2 \cdot 10^5$).
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the minimum possible value of $F$.
-----Examples-----
Input
10
5 0
2 0 1 3 4
5 1
5 3 4 3 5
7 2
7 6 5 4 3 2 1
5 1
1 2 3 4 5
5 2
1 2 3 4 5
4 0
0 1 1 1
5 5
4 3 5 6 4
4 1
0 2 1 0
3 99999
200000 200000 200000
6 8139
7976 129785 12984 78561 173685 15480
Output
0
18
32
11
14
0
16
0
40000000000
2700826806
-----Note-----
In the first test case, $2\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0$.
In the second test case, $5\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18$.
|
for _ in range(int(input())):
n, s = map(int, input().split())
l1 = [int(a) for a in input().split()]
m1 = [0, l1[0]]
m2 = [0, l1[0] + 1]
for i in range(1, n - 1):
if s <= l1[i]:
l3 = [s, l1[i] - s]
else:
l3 = [0, l1[i]]
n1 = m1[0] + m1[1] * l3[0]
n2 = m2[0] + m2[1] * l3[0]
if n1 < n2:
k1 = [n1, l3[1]]
else:
k1 = [n2, l3[1]]
l3.reverse()
n1 = m1[0] + m1[1] * l3[0]
n2 = m2[0] + m2[1] * l3[0]
if n1 < n2:
k2 = [n1, l3[1]]
else:
k2 = [n2, l3[1]]
m1 = [int(a) for a in k1]
m2 = [int(a) for a in k2]
x1 = m1[0] + m1[1] * l1[-1]
x2 = m2[0] + m2[1] * l1[-1]
print(min(x1, x2))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
RSJ has a sequence $a$ of $n$ integers $a_1,a_2, \ldots, a_n$ and an integer $s$. For each of $a_2,a_3, \ldots, a_{n-1}$, he chose a pair of non-negative integers $x_i$ and $y_i$ such that $x_i+y_i=a_i$ and $(x_i-s) \cdot (y_i-s) \geq 0$.
Now he is interested in the value $$F = a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot x_4 + \ldots + y_{n - 2} \cdot x_{n-1}+y_{n-1} \cdot a_n.$$
Please help him find the minimum possible value $F$ he can get by choosing $x_i$ and $y_i$ optimally. It can be shown that there is always at least one valid way to choose them.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains two integers $n$, $s$ ($3 \le n \le 2 \cdot 10^5$; $0 \le s \le 2 \cdot 10^5$).
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 2 \cdot 10^5$).
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the minimum possible value of $F$.
-----Examples-----
Input
10
5 0
2 0 1 3 4
5 1
5 3 4 3 5
7 2
7 6 5 4 3 2 1
5 1
1 2 3 4 5
5 2
1 2 3 4 5
4 0
0 1 1 1
5 5
4 3 5 6 4
4 1
0 2 1 0
3 99999
200000 200000 200000
6 8139
7976 129785 12984 78561 173685 15480
Output
0
18
32
11
14
0
16
0
40000000000
2700826806
-----Note-----
In the first test case, $2\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0$.
In the second test case, $5\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18$.
|
for _ in range(int(input())):
n, s = map(int, input().split(" "))
nums = list(map(int, input().split(" ")))
helpofx = [0] * n
helpofy = [0] * n
for i in range(1, n - 1):
claci1 = s
calci2 = nums[i] - s
if calci2 <= 0:
claci1 += calci2
calci2 = 0
helpofx[i], helpofy[i] = claci1, calci2
precomputation = [([0] * 2) for _ in range(n)]
precomputation[1][0], precomputation[1][1] = (
nums[0] * helpofx[1],
nums[0] * helpofy[1],
)
for i in range(2, n - 1):
precomputation[i][0] = min(
precomputation[i - 1][0] + helpofx[i] * helpofy[i - 1],
precomputation[i - 1][1] + helpofx[i] * helpofx[i - 1],
)
precomputation[i][1] = min(
precomputation[i - 1][0] + helpofy[i] * helpofy[i - 1],
precomputation[i - 1][1] + helpofy[i] * helpofx[i - 1],
)
print(
min(
precomputation[n - 2][0] + helpofy[n - 2] * nums[n - 1],
precomputation[n - 2][1] + helpofx[n - 2] * nums[n - 1],
)
)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
|
RSJ has a sequence $a$ of $n$ integers $a_1,a_2, \ldots, a_n$ and an integer $s$. For each of $a_2,a_3, \ldots, a_{n-1}$, he chose a pair of non-negative integers $x_i$ and $y_i$ such that $x_i+y_i=a_i$ and $(x_i-s) \cdot (y_i-s) \geq 0$.
Now he is interested in the value $$F = a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot x_4 + \ldots + y_{n - 2} \cdot x_{n-1}+y_{n-1} \cdot a_n.$$
Please help him find the minimum possible value $F$ he can get by choosing $x_i$ and $y_i$ optimally. It can be shown that there is always at least one valid way to choose them.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains two integers $n$, $s$ ($3 \le n \le 2 \cdot 10^5$; $0 \le s \le 2 \cdot 10^5$).
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 2 \cdot 10^5$).
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the minimum possible value of $F$.
-----Examples-----
Input
10
5 0
2 0 1 3 4
5 1
5 3 4 3 5
7 2
7 6 5 4 3 2 1
5 1
1 2 3 4 5
5 2
1 2 3 4 5
4 0
0 1 1 1
5 5
4 3 5 6 4
4 1
0 2 1 0
3 99999
200000 200000 200000
6 8139
7976 129785 12984 78561 173685 15480
Output
0
18
32
11
14
0
16
0
40000000000
2700826806
-----Note-----
In the first test case, $2\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0$.
In the second test case, $5\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18$.
|
for _ in range(int(input())):
n, s = map(int, input().split())
k = [*map(int, input().split())]
fx, fy = 0, 0
px, py = k[0], k[0]
for i in k[1 : n - 1]:
[x, y] = [0, i] if s > i else [i - s, s]
fx, fy = min(fx + py * x, fy + px * x), min(fx + py * y, fy + px * y)
px, py = x, y
print(min(fx + y * k[-1], fy + x * k[-1]))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN LIST VAR VAR VAR VAR LIST NUMBER VAR LIST BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER
|
RSJ has a sequence $a$ of $n$ integers $a_1,a_2, \ldots, a_n$ and an integer $s$. For each of $a_2,a_3, \ldots, a_{n-1}$, he chose a pair of non-negative integers $x_i$ and $y_i$ such that $x_i+y_i=a_i$ and $(x_i-s) \cdot (y_i-s) \geq 0$.
Now he is interested in the value $$F = a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot x_4 + \ldots + y_{n - 2} \cdot x_{n-1}+y_{n-1} \cdot a_n.$$
Please help him find the minimum possible value $F$ he can get by choosing $x_i$ and $y_i$ optimally. It can be shown that there is always at least one valid way to choose them.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains two integers $n$, $s$ ($3 \le n \le 2 \cdot 10^5$; $0 \le s \le 2 \cdot 10^5$).
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 2 \cdot 10^5$).
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the minimum possible value of $F$.
-----Examples-----
Input
10
5 0
2 0 1 3 4
5 1
5 3 4 3 5
7 2
7 6 5 4 3 2 1
5 1
1 2 3 4 5
5 2
1 2 3 4 5
4 0
0 1 1 1
5 5
4 3 5 6 4
4 1
0 2 1 0
3 99999
200000 200000 200000
6 8139
7976 129785 12984 78561 173685 15480
Output
0
18
32
11
14
0
16
0
40000000000
2700826806
-----Note-----
In the first test case, $2\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0$.
In the second test case, $5\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18$.
|
def solve(n, s, a):
f = lambda u, v, x: min(u[0] + u[1] * x, v[0] + v[1] * x)
lo, hi = (0, a[0]), (0, a[0])
for i in range(1, n - 1):
x1, x2 = min(a[i], s), max(0, a[i] - s)
lo, hi = (f(lo, hi, x1), x2), (f(lo, hi, x2), x1)
print(f(lo, hi, a[-1]))
t = int(input())
for test in range(t):
n, s = list(map(int, input().split()))
a = list(map(int, input().split()))
solve(n, s, a)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
|
RSJ has a sequence $a$ of $n$ integers $a_1,a_2, \ldots, a_n$ and an integer $s$. For each of $a_2,a_3, \ldots, a_{n-1}$, he chose a pair of non-negative integers $x_i$ and $y_i$ such that $x_i+y_i=a_i$ and $(x_i-s) \cdot (y_i-s) \geq 0$.
Now he is interested in the value $$F = a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot x_4 + \ldots + y_{n - 2} \cdot x_{n-1}+y_{n-1} \cdot a_n.$$
Please help him find the minimum possible value $F$ he can get by choosing $x_i$ and $y_i$ optimally. It can be shown that there is always at least one valid way to choose them.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains two integers $n$, $s$ ($3 \le n \le 2 \cdot 10^5$; $0 \le s \le 2 \cdot 10^5$).
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 2 \cdot 10^5$).
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the minimum possible value of $F$.
-----Examples-----
Input
10
5 0
2 0 1 3 4
5 1
5 3 4 3 5
7 2
7 6 5 4 3 2 1
5 1
1 2 3 4 5
5 2
1 2 3 4 5
4 0
0 1 1 1
5 5
4 3 5 6 4
4 1
0 2 1 0
3 99999
200000 200000 200000
6 8139
7976 129785 12984 78561 173685 15480
Output
0
18
32
11
14
0
16
0
40000000000
2700826806
-----Note-----
In the first test case, $2\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0$.
In the second test case, $5\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18$.
|
def solve():
n, s = map(int, input().split())
a = list(map(int, input().split()))
x = [0] * (n + 1)
y = [0] * (n + 1)
for i in range(n):
if s >= a[i]:
x[i] = 0
y[i] = a[i]
else:
x[i] = s
y[i] = a[i] - s
x[n - 1] = y[n - 1] = a[n - 1]
x[0] = y[0] = a[0]
dp = [[(0) for _ in range(2)] for _ in range(n + 1)]
for i in range(1, n + 1):
dp[i][0] = min(dp[i - 1][0] + x[i - 1] * y[i], dp[i - 1][1] + y[i - 1] * y[i])
dp[i][1] = min(dp[i - 1][0] + x[i - 1] * x[i], dp[i - 1][1] + y[i - 1] * x[i])
print(dp[n - 1][0])
T = int(input().strip())
for _ in range(T):
solve()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
def solve(arr, n, i):
if i < 0 or i > n - 1:
return 2
if dp[i] == 1 or dp[i] == 2:
return dp[i]
dp[i] = 1
dp[i] = solve(arr, n, i + arr[i])
return dp[i]
dp = [0] * n
for i in range(n):
if dp[i] == 0:
dp[i] = solve(arr, n, i)
count = 0
for i in range(n):
if dp[i] == 2:
count += 1
return count
|
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def explorePath(self, start, stone_status, n, arr):
idx = start
path = []
while idx < n and idx >= 0 and stone_status[idx] == 0:
path.append(idx)
stone_status[idx] = 2
idx += arr[idx]
value = -1
if idx >= n or idx < 0:
value = 1
else:
value = 1 if stone_status[idx] == 1 else -1
for idx in path:
stone_status[idx] = value
def goodStones(self, n, arr) -> int:
stone_status = [0] * n
for idx in range(n):
if stone_status[idx] == 0:
self.explorePath(idx, stone_status, n, arr)
return stone_status.count(1)
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
import sys
sys.setrecursionlimit(10**6)
class Solution:
def dfs(self, arr, visited, n, i):
if visited[i]:
return visited[i] == 2
visited[i] = 1
j = i + arr[i]
if 0 <= j < n and not self.dfs(arr, visited, n, j):
return False
visited[i] = 2
return True
def goodStones(self, n, arr) -> int:
visited = [0] * n
return sum([self.dfs(arr, visited, n, i) for i in range(n)])
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
new_arr = [(-1) for _ in range(n)]
cycle = []
for i in range(n):
if new_arr[i]:
while True:
cycle.append(i)
i += arr[i]
if i >= n or i < 0 or new_arr[i] == 1:
for i in cycle:
new_arr[i] = 1
cycle.clear()
break
elif new_arr[i] == 0 or i in cycle:
for i in cycle:
new_arr[i] = 0
cycle.clear()
break
return sum(new_arr)
def update_cycle(self, new_arr, cycle, exit):
for i in cycle:
new_arr[i] = exit
cycle.clear()
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
G = [[] for i in range(n)]
p = []
for i in range(n):
ni = i + arr[i]
if ni >= 0 and ni < n:
if ni != i:
G[ni].append(i)
else:
p.append(i)
used = [(False) for i in range(n)]
def dfs(s):
used[s] = True
c = 1
for v in G[s]:
if not used[v]:
c += dfs(v)
return c
c = 0
for i in p:
if not used[i]:
c += dfs(i)
return c
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.