Coins in a Line

There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.

Could you please decide the first player will win or lose?

If the first player wins, return true, otherwise return false.

Example

Example 1:

Input: 1
Output: true

Example 2:

Input: 4
Output: true
Explanation:
The first player takes 1 coin at first. Then there are 3 coins left.
Whether the second player takes 1 coin or two, then the first player can take all coin(s) left.

Challenge

O(n) time and O(1) memory

思路:前面只要有必败的路径,那么我必胜;也就是说,只要前面f[i-1] f[i-2]只要有一个false,我就是true。

public class Solution {
    /**
     * @param n: An integer
     * @return: A boolean which equals to true if the first player will win
     */
    public boolean firstWillWin(int n) {
        // write your code here
        boolean[] dp = new boolean[n + 1];
        if(n == 0) return false;
        if(n <= 2) return true;
        dp[0] = false;
        dp[1] = dp[2] = true;
        for(int i = 3; i <= n; i++) {
            dp[i] = dp[i - 1] == false || dp[i - 2] == false;
        }
        return dp[n];
    }
}

题目想要O(1) memory, 那么就可以ongoing滚动数组;取模就可以了。

public class Solution {
    /**
     * @param n: An integer
     * @return: A boolean which equals to true if the first player will win
     */
    public boolean firstWillWin(int n) {
        // write your code here
        boolean[] dp = new boolean[2];
        if(n == 0) return false;
        if(n <= 2) return true;
        dp[0 % 2] = false;
        dp[1 % 2] = dp[2 % 2] = true;
        for(int i = 3; i <= n; i++) {
            dp[i % 2] = dp[(i - 1) % 2] == false || dp[(i - 2) % 2] == false;
        }
        return dp[n % 2];
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Let's approach this problem using a systematic method. We can start by listing all the possible combinations of coins that add up to 20. 1. 20 pennies 2. 19 pennies and 1 nickel 3. 18 pennies and 2 nickels 4. 18 pennies, 1 nickel, and 1 dime 5. 17 pennies and 3 nickels 6. 17 pennies, 2 nickels, and 1 dime 7. 16 pennies and 4 nickels 8. 16 pennies, 3 nickels, and 1 dime 9. 16 pennies, 2 nickels, and 2 dimes 10. 15 pennies and 5 nickels 11. 15 pennies, 4 nickels, and 1 dime 12. 15 pennies, 3 nickels, and 2 dimes 13. 14 pennies and 6 nickels 14. 14 pennies, 5 nickels, and 1 dime 15. 14 pennies, 4 nickels, and 2 dimes 16. 14 pennies, 3 nickels, 1 dime, and 1 quarter 17. 13 pennies and 7 nickels 18. 13 pennies, 6 nickels, and 1 dime 19. 13 pennies, 5 nickels, and 2 dimes 20. 13 pennies, 4 nickels, 1 dime, and 1 quarter 21. 12 pennies and 8 nickels 22. 12 pennies, 7 nickels, and 1 dime 23. 12 pennies, 6 nickels, and 2 dimes 24. 12 pennies, 5 nickels, 1 dime, and 1 quarter 25. 11 pennies and 9 nickels 26. 11 pennies, 8 nickels, and 1 dime 27. 11 pennies, 7 nickels, and 2 dimes 28. 11 pennies, 6 nickels, 1 dime, and 1 quarter 29. 10 pennies and 10 nickels 30. 10 pennies, 9 nickels, and 1 dime 31. 10 pennies, 8 nickels, and 2 dimes 32. 10 pennies, 7 nickels, 1 dime, and 1 quarter 33. 9 pennies and 11 nickels 34. 9 pennies, 10 nickels, and 1 dime 35. 9 pennies, 9 nickels, and 2 dimes 36. 9 pennies, 8 nickels, 1 dime, and 1 quarter 37. 8 pennies and 12 nickels 38. 8 pennies, 11 nickels, and 1 dime 39. 8 pennies, 10 nickels, and 2 dimes 40. 8 pennies, 9 nickels, 1 dime, and 1 quarter 41. 7 pennies and 13 nickels 42. 7 pennies, 12 nickels, and 1 dime 43. 7 pennies, 11 nickels, and 2 dimes 44. 7 pennies, 10 nickels, 1 dime, and 1 quarter 45. 6 pennies and 14 nickels 46. 6 pennies, 13 nickels, and 1 dime 47. 6 pennies, 12 nickels, and 2 dimes 48. 6 pennies, 11 nickels, 1 dime, and 1 quarter 49. 5 pennies and 15 nickels 50. 5 pennies, 14 nickels, and 1 dime 51. 5 pennies, 13 nickels, and 2 dimes 52. 5 pennies, 12 nickels, 1 dime, and 1 quarter 53. 4 pennies and 16 nickels 54. 4 pennies, 15 nickels, and 1 dime 55. 4 pennies, 14 nickels, and 2 dimes 56. 4 pennies, 13 nickels, 1 dime, and 1 quarter 57. 3 pennies and 17 nickels 58. 3 pennies, 16 nickels, and 1 dime 59. 3 pennies, 15 nickels, and 2 dimes 60. 3 pennies, 14 nickels, 1 dime, and 1 quarter 61. 2 pennies and 18 nickels 62. 2 pennies, 17 nickels, and 1 dime 63. 2 pennies, 16 nickels, and 2 dimes 64. 2 pennies, 15 nickels, 1 dime, and 1 quarter 65. 1 penny and 19 nickels 66. 1 penny, 18 nickels, and 1 dime 67. 1 penny, 17 nickels, and 2 dimes 68. 1 penny, 16 nickels, 1 dime, and 1 quarter 69. 20 nickels 70. 19 nickels and 1 dime 71. 18 nickels and 2 dimes 72. 17 nickels, 1 dime, and 1 quarter 73. 16 nickels and 4 dimes 74. 15 nickels, 3 dimes, and 1 quarter 75. 14 nickels, 2 dimes, and 3 quarters 76. 13 nickels, 1 dime, 3 quarters, and 1 half dollar 77. 12 nickels, 4 dimes, 1 quarter, and 1 half dollar 78. 11 nickels, 3 dimes, 3 quarters, and 1 half dollar 79. 10 nickels, 2 dimes, 5 quarters 80. 9 nickels, 1 dime, 7 quarters, and 1 half dollar 81. 8 nickels, 4 dimes, 2 quarters, and 1 half dollar 82. 7 nickels, 3 dimes, 4 quarters, and 1 half dollar 83. 6 nickels, 2 dimes, 6 quarters 84. 5 nickels, 1 dime, 8 quarters, and 1 half dollar 85. 4 nickels, 4 dimes, 4 quarters, and 1 half dollar 86. 3 nickels, 3 dimes, 6 quarters 87. 2 nickels, 2 dimes, 8 quarters, and 1 half dollar 88. 1 nickel, 1 dime, 10 quarters, and 1 half dollar 89. 12 dimes and 1 half dollar 90. 11 dimes and 3 quarters 91. 10 dimes, 2 quarters, and 1 half dollar 92. 9 dimes, 4 quarters 93. 8 dimes, 6 quarters, and 1 half dollar 94. 7 dimes, 8 quarters 95. 6 dimes, 10 quarters, and 1 half dollar 96. 5 dimes, 12 quarters 97. 4 dimes, 14 quarters, and 1 half dollar 98. 3 dimes, 16 quarters 99. 2 dimes, 18 quarters, and 1 half dollar 100. 1 dime, 20 quarters Therefore, there are 100 different combinations of pennies, nickels, dimes, quarters, and half dollars that a piggy bank can contain if it has 20 coins in it.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值