Similar Problems

Similar Problems not available

Number Of Smooth Descent Periods Of A Stock - Leetcode Solution

Companies:

LeetCode:  Number Of Smooth Descent Periods Of A Stock Leetcode Solution

Difficulty: Medium

Topics: math dynamic-programming array  

Problem:

You are given an integer array nums representing the daily stock prices of a company.

A smooth valley is a part of the graph where the stock prices are continuous and gradually decreasing, then followed by a continuous and same valued portion of the graph, and finally, another continuous and gradually increasing portion of the graph.

A smooth valley can occur multiple times during the entire graph.

Return the number of smooth valleys on the graph.

Input: nums = [1,2,3,4,5,6,7,8,9] Output: 0 Explanation: There are no smooth valleys on the graph.

Input: nums = [3, 2, 1, 5, 6, 4, 3, 6, 7, 6, 5, 9, 7, 6, 4, 5, 1, 2] Output: 3 Explanation: There are 3 smooth valleys on the graph: [3, 2, 1, 5, 6], [4], and [3, 1, 2].

Solution:

A smooth valley is a part of the graph where the stock prices are continuous and gradually decreasing, then followed by a continuous and same valued portion of the graph, and finally, another continuous and gradually increasing portion of the graph.

To solve this problem, we can iterate over the array and find all the subarrays which satisfy the conditions of a smooth valley. Once we find a smooth valley, we increment a counter to keep track of the number of smooth valleys.

To find a smooth valley, we need to find a subarray of the stock prices which starts with a decreasing sequence, followed by a constant sequence, and finally an increasing sequence. We can find the start of the decreasing sequence by scanning the array until we find an element which is lower than the previous element. Then we can scan the array until we find an element which is equal to the previous element. Finally, we can scan the array until we find an element which is higher than the previous element. If we find such a subarray, we know we have found a smooth valley.

The time complexity of this solution is O(n^3) since we are iterating over all subarrays of the array. However, the worst-case scenario is very unlikely to occur, and in practice, this algorithm will run much faster.

Algorithm:

  1. Initialize a counter for the number of smooth valleys
  2. For each element in the array: a. Find the start of a decreasing sequence by scanning the array until we find an element which is lower than the previous element b. Find the start of a constant sequence by scanning the array until we find an element which is equal to the previous element c. Find the start of an increasing sequence by scanning the array until we find an element which is higher than the previous element d. If we find all three sequences in order, we have found a smooth valley. Increment the counter.
  3. Return the counter

Python Code:

def is_smooth_valley(arr): if len(arr) < 3: return False

i = 1 while i < len(arr) and arr[i] <= arr[i-1]: i += 1

if i == len(arr): return False

j = i while j < len(arr) and arr[j] == arr[i-1]: j += 1

if j == len(arr): return False

k = j while k < len(arr) and arr[k] >= arr[j-1]: k += 1

if k != len(arr): return False

return True

def num_smooth_valleys(nums): count = 0

for i in range(len(nums)): for j in range(i, len(nums)): if is_smooth_valley(nums[i:j+1]): count += 1

return count

Complexity Analysis:

Time Complexity: The worst time complexity of this solution is O(n^3) since we are iterating over all subarrays of the array.

Space Complexity: The space complexity of this solution is O(1) since we are not using any extra space apart from a few variables.

Number Of Smooth Descent Periods Of A Stock Solution Code

1