From 63fbbcb52112287ef5fd44677690905e8dc88551 Mon Sep 17 00:00:00 2001 From: Yelingyuan Date: Thu, 18 Sep 2025 20:30:52 +0800 Subject: [PATCH] 20250918 --- ..._\350\251\271\345\271\277\350\266\205.cpp" | 44 +++++++++++++ ..._\350\251\271\345\271\277\350\266\205.cpp" | 44 +++++++++++++ ..._\350\251\271\345\271\277\350\266\205.cpp" | 44 +++++++++++++ ..._\350\251\271\345\271\277\350\266\205.cpp" | 40 ++++++++++++ ..._\350\251\271\345\271\277\350\266\205.cpp" | 65 +++++++++++++++++++ 5 files changed, 237 insertions(+) create mode 100644 "topic01/submit/LC1944_\350\251\271\345\271\277\350\266\205.cpp" create mode 100644 "topic01/submit/LC1996_\350\251\271\345\271\277\350\266\205.cpp" create mode 100644 "topic01/submit/LC42_\350\251\271\345\271\277\350\266\205.cpp" create mode 100644 "topic01/submit/LC739_\350\251\271\345\271\277\350\266\205.cpp" create mode 100644 "topic01/submit/LC84_\350\251\271\345\271\277\350\266\205.cpp" diff --git "a/topic01/submit/LC1944_\350\251\271\345\271\277\350\266\205.cpp" "b/topic01/submit/LC1944_\350\251\271\345\271\277\350\266\205.cpp" new file mode 100644 index 0000000..26f6a4b --- /dev/null +++ "b/topic01/submit/LC1944_\350\251\271\345\271\277\350\266\205.cpp" @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; + +//【题目】1944.队列中可以看到的人数 +//【难度】困难 +//【提交】https://leetcode.cn/problems/number-of-visible-people-in-a-queue/submissions/663034218/ +//【标签】栈;数组;单调栈;第57场双周赛 +class Solution { +public: + vector canSeePersonsCount(vector& heights) { + int n = heights.size(); + stacks; + vectorres(n, 0); + + for (int i = n - 1; i >= 0; i--) + { + int h = heights[i]; + while (!s.empty() && s.top() < h) + { + s.pop(); + res[i] += 1; + } + if (!s.empty()) + { + res[i] += 1; + } + s.push(h); + } + return res; + } +}; + +/* +学习总结: +使用单调栈(递减栈)从右向左处理。 +对于当前元素,弹出所有比它矮的栈中元素(这些矮的人会被当前人挡住,但弹出时计数+1,因为当前人能看到他们),直到遇到比当前高的元素(计数再+1,因为当前人也能看到这个更高的人)。 +初始化栈和结果数组(初始为0)。 +从右向左遍历数组: +弹出栈中所有比当前元素小的元素(每弹出一个,计数加1,因为这些矮的人可见但会被挡住后续)。 +如果栈非空(说明有一个更高的人挡住了更后面的人),计数再加1(当前人能看到这个更高的人)。 +将当前元素入栈(维护递减性)。 +*/ \ No newline at end of file diff --git "a/topic01/submit/LC1996_\350\251\271\345\271\277\350\266\205.cpp" "b/topic01/submit/LC1996_\350\251\271\345\271\277\350\266\205.cpp" new file mode 100644 index 0000000..b77741b --- /dev/null +++ "b/topic01/submit/LC1996_\350\251\271\345\271\277\350\266\205.cpp" @@ -0,0 +1,44 @@ +#include +#include +#include +#include +using namespace std; + +//【题目】力扣1996.游戏中弱角色的数量 +//【难度】中等 +//【提交】https://leetcode.cn/problems/the-number-of-weak-characters-in-the-game/submissions/663692307/ +//【标签】栈;贪心;数组;排序;单调栈 +int cmp(vector& a, vector& b) +{ + return a[0] == b[0] ? (a[1] > b[1]) : (a[0] < b[0]); +} +class Solution +{ +public: + int numberOfWeakCharacters(vector>& properties) + { + sort(properties.begin(), properties.end(), cmp); + stacks; + int res = 0; + for (int i = 0; i < properties.size(); i++) + { + while (!s.empty() && s.top() < properties[i][1]) + { + res++; + s.pop(); + } + s.push(properties[i][1]); + } + return res; + } +}; + +/* +学习总结: +先按攻击力升序排序(攻击力相同时按防御力降序),然后遍历角色 +维护一个单调递减栈(存储防御力),这样能高效判断是否存在“严格更强”的角色。 +排序处理:通过自定义排序将角色按攻击力分组,攻击力相同的角色中,防御力高的排在前面(避免同攻击力角色误判为弱角色)。 +单调栈:从前往后遍历,栈中存储防御力值。 +若当前角色防御力大于栈顶,说明栈顶角色是弱角色(因为当前攻击力更高或相等,但防御力更高),弹出并计数,直到栈保持递减。 +这样确保栈内元素防御力递减,同时攻击力递增(因排序保证)。 +*/ \ No newline at end of file diff --git "a/topic01/submit/LC42_\350\251\271\345\271\277\350\266\205.cpp" "b/topic01/submit/LC42_\350\251\271\345\271\277\350\266\205.cpp" new file mode 100644 index 0000000..eee1445 --- /dev/null +++ "b/topic01/submit/LC42_\350\251\271\345\271\277\350\266\205.cpp" @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; + +//【题目】力扣.42接雨水 +//【难度】困难 +//【提交】https://leetcode.cn/problems/trapping-rain-water/submissions/660740768/ +//【标签】栈;数组;双指针;动态规划;单调栈 + +class Solution +{ +public: + int trap(vector& height) + { + int res = 0; + int left = 0, right = height.size() - 1; + int leftMax = 0, rightMax = 0; + while (left < right) + { + leftMax = max(leftMax, height[left]); + rightMax = max(rightMax, height[right]); + if (height[left] < height[right]) + { + res += leftMax - height[left]; + ++left; + } + else + { + res += rightMax - height[right]; + --right; + } + } + return res; + } +}; + +/* +学习总结: +核心思路是通过左右指针向中间遍历,动态维护左右两侧的最大高度(leftMax 和 rightMax)并根据当前左右指针指向的柱子高度决定计算哪一侧的雨水容量。 +具体来说: +如果 height[left] < height[right],说明左侧当前柱子的雨水容量由 leftMax 决定(因为右侧有更高的柱子保证雨水不会溢出),计算并累加雨水后左指针右移。 +否则,右侧当前柱子的雨水容量由 rightMax 决定,计算后右指针左移。 +*/ \ No newline at end of file diff --git "a/topic01/submit/LC739_\350\251\271\345\271\277\350\266\205.cpp" "b/topic01/submit/LC739_\350\251\271\345\271\277\350\266\205.cpp" new file mode 100644 index 0000000..6baa620 --- /dev/null +++ "b/topic01/submit/LC739_\350\251\271\345\271\277\350\266\205.cpp" @@ -0,0 +1,40 @@ +锘#include +#include +#include +using namespace std; + +//銆愰鐩戝姏鎵739.姣忔棩娓╁害 +//銆愰毦搴︺戜腑绛 +//銆愭彁浜ゃ慼ttps://leetcode.cn/problems/daily-temperatures/submissions/663423549/ +//銆愭爣绛俱戞爤锛涙暟缁勶紱鍗曡皟鏍 +class Solution +{ +public: + vector dailyTemperatures(vector& temperatures) + { + int n = temperatures.size(); + stacks; + vectorres(n, 0); + + for (int i = 0; i < n; i++) + { + while (!s.empty() && temperatures[s.top()] < temperatures[i]) + { + int index = s.top(); + res[index] = i - index; + s.pop(); + } + + s.push(i); + } + return res; + } +}; + +/* +瀛︿範鎬荤粨锛 +閫氳繃缁存姢涓涓崟璋冮掑噺鏍堬紙瀛樺偍绱㈠紩锛夛紝褰撻亣鍒版瘮鏍堥《娓╁害楂樼殑澶╂暟鏃讹紝寮瑰嚭鏍堥《骞惰绠楀ぉ鏁板樊锛屽瓨鍏ョ粨鏋滄暟缁勩 +鍓╀綑鏈脊鍑虹殑鍏冪礌瀵瑰簲娌℃湁鏇撮珮娓╁害鐨勬棩瀛愶紝缁撴灉鑷姩涓0锛堝垵濮嬪寲宸插鐞嗭級銆 +鍒╃敤鍗曡皟鏍堬紙閫掑噺锛夐珮鏁堟壘鍒颁笅涓涓洿澶у厓绱犵殑浣嶇疆锛岄伩鍏嶆毚鍔汷(n虏)鎼滅储銆 +鏍堜腑瀛樺偍绱㈠紩鑰岄潪鍊硷紝渚夸簬璁$畻澶╂暟宸紱閬嶅巻鏃舵瘮杈冨綋鍓嶆俯搴︿笌鏍堥《娓╁害锛岃Е鍙戝脊鍑哄拰缁撴灉鏇存柊 +*/ \ No newline at end of file diff --git "a/topic01/submit/LC84_\350\251\271\345\271\277\350\266\205.cpp" "b/topic01/submit/LC84_\350\251\271\345\271\277\350\266\205.cpp" new file mode 100644 index 0000000..b733a84 --- /dev/null +++ "b/topic01/submit/LC84_\350\251\271\345\271\277\350\266\205.cpp" @@ -0,0 +1,65 @@ +#include +#include +#include +#include +using namespace std; + + +//暴力枚举 O(n^2) 超时 +//class Solution +//{ +//public: +// int largestRectangleArea(vector& heights) +// { +// int n = heights.size(); +// int res = 0; +// for (int i = 0; i < n; i++) +// { +// int min_height = INT_MAX; +// for (int j = i; j < n; j++) +// { +// min_height = min(min_height, heights[j]); +// int s = min_height * (j - i + 1); +// res = max(res, s); +// } +// } +// return res; +// } +//}; + +//【题目】力扣84.柱状图中最大的矩形 +//【难度】困难 +//【提交】https://leetcode.cn/problems/largest-rectangle-in-histogram/submissions/663978174/ +//【标签】栈;数组;单调栈 +class Solution +{ +public: + int largestRectangleArea(vector& heights) + { + int n = heights.size(); + stacks; + int res = 0; + + for (int i = 0; i <= n; i++) + { + int h = (i == n) ? 0 : heights[i]; + while (!s.empty() && h < heights[s.top()]) + { + int height = heights[s.top()]; + s.pop(); + int left = s.empty() ? -1 : s.top(); + int width = i - left - 1; + res = max(res, height * width); + } + s.push(i); + } + return res; + } +}; + +/* +学习总结: +通过维护一个单调递增栈(存储索引),在遇到递减元素时触发计算 +以当前高度为矩形高度,利用栈中前一个索引确定左边界,当前索引确定右边界,从而快速计算矩形面积。 +算法时间复杂度为 O(n),空间复杂度为 O(n)。 +*/ \ No newline at end of file -- Gitee