0% found this document useful (0 votes)
2 views

Week 9 Live Coding Solution

The document presents solutions for four coding problems, including finding the minimum cost for a frog to jump across stones, counting valid subsequences from a string of digits, maximizing value in a knapsack, and determining the maximum obtainable value from cutting a rod. Each problem includes a description, sample inputs and outputs, and various solution approaches such as recursive, dynamic programming, and tabular methods. Public and private test cases are provided for validation of the solutions.

Uploaded by

SubhashVenkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 9 Live Coding Solution

The document presents solutions for four coding problems, including finding the minimum cost for a frog to jump across stones, counting valid subsequences from a string of digits, maximizing value in a knapsack, and determining the maximum obtainable value from cutting a rod. Each problem includes a description, sample inputs and outputs, and various solution approaches such as recursive, dynamic programming, and tabular methods. Public and private test cases are provided for validation of the solutions.

Uploaded by

SubhashVenkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Week 9 Live coding Solution

Week 9 Live coding Solution


Problem 1
Solution
Public Test case
Private Test case
Problem 2
Solution
Public Test case
Private Test case
Problem 3
Solution
Public Test case
Private Test case
Problem 4
Solution
Public Test case
Private Test case

Problem 1
There are stones, numbered . For each , the height of
Stone is .

There is a frog who is initially on Stone . He will repeat the following action some number of
times to reach last stone.

If the frog is currently on Stone , can jump to Stone or Stone . Here, a cost of
is incurred, where is the stone to land on.

Find the minimum possible total cost to reach at last stone.

Write a function minCost(H), where is a list of heights for stones. The function returns the
minimum possible total cost to reach at last stone.

Sample Input

1 [10 30 40 20]

Output

1 30

Explanation

If we follow the path , the total cost incurred would be


.
Solution
Recursive

1 def solver(n,height):
2 if n == 0:
3 ans = 0
4 elif n == 1:
5 ans = abs(height[1]-height[0])
6 elif n > 1:
7 ans = min(solver(n-1,height)+abs(height[n]-height[n-
1]),solver(n-2,height)+abs(height[n]-height[n-2]))
8 return ans
9 def minCost(H):
10 return solver(len(H)-1,H)

DP Memoization (Top down approach)

1 def solvem(n,height,memo):
2 if memo[n]==-1:
3 if n == 0:
4 ans = 0
5 elif n == 1:
6 ans = abs(height[1]-height[0])
7 elif n > 1:
8 ans = min(solvem(n-1,height,memo)+abs(height[n]-height[n-
1]),solvem(n-2,height,memo)+abs(height[n]-height[n-2]))
9 memo[n]=ans
10 return memo[n]
11
12 def minCost(H):
13 memo={}
14 for i in range(len(H)):
15 memo[i]=-1
16 return solvem(len(H)-1,H,memo)

DP Tabular (Bottom-up approach)


1 def solvet(N, h):
2 dp = [0]*N
3 dp[1] = abs(h[1] - h[0])
4
5 for i in range(2, N):
6 dp[i] = min(dp[i-1] + abs(h[i-1] - h[i]), dp[i-2] + abs(h[i-2] -
h[i]))
7
8 return dp[N-1]
9 def minCost(h):
10 N = len(h)
11 return solvet(N, h)

Prefix Code

1 H = eval(input())
2 print(minCost(H))

Public Test case


Input 1

1 [10, 30, 40, 20]

Output

1 30

Input 2

1 [10, 10]

Output

1 0

Input 3

1
2 [30, 10, 60, 10, 60, 50]

Output

1 40
Private Test case
Input 1

1 [1, 2, 3, 4, 5, 6, 7, 8, 9]

Output

1 8

Input 2

1 [1,2,2,3,3,4,4,5,5]

Output

1 4

Input 3

1 [1,2,3,4,5,5,5,5,5]

Output

1 4

Problem 2
Count Subsequence

A subsequence is a sequence that can be derived from another sequence by deleting some
elements without changing the order of the remaining elements.

Write a function countSubseq(S) that accepts a string S which contains only digit characters. The
function returns the number of non-empty subsequences that can be obtained from S such that
every digit in the subsequence is strictly greater than all previous digits(if exist).

Example:-

If S = '7598' then there are 8 subsequences which follow the above constraint. These are
'7','5','9','8','79','78','59','58' . Notice that '7598' is not a valid required
subsequence because 7 > 5 and 9 > 8 .
Input

1 7598

Output

1 8

Solution
Solution Code

1 def countSubseq(S):
2 L=[]
3 n = len(S)
4 for d in S:
5 L.append(int(d))
6 count = [0 for i in range(10)]
7 for i in range(n):
8 for j in range(L[i] - 1, -1, -1):
9 count[L[i]] += count[j]
10 count[L[i]] += 1
11 result = 0
12 for i in range(10):
13 result += count[i]
14 return result

