1. 程式人生 > 其它 >LeetCode刷題記錄(426-450)

LeetCode刷題記錄(426-450)

435. 無重疊區間

Tag: 動態規劃 貪心

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if (intervals.empty()) {
            return 0;
        }
        sort(intervals.begin(), intervals.end(), [](const vector<int>& u, const vector<int>& v) {
            return u[0] < v[0];
        });
        int n = intervals.size();
        vector<int> dp(n, 1);
        for (int i = 1; i < n; i++) {
            for(int j = i - 1; j >= 0; j--) {
                if(intervals[j][1] <= intervals[i][0]) {
                    dp[i] = max(dp[i], dp[j] + 1);
                    break;
                }
            }
        }
        return n - *max_element(dp.begin(), dp.end());
    }
};
class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if (intervals.empty()) {
            return 0;
        }
        sort(intervals.begin(), intervals.end(), [](const vector<int>& u, const vector<int>& v) {
            return u[1] < v[1];
        });
        int n = intervals.size();
        int ans = 1;
        int right = intervals[0][1];
        for (int i = 1; i < intervals.size(); i++) {
            if (right <= intervals[i][0]) {
                ans++;
                right = intervals[i][1];
            }
        }
        return n - ans;
    }
};

436. 尋找右區間

Tag: 二分查詢

class Solution {
public:
    vector<int> findRightInterval(vector<vector<int>>& intervals) {
        int n = intervals.size();
        unordered_map<int, int> idxMap;
        vector<int> leftArr(n, 0);
        vector<int> ans(n, 0);
        for (int i = 0; i < n; i++) {
            idxMap[intervals[i][0]] = i;
            leftArr[i] = intervals[i][0];
        }

        sort(leftArr.begin(), leftArr.end());

        for (int i = 0; i < n; i++) {
            int idx = binarySearch(leftArr, intervals[i][1]);
            if (idx == -1) {
                ans[i] = -1;
            } else {
                ans[i] = idxMap[leftArr[idx]];
            }
        }
        return ans;
    }

    int binarySearch(vector<int>& nums, int target) {
        int n = nums.size();
        if (nums[n - 1] < target) {
            return -1;
        }
        int left = 0, right = n - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }
};

437. 路徑總和 III

Tag: 字首和

class Solution {
private:
    unordered_map<int, int> counter;

public:
    int pathSum(TreeNode *root, int targetSum) {
        counter[0] = 1;
        return dfs(root, targetSum, 0);
    }

    int dfs(TreeNode *root, int targetSum, int currSum) {
        if (root == nullptr) {
            return 0;
        }
        int ans = 0;
        currSum += root->val;
        if (counter.find(currSum - targetSum) != counter.end()) {
            ans += counter[currSum - targetSum];
        }
        counter[currSum]++;
        ans += dfs(root->left, targetSum, currSum);
        ans += dfs(root->right, targetSum, currSum);
        counter[currSum]--;

        return ans;
    }
};

438. 找到字串中所有字母異位詞

Tag: 雜湊表

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> res;
        int lenS = s.size(), lenP = p.size();
        if (lenS < lenP) {
            return res;
        }
        vector<int> cnt1(26, 0), cnt2(26, 0);
        for (int i = 0; i < p.size(); i++) {
            cnt1[s[i] - 'a']++;
            cnt2[p[i] - 'a']++;
        }
        if (cnt1 == cnt2) {
            res.push_back(0);
        }
        for (int i = p.size(); i < s.size(); i++) {
            cnt1[s[i] - 'a']++;
            cnt1[s[i - p.size()] - 'a']--;
            if (cnt1 == cnt2) {
                res.push_back(i - p.size() + 1);
            }
        }
        return res;
    }
};

450. 刪除二叉搜尋樹中的節點

Tag: 二叉樹

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (!root) {
            return root;
        }
        if (root->val > key) {
            root->left = deleteNode(root->left, key);
        } else if (root->val < key) {
            root->right = deleteNode(root->right, key);
        } else {
            if (root->left == nullptr) {
                return root->right;
            } else if (root->right == nullptr) {
                return root->left;
            } else {
                TreeNode* node = findMin(root->right);
                root->val = node->val;
                root->right = deleteNode(root->right, node->val);
            }
        }
        return root;
    }

    TreeNode* findMin(TreeNode* root) {
        while (root->left) {
            root = root->left;
        }
        return root;
    }
};