Answered step by step
Verified Expert Solution
Question
1 Approved Answer
5. Sloth Sequence (HARD!) A sloth has a sequence S of numbers giving the calories obtainable by hunting on each day. The sloth must obtain
5. Sloth Sequence (HARD!) A sloth has a sequence S of numbers giving the calories obtainable by hunting on each day. The sloth must obtain at least T calories total, but wants to find a smallest sequence of contiguous days to do this. Formally, you are given a sequence S = $1,..., Sn of numbers, and a number T. You want to find indices f and l, 1 T, and l - f is as small as possible. If the numbers in the sequence sum to less than T, then you will return NIL. For example if T = 8, and S = 2,4, 1,6, 0, 4, 5, 2, 3, 4, then the answer should be the sequence 4,5 (f = 6, 1 = 7) since this gives the shortest contiguous sequence with value at least T. You want to solve this problem in o(n?) time. You will use 2 functions, defined as follows: lastInder(i) returns the smallest index r such that Di Si > T, when Lj si > T. Returns the value 1 when L315; T, and l - f is as small as possible. If the numbers in the sequence sum to less than T, then you will return NIL. For example if T = 8, and S = 2,4, 1,6, 0, 4, 5, 2, 3, 4, then the answer should be the sequence 4,5 (f = 6, 1 = 7) since this gives the shortest contiguous sequence with value at least T. You want to solve this problem in o(n?) time. You will use 2 functions, defined as follows: lastInder(i) returns the smallest index r such that Di Si > T, when Lj si > T. Returns the value 1 when L315;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started