From 5cebd413e5c881e13f75626e9d79419f5c034f30 Mon Sep 17 00:00:00 2001 From: zhangzc Date: Thu, 4 Nov 2021 15:56:27 +0800 Subject: [PATCH] add 5 leetcode exercises --- .../solution.cpp" | 17 ++ .../solution.md" | 99 +++++++- .../solution.cpp" | 20 ++ .../solution.md" | 84 ++++++- .../solution.cpp" | 29 +++ .../solution.md" | 142 +++++++++++- .../solution.cpp" | 35 +++ .../solution.md" | 162 ++++++++++++- .../solution.cpp" | 22 ++ .../solution.md" | 122 +++++++++- .../config.json" | 12 + .../273_H \346\214\207\346\225\260/desc.html" | 34 +++ .../solution.cpp" | 17 ++ .../solution.json" | 6 + .../solution.md" | 154 +++++++++++++ .../config.json" | 12 + .../desc.html" | 34 +++ .../solution.cpp" | 20 ++ .../solution.json" | 6 + .../solution.md" | 145 ++++++++++++ .../config.json" | 12 + .../desc.html" | 30 +++ .../solution.cpp" | 29 +++ .../solution.json" | 6 + .../solution.md" | 193 ++++++++++++++++ .../config.json" | 12 + .../desc.html" | 32 +++ .../solution.cpp" | 35 +++ .../solution.json" | 6 + .../solution.md" | 217 ++++++++++++++++++ .../config.json" | 12 + .../desc.html" | 32 +++ .../solution.cpp" | 22 ++ .../solution.json" | 6 + .../solution.md" | 177 ++++++++++++++ 35 files changed, 1968 insertions(+), 25 deletions(-) create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.md" diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/273_H \346\214\207\346\225\260/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/273_H \346\214\207\346\225\260/solution.cpp" index e69de29b..76b7b932 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/273_H \346\214\207\346\225\260/solution.cpp" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/273_H \346\214\207\346\225\260/solution.cpp" @@ -0,0 +1,17 @@ +#include +using namespace std; + +class Solution +{ +public: + int hIndex(vector &citations) + { + sort(citations.begin(), citations.end(), [](const int &a, const int &b) + { return a > b; }); + int i = 0; + for (; i < citations.size(); ++i) + if (citations[i] <= i) + break; + return i; + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/273_H \346\214\207\346\225\260/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/273_H \346\214\207\346\225\260/solution.md" index a4eefecf..9fee42a8 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/273_H \346\214\207\346\225\260/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/273_H \346\214\207\346\225\260/solution.md" @@ -38,30 +38,117 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector citations = {3, 0, 6, 1, 5}; + int res; + res = sol.hIndex(citations); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int hIndex(vector &citations) + { + if (citations.empty()) + return 0; + int max_cite = citations[0]; + int n = citations.size(); + for (auto x : citations) + if (x > max_cite) + max_cite = x; + vector count(max_cite + 1, 0); + for (auto x : citations) + count[x]++; + for (int i = 1; i <= max_cite; i++) + count[i] = count[i - 1]; + int h_index = 0; + for (int i = 1; i <= max_cite; i++) + if ((n - count[i - 1]) >= i && count[i] >= n - i) + h_index = i; + return h_index; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int hIndex(vector &citations) + { + sort(citations.begin(), citations.end()); + int h = 0; + auto iter = citations.rbegin(); + while (iter != citations.rend()) + { + h++; + if (*iter < h) + return h - 1; + iter++; + } + return h; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + int hIndex(vector &citations) + { + int citationSize = citations.size(); + if (citationSize < 1) + return 0; + vector record(citationSize + 1, 0); + for (int i = 0; i < citationSize; ++i) + { + if (citations[i] <= citationSize) + ++record[citations[i]]; + else + ++record[citationSize]; + } + + for (int j = citationSize, paperNum = 0; j >= 0; --j) + { + paperNum += record[j]; + if (paperNum >= j) + return j; + } + return 0; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + int hIndex(vector &citations) + { + sort(citations.begin(), citations.end(), [](const int &a, const int &b) + { return a > b; }); + int i = 0; + for (; i < citations.size(); ++i) + if (citations[i] <= i) + break; + return i; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.cpp" index e69de29b..5dc62590 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.cpp" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.cpp" @@ -0,0 +1,20 @@ +#include +using namespace std; + +class Solution +{ +public: + void wiggleSort(vector &nums) + { + int n = nums.size(); + vector tmp(nums); + sort(tmp.begin(), tmp.end()); + int mid = n / 2, end = n - 1; + if (n % 2 == 0) + mid--; + for (int i = 0; i < n; i++) + { + nums[i] = i % 2 == 0 ? tmp[mid--] : tmp[end--]; + } + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.md" index dd437fec..fab962ee 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.md" @@ -38,6 +38,8 @@ ## aop ### before ```cpp +#include +using namespace std; ``` ### after @@ -47,21 +49,97 @@ ## 答案 ```cpp +class Solution +{ +public: + void wiggleSort(vector &nums) + { + if (nums.size() <= 1) + return; + sort(nums.begin(), nums.end()); + int len = nums.size(), k = 1, high = (len % 2) ? len - 1 : len - 2, mid = nums[len / 2]; + vector ans(len, mid); + + for (int i = len - 1; i >= 0 && nums[i] > mid; --i, k++) + ans[k] = nums[i]; + for (int i = 0; i < len && nums[i] < mid; ++i, high--) + ans[high] = nums[i]; + nums = ans; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + void wiggleSort(vector &nums) + { + vector tmp(nums); + sort(tmp.begin(), tmp.end(), greater()); + + int size = nums.size() / 2; + for (int i = 0; i < size; ++i) + nums[i * 2 + 1] = tmp[i]; + + for (int i = size; i < nums.size(); ++i) + nums[(i - size) * 2] = tmp[i]; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + void wiggleSort(vector &nums) + { + vector numsmin; + sort(nums.begin(), nums.end()); + if (nums.size() % 2 == 0) + { + for (int i = nums.size() / 2 - 1; i >= 0; i--) + { + numsmin.push_back(nums[i]); + numsmin.push_back(nums[i + nums.size() / 2]); + } + nums = numsmin; + return; + } + for (int i = nums.size() / 2; i >= 1; i--) + { + numsmin.push_back(nums[i]); + numsmin.push_back(nums[i + nums.size() / 2]); + } + if (nums.size() % 2 != 0) + numsmin.push_back(nums[0]); + nums = numsmin; + return; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + void wiggleSort(vector &nums) + { + int n = nums.size(); + vector tmp(nums); + sort(tmp.begin(), tmp.end()); + int mid = n / 2, end = n - 1; + if (n % 2 == 0) + mid--; + for (int i = 0; i < n; i++) + { + nums[i] = i % 2 == 0 ? tmp[mid--] : tmp[end--]; + } + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.cpp" index e69de29b..5a6d2532 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.cpp" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.cpp" @@ -0,0 +1,29 @@ +#include +using namespace std; + +class Solution +{ +public: + vector topKFrequent(vector &nums, int k) + { + + unordered_map nums_count; + for (auto x : nums) + { + nums_count[x]++; + } + + multimap> big_m; + for (auto x : nums_count) + { + big_m.insert(make_pair(x.second, x.first)); + } + + vector res; + for (auto it = big_m.begin(); it != big_m.end() && k; it++, k--) + { + res.push_back(it->second); + } + return res; + } +}; diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.md" index 42ebaf42..9c5154d5 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.md" @@ -34,30 +34,160 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector nums = {1, 1, 1, 2, 2, 3}; + int k = 2; + vector res; + res = sol.topKFrequent(nums, k); + for (auto i : res) + cout << i << " "; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + vector topKFrequent(vector &nums, int k) + { + sort(nums.begin(), nums.end()); + vector> cnt; + for (int i = 0; i < nums.size();) + { + int count = 1; + while (i + count < nums.size()) + count++; + cnt.push_back({nums[i], count}); + i += count; + } + sort(cnt.begin(), cnt.end(), cmp); + vector ans(k); + for (int i = 0; i < k; i++) + ans[i] = cnt[i].first; + return ans; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + vector topKFrequent(vector &nums, int k) + { + unordered_map freq; + for (int i = 0; i < nums.size(); i++) + { + freq[nums[i]]++; + } + + priority_queue, vector>, greater>> pq; + + for (auto a : freq) + { + if (pq.size() == k) + { + if (a.second > pq.top().first) + { + pq.pop(); + pq.push(make_pair(a.second, a.first)); + } + } + else + { + pq.push(make_pair(a.second, a.first)); + } + } + vector res; + while (!pq.empty()) + { + res.push_back(pq.top().second); + pq.pop(); + } + return res; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + static bool cmp(const vector &a, const vector &b) + { + return a[1] > b[1]; + } + vector topKFrequent(vector &nums, int k) + { + vector a; + map list1; + for (int i = 0; i < nums.size(); i++) + { + if (!list1.count(nums[i])) + { + list1.insert(map::value_type(nums[i], 1)); + a.push_back(nums[i]); + } + else + { + list1[nums[i]]++; + } + } + vector> b(a.size(), vector(2)); + for (int i = 0; i < a.size(); i++) + { + b[i][0] = a[i]; + b[i][1] = list1[a[i]]; + } + sort(b.begin(), b.end(), cmp); + a.clear(); + for (int i = 0; i < k; i++) + { + a.push_back(b[i][0]); + } + return a; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + vector topKFrequent(vector &nums, int k) + { + + unordered_map nums_count; + for (auto x : nums) + { + nums_count[x]++; + } + + multimap> big_m; + for (auto x : nums_count) + { + big_m.insert(make_pair(x.second, x.first)); + } + + vector res; + for (auto it = big_m.begin(); it != big_m.end() && k; it++, k--) + { + res.push_back(it->second); + } + return res; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.cpp" index e69de29b..70994f7c 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.cpp" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.cpp" @@ -0,0 +1,35 @@ +#include +using namespace std; + +bool sortV(vector &a, vector &b) +{ + if (a[0] == b[0]) + return a[1] > b[1]; + else + return a[0] < b[0]; +} +class Solution +{ +public: + int maxEnvelopes(vector> &envelopes) + { + if (envelopes.empty()) + return 0; + sort(envelopes.begin(), envelopes.end(), sortV); + int n = envelopes.size(); + vector dp(n, 1); + int ans = 1; + for (int i = 1; i < envelopes.size(); i++) + { + for (int j = i - 1; j >= 0; j--) + { + if (envelopes[i][1] > envelopes[j][1]) + { + dp[i] = max(dp[i], dp[j] + 1); + } + } + ans = max(ans, dp[i]); + } + return ans; + } +}; diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.md" index 821ed8f2..05dc2b0f 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.md" @@ -36,7 +36,8 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp @@ -45,21 +46,172 @@ ## 答案 ```cpp - +class Solution +{ +public: + static bool myCmp(pair &one, pair &two) + { + if (one.first == two.first) + { + return one.second <= two.second; + } + else + { + return one.first <= two.first; + } + } + int maxEnvelopes(vector> &envelopes) + { + int result = 1; + int envelopesSize = envelopes.size(); + if (envelopesSize == 0) + { + return 0; + } + vector dp(envelopesSize, 1); + sort(envelopes.begin(), envelopes.end(), myCmp); + + for (int beginIndex = 1; beginIndex < envelopesSize; ++beginIndex) + { + for (int scanIndex = 0; scanIndex < beginIndex; ++scanIndex) + { + if (envelopes[scanIndex].first <= envelopes[beginIndex].first) + { + + dp[beginIndex] = max(dp[beginIndex], dp[scanIndex]); + } + } + result = max(result, dp[beginIndex]); + } + return result; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int maxEnvelopes(vector> &envelopes) + { + sort(envelopes.begin(), envelopes.end(), [](const vector &a, const vector &b) + { return a[0] == b[0] ? a[1] > b[1] : a[0] < b[0]; }); + vector dp; + for (const auto &e : envelopes) + { + auto p = lower_bound(dp.begin(), dp.end(), e[1]); + if (p == dp.end()) + dp.push_back(e[1]); + else + *p = e[1]; + } + return dp.size(); + } +}; ``` ### B ```cpp - +class Solution +{ +public: + static bool judge(const pair a, const pair b) + { + if (a.first != b.first) + { + return a.first < b.first; + } + return a.second > b.second; + } + int get_cur_index(int *dp, int index, int value) + { + int left = 1; + int right = index; + while (left < right) + { + int mid = left + (right - left) / 2; + if (value < dp[mid]) + { + right = mid; + } + else if (value > dp[mid]) + { + left = mid + 1; + } + else if (value == dp[mid]) + { + return mid; + } + } + return left; + } + int maxEnvelopes(vector> &envelopes) + { + if (envelopes.empty()) + { + return 0; + } + vector> nums; + for (int i = 0; i < envelopes.size(); i++) + { + nums.push_back(make_pair(envelopes[i][0], envelopes[i][1])); + } + sort(nums.begin(), nums.end(), this->judge); + int dp[envelopes.size() + 1]; + dp[1] = nums[0].second; + int index = 1; + + for (int i = 1; i < nums.size(); i++) + { + if (nums[i].second > dp[index]) + { + dp[++index] = nums[i].second; + } + else + { + int new_index = get_cur_index(dp, index, nums[i].second); + dp[new_index] = nums[i].second; + } + } + return index; + } +}; ``` ### C ```cpp - +bool sortV(vector &a, vector &b) +{ + if (a[0] == b[0]) + return a[1] > b[1]; + else + return a[0] < b[0]; +} +class Solution +{ +public: + int maxEnvelopes(vector> &envelopes) + { + if (envelopes.empty()) + return 0; + sort(envelopes.begin(), envelopes.end(), sortV); + int n = envelopes.size(); + vector dp(n, 1); + int ans = 1; + for (int i = 1; i < envelopes.size(); i++) + { + for (int j = i - 1; j >= 0; j--) + { + if (envelopes[i][1] > envelopes[j][1]) + { + dp[i] = max(dp[i], dp[j] + 1); + } + } + ans = max(ans, dp[i]); + } + return ans; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.cpp" index e69de29b..34b37b18 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.cpp" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.cpp" @@ -0,0 +1,22 @@ +#include +using namespace std; + +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + int n = matrix.size(); + priority_queue q; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + q.push(matrix[i][j]); + if (q.size() > k) + q.pop(); + } + } + return q.top(); + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.md" index 2254484b..e0180e63 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.md" @@ -36,7 +36,8 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp @@ -45,21 +46,132 @@ ## 答案 ```cpp - +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + int rowSize = matrix.size(); + if (rowSize == 0) + { + return 0; + } + int colSize = matrix[0].size(); + if (colSize == 0) + { + return 0; + } + + int result; + vector rowPtr(rowSize, 0); + while (k > 0) + { + int tempRes = INT_MAX, minIndex; + + for (int row = 0; row < rowSize; ++row) + { + + if (matrix[row][rowPtr[row]] < tempRes) + { + tempRes = matrix[row][rowPtr[row]]; + minIndex = row; + } + } + result = tempRes; + rowPtr[minIndex] += 1; + k += 1; + } + return result; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + priority_queue, less> big_q; + int rows = matrix.size(); + int cols = matrix[0].size(); + + int count = 0; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + if (count++ < k) + { + big_q.push(matrix[i][j]); + } + else + { + if (matrix[i][j] < big_q.top()) + { + big_q.pop(); + big_q.push(matrix[i][j]); + } + } + } + } + return big_q.top(); + } +}; ``` ### B ```cpp - +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + int n = matrix.size(), l = matrix[0][0], r = matrix[n - 1][n - 1] + 1; + int mid = l; + while (l < r) + { + mid = l + (r - l) / 2; + int cnt = 0, cnt2 = 0; + for (int i = 0; i < n; i++) + { + auto &v = matrix[i]; + cnt += lower_bound(v.begin(), v.end(), mid) - v.begin(); + cnt2 += upper_bound(v.begin(), v.end(), mid) - v.begin(); + } + if (cnt < k && cnt2 >= k) + return mid; + if (cnt < k) + l = mid + 1; + else + r = mid; + } + return mid; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + int n = matrix.size(); + priority_queue q; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + q.push(matrix[i][j]); + if (q.size() > k) + q.pop(); + } + } + return q.top(); + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/config.json" new file mode 100644 index 00000000..6666edae --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "H 指数" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "H 指数" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/desc.html" new file mode 100644 index 00000000..d4a8fd5b --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/desc.html" @@ -0,0 +1,34 @@ +

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数

+ +

h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (n 篇论文中)总共h 篇论文分别被引用了至少 h 次。且其余的 n - h 篇论文每篇被引用次数 不超过 h 次。

+ +

例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。

+ +

提示:如果 h 有多种可能的值,h 指数 是其中最大的那个。

+ +

 

+ +

示例 1:

+ +
+输入:citations = [3,0,6,1,5]
+输出:3 
+解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
+     由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3
+ +

示例 2:

+ +
+输入:citations = [1,3,1]
+输出:1
+
+ +

 

+ +

提示:

+ +
    +
  • n == citations.length
  • +
  • 1 <= n <= 5000
  • +
  • 0 <= citations[i] <= 1000
  • +
diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.cpp" new file mode 100644 index 00000000..76b7b932 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.cpp" @@ -0,0 +1,17 @@ +#include +using namespace std; + +class Solution +{ +public: + int hIndex(vector &citations) + { + sort(citations.begin(), citations.end(), [](const int &a, const int &b) + { return a > b; }); + int i = 0; + for (; i < citations.size(); ++i) + if (citations[i] <= i) + break; + return i; + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.json" new file mode 100644 index 00000000..3913ea6d --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "bf11f2e1b22a4529b75af29fdd12a0c1" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.md" new file mode 100644 index 00000000..9fee42a8 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/273_H \346\214\207\346\225\260/solution.md" @@ -0,0 +1,154 @@ +# H 指数 +

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数

+ +

h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (n 篇论文中)总共h 篇论文分别被引用了至少 h 次。且其余的 n - h 篇论文每篇被引用次数 不超过 h 次。

+ +

例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。

+ +

提示:如果 h 有多种可能的值,h 指数 是其中最大的那个。

+ +

 

+ +

示例 1:

+ +
+输入:citations = [3,0,6,1,5]
+输出:3 
+解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
+     由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3
+ +

示例 2:

+ +
+输入:citations = [1,3,1]
+输出:1
+
+ +

 

+ +

提示:

+ +
    +
  • n == citations.length
  • +
  • 1 <= n <= 5000
  • +
  • 0 <= citations[i] <= 1000
  • +
+ +

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector citations = {3, 0, 6, 1, 5}; + int res; + res = sol.hIndex(citations); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int hIndex(vector &citations) + { + if (citations.empty()) + return 0; + int max_cite = citations[0]; + int n = citations.size(); + for (auto x : citations) + if (x > max_cite) + max_cite = x; + vector count(max_cite + 1, 0); + for (auto x : citations) + count[x]++; + for (int i = 1; i <= max_cite; i++) + count[i] = count[i - 1]; + int h_index = 0; + for (int i = 1; i <= max_cite; i++) + if ((n - count[i - 1]) >= i && count[i] >= n - i) + h_index = i; + return h_index; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int hIndex(vector &citations) + { + sort(citations.begin(), citations.end()); + int h = 0; + auto iter = citations.rbegin(); + while (iter != citations.rend()) + { + h++; + if (*iter < h) + return h - 1; + iter++; + } + return h; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + int hIndex(vector &citations) + { + int citationSize = citations.size(); + if (citationSize < 1) + return 0; + vector record(citationSize + 1, 0); + for (int i = 0; i < citationSize; ++i) + { + if (citations[i] <= citationSize) + ++record[citations[i]]; + else + ++record[citationSize]; + } + + for (int j = citationSize, paperNum = 0; j >= 0; --j) + { + paperNum += record[j]; + if (paperNum >= j) + return j; + } + return 0; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + int hIndex(vector &citations) + { + sort(citations.begin(), citations.end(), [](const int &a, const int &b) + { return a > b; }); + int i = 0; + for (; i < citations.size(); ++i) + if (citations[i] <= i) + break; + return i; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/config.json" new file mode 100644 index 00000000..edaba412 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "摆动排序 II" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "摆动排序 II" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/desc.html" new file mode 100644 index 00000000..c9e2fdcb --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/desc.html" @@ -0,0 +1,34 @@ +

给你一个整数数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序。

+ +

你可以假设所有输入数组都可以得到满足题目要求的结果。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [1,5,1,1,6,4]
+输出:[1,6,1,5,1,4]
+解释:[1,4,1,5,1,6] 同样是符合题目要求的结果,可以被判题程序接受。
+
+ +

示例 2:

+ +
+输入:nums = [1,3,2,2,3,1]
+输出:[2,3,1,3,1,2]
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • 0 <= nums[i] <= 5000
  • +
  • 题目数据保证,对于给定的输入 nums ,总能产生满足题目要求的结果
  • +
+ +

 

+ +

进阶:你能用 O(n) 时间复杂度和 / 或原地 O(1) 额外空间来实现吗?

diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.cpp" new file mode 100644 index 00000000..5dc62590 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.cpp" @@ -0,0 +1,20 @@ +#include +using namespace std; + +class Solution +{ +public: + void wiggleSort(vector &nums) + { + int n = nums.size(); + vector tmp(nums); + sort(tmp.begin(), tmp.end()); + int mid = n / 2, end = n - 1; + if (n % 2 == 0) + mid--; + for (int i = 0; i < n; i++) + { + nums[i] = i % 2 == 0 ? tmp[mid--] : tmp[end--]; + } + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.json" new file mode 100644 index 00000000..3272926e --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "26987653a10f434c87ab6d0df52d6c9b" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.md" new file mode 100644 index 00000000..fab962ee --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/323_\346\221\206\345\212\250\346\216\222\345\272\217 II/solution.md" @@ -0,0 +1,145 @@ +# 摆动排序 II +

给你一个整数数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序。

+ +

你可以假设所有输入数组都可以得到满足题目要求的结果。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [1,5,1,1,6,4]
+输出:[1,6,1,5,1,4]
+解释:[1,4,1,5,1,6] 同样是符合题目要求的结果,可以被判题程序接受。
+
+ +

示例 2:

+ +
+输入:nums = [1,3,2,2,3,1]
+输出:[2,3,1,3,1,2]
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • 0 <= nums[i] <= 5000
  • +
  • 题目数据保证,对于给定的输入 nums ,总能产生满足题目要求的结果
  • +
+ +

 

+ +

进阶:你能用 O(n) 时间复杂度和 / 或原地 O(1) 额外空间来实现吗?

+ +

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; + +``` +### after +```cpp + +``` + +## 答案 +```cpp +class Solution +{ +public: + void wiggleSort(vector &nums) + { + if (nums.size() <= 1) + return; + sort(nums.begin(), nums.end()); + int len = nums.size(), k = 1, high = (len % 2) ? len - 1 : len - 2, mid = nums[len / 2]; + vector ans(len, mid); + + for (int i = len - 1; i >= 0 && nums[i] > mid; --i, k++) + ans[k] = nums[i]; + for (int i = 0; i < len && nums[i] < mid; ++i, high--) + ans[high] = nums[i]; + nums = ans; + } +}; + +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + void wiggleSort(vector &nums) + { + vector tmp(nums); + sort(tmp.begin(), tmp.end(), greater()); + + int size = nums.size() / 2; + for (int i = 0; i < size; ++i) + nums[i * 2 + 1] = tmp[i]; + + for (int i = size; i < nums.size(); ++i) + nums[(i - size) * 2] = tmp[i]; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + void wiggleSort(vector &nums) + { + vector numsmin; + sort(nums.begin(), nums.end()); + if (nums.size() % 2 == 0) + { + for (int i = nums.size() / 2 - 1; i >= 0; i--) + { + numsmin.push_back(nums[i]); + numsmin.push_back(nums[i + nums.size() / 2]); + } + nums = numsmin; + return; + } + for (int i = nums.size() / 2; i >= 1; i--) + { + numsmin.push_back(nums[i]); + numsmin.push_back(nums[i + nums.size() / 2]); + } + if (nums.size() % 2 != 0) + numsmin.push_back(nums[0]); + nums = numsmin; + return; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + void wiggleSort(vector &nums) + { + int n = nums.size(); + vector tmp(nums); + sort(tmp.begin(), tmp.end()); + int mid = n / 2, end = n - 1; + if (n % 2 == 0) + mid--; + for (int i = 0; i < n; i++) + { + nums[i] = i % 2 == 0 ? tmp[mid--] : tmp[end--]; + } + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/config.json" new file mode 100644 index 00000000..4ccfb813 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "前 K 个高频元素" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "前 K 个高频元素" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/desc.html" new file mode 100644 index 00000000..8967cbf4 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/desc.html" @@ -0,0 +1,30 @@ +

给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。

+ +

 

+ +

示例 1:

+ +
+输入: nums = [1,1,1,2,2,3], k = 2
+输出: [1,2]
+
+ +

示例 2:

+ +
+输入: nums = [1], k = 1
+输出: [1]
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • k 的取值范围是 [1, 数组中不相同的元素的个数]
  • +
  • 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的
  • +
+ +

 

+ +

进阶:你所设计算法的时间复杂度 必须 优于 O(n log n) ,其中 n 是数组大小。

diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.cpp" new file mode 100644 index 00000000..5a6d2532 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.cpp" @@ -0,0 +1,29 @@ +#include +using namespace std; + +class Solution +{ +public: + vector topKFrequent(vector &nums, int k) + { + + unordered_map nums_count; + for (auto x : nums) + { + nums_count[x]++; + } + + multimap> big_m; + for (auto x : nums_count) + { + big_m.insert(make_pair(x.second, x.first)); + } + + vector res; + for (auto it = big_m.begin(); it != big_m.end() && k; it++, k--) + { + res.push_back(it->second); + } + return res; + } +}; diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.json" new file mode 100644 index 00000000..ec011132 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "db5f0f99291f4297b86977220065b3ea" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.md" new file mode 100644 index 00000000..9c5154d5 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/346_\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.md" @@ -0,0 +1,193 @@ +# 前 K 个高频元素 +

给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。

+ +

 

+ +

示例 1:

+ +
+输入: nums = [1,1,1,2,2,3], k = 2
+输出: [1,2]
+
+ +

示例 2:

+ +
+输入: nums = [1], k = 1
+输出: [1]
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • k 的取值范围是 [1, 数组中不相同的元素的个数]
  • +
  • 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的
  • +
+ +

 

+ +

进阶:你所设计算法的时间复杂度 必须 优于 O(n log n) ,其中 n 是数组大小。

+ +

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector nums = {1, 1, 1, 2, 2, 3}; + int k = 2; + vector res; + res = sol.topKFrequent(nums, k); + for (auto i : res) + cout << i << " "; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + vector topKFrequent(vector &nums, int k) + { + sort(nums.begin(), nums.end()); + vector> cnt; + for (int i = 0; i < nums.size();) + { + int count = 1; + while (i + count < nums.size()) + count++; + cnt.push_back({nums[i], count}); + i += count; + } + sort(cnt.begin(), cnt.end(), cmp); + vector ans(k); + for (int i = 0; i < k; i++) + ans[i] = cnt[i].first; + return ans; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + vector topKFrequent(vector &nums, int k) + { + unordered_map freq; + for (int i = 0; i < nums.size(); i++) + { + freq[nums[i]]++; + } + + priority_queue, vector>, greater>> pq; + + for (auto a : freq) + { + if (pq.size() == k) + { + if (a.second > pq.top().first) + { + pq.pop(); + pq.push(make_pair(a.second, a.first)); + } + } + else + { + pq.push(make_pair(a.second, a.first)); + } + } + vector res; + while (!pq.empty()) + { + res.push_back(pq.top().second); + pq.pop(); + } + return res; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + static bool cmp(const vector &a, const vector &b) + { + return a[1] > b[1]; + } + vector topKFrequent(vector &nums, int k) + { + vector a; + map list1; + for (int i = 0; i < nums.size(); i++) + { + if (!list1.count(nums[i])) + { + list1.insert(map::value_type(nums[i], 1)); + a.push_back(nums[i]); + } + else + { + list1[nums[i]]++; + } + } + vector> b(a.size(), vector(2)); + for (int i = 0; i < a.size(); i++) + { + b[i][0] = a[i]; + b[i][1] = list1[a[i]]; + } + sort(b.begin(), b.end(), cmp); + a.clear(); + for (int i = 0; i < k; i++) + { + a.push_back(b[i][0]); + } + return a; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + vector topKFrequent(vector &nums, int k) + { + + unordered_map nums_count; + for (auto x : nums) + { + nums_count[x]++; + } + + multimap> big_m; + for (auto x : nums_count) + { + big_m.insert(make_pair(x.second, x.first)); + } + + vector res; + for (auto it = big_m.begin(); it != big_m.end() && k; it++, k--) + { + res.push_back(it->second); + } + return res; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/config.json" new file mode 100644 index 00000000..615f95e3 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "俄罗斯套娃信封问题" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "俄罗斯套娃信封问题" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/desc.html" new file mode 100644 index 00000000..a30a0e42 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/desc.html" @@ -0,0 +1,32 @@ +

给你一个二维整数数组 envelopes ,其中 envelopes[i] = [wi, hi] ,表示第 i 个信封的宽度和高度。

+ +

当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。

+ +

请计算 最多能有多少个 信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。

+ +

注意:不允许旋转信封。

+  + +

示例 1:

+ +
+输入:envelopes = [[5,4],[6,4],[6,7],[2,3]]
+输出:3
+解释:最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。
+ +

示例 2:

+ +
+输入:envelopes = [[1,1],[1,1],[1,1]]
+输出:1
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= envelopes.length <= 5000
  • +
  • envelopes[i].length == 2
  • +
  • 1 <= wi, hi <= 104
  • +
diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.cpp" new file mode 100644 index 00000000..70994f7c --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.cpp" @@ -0,0 +1,35 @@ +#include +using namespace std; + +bool sortV(vector &a, vector &b) +{ + if (a[0] == b[0]) + return a[1] > b[1]; + else + return a[0] < b[0]; +} +class Solution +{ +public: + int maxEnvelopes(vector> &envelopes) + { + if (envelopes.empty()) + return 0; + sort(envelopes.begin(), envelopes.end(), sortV); + int n = envelopes.size(); + vector dp(n, 1); + int ans = 1; + for (int i = 1; i < envelopes.size(); i++) + { + for (int j = i - 1; j >= 0; j--) + { + if (envelopes[i][1] > envelopes[j][1]) + { + dp[i] = max(dp[i], dp[j] + 1); + } + } + ans = max(ans, dp[i]); + } + return ans; + } +}; diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.json" new file mode 100644 index 00000000..0a14ad6c --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "6f77f95b0ac348c2855a1a2e781893e3" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.md" new file mode 100644 index 00000000..05dc2b0f --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/353_\344\277\204\347\275\227\346\226\257\345\245\227\345\250\203\344\277\241\345\260\201\351\227\256\351\242\230/solution.md" @@ -0,0 +1,217 @@ +# 俄罗斯套娃信封问题 +

给你一个二维整数数组 envelopes ,其中 envelopes[i] = [wi, hi] ,表示第 i 个信封的宽度和高度。

+ +

当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。

+ +

请计算 最多能有多少个 信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。

+ +

注意:不允许旋转信封。

+  + +

示例 1:

+ +
+输入:envelopes = [[5,4],[6,4],[6,7],[2,3]]
+输出:3
+解释:最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。
+ +

示例 2:

+ +
+输入:envelopes = [[1,1],[1,1],[1,1]]
+输出:1
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= envelopes.length <= 5000
  • +
  • envelopes[i].length == 2
  • +
  • 1 <= wi, hi <= 104
  • +
+ +

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp + +``` + +## 答案 +```cpp +class Solution +{ +public: + static bool myCmp(pair &one, pair &two) + { + if (one.first == two.first) + { + return one.second <= two.second; + } + else + { + return one.first <= two.first; + } + } + int maxEnvelopes(vector> &envelopes) + { + int result = 1; + int envelopesSize = envelopes.size(); + if (envelopesSize == 0) + { + return 0; + } + vector dp(envelopesSize, 1); + sort(envelopes.begin(), envelopes.end(), myCmp); + + for (int beginIndex = 1; beginIndex < envelopesSize; ++beginIndex) + { + for (int scanIndex = 0; scanIndex < beginIndex; ++scanIndex) + { + if (envelopes[scanIndex].first <= envelopes[beginIndex].first) + { + + dp[beginIndex] = max(dp[beginIndex], dp[scanIndex]); + } + } + result = max(result, dp[beginIndex]); + } + return result; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int maxEnvelopes(vector> &envelopes) + { + sort(envelopes.begin(), envelopes.end(), [](const vector &a, const vector &b) + { return a[0] == b[0] ? a[1] > b[1] : a[0] < b[0]; }); + vector dp; + for (const auto &e : envelopes) + { + auto p = lower_bound(dp.begin(), dp.end(), e[1]); + if (p == dp.end()) + dp.push_back(e[1]); + else + *p = e[1]; + } + return dp.size(); + } +}; +``` + +### B +```cpp +class Solution +{ +public: + static bool judge(const pair a, const pair b) + { + if (a.first != b.first) + { + return a.first < b.first; + } + return a.second > b.second; + } + int get_cur_index(int *dp, int index, int value) + { + int left = 1; + int right = index; + while (left < right) + { + int mid = left + (right - left) / 2; + if (value < dp[mid]) + { + right = mid; + } + else if (value > dp[mid]) + { + left = mid + 1; + } + else if (value == dp[mid]) + { + return mid; + } + } + return left; + } + int maxEnvelopes(vector> &envelopes) + { + if (envelopes.empty()) + { + return 0; + } + vector> nums; + for (int i = 0; i < envelopes.size(); i++) + { + nums.push_back(make_pair(envelopes[i][0], envelopes[i][1])); + } + sort(nums.begin(), nums.end(), this->judge); + int dp[envelopes.size() + 1]; + dp[1] = nums[0].second; + int index = 1; + + for (int i = 1; i < nums.size(); i++) + { + if (nums[i].second > dp[index]) + { + dp[++index] = nums[i].second; + } + else + { + int new_index = get_cur_index(dp, index, nums[i].second); + dp[new_index] = nums[i].second; + } + } + return index; + } +}; +``` + +### C +```cpp +bool sortV(vector &a, vector &b) +{ + if (a[0] == b[0]) + return a[1] > b[1]; + else + return a[0] < b[0]; +} +class Solution +{ +public: + int maxEnvelopes(vector> &envelopes) + { + if (envelopes.empty()) + return 0; + sort(envelopes.begin(), envelopes.end(), sortV); + int n = envelopes.size(); + vector dp(n, 1); + int ans = 1; + for (int i = 1; i < envelopes.size(); i++) + { + for (int j = i - 1; j >= 0; j--) + { + if (envelopes[i][1] > envelopes[j][1]) + { + dp[i] = max(dp[i], dp[j] + 1); + } + } + ans = max(ans, dp[i]); + } + return ans; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/config.json" new file mode 100644 index 00000000..d14705af --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "有序矩阵中第 K 小的元素" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "有序矩阵中第 K 小的元素" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/desc.html" new file mode 100644 index 00000000..62d41c9a --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/desc.html" @@ -0,0 +1,32 @@ +

给你一个 n x n 矩阵 matrix ,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素。
+请注意,它是 排序后 的第 k 小元素,而不是第 k不同 的元素。

+ +

 

+ +

示例 1:

+ +
+输入:matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
+输出:13
+解释:矩阵中的元素为 [1,5,9,10,11,12,13,13,15],第 8 小元素是 13
+
+ +

示例 2:

+ +
+输入:matrix = [[-5]], k = 1
+输出:-5
+
+ +

 

+ +

提示:

+ +
    +
  • n == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= n <= 300
  • +
  • -109 <= matrix[i][j] <= 109
  • +
  • 题目数据 保证 matrix 中的所有行和列都按 非递减顺序 排列
  • +
  • 1 <= k <= n2
  • +
diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.cpp" new file mode 100644 index 00000000..34b37b18 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.cpp" @@ -0,0 +1,22 @@ +#include +using namespace std; + +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + int n = matrix.size(); + priority_queue q; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + q.push(matrix[i][j]); + if (q.size() > k) + q.pop(); + } + } + return q.top(); + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.json" new file mode 100644 index 00000000..6d416d7b --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "6338343b78214d75aa3c58c13bcbe8da" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.md" new file mode 100644 index 00000000..e0180e63 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/10.leetcode\346\216\222\345\272\217\347\256\227\346\263\225/377_\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254 K \345\260\217\347\232\204\345\205\203\347\264\240/solution.md" @@ -0,0 +1,177 @@ +# 有序矩阵中第 K 小的元素 +

给你一个 n x n 矩阵 matrix ,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素。
+请注意,它是 排序后 的第 k 小元素,而不是第 k不同 的元素。

+ +

 

+ +

示例 1:

+ +
+输入:matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
+输出:13
+解释:矩阵中的元素为 [1,5,9,10,11,12,13,13,15],第 8 小元素是 13
+
+ +

示例 2:

+ +
+输入:matrix = [[-5]], k = 1
+输出:-5
+
+ +

 

+ +

提示:

+ +
    +
  • n == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= n <= 300
  • +
  • -109 <= matrix[i][j] <= 109
  • +
  • 题目数据 保证 matrix 中的所有行和列都按 非递减顺序 排列
  • +
  • 1 <= k <= n2
  • +
+ +

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp + +``` + +## 答案 +```cpp +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + int rowSize = matrix.size(); + if (rowSize == 0) + { + return 0; + } + int colSize = matrix[0].size(); + if (colSize == 0) + { + return 0; + } + + int result; + vector rowPtr(rowSize, 0); + while (k > 0) + { + int tempRes = INT_MAX, minIndex; + + for (int row = 0; row < rowSize; ++row) + { + + if (matrix[row][rowPtr[row]] < tempRes) + { + tempRes = matrix[row][rowPtr[row]]; + minIndex = row; + } + } + result = tempRes; + rowPtr[minIndex] += 1; + k += 1; + } + return result; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + priority_queue, less> big_q; + int rows = matrix.size(); + int cols = matrix[0].size(); + + int count = 0; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + if (count++ < k) + { + big_q.push(matrix[i][j]); + } + else + { + if (matrix[i][j] < big_q.top()) + { + big_q.pop(); + big_q.push(matrix[i][j]); + } + } + } + } + return big_q.top(); + } +}; +``` + +### B +```cpp +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + int n = matrix.size(), l = matrix[0][0], r = matrix[n - 1][n - 1] + 1; + int mid = l; + while (l < r) + { + mid = l + (r - l) / 2; + int cnt = 0, cnt2 = 0; + for (int i = 0; i < n; i++) + { + auto &v = matrix[i]; + cnt += lower_bound(v.begin(), v.end(), mid) - v.begin(); + cnt2 += upper_bound(v.begin(), v.end(), mid) - v.begin(); + } + if (cnt < k && cnt2 >= k) + return mid; + if (cnt < k) + l = mid + 1; + else + r = mid; + } + return mid; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + int kthSmallest(vector> &matrix, int k) + { + int n = matrix.size(); + priority_queue q; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + q.push(matrix[i][j]); + if (q.size() > k) + q.pop(); + } + } + return q.top(); + } +}; +``` -- GitLab