Connect K with Push-Up Drops
Jane StreetPhone screenMedium
Build a two-player connection game on a board that is the upper half of the integer grid: columns are any integers (negative allowed) and rows are 0, 1, 2, ... counting up from the bottom. PushConnect(k) starts with an empty board. The two players are the strings "B" and "R". A drop into a column places the new piece at row 0; if the column already holds pieces, every one of them moves up one row first. Each test drives one PushConnect(k) instance through a sequence of calls and checks every return value.
Part 1: Dropping pieces
Implement drop(player, col) and column(col).
drop(player, col) drops a piece for player into column col: the piece lands at row 0, and any pieces already in that column each move up one row first. It returns how many pieces that column holds after the drop.
column(col) returns the column's pieces as a string read from row 0 upward, so "RB" means R at row 0 and B at row 1. An empty column returns "".
For this part winners is not tested.
- Example 1
- init
[3]- operations
[["drop",["B",0]],["drop",["R",0]],["column",[0]],["drop",["B",5]],["column",[5]],["column",[2]],["drop",["B",0]],["column",[0]],["drop",["R",-1]],["column",[-1]]]- Output
[1,2,"RB",1,"B","",3,"BRB",1,"R"]
Why: Dropping
Rinto column 0 pushes the loneBup, so column 0 readsRBfrom the bottom. A third drop givesBRB. Column 2 never received a piece, so it reads as an empty string.
Constraints
1 <= k <= 501 <= len(operations) <= 5000-10^6 <= col <= 10^6playeris"B"or"R"