Skip to content

Latest commit

 

History

History
54 lines (48 loc) · 1.46 KB

2029.石子游戏-ix.md

File metadata and controls

54 lines (48 loc) · 1.46 KB

2029.石子游戏-ix

/*
 * @lc app=leetcode.cn id=2029 lang=typescript
 *
 * [2029] 石子游戏 IX
 */

// @lc code=start
function stoneGameIX(stones: number[]): boolean {}
// @lc code=end

解法 1: 找规律

function stoneGameIX(stones: number[]): boolean {
  const count = [0, 0, 0]
  for (const stone of stones) {
    const num = stone % 3
    count[num]++
  }
  if (count[0] & 1) {
    return Math.abs(count[1] - count[2]) > 2
  } else {
    return count[1] > 0 && count[2] > 0
  }
}
test.each([
  { input: { stones: [20, 3, 20, 17, 2, 12, 15, 17, 4] }, output: true },
  {
    input: {
      stones: [
        2, 33, 90, 62, 43, 21, 96, 20, 18, 84, 74, 61, 100, 5, 11, 4, 67, 96, 18, 6, 68, 82, 32, 76, 33, 93, 33, 71, 32,
        30, 63, 37, 46, 95, 51, 63, 77, 63, 84, 52, 78, 66, 76, 66, 9, 73, 92, 79, 65, 29, 42, 64, 46, 84, 95, 71, 15,
        68, 55, 9, 22, 64, 56, 83, 52, 47, 38, 19, 59, 32, 89, 29, 56, 84, 57, 90, 96, 19, 38, 13, 49, 65, 93, 8, 30,
        15, 12, 40, 84, 7, 6, 75, 36, 31, 6, 78, 64, 33, 49,
      ],
    },
    output: false,
  },
  { input: { stones: [2, 1] }, output: true },
  { input: { stones: [2] }, output: false },
  { input: { stones: [5, 1, 2, 4, 3] }, output: false },
])('input: stones = $input.stones', ({ input: { stones }, output }) => {
  expect(stoneGameIX(stones)).toEqual(output)
})