Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Rising Market

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Collect every positive slope: The Rising Market.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Greedy stock transactions matches the clues
  2. 2Keep this true after every move: profit equals all positive price differences in the processed prefix
  3. 3Reach the result within O(n)

Rules and pressure

  • Target cost: O(n)
  • State rule: profit equals all positive price differences in the processed prefix
Lesson 1 of 3

Live algorithm trace

Greedy stock transactions

Complete execution
1 of 6

Begin with no captured gains.

1profit = 0
2for each adjacent day pair
3gain = prices[i] - prices[i-1]
4if gain > 0: profit += gain
5return profit
profit = 0
Truth to preserve / Cost target
Truth to preserve

profit equals all positive price differences in the processed prefix

Cost target

O(n)

With unlimited nonoverlapping transactions, every upward day-to-day move can be collected. Combining adjacent gains or taking them separately gives the same profit.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead