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

【LeetCode每日一题合集】2023.10.23-2023.10.29(简单的一周)

文章目录

  • 2678. 老人的数目(简单遍历模拟)
  • 1155. 掷骰子等于目标和的方法数(动态规划)
  • 2698. 求一个整数的惩罚数(预处理+dfs回溯)
  • 2520. 统计能整除数字的位数(简单模拟)
  • 1465. 切割后面积最大的蛋糕(贪心)
  • 2558. 从数量最多的堆取走礼物(优先队列)
  • 274. H 指数(二分查找)
    • 先排序,再二分
    • O(n)计数排序

2678. 老人的数目(简单遍历模拟)

https://leetcode.cn/problems/number-of-senior-citizens/description/?envType=daily-question&envId=2023-10-23

在这里插入图片描述

在这里插入图片描述

class Solution {
    public int countSeniors(String[] details) {
        int ans = 0;
        for (String s: details) {
            int age = (s.charAt(11) - '0') * 10 + s.charAt(12) - '0';
            ans += age > 60? 1: 0;
        }
        return ans;
    }
}

会比下面的代码快一些。

class Solution {
    public int countSeniors(String[] details) {
        int ans = 0;
        for (String detail: details) {
            int age = Integer.parseInt(detail.substring(11, 13));
            ans += age > 60? 1: 0;
        }
        return ans;
    }
}

1155. 掷骰子等于目标和的方法数(动态规划)

https://leetcode.cn/problems/number-of-dice-rolls-with-target-sum/description/?envType=daily-question&envId=2023-10-24

在这里插入图片描述
提示:

1 <= n, k <= 30
1 <= target <= 1000

数据范围很小,采用三层循环。

class Solution {
    public int numRollsToTarget(int n, int k, int target) {
        long[][] dp = new long[n + 1][target + 1];
        final long MOD = (long)1e9 + 7;
        dp[0][0] = 1;
        for (int i = 1; i <= n; ++i) {          // 枚举骰子
            for (int j = 1; j <= k; j++) {      // 枚举当前面
                for (int x = 0; x <= target - j; ++x) { // 枚举上个骰子的和
                    dp[i][x + j] = (dp[i][x + j] + dp[i - 1][x]) % MOD;
                }
            }
        }
        return (int)dp[n][target];
    }
}

2698. 求一个整数的惩罚数(预处理+dfs回溯)

https://leetcode.cn/problems/find-the-punishment-number-of-an-integer/description/?envType=daily-question&envId=2023-10-25
在这里插入图片描述

提示:
1 <= n <= 1000

class Solution {
    static int[] ans = new int[1001];
    static int target = 0;
    // 预处理
    static {
        for (int i = 1; i <= 1000; ++i) {
            if (op(i)) {
                ans[i] = ans[i - 1] + i * i;
            } else ans[i] = ans[i - 1];
        }
    }

    public int punishmentNumber(int n) {
        System.out.println(op(1));
        return ans[n];
    }

    // 判断x是否满足条件
    public static boolean op(int x) {
        String s = String.valueOf(x * x);
        target = x;
        return dfs(s, 0, 0);
    }

    public static boolean dfs(String s, int i, int t) {
        if (i == s.length() && t == target) return true;
        if (i >= s.length()) return false;
        boolean res = false;
        for (int j = i + 1; j <= s.length() && !res; ++j) {
            res |= dfs(s, j, t + Integer.parseInt(s.substring(i, j)));
        }
        return res;
    }
}

2520. 统计能整除数字的位数(简单模拟)

https://leetcode.cn/problems/count-the-digits-that-divide-a-number/description/?envType=daily-question&envId=2023-10-26

在这里插入图片描述

提示:
1 <= num <= 10^9
num 的数位中不含 0

class Solution {
    public int countDigits(int num) {
        int t = num, ans = 0;
        while (t != 0) {
            if (num % (t % 10) == 0) ans++;
            t /= 10;
        }
        return ans;
    }
}

1465. 切割后面积最大的蛋糕(贪心)

https://leetcode.cn/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/description/?envType=daily-question&envId=2023-10-27

在这里插入图片描述
在这里插入图片描述

贪心得想,任意两个长和宽都可以组合起来。那么最大面积就是由最大的长和宽组合起来的结果。

class Solution {
    public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
        Arrays.sort(horizontalCuts);
        Arrays.sort(verticalCuts);
        int m = horizontalCuts.length, n = verticalCuts.length;
        int mxH = Math.max(h - horizontalCuts[m - 1], horizontalCuts[0]), mxW = Math.max(w - verticalCuts[n - 1], verticalCuts[0]);
        for (int i = 1; i < m; ++i) mxH = Math.max(mxH, horizontalCuts[i] - horizontalCuts[i - 1]);
        for (int i = 1; i < n; ++i) mxW = Math.max(mxW, verticalCuts[i] - verticalCuts[i - 1]);
        return (int)((long)mxH * mxW % (long)(1e9 + 7));
    }
}

2558. 从数量最多的堆取走礼物(优先队列)

https://leetcode.cn/problems/take-gifts-from-the-richest-pile/description/?envType=daily-question&envId=2023-10-28
在这里插入图片描述

提示:
1 <= gifts.length <= 10^3
1 <= gifts[i] <= 10^9
1 <= k <= 10^3

class Solution {
    public long pickGifts(int[] gifts, int k) {
        long s = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
        for (int g: gifts) {
            s += g;
            pq.offer(g);
        }
        for (int i = 0 ; i < k; ++i) {
            int v = pq.poll(), x = (int)Math.sqrt(v);
            s -= v - x;
            pq.offer(x);
        }
        return s;
    }
}

274. H 指数(二分查找)

https://leetcode.cn/problems/h-index/description/?envType=daily-question&envId=2023-10-29
在这里插入图片描述

提示:

n == citations.length
1 <= n <= 5000
0 <= citations[i] <= 1000

先排序,再二分

class Solution {
    public int hIndex(int[] citations) {
        Arrays.sort(citations);
        int n = citations.length, l = 0, r = n; // 二分h
        while (l < r) {
            int mid = l + r + 1 >> 1, v = citations[n - mid];
            if (v >= mid) l = mid;
            else r = mid - 1;
        }
        return l;
    }
}

O(n)计数排序

见:https://leetcode.cn/problems/h-index/solutions/869042/h-zhi-shu-by-leetcode-solution-fnhl/

倒序枚举统计引用数量>=i的论文数量。

class Solution {
    public int hIndex(int[] citations) {
        int n = citations.length, tot = 0;
        int[] cnt = new int[n + 1];
        for (int i = 0; i < n; ++i) {
            cnt[Math.min(citations[i], n)]++;
        }
        for (int i = n; i >= 0; --i) {
            tot += cnt[i];
            if (tot >= i) return i;
        }
        return 0;
    }
}

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

相关文章:

  • SparkSQL综合案例-省份维度的销售情况统计分析
  • 【深度学习】Python使用指定gpu运行代码
  • 基于 matplotlib 实现的基本排序算法的动态可视化项目源码,通过 pyaudio 增加音效,冒泡、选择、插入、快速等排序
  • RabbitMQ (4)
  • 机器学习之IV编码,分箱WOE编码
  • 云起无垠典型案例入选《2023软件供应链安全洞察》报告
  • MySQL-DQL【数据查询语言】(图码结合)
  • 首次cmake 多目录构建失败
  • 微信小程序 slot 不显示
  • 私有云:【3】NFS存储服务器的安装
  • Linux内核驱动开发的需要掌握的知识点
  • 虚拟化、容器与Docker基本介绍以及安装部署(Docker 基本管理)
  • 前端、HTTP协议(重点)
  • 阿里云企业邮箱基于Spring Boot快速实现发送邮件功能
  • SQLi靶场
  • C语言每日一题(21)删除排序数组中的重复项
  • maven之父子工程版本控制案例实战,及拓展groupId和artifactId的含义
  • 67 内网安全-域横向smbwmi明文或hash传递
  • MacOS将Node.js升级到最新版本
  • 服务器之日常整活
  • Go语言用Resty库编写的音频爬虫代码
  • MatlabR2019b安装教程
  • 《Attention Is All You Need》阅读笔记
  • Hadoop、Hive安装
  • 【Qt6】为什么“转到槽函数“能在没有添加connect的情况下,把一个组件或对象和一个函数关联起来?
  • 力扣labuladong——一刷day12
  • 拿捏面试官,高频接口自动化测试面试题总结(附答案)狂收offer...
  • 是顺流还是逆流?未来物流作业是否将被机器人全面取代?
  • 安装 GMP、NTL、CTMalloc ,编译 OpenFHE
  • matlab将十六进制转换为十进制(hex2dec函数)