File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 1+ # @param {Integer[][]} grid
2+ # @return {Integer}
3+ def grid_game ( grid )
4+ first_line_sum = grid [ 0 ] . sum
5+ second_line_sum = grid [ 1 ] . sum
6+ min_second_robot_can_collect = Float ::INFINITY
7+
8+ first_line_collected = 0
9+ second_line_ignored = 0
10+ ( 0 ...grid [ 0 ] . size ) . each { |i |
11+ first_line_collected += grid [ 0 ] [ i ]
12+
13+ # The key is not to maximize the points collected by the first robot
14+ # but to find a path for the first robot that minimizes the points the second robot can collect.
15+ min_second_robot_can_collect = [
16+ min_second_robot_can_collect ,
17+ # max points the second robot can get
18+ [ first_line_sum - first_line_collected , second_line_ignored ] . max
19+ ] . min
20+
21+ second_line_ignored += grid [ 1 ] [ i ]
22+ }
23+
24+ min_second_robot_can_collect
25+ end
You can’t perform that action at this time.
0 commit comments