当前位置: 首页 > article >正文

LeetCode的几道题

一、捡石头 292

思路就是:

谁面对4块石头的时候,谁就输
(因为每次就是1-3块石头,如果剩下4块石头,你怎么拿,我都能把剩下的拿走,所以你就要想尽办法让对面面对4块石头的倍数,

  • 比如有10块石头,你想办法让对方面对4的倍数,10%4=2,也就是你先手拿走2块
  • 比如有13块石头,你想办法让对方面对4的倍数,13%4=1,也就是你先手拿走1块

但是假如你面对了4的倍数,你铁定输,因为对方也是聪明人。

于是先手能不能赢,就看

class Solution {
    public boolean canWinNim(int n) {
        return n % 4 != 0 ;
    }
}

二、捡石头 Nim 游戏 II 1908

int  nums = [ 1, 5, 8, 6 ]

我和你进行捡石头游戏,假如有4堆石头,
第一堆有1个石头,
第二堆有5个石头,
第三堆有8个石头,
第四堆有6个石头,

每次只能从最前面或者最后面取1堆石头,能否保证先手一定能赢

分析如下:

public static void main(String[] args) {
        int[] nums = {1, 5, 8, 6};
        int[] nums2 = {3, 9, 1, 2};
        int[] nums3 = {1, 1, 1, 1};
        int[] nums4 = {2, 5, 1, 3, 7, 8, 9, 11};

        int[] nums5 = {1000,0,10000,2,1};
        int[] nums6 = {10, 8, 20, 15, 3};
        int[] nums7 = {1, 1, 1, 10};

//        int[] nums0 = {5, 8, 6};
//        System.out.println(firstHandCanScore(nums0));

        System.out.println(firstHandCanScore(nums));
        System.out.println(firstHandCanScore(nums2));
        System.out.println(firstHandCanScore(nums3));
        System.out.println(firstHandCanScore(nums4));
        System.out.println(firstHandCanScore(nums5));
    }

    private static boolean firstHandCanScore(int[] nums) {
        WinScoreData winScoreData = process(nums, 0, nums.length - 1);
        System.out.println(winScoreData.winScore);
        return winScoreData.winScore > 0;
    }

    private static WinScoreData process(int[] nums, int fromIndex, int toIndex) {
        if (fromIndex == toIndex) {
            return new WinScoreData(nums, fromIndex, toIndex, nums[fromIndex]);
        }
        int startLeft = nums[fromIndex];
        WinScoreData chooseLeftWinScore = process(nums, fromIndex + 1, toIndex);
        int leftWinScore = startLeft - chooseLeftWinScore.winScore; // 选左边之后的赢面

        int startRight = nums[toIndex];
        WinScoreData chooseRightWinScore = process(nums, fromIndex, toIndex - 1);
        int rightWinScore = startRight - chooseRightWinScore.winScore; // 选右边之后的赢面


        int winScore = Math.max(leftWinScore, rightWinScore);
        return new WinScoreData(nums, fromIndex, toIndex, winScore);
    }

    @AllArgsConstructor
    public static class WinScoreData {
        private int[] nums;
        private int fromIndex;
        private int toIndex;
        private int winScore;
    }


http://www.kler.cn/news/155290.html

相关文章:

  • 程序员の养生之道
  • map优化对象数组
  • ThinkPHP 5 中,你可以使用定时任务调度器(TaskScheduler)来执行其他定时任务
  • Linux:动态查看服务器磁盘IO使用情况(IOTOP)
  • 若依框架分页
  • 栈和队列算法总结
  • springboot 2.4.4集成 hikari连接池多数据源实例
  • React-hook-form-mui (二):表单数据处理
  • 拥抱变化,良心AI工具推荐
  • 【物联网无线通信技术】ZigBee从理论到实践(CC2530)
  • Docker下安装MySQL
  • 2023年第十六届山东省职业院校技能大赛中职组“网络安全”赛项竞赛正式试题
  • 【最通用版FPGA 实现 SPI 驱动】
  • 力扣116. 填充每个节点的下一个右侧节点指针(详细讲解root根节点的理解)
  • 种群和种群之间连接的设计
  • 树莓派多串口通信
  • 力扣5.最长回文子串
  • 变分和导数有什么关系
  • 智能优化算法应用:基于动物迁徙算法无线传感器网络(WSN)覆盖优化 - 附代码
  • Linux 命令stat
  • Spring学习笔记:Day2
  • docker容器中创建非root用户
  • PMP-01
  • Docker安装Elasticsearch以及ik分词器
  • 8-1运用指针比较三个数的大小
  • 深入理解Servlet(下)
  • 【车载开发系列】FlashMemory基本概念
  • 使用Redis和opcache为网站加速教程
  • Filament引擎分析--command抽象设备API
  • 深入理解网络非阻塞 I/O:NIO