• LeetCode Weekly Contest 21


    LeetCode Weekly Contest 21

    530. Minimum Absolute Difference in BST

     
    • User Accepted: 1081
    • User Tried: 1220
    • Total Accepted: 1107
    • Total Submissions: 2328
    • Difficulty: Easy

    Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

    Example:

    Input:
    
       1
        
         3
        /
       2
    
    Output:
    1
    
    Explanation:
    The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
    

    Note: There are at least two nodes in this BST.

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        void dfs(TreeNode *root, int cur, int& val){
            if(root->left){
                if(abs(cur - root->left->val) < val){
                    val = abs(cur - root->left->val); 
                }
                dfs(root->left, cur, val); 
            }
            if(root->right){
                if(abs(cur - root->right->val) < val){
                    val = abs(cur - root->right->val); 
                }
                dfs(root->right, cur, val); 
            }
        }
        
        int getMinimumDifference(TreeNode* root) {
            if(root == NULL){
                return 0; 
            }
            int val = 0x3f3f3f3f; 
            dfs(root, root->val, val); 
            if(root->left){
                val = min(val, getMinimumDifference(root->left)); 
            }
            if(root->right){
                val = min(val, getMinimumDifference(root->right)); 
            }
            return val; 
        }
    };
    

      

    523. Continuous Subarray Sum

     
    • User Accepted: 824
    • User Tried: 1109
    • Total Accepted: 837
    • Total Submissions: 5803
    • Difficulty: Medium

    Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

    Example 1:

    Input: [23, 2, 4, 6, 7],  k=6
    Output: True
    Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
    

    Example 2:

    Input: [23, 2, 6, 4, 7],  k=6
    Output: True
    Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
    

    Note:

    1. The length of the array won't exceed 10,000.
    2. You may assume the sum of all the numbers is in the range of a signed 32-bit integer.
    class Solution {
    public:
        bool checkSubarraySum(vector<int>& nums, int k) {
            if(k == 0){
                int cnt = 0, i = 0; 
                while(i<nums.size()){
                    while(i<nums.size() && nums[i] == 0){
                        cnt++;
                        i++; 
                    }
                    if(cnt >= 2){
                        return true; 
                    }else{
                        cnt = 0; 
                    }
                    i++; 
                }
                return false; 
            }
            map<int, int> mp; 
            mp[0] = 0; 
            int tmp, sum = 0; 
            for(int i=0; i<nums.size(); ++i){
                sum += nums[i]; 
                tmp = sum % k; 
                if(mp.find(tmp) == mp.end()){
                    mp[tmp] = i+1; 
                }else{
                    if( (i+1 - mp[tmp]) >= 2){
                        return true; 
                    }
                }
            }
            return false; 
        }
    };
    

      

    524. Longest Word in Dictionary through Deleting

     
    • User Accepted: 609
    • User Tried: 827
    • Total Accepted: 619
    • Total Submissions: 2039
    • Difficulty: Medium

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

    Example 1:

    Input:
    s = "abpcplea", d = ["ale","apple","monkey","plea"]
    
    Output: 
    "apple"
    

    Example 2:

    Input:
    s = "abpcplea", d = ["a","b","c"]
    
    Output: 
    "a"
    

    Note:

    1. All the strings in the input will only contain lower-case letters.
    2. The size of the dictionary won't exceed 1,000.
    3. The length of all the strings in the input won't exceed 1,000.
        bool mycmp(string a, string b){
            if(a.length() == b.length()){
                for(int i=0; i<a.length(); ++i){
                    if(a[i] != b[i]){
                        return a[i] < b[i]; 
                    }
                }
            }
            return a.length() > b.length(); 
        }
    
    class Solution {
    public:
    
        string findLongestWord(string s, vector<string>& d) {
            sort(d.begin(), d.end(), mycmp); 
            
            for(int i=0; i<d.size(); ++i){
                int k = 0; 
                for(int j=0; j<s.length(); ++j){
                    if(k == d[i].length()){
                        return d[i]; 
                    }
                    if(s[j] == d[i][k]){
                        k++; 
                    }
                }
                if(k == d[i].length()){
                    return d[i]; 
                }
            }
            return "";
        }
    };
    

      

  • 相关阅读:
    快速排序和随机化快排学习
    P1330 封锁阳光大学 DFS
    P2577 [ZJOI2005]午餐 状压DP
    M. Subsequence 南昌邀请赛
    P1441 砝码称重 DFS回溯+DP
    P2661 信息传递 二分图的最小环
    P1196 [NOI2002]银河英雄传说 带权并查集
    P2024 [NOI2001]食物链 并查集
    F. Shovels Shop 背包DP
    P1514 引水入城 DFS
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/6446045.html
Copyright © 2020-2023  润新知