[data structure and algorithm] in-depth analysis of the solution idea and algorithm example of "stone game IX"

Posted by NME on Tue, 04 Jan 2022 23:15:23 +0100

1, Title Description

  • Alice and Bob designed a new stone game again. There are n stones in a row. Each stone has an associated number to represent its value. They give you an integer array stones, where stones[i] is the value of the ith stone.
  • Alice and Bob take turns in their own rounds. Alice takes the lead. In each round, players need to remove any stones from stones.
    • If the total value of all removed stones can be divided by 3 after the player removes the stones, the player will lose the game;
    • If the previous item is not satisfied and there are no stones left after removal, Bob will win directly (even in Alice's turn).
  • Assuming that both players adopt the best decision, if Alice wins, return true; If Bob wins, false is returned.
  • Example 1:
Input: stones = [2,1]
Output: true
 Explanation: the game is as follows:
- Round 1: Alice You can remove any stone.
- Round 2: Bob Remove the remaining stones. 
The sum of the removed stones is 1 + 2 = 3 And can be divided by 3. So, Bob Lose, Alice win victory.
  • Example 2:
Input: stones = [2]
Output: false
 Explanation: Alice The only stone will be removed, and the sum of the values of the removed stones is 2. 
Since all stones have been removed and the sum of values cannot be divided by 3, Bob win victory.
  • Example 3:
Input: stones = [5,1,2,4,3]
Output: false
 Explanation: Bob Always win. One possible way to play the game is as follows:
- Round 1: Alice You can remove the second stone with a value of 1. The sum of the removed stones is 1.
- Round 2: Bob The 5th stone with a value of 3 can be removed. The sum of the removed stones is = 1 + 3 = 4 . 
- Round 3: Alices You can remove the fourth stone with a value of 4. The sum of the removed stones is = 1 + 3 + 4 = 8 . 
- Round 4: Bob You can remove the third stone with a value of 2. The sum of the removed stones is = 1 + 3 + 4 + 2 = 10.
- Round 5: Alice The first stone with a value of 5 can be removed. The sum of the removed stones is = 1 + 3 + 4 + 2 + 5 = 15.
Alice Lose the game because the sum of the removed stones (15) can be divided by 3, Bob win victory.

2, Solution algorithm: construct the removal sequence and calculate the number of rounds

  • Since we only care about whether the sum can be divided by 3, we can divide stones into three groups according to the result of module 3, namely 0, 1 and 2.
  • According to the meaning of the question, 0 cannot be removed in the first round, otherwise the game will be lost directly. Therefore, only 1 or 2 can be removed in the first round. You can enumerate these two situations. If one of them can make Alice win, return true, otherwise return false.
  • The following is explained by removing 1 in the first round. Without considering the removal of 0, the subsequent removal can not be divided by 3. Therefore, the removed stones are fixed and form a sequence of 112121212... Cycles as a whole.
  • For 0, since the result of summation module 3 will not be changed after removal, and therefore the removal order of subsequent 1 and 2 will not be changed, 0 can be inserted at any non beginning position of the sequence.
  • In order not to let themselves lose, they must follow the above sequence until there are no stones, or one party can only remove the stones that cause the total to be divided by 3. Therefore, it is necessary to find the maximum number of rounds in which the sum of transfers cannot be divided by 3, which is equivalent to the longest length of 112121212... Sequence plus the number of 0.
  • If the number of stones in this round is odd and there are still stones left, it's Bob's turn to remove stones in the next round, and he can only remove one stone that divides the total by 3, so Alice wins; Otherwise Bob wins.
  • For the case of removing 2 in the first round, it will also form a 221212121... Cycle sequence. The practice is the same as above.
  • An example of the algorithm is as follows:
func check(c [3]int) bool {
	if c[1] == 0 {
		return false
	}
	c[1]-- // Start with 1
	turn := 1 + min(c[1], c[2])*2 + c[0] // Count rounds
	if c[1] > c[2] { // You can add a 1 to the end of the sequence
		turn++
		c[1]--
	}
	return turn%2 == 1 && c[1] != c[2] // The number of rounds is odd, and there are stones left
}

func stoneGameIX(stones []int) bool {
	c := [3]int{}
	for _, v := range stones {
		c[v%3]++
	}
	return check(c) || check([3]int{c[0], c[2], c[1]}) // Enumerate whether 1 or 2 were removed in the first round
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
  • Note that for the number of rounds, only its parity needs to be considered, so the min(c[1], c[2])*2 that is always even can be removed, and then it is discussed according to the parity of c[0]:
    • If c[0] is an even number, in order to make the number of rounds odd, c[1] > c[2] must not be true. You can select the smaller value of c[1] and c[2] as the stone removed in the first round. In addition to making c[1] > c[2] not true, due to c[1] --, you can also make c[1]= c[2] established. Therefore, when c[0] is an even number, Alice can win only when min (c[1], c[2]) > 0, that is, c[1] > 0 and c[2] > 0;
    • If c[0] is odd, to make the rounds odd, c[1] > c[2] must be true. After executing c[1] - twice, because the last c[1] is to be met= c[2], which is equivalent to satisfying c[1] - 2 > c[2] at the beginning. Therefore, when c[0] is an odd number, Alice can win only when c[1] − 2 > c[2] or c[2] − 2 > c[1] is satisfied.
func stoneGameIX(stones []int) bool {
	c := [3]int{}
	for _, v := range stones {
		c[v%3]++
	}
	if c[0]%2 == 0 {
		return c[1] > 0 && c[2] > 0 // min(c[1], c[2]) > 0
	}
	return c[1]-2 > c[2] || c[2]-2 > c[1]
}

3, Blog star

  • This year is my first time to participate in the blog star. I need the support of all leaders. In my busy schedule, take some valuable time to vote for me: https://bbs.csdn.net/topics/603955258 Give me a five-star (the list will reply one by one). Thank you very much!

Topics: data structure leetcode