PBT CODING JULY 8
PROBLEM 1:
Problem statement
There are n people and two identical voting machines. You are also given an array a[] of size n such that
ai stores time required by i-th person to go to any machine, mark his vote and come back. At one time
instant, only one person can be there on each of the machines. Given a value x, defining the maximum
allowable time for which machines are operational, check whether all persons can cast their vote or
not.
Input Format
First line of input contains n- the number of people. Next line of input contains x- the maximum
allowable time for which machines are operational.Next line contains n integers where ith integer
represents time required by i-th person to go to any machine, mark his vote and come back.
Constraints
1<=n<=100
1<=x<=100
1<=ai<=100
Output Format
Print 'True' (without quotes) if all persons can cast their vote else print 'False'(without quotes).
For example, in first sample test case,
There are n = 3 persons say and maximum allowed time is x = 4 units. Let the persons be P0, P1, and P2
and two machines be M0 and M1. At t0: P0 goes to M0 At t0: P2 goes to M1 At t2: M0 is free, p3 goes to
M0 At t4: both M0 and M1 are free and all 3 have given their vote
Python Solution:
def canVote(a, n, x):
dp = [[0] * (x + 1) for _ in range(n + 1)]
a = a[:]
a.append(0)
sm = 0
for i in range(n + 1):
sm += a[i]
for i in range(1, n + 1):
for j in range(1, x + 1):
if a[i] <= j:
dp[i][j] = max(dp[i - 1][j], a[i] +
dp[i - 1][j - a[i]])
else:
dp[i][j] = dp[i - 1][j]
return (sm - dp[n][x]) <= x
if __name__ == "__main__":
n = int(input())
x = int(input())
a = list(map(int,input().split()))
print("True" if canVote(a, n, x) else "False")
PROBLEM 2:
Problem statement
Given a string str of length N, you have to find number of palindromic subsequence (need not
necessarily be distinct) which could be formed from the string str. Note: You have to return the answer
module (10^9)+7;
Input Format
A single line of input contains string 's'.
Constraints
1<=|s|<=1000
Output format
Print the number of palindromic subsequence.
For second sample test case, palindromic subsequence are :"a", "a", "b", "aa"
Post a Comment