Suffix code(Visible)

1 #Suffix Code
2 S = input()
3 print(countSubseq(S))

Public Test case


Input 1

1 7598

Output

1 8

Input 2

1 111324355

Output
1 95

Input 3

1 1123

Output

1 11

Input 4

1 54321

Output

1 5

Private Test case


Input 1

1 543216

Output

1 11

Input 2

1 6458132

Output

1 14

Input 3

1 1653587269

Output

1 99

Input 4

1 546766112378
Output

1 103

Input 5

1 987654321

Output

1 9

Problem 3
You are given weights and values of N items, put these items in a knapsack of capacity W to get
the maximum total value in the knapsack. Note that we have only one quantity of each item. In
other words, given two integer list value[0..N-1] and weight[0..N-1] which represent values
and weights associated with N items respectively. Also given an integer W which represents
knapsack capacity, find out the maximum value subset of value such that sum of the weights of
this subset is smaller than or equal to capacity W . You cannot break an item, either pick the
complete item or don’t pick it (0-1 property).

Write the function knapSack(W, weight, value, N) that returns the maximum possible value
you can get.

Sample Input

1 3 #N
2 4 #W
3 [4,5,1] #Weight
4 [1,20,3] #value

Output:

1 3

Solution
Solution Code
1 def knapSack(W, weight, value, N):
2 st = [[0 for i in range(W+1)]for j in range(N+1)]
3 for i in range(1,N+1):
4 for j in range(1,W+1):
5 if (weight[i-1]<=j):
6 st[i][j]=max(value[i-1]+st[i-1][j-weight[i-1]],st[i-1][j])
7 else:
8 st[i][j]=st[i-1][j]
9 return st[N][W]

Suffix Code

1 N=int(input())
2 W=int(input())
3 weight=eval(input())
4 values=eval(input())
5 print(knapSack(W,weight,values,N))

Public Test case


Input 1

1 3
2 4
3 [4,5,1]
4 [1,20,3]

Output

1 3

Input 2

1 3
2 3
3 [4,5,6]
4 [1,2,3]

Output

1 0

Input 3

1 6
2 10
3 [4,4,5,6,7,2]
4 [50,40,60,6,91,2]
Output

1 110

Private Test case


Input 1

1 6
2 8
3 [4,4,5,6,7,2]
4 [60,40,60,90,108,30]

Output

1 120

Input 2

1 6
2 10
3 [1, 2, 3, 8, 7, 4]
4 [20, 5, 10, 40, 15, 25]

Output

1 60

Input 3

1 8
2 100
3 [25, 35, 30, 46, 12, 65, 19, 32]
4 [22, 34, 56, 77, 86, 12, 33, 60]

Output

1 235

Input 4

1 8
2 50
3 [20, 30, 22, 10, 33, 19, 20, 40]
4 [34, 56, 78, 23, 45, 70, 67, 45]

Output
1 160

Problem 4
Given a rod of length n inches and an list of prices price that contains prices of all pieces of size
smaller or equal n . Determine the maximum value obtainable by cutting up the rod and selling
the pieces.

Write a function cutRod(n,price) that return the he maximum value obtainable by cutting up
the rod and selling the pieces.

Sample Input

1 8 #n
2 [1, 5, 8, 9, 10, 17, 17, 20] #price

Output

1 22 #maximum value

Explanation:

The maximum obtainable value is 22 by cutting in two pieces of lengths 2 and 6, i.e., 5+17=22.

Solution
Solution code

1 def cutRod(n,price):
2 length=[]
3 st=[[0 for i in range(n+1)] for j in range(n+1)]
4 for i in range(1,n+1):
5 length.append(i)
6 for i in range(1,n+1):
7 for j in range(1,n+1):
8 if(length[i-1]<=j):
9 st[i][j]=max(price[i-1]+st[i][j-length[i-1]],st[i-1][j])
10 else:
11 st[i][j]=st[i-1][j]
12 return st[n][n]

Suffix code (visible)

1 N = int(input())
2 price= eval(input())
3 print(cutRod(N,price))
Public Test case
Input 1

1 8
2 [1, 5, 8, 9, 10, 17, 17, 20]

Output

1 22

Input 2

1 8
2 [10, 5, 8, 9, 10, 17, 17, 20]

Output

1 80

Input 3

1 8
2 [1, 5, 8, 9, 10, 17, 17, 25]

Output

1 25

Private Test case


Input 1

1 10
2 [1,2,3,4,5,6,7,8,9,10]

Output

1 10

Input 2

1 10
2 [2,1,10,20,5,16,7,8,9,10]

Output
1 44

Input 3

1 10
2 [2,1,10,2,50,16,7,8,9,10]

Output

1 100

You might also like