Furthest Point From Origin
Real-world version: Smudged Cradle LogThe statement here is our own: the same inputs, outputs and technique.
A window-washing cradle begins the day parked at its rest position on the side of a tower. The shift log moves records, in order, what the cradle did during each stage of the day. One character stands for one stage:
'U'means the cradle rose exactly one floor,'D'means the cradle dropped exactly one floor,'?'means the entry is smudged, and that stage was either a rise of one floor or a drop of one floor.
The offset at the end of the day is how many floors separate the cradle's final position from its rest position, counted without regard to direction. Each smudged stage may be read either way, and two smudged stages need not be read the same way.
Return the largest offset that is consistent with the log.
Examples
Example 1
- Input
- moves = "UUDD??"
- Output
- 2
Reading both smudged stages as drops leaves the cradle two floors below the rest position, so the offset is 2.
Example 2
- Input
- moves = "DDDU?"
- Output
- 3
Reading the smudged stage as a drop leaves the cradle three floors below the rest position, so the offset is 3.
Example 3
- Input
- moves = "??????"
- Output
- 6
Reading all six smudged stages as rises leaves the cradle six floors above the rest position, so the offset is 6.
Constraints
- 1 <= moves.length <= 50
- moves consists only of the characters 'U', 'D' and '?'.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def furthest_offset(moves: str) -> int:public int furthestOffset(String moves)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- First Unique Character in a StringReal-world version: First Solo StopEasy
- Ransom NoteReal-world version: Spell It With TilesEasy
- Maximum Number of BalloonsReal-world version: Kettle Signs from the Tile TrayEasy
- Find Words That Can Be Formed by CharactersReal-world version: Signs Spellable From A Tile TrayEasy