diff --git "a/topic01/submit/LC0042_\351\231\210\346\230\237\350\260\225.cpp" "b/topic01/submit/LC0042_\351\231\210\346\230\237\350\260\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0647d3eee47a9f559079c9f3cbaec6f53baaff3f --- /dev/null +++ "b/topic01/submit/LC0042_\351\231\210\346\230\237\350\260\225.cpp" @@ -0,0 +1,27 @@ +class Solution { +public: + int trap(vector& height) { + int n = height.size(); + if (n == 0) return 0; + + vector left_max(n); + vector right_max(n); + + left_max[0] = height[0]; + for (int i = 1; i < n; i++) { + left_max[i] = max(left_max[i - 1], height[i]); + } + + right_max[n - 1] = height[n - 1]; + for (int i = n - 2; i >= 0; i--) { + right_max[i] = max(right_max[i + 1], height[i]); + } + + int ans = 0; + for (int i = 0; i < n; i++) { + ans += min(left_max[i], right_max[i]) - height[i]; + } + + return ans; + } +}; \ No newline at end of file diff --git "a/topic01/submit/LC0496_\351\231\210\346\230\237\350\260\225.cpp" "b/topic01/submit/LC0496_\351\231\210\346\230\237\350\260\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0d6c84e32b249d962bb07fc877aa90ff828a55f7 --- /dev/null +++ "b/topic01/submit/LC0496_\351\231\210\346\230\237\350\260\225.cpp" @@ -0,0 +1,26 @@ +class Solution { +public: + vector nextGreaterElement(vector& nums1, vector& nums2) { + unordered_map nextGreater; + stack st; + + for (int num : nums2) { + while (!st.empty() && st.top() < num) { + nextGreater[st.top()] = num; + st.pop(); + } + st.push(num); + } + + while (!st.empty()) { + nextGreater[st.top()] = -1; + st.pop(); + } + + vector ans; + for (int num : nums1) { + ans.push_back(nextGreater[num]); + } + return ans; + } +}; \ No newline at end of file diff --git "a/topic01/submit/LC0503_\351\231\210\346\230\237\350\260\225.cpp" "b/topic01/submit/LC0503_\351\231\210\346\230\237\350\260\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7856ac3c821eb4674e5b1275e72152788220028e --- /dev/null +++ "b/topic01/submit/LC0503_\351\231\210\346\230\237\350\260\225.cpp" @@ -0,0 +1,19 @@ +class Solution { +public: + vector nextGreaterElements(vector& nums) { + int n = nums.size(); + vector res(n, -1); + stack st; + for (int i = 0; i < 2 * n; i++) { + int idx = i % n; + while (!st.empty() && nums[st.top()] < nums[idx]) { + res[st.top()] = nums[idx]; + st.pop(); + } + if (i < n) { + st.push(i); + } + } + return res; + } +}; \ No newline at end of file diff --git "a/topic01/submit/LC0739_\351\231\210\346\230\237\350\260\225.cpp" "b/topic01/submit/LC0739_\351\231\210\346\230\237\350\260\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b532071e266d30229e6ad0f87e82e8549e4f2982 --- /dev/null +++ "b/topic01/submit/LC0739_\351\231\210\346\230\237\350\260\225.cpp" @@ -0,0 +1,17 @@ +class Solution { +public: + vector dailyTemperatures(vector& temperatures) { + int n = temperatures.size(); + vectorans(n, 0); + stackst; + for (int i = 0; i < n; i++) { + while (!st.empty() && temperatures[i] > temperatures[st.top()]) { + int idx = st.top(); + st.pop(); + ans[idx] = i - idx; + } + st.push(i); + } + return ans; + } +}; \ No newline at end of file diff --git "a/topic01/submit/LC1944_\351\231\210\346\230\237\350\260\225.cpp" "b/topic01/submit/LC1944_\351\231\210\346\230\237\350\260\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0ba4a94d5cd2a786bf84f9918fb987676a8ccffe --- /dev/null +++ "b/topic01/submit/LC1944_\351\231\210\346\230\237\350\260\225.cpp" @@ -0,0 +1,21 @@ +class Solution { +public: + vector canSeePersonsCount(vector& heights) { + int n = heights.size(); + vector ans(n, 0); + stack st; + for (int i = n - 1; i >= 0; i--) { + int count = 0; + while (!st.empty() && heights[st.top()] < heights[i]) { + count++; + st.pop(); + } + if (!st.empty()) { + count++; + } + ans[i] = count; + st.push(i); + } + return ans; + } +}; \ No newline at end of file diff --git "a/topic01/submit/LC1996_\351\231\210\346\230\237\350\260\225.cpp" "b/topic01/submit/LC1996_\351\231\210\346\230\237\350\260\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e82326e5306d1cd93639aaa1b6c621682ee8de1a --- /dev/null +++ "b/topic01/submit/LC1996_\351\231\210\346\230\237\350\260\225.cpp" @@ -0,0 +1,22 @@ +class Solution { +public: + int numberOfWeakCharacters(vector>& properties) { + sort(properties.begin(), properties.end(), [](const vector& a, const vector& b) { + if (a[0] == b[0]) { + return a[1] > b[1]; + } + return a[0] < b[0]; + }); + stackst; + int count = 0; + for (const auto prop : properties) { + int defense = prop[1]; + while (!st.empty() && st.top() < defense) { + st.pop(); + count++; + } + st.push(defense); + } + return count; + } +}; \ No newline at end of file