第 320 场周赛

1.数组中不等三元组的数目

数组中不等三元组的数目

Solution

class Solution {
public:
    int unequalTriplets(vector<int>& nums) {
        int n = nums.size();
        int cnt = 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[i] != nums[k] && nums[k] != nums[j]){
                        cnt++;
                    }
                }
            }
        }
        return cnt;
    }
};

2.二叉搜索树最近节点查询

二叉搜索树最近节点查询

Solution

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void find(vector<int>& ans, TreeNode* root){//中序遍历 
        if(root == nullptr){
            return ;
        }
        find(ans,root->left);
        ans.push_back(root->val);
        find(ans,root->right);
    }
    vector<vector<int>> closestNodes(TreeNode* root, vector<int>& queries) {
        //中序遍历到数组中
        vector<int> ans;
        find(ans,root);
        //二分
        int n = queries.size();
        int len = ans.size()-1;
        vector<vector<int>> w;
        for(int i = 0;i < n;i++){
            if(queries[i] > ans[len]){
                w.push_back({ans[len],-1});
                continue;
            }
            if(queries[i] < ans[0]){
                w.push_back({-1,ans[0]});
                continue;
            }
            //二分找
            int r = ans.size() - 1;
            int l = 0;
            while(l <= r){
                int mid = l + (r - l) / 2;
                if(ans[mid] <= queries[i]){
                    l = mid + 1;
                }else{
                    r = mid - 1;
                }
            }
            //找完判断
            if(ans[r] == queries[i]){
                w.push_back({queries[i],queries[i]});
            }else{
                w.push_back({ans[r],ans[l]});
            }
        }
        return w;
    }
};

3.到达首都的最少油耗

到达首都的最少油耗

Solution

class Solution {
public:
    long long minimumFuelCost(vector<vector<int>>& roads, int seats) {
        //建图,邻接矩阵
        int n = roads.size() + 1;
        vector<vector<int>> g(n);
        for(auto& v : roads){
            int f = v[0], t = v[1];
            g[f].emplace_back(t);
            g[t].emplace_back(f);
        }
        long long ans = 0;
        function<int(int,int)> dfs= [&](int o,int p)->int{
            int size = 1;
            for(auto i : g[o]){
                if(i == p) continue;
                size += dfs(i,o);
            }
            if(o != 0) ans += (size + seats - 1) / seats;
            return size;
        };
        dfs(0,-1);
        return ans;
    }
};

4.完美分割的方案数

完美分割的方案数

Solution

class Solution {
    const int mod = 1e9+7;
    bool isprime(char c){
        return c == '2'||c == '5'||c == '7'||c == '3';
    }
    //判断能否在j和j-1之间分割
    bool check(string &s, int j){
        return j == 0 || j == s.length() || !isprime(s[j - 1]) && isprime(s[j]);
    }
public:
    int beautifulPartitions(string s, int k, int l) {
        //判断明显不合理的
        int n = s.length();
        if(k * l > n || !isprime(s[0]) || isprime(s[n-1])){
            return 0;
        }
        //动态规划
        int f[n + 1][n + 1];memset(f,0,sizeof(f));
        //初始化
        f[0][0] = 1;
        for(int i = 1;i <= k;i++){
            int sum = 0;
            for (int j = i * l; j + (k - i) * l <= n; j++){
                if(check(s, j - l)) sum = (sum + f[i - 1][j - l]) % mod;
                if (check(s, j)) f[i][j] = sum;
            }
        }
        return f[k][n];
    }
};
posted @ 2023-01-10 23:36  TTS-S  阅读(21)  评论(0编辑  收藏  举报