CSCI 570: Homework 7

$24.99 $18.99

You are given an integer array [1], . . ., [ ], find the contiguous subarray (containing at least one number) which has the largest sum and only return its sum. The optimal subarray is not required to return or compute. Taking = [5, 4, − 1, 7, 8] as an example: the subarray [5]…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:

Description

5/5 – (2 votes)
  1. You are given an integer array [1], . . ., [ ], find the contiguous subarray (containing at least one number) which has the largest sum and only return its sum. The optimal subarray is not

required to return or compute. Taking = [5, 4, − 1, 7, 8] as an example: the subarray [5] is considered as a valid subarray with sum 5, though it only has one single element; the subarray [5, 4, − 1, 7, 8] achieves the largest sum 23; on the other hand, [5, 4, 7, 8] is not a valid subarray as the numbers 4 and 7 are not contiguous. (20 pts)

    1. Define (in plain English) subproblems to be solved. (4 pts)

    1. Write a recurrence relation for the subproblems (6 pts)

    1. Using the recurrence formula in part b, write pseudocode to find the subarray (containing at least one number) which has the largest sum. (5 pts)

    2. Make sure you specify

      1. base cases and their values (2 pts)

      1. where the final answer can be found (1 pt)

    1. What is the complexity of your solution? (2 pts)

  1. You’ve started a hobby of retail investing into stocks using a mobile app, RogerGood. You magically gained the power to see N days into the future and you can see the prices of one particular stock. Given an array of prices of this particular stock, where prices[i] is the price of a given stock on the ith day, find the maximum profit you can achieve through various buy/sell actions. RogerGood also has a fixed fee per transaction. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. (20 pt).

    1. Define (in plain English) subproblems to be solved. (4 pts)

    1. Write a recurrence relation for the subproblems (6 pts)

    1. Using the recurrence formula in part b, write pseudocode to solve the problem. (5 pts)

    1. Make sure you specify

      1. base cases and their values (2 pts)

      1. where the final answer can be found (1 pt)

    1. What is the complexity of your solution? (2 pts)

3. You are given an array of positive numbers [1], . . ., [ ]. For a sub-sequence

[ 1], [ 2], . . ., [ ] of array (that is, 1 < 2 <, . . ., ): if it is an increasing sequence of numbers, that is, [ 1] < [ 2] < . . . < [ ], its happiness score is given by

× [ ]

=1

Otherwise, the happiness score of this array is zero.

For example, for the input = [22, 44, 33, 66, 55], the increasing subse-quence [22, 44, 55] has happiness score (1) × (22) + (2) × (44) + (3) × (55) = 275; the increasing subsequence [22, 33, 55] has happiness score (1) × (22) + (2) × (33) + (3) × (55) = 253; the subsequence [33, 66, 55] has happiness score 0 as this sequence is not increasing. Please design an efficient algorithm to only return the highest happiness score over all the subsequences (20 pts)

    1. Define (in plain English) subproblems to be solved. (4 pts)

    1. Write a recurrence relation for the subproblems (6 pts)

    1. Using the recurrence formula in part b, write pseudocode to find the highest happiness score over all the subsequences. (5 pts)

    2. Make sure you specify

      1. base cases and their values (2 pts)

      1. where the final answer can be found (1 pt)

    1. What is the complexity of your solution? (2 pts)

  1. You are given an × binary matrix ∈ {0, 1} × . Each cell either contains a “0” or a “1”. Give an efficient algorithm that takes the binary matrix , and returns the largest side length of a square that only contains 1’s. You are not required to give the optimal solution. (20 pts)

    1. Define (in plain English) subproblems to be solved. (4 pts)

    1. Write a recurrence relation for the subproblems (6 pts)

    1. Using the recurrence formula in part b, write pseudocode using iteration to compute the largest side length of a square that only contains 1’s to meet the objective. (5 pts)

    2. Make sure you specify

      1. base cases and their values (2 pts)

      1. where the final answer can be found (1 pt)

    1. What is the complexity of your solution? (2 pts)

Practice Problems

one gets a single sand pile of weight wi + wi+1 in its place which is now adjacent to sand piles − 1 and + 2. Note that different merging orders will result in different final costs, please find the minimum cost to merge all the sand piles. The optimal merging order is not required. (20 pts)

Sample Input: = 4, [1, . . . , ] = [100, 1, 1, 100].

  • Sample Output: the minimum cost is 1+1+2+100+102+100 = 306 by first merging (1, 1), then (100, 2) and (102, 100).

  1. Define (in plain English) subproblems to be solved. (4 pts)

  1. Write a recurrence relation for the subproblems (6 pts)

  1. Using the recurrence formula in part b, write pseudocode using iteration to compute the minimum cost to merge all the sand piles to meet the objective. (5 pts)

  2. Make sure you specify

    1. base cases and their values (2 pts)

    1. where the final answer can be found (1 pt)

  1. What is the complexity of your solution? (2 pts)

CSCI 570: Homework 7
$24.99 $18.99