Ctrl K

Spot the Price Trend

Medium
The array prices holds a stock's price for each day. An analyst describes the movement they are looking for with an array trend made of -1, 0 and 1: 1 means the price went up the next day, 0 means it stayed the same, -1 means it went down. A segment of len(trend) + 1 consecutive days fits the trend if, for every j, the move from its j-th day to the next one is the one trend[j] asks for. Return the number of segments that fit. Segments may overlap.
Example 1
prices=[5, 7, 7, 3, 4, 4, 2], trend=[1, 0, -1] → 2

The trend "up, flat, down" matches the segments [5, 7, 7, 3] and [3, 4, 4, 2].

Example 2
prices=[9, 8, 7, 6], trend=[-1, -1] → 2

Two falls in a row: [9, 8, 7] and [8, 7, 6]; matching segments may overlap.

Example 3
prices=[1, 2, 1, 2], trend=[0] → 0
Constraints
  • 2 ≤ len(prices) ≤ 100
  • 1 ≤ prices[i] ≤ 10⁹
  • 1 ≤ len(trend) < len(prices)
  • each trend[j] is -1, 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTarget From Word Beginnings MediumSpot the Price Trend in a Long History HardBookend Word Pairs EasyFraming Word Pairs HardPhrase From Word Beginnings, Long Edition Hard
loading Python…
Input
prices=[5, 7, 7, 3, 4, 4, 2], trend=[1, 0, -1]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 112 tests on the server, hidden ones included.