320力扣周赛总结

目录

 一、三元组数目

二、二叉树最近结点查询

三、到达首都的最少油耗

四、完美分割的方案数


 一、三元组数目

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

 

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

class Solution {
    public int unequalTriplets(int[] nums) {
        int n = nums.length;
        int res = 0;
        for (int i = 0; i < n; i ++) {
            for (int j = i + 1; j < n; j ++) {
                for (int k = j + 1; k < n; k ++) {
                    if (nums[i] != nums[j] && nums[j] != nums[k] && nums[i] != nums[k]) res ++;
                }
            }
        }
        return res;
    }
}

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

 

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

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<Integer> list;

    public List<List<Integer>> closestNodes(TreeNode root, List<Integer> queries) {
        list = new ArrayList<>();
        dfs(root); // dfs求中序遍历
        System.out.print(list);
        int n = queries.size();
        int m = list.size();
        List<List<Integer>> res = new ArrayList<>();

        for (int i = 0; i < n; i ++) {
            List<Integer> t = new ArrayList<>();
            int l = 0,r = m - 1;
            while (l < r) {
                int mid = (l + r + 1) >> 1;
                if (list.get(mid) <= queries.get(i)) l = mid;
                else r = mid - 1; 
            }
            if (list.get(l) > queries.get(i)) t.add(-1);
            else t.add(list.get(l));

            l = 0;
            r = m - 1; 
            while (l < r) {
                int mid = (l + r) >> 1;
                if (list.get(mid) >= queries.get(i)) r = mid;
                else l = mid + 1; 
            }
            if (list.get(l) < queries.get(i)) t.add(-1);
            else t.add(list.get(l));
            
            res.add(t);
        }
        return res;
    }

    public void dfs(TreeNode root) {
        if (root == null) return;
        dfs(root.left);
        list.add(root.val);
        dfs(root.right);
    }
}

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

class Solution {
    TreeSet<Integer> set;
    public List<List<Integer>> closestNodes(TreeNode root, List<Integer> q) {
        set = new TreeSet<Integer>();
        int n = q.size();
        dfs(root);
        List<List<Integer>> res = new ArrayList();
        // System.out.println(n);
        for (int i = 0; i < n; i ++) {
            // System.out.println(q.get(i));
            List<Integer> t = new ArrayList<>();
            // System.out.println(set.floor(q.get(i)));
            t.add(set.floor(q.get(i)) == null ? -1 : set.floor(q.get(i)));
            t.add(set.ceiling(q.get(i)) == null ? -1 : set.ceiling(q.get(i)));
            res.add(t);
        }
        return res;
    }
    
    public void dfs(TreeNode root) {
        if (root == null) return;
        set.add(root.val);
        dfs(root.left);
        dfs(root.right);
    }
}

三、到达首都的最少油耗

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

class Solution {
    static int N = 200010;
    static int M = 2 * N;
    static int[] e = new int[M],ne = new int[M],h = new int[N];
    static int idx = 0;
    static int seats;
    long res = 0;

    public void init() {
        Arrays.fill(e,0);
        Arrays.fill(ne,0);
        Arrays.fill(h,-1);
        idx = 0;
    }

    public void add(int a,int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx ++; 
    } 

    public long minimumFuelCost(int[][] roads, int seats) {
        init();
        this.seats = seats;
        int n = roads.length;
        for (int i = 0; i < n; i ++) {
            int a = roads[i][0];
            int b = roads[i][1];
            add(a,b);
            add(b,a);
        }
        dfs(0,-1);
        return res;
    }


    public int dfs(int u,int fa) {
        int size = 1;

        for (int i = h[u]; i != -1; i = ne[i]) {
            int j = e[i];
            if (fa != j) size += dfs(j,u);
        }
        if (u > 0) res += (size + seats - 1) / seats;
        return size;
    }

}

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

四、完美分割的方案数

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值