You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive “++” into “–”. The game ends when a person can no longer make a move and therefore the other person will be the winner.
Write a function to determine if the starting player can guarantee a win.
For example, given s = “++++”, return true. The starting player can guarantee a win by flipping the middle “++” to become “+–+”.
一开始写的算法是只要能赢就返回true,但题意是guarantee,即对手无论如何下都能保证赢。无奈刚过subscription时间,无法提交验证他们提供的test cases。
|
|
discussion里有更简洁的做法,在每次canWin里只考虑一方是否能赢,而不是先手下完再用一个循环考虑后手,两步之后再递归。
|
|
(ref: https://discuss.leetcode.com/topic/63191/c-backtracking-with-memorization-26ms)