• 320力扣周赛总结


    目录

     一、三元组数目

    二、二叉树最近结点查询

    三、到达首都的最少油耗

    四、完美分割的方案数


     一、三元组数目

    6241. 数组中不等三元组的数目icon-default.png?t=M85Bhttps://leetcode.cn/problems/number-of-unequal-triplets-in-array/

     

     思路:数据范围都非常小,三重循环即可,开胃小菜!

    1. class Solution {
    2. public int unequalTriplets(int[] nums) {
    3. int n = nums.length;
    4. int res = 0;
    5. for (int i = 0; i < n; i ++) {
    6. for (int j = i + 1; j < n; j ++) {
    7. for (int k = j + 1; k < n; k ++) {
    8. if (nums[i] != nums[j] && nums[j] != nums[k] && nums[i] != nums[k]) res ++;
    9. }
    10. }
    11. }
    12. return res;
    13. }
    14. }

    二、二叉树最近结点查询二叉搜索树最近节点查询icon-default.png?t=M85Bhttps://leetcode.cn/problems/closest-nodes-queries-in-a-binary-search-tree/

     

    思路:给我们的是二叉搜索树,做二叉搜索树最关键的一点一定要记得它的中序遍历是有序的,因此我们只需要求出中序遍历,再二分求小于它的最大值和大于它的最小值(当然也可以用TreeSet)

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. List list;
    18. public List> closestNodes(TreeNode root, List queries) {
    19. list = new ArrayList<>();
    20. dfs(root); // dfs求中序遍历
    21. System.out.print(list);
    22. int n = queries.size();
    23. int m = list.size();
    24. List> res = new ArrayList<>();
    25. for (int i = 0; i < n; i ++) {
    26. List t = new ArrayList<>();
    27. int l = 0,r = m - 1;
    28. while (l < r) {
    29. int mid = (l + r + 1) >> 1;
    30. if (list.get(mid) <= queries.get(i)) l = mid;
    31. else r = mid - 1;
    32. }
    33. if (list.get(l) > queries.get(i)) t.add(-1);
    34. else t.add(list.get(l));
    35. l = 0;
    36. r = m - 1;
    37. while (l < r) {
    38. int mid = (l + r) >> 1;
    39. if (list.get(mid) >= queries.get(i)) r = mid;
    40. else l = mid + 1;
    41. }
    42. if (list.get(l) < queries.get(i)) t.add(-1);
    43. else t.add(list.get(l));
    44. res.add(t);
    45. }
    46. return res;
    47. }
    48. public void dfs(TreeNode root) {
    49. if (root == null) return;
    50. dfs(root.left);
    51. list.add(root.val);
    52. dfs(root.right);
    53. }
    54. }

     这里我犯了一个大错误:二分的边界是我们dfs后的那个数组,而不是queri数组。。。。比赛的时候调用api了(不建议大家学我哈哈哈)

    1. class Solution {
    2. TreeSet set;
    3. public List> closestNodes(TreeNode root, List q) {
    4. set = new TreeSet();
    5. int n = q.size();
    6. dfs(root);
    7. List> res = new ArrayList();
    8. // System.out.println(n);
    9. for (int i = 0; i < n; i ++) {
    10. // System.out.println(q.get(i));
    11. List t = new ArrayList<>();
    12. // System.out.println(set.floor(q.get(i)));
    13. t.add(set.floor(q.get(i)) == null ? -1 : set.floor(q.get(i)));
    14. t.add(set.ceiling(q.get(i)) == null ? -1 : set.ceiling(q.get(i)));
    15. res.add(t);
    16. }
    17. return res;
    18. }
    19. public void dfs(TreeNode root) {
    20. if (root == null) return;
    21. set.add(root.val);
    22. dfs(root.left);
    23. dfs(root.right);
    24. }
    25. }

    三、到达首都的最少油耗

     思路:当我们其他车汇聚到一个点的时候,我们要尽量去蹭我们当前点的车,能省尽量省,实在省不了的话就用它原有的车(仔细思考,一定不存在说车不够的情况,因为你要到达我这里你就必须坐车来,大不了我继续用你原有的车就行了,因此这道题变成了计算 车的数量的问题 ),车的数量就是 人数 / 座位数 上取整,dfs 求子树结点即可,记住结点为 0 的时候就不用算车的数量了,因为我们已经到达终点了~~

    1. class Solution {
    2. static int N = 200010;
    3. static int M = 2 * N;
    4. static int[] e = new int[M],ne = new int[M],h = new int[N];
    5. static int idx = 0;
    6. static int seats;
    7. long res = 0;
    8. public void init() {
    9. Arrays.fill(e,0);
    10. Arrays.fill(ne,0);
    11. Arrays.fill(h,-1);
    12. idx = 0;
    13. }
    14. public void add(int a,int b) {
    15. e[idx] = b;
    16. ne[idx] = h[a];
    17. h[a] = idx ++;
    18. }
    19. public long minimumFuelCost(int[][] roads, int seats) {
    20. init();
    21. this.seats = seats;
    22. int n = roads.length;
    23. for (int i = 0; i < n; i ++) {
    24. int a = roads[i][0];
    25. int b = roads[i][1];
    26. add(a,b);
    27. add(b,a);
    28. }
    29. dfs(0,-1);
    30. return res;
    31. }
    32. public int dfs(int u,int fa) {
    33. int size = 1;
    34. for (int i = h[u]; i != -1; i = ne[i]) {
    35. int j = e[i];
    36. if (fa != j) size += dfs(j,u);
    37. }
    38. if (u > 0) res += (size + seats - 1) / seats;
    39. return size;
    40. }
    41. }

    (上边代码用的是数组模拟邻接表,你也可以用list,更简单,用什么存储图不重要,重要的是理解思路)

    四、完美分割的方案数

     

     复杂dp 会了再来写。。。。。

  • 相关阅读:
    cesium 相机围绕视图中心点旋转
    弦截法及Python实现
    北邮22级信通院数电:Verilog-FPGA(9)第九周实验(2)实现下降沿触发的JK触发器(带异步复位和置位功能)
    数字化工厂管理系统的三个关键技术是什么
    【面试】摸鱼快看:关于selenium/ui自动化的面试题
    LVDS转换芯片,GM8284C数据手册!!!!
    pandas学习(四) apply
    GaussDB数据库SQL系列:DROP & TRUNCATE & DELETE
    好题记录 Leetcode 394.字符串解码 中等难度
    Django 做migrations时出错,解决方案
  • 原文地址:https://blog.csdn.net/qq_59539549/article/details/127949492