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

文章目录

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

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

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

java 复制代码
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;
    }
}

会比下面的代码快一些。

java 复制代码
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

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

java 复制代码
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

java 复制代码
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

java 复制代码
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


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

java 复制代码
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

java 复制代码
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

先排序,再二分

java 复制代码
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的论文数量。

java 复制代码
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;
    }
}
相关推荐
qq_4298796743 分钟前
省略号和可变参数模板
开发语言·c++·算法
飞川撸码2 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
Neil今天也要学习2 小时前
永磁同步电机参数辨识算法--IPMSM拓展卡尔曼滤波全参数辨识
单片机·嵌入式硬件·算法
yzx9910133 小时前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
亮亮爱刷题3 小时前
算法练习-回溯
算法
眼镜哥(with glasses)4 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
int型码农8 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
UFIT9 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面9 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked939 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise