Ruby O(N)
\nruby\n# @param {Integer[]} prize_positions\n# @param {Integer} k\n# @return {Integer}\ndef maximize_win(prize_positions, k)\n  # dp[idx] := max prize if one o
\n```ruby\n# @param {Integer[]} prize_positions\n# @param {Integer} k\n# @return {Integer}\ndef maximize_win(prize_positions, k)\n  # dp[idx] := max prize if one optimal segment is chosen among the first k prizes\n  dp = Array.new(prize_positions.length + 1, 0)\n  res, left = 0, 0\n  for right in 0...prize_positions.length\n    while prize_positions[right] - prize_positions[left] > k\n      left += 1\n    end\n    dp[right + 1] = [dp[right], right - left + 1].max\n    res = [res, right - left + 1 + dp[left]].max\n  end\n  res\nend\n```
['Ruby']