{"id":374,"date":"2026-03-30T17:09:09","date_gmt":"2026-03-30T09:09:09","guid":{"rendered":"https:\/\/hhh3m.top\/?p=374"},"modified":"2026-04-10T15:54:24","modified_gmt":"2026-04-10T07:54:24","slug":"leecode_exploring","status":"publish","type":"post","link":"https:\/\/hhh3m.top\/index.php\/2026\/03\/30\/leecode_exploring\/","title":{"rendered":"\u529b\u6263\u7b97\u6cd5\u63a2\u9669\u6a21\u5f0f\u5b66\u4e60\u8bb0\u5f55"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5<\/h2>\n\n\n\n<p>\u524d\u9762\u7684\u6ca1\u8bb0\u5f55\u5c31\u8df3\u8fc7\u5427<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">(\u4e00) \u5355\u8c03\u6808<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/final-prices-with-a-special-discount-in-a-shop\/\">Q1. \u5546\u54c1\u6298\u6263\u540e\u7684\u6700\u7ec8\u4ef7\u683c<\/a><\/h4>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4&nbsp;<code>prices<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>prices[i]<\/code>&nbsp;\u662f\u5546\u5e97\u91cc\u7b2c&nbsp;<code>i<\/code>&nbsp;\u4ef6\u5546\u54c1\u7684\u4ef7\u683c\u3002<\/p>\n\n\n\n<p>\u5546\u5e97\u91cc\u6b63\u5728\u8fdb\u884c\u4fc3\u9500\u6d3b\u52a8\uff0c\u5982\u679c\u4f60\u8981\u4e70\u7b2c&nbsp;<code>i<\/code>&nbsp;\u4ef6\u5546\u54c1\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u5f97\u5230\u4e0e&nbsp;<code>prices[j]<\/code>&nbsp;\u76f8\u7b49\u7684\u6298\u6263\uff0c\u5176\u4e2d&nbsp;<code>j<\/code>&nbsp;\u662f\u6ee1\u8db3&nbsp;<code>j &gt; i<\/code>&nbsp;\u4e14&nbsp;<code>prices[j] &lt;= prices[i]<\/code>&nbsp;\u7684&nbsp;<strong>\u6700\u5c0f\u4e0b\u6807<\/strong>&nbsp;\uff0c\u5982\u679c\u6ca1\u6709\u6ee1\u8db3\u6761\u4ef6\u7684&nbsp;<code>j<\/code>&nbsp;\uff0c\u4f60\u5c06\u6ca1\u6709\u4efb\u4f55\u6298\u6263\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7b2c&nbsp;<code>i<\/code>&nbsp;\u4e2a\u5143\u7d20\u662f\u6298\u6263\u540e\u4f60\u8d2d\u4e70\u5546\u54c1&nbsp;<code>i<\/code>&nbsp;\u6700\u7ec8\u9700\u8981\u652f\u4ed8\u7684\u4ef7\u683c\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>prices = [8,4,6,2,3]<br><strong>\u8f93\u51fa\uff1a<\/strong>[4,2,4,2,3]<br><strong>\u89e3\u91ca\uff1a<\/strong><br>\u5546\u54c1 0 \u7684\u4ef7\u683c\u4e3a price[0]=8 \uff0c\u4f60\u5c06\u5f97\u5230 prices[1]=4 \u7684\u6298\u6263\uff0c\u6240\u4ee5\u6700\u7ec8\u4ef7\u683c\u4e3a 8 &#8211; 4 = 4 \u3002<br>\u5546\u54c1 1 \u7684\u4ef7\u683c\u4e3a price[1]=4 \uff0c\u4f60\u5c06\u5f97\u5230 prices[3]=2 \u7684\u6298\u6263\uff0c\u6240\u4ee5\u6700\u7ec8\u4ef7\u683c\u4e3a 4 &#8211; 2 = 2 \u3002<br>\u5546\u54c1 2 \u7684\u4ef7\u683c\u4e3a price[2]=6 \uff0c\u4f60\u5c06\u5f97\u5230 prices[3]=2 \u7684\u6298\u6263\uff0c\u6240\u4ee5\u6700\u7ec8\u4ef7\u683c\u4e3a 6 &#8211; 2 = 4 \u3002<br>\u5546\u54c1 3 \u548c 4 \u90fd\u6ca1\u6709\u6298\u6263<br><\/p>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>prices = [1,2,3,4,5]\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,2,3,4,5]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u5546\u54c1\u90fd\u6ca1\u6709\u6298\u6263\u3002\n<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>prices = [10,1,1,6]\n<strong>\u8f93\u51fa\uff1a<\/strong>[9,0,1,6]\n<\/p>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= prices.length &lt;= 500<\/code><\/li>\n\n\n\n<li><code>1 &lt;= prices[i] &lt;= 10^3<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u89e3\u6790<\/strong>\uff1a\u7b2c\u4e00\u773c\u60f3\u5230\u53cc\u91cd\u5faa\u73af\u66b4\u529b\u89e3\uff0c\u4e0d\u8fc7\u8fd9\u91cc\u9700\u8981\u7528\u5230\u5355\u8c03\u6808\u3002\u5148\u904d\u5386\uff0c\u6808\u91cc\u5b58\u201c\u5f85\u6253\u6298\u201d\u7684\u5546\u54c1\u5e8f\u53f7\uff0c\u7136\u540e\u662f\u76f8\u5173\u64cd\u4f5c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int&#91;] finalPrices(int&#91;] prices) {\n        \n        Deque&lt;Integer&gt; queue = new ArrayDeque&lt;&gt;();\/\/\u8bb0\u5f55\u5f85\u6253\u6298\u6216\u65e0\u6298\u6263\u5546\u54c1\u5e8f\u53f7\n        for(int i = 0;i&lt;prices.length;i++){\n            while(queue.size()&gt;0 &amp;&amp; prices&#91;i]&lt;=prices&#91;queue.getLast()]){\n                prices&#91;queue.getLast()] -= prices&#91;i];\/\/\u539f\u5730\u64cd\u4f5c\u66f4\u65b0\u6253\u6298\u540e\u7684\u503c\n                queue.pollLast();\/\/\u5f39\u51fa\u6808\u9876\u5546\u54c1\u5e8f\u53f7\n            }\n            queue.addLast(i);\/\/\u5f53\u524d\u5546\u54c1\u5e8f\u53f7\u5165\u6808\n        }\n        return prices;\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/daily-temperatures\/\">Q2. \u6bcf\u65e5\u6e29\u5ea6<\/a><\/h4>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>temperatures<\/code>&nbsp;\uff0c\u8868\u793a\u6bcf\u5929\u7684\u6e29\u5ea6\uff0c\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4&nbsp;<code>answer<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>answer[i]<\/code>&nbsp;\u662f\u6307\u5bf9\u4e8e\u7b2c&nbsp;<code>i<\/code>&nbsp;\u5929\uff0c\u4e0b\u4e00\u4e2a\u66f4\u9ad8\u6e29\u5ea6\u51fa\u73b0\u5728\u51e0\u5929\u540e\u3002\u5982\u679c\u6c14\u6e29\u5728\u8fd9\u4e4b\u540e\u90fd\u4e0d\u4f1a\u5347\u9ad8\uff0c\u8bf7\u5728\u8be5\u4f4d\u7f6e\u7528&nbsp;<code>0<\/code>&nbsp;\u6765\u4ee3\u66ff\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165:<\/strong> temperatures = [73,74,75,71,69,72,76,73]\n<strong>\u8f93\u51fa:<\/strong>&nbsp;[1,1,4,2,1,1,0,0]\n<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165:<\/strong> temperatures = [30,40,50,60]\n<strong>\u8f93\u51fa:<\/strong>&nbsp;[1,1,1,0]\n<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165:<\/strong> temperatures = [30,60,90]\n<strong>\u8f93\u51fa: <\/strong>[1,1,0]<\/p>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;=&nbsp;temperatures.length &lt;= 10<sup>5<\/sup><\/code><\/li>\n\n\n\n<li><code>30 &lt;=&nbsp;temperatures[i]&nbsp;&lt;= 100<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u6790<\/strong>\uff1a\u7c7b\u4f3c\u4e0a\u4e00\u9898\u64cd\u4f5c\uff0c\u8fd8\u662f\u5355\u8c03\u6808\u5b58\u5f85\u8ba1\u7b97\u7684\u5929\u6570\uff0cwhile\u5faa\u73af\u64cd\u4f5c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int&#91;] dailyTemperatures(int&#91;] temperatures) {\n        int n=temperatures.length;\n        int&#91;] answer=new int&#91;n];\n        Deque&lt;Integer&gt; stack=new ArrayDeque();\n        for(int i=0;i&lt;n;i++){\n            while(!stack.isEmpty()&amp;&amp;temperatures&#91;i]&gt;temperatures&#91;stack.peek()]){\n                answer&#91;stack.peek()]=i-stack.peek();\n                stack.pop();\n            }\n            stack.push(i);\n        }\n        return answer;\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/largest-rectangle-in-histogram\/\">Q3. \u67f1\u72b6\u56fe\u4e2d\u6700\u5927\u7684\u77e9\u5f62<\/a><\/h4>\n\n\n\n<p>\u7ed9\u5b9a&nbsp;<em>n<\/em>&nbsp;\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u7528\u6765\u8868\u793a\u67f1\u72b6\u56fe\u4e2d\u5404\u4e2a\u67f1\u5b50\u7684\u9ad8\u5ea6\u3002\u6bcf\u4e2a\u67f1\u5b50\u5f7c\u6b64\u76f8\u90bb\uff0c\u4e14\u5bbd\u5ea6\u4e3a 1 \u3002<\/p>\n\n\n\n<p>\u6c42\u5728\u8be5\u67f1\u72b6\u56fe\u4e2d\uff0c\u80fd\u591f\u52fe\u52d2\u51fa\u6765\u7684\u77e9\u5f62\u7684\u6700\u5927\u9762\u79ef\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/assets.leetcode.com\/uploads\/2021\/01\/04\/histogram.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/assets.leetcode.com\/uploads\/2021\/01\/04\/histogram.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>heights = [2,1,5,6,2,3]\n<strong>\u8f93\u51fa\uff1a<\/strong>10\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6700\u5927\u7684\u77e9\u5f62\u4e3a\u56fe\u4e2d\u7ea2\u8272\u533a\u57df\uff0c\u9762\u79ef\u4e3a 10\n<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/assets.leetcode.com\/uploads\/2021\/01\/04\/histogram-1.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/assets.leetcode.com\/uploads\/2021\/01\/04\/histogram-1.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong> heights = [2,4]\n<strong>\u8f93\u51fa\uff1a<\/strong> 4<\/p>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= heights.length &lt;=10<sup>5<\/sup><\/code><\/li>\n\n\n\n<li><code>0 &lt;= heights[i] &lt;= 10<sup>4<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u6790<\/strong>\uff1a\u57fa\u672c\u601d\u8def\u5c31\u662f\u627e\u4e00\u4e2a\u6570\u5b57\u53f3\u8fb9\u51fa\u73b0\u7684\u7b2c\u4e00\u4e2a\u6bd4\u4ed6\u5c0f\u7684\u6570\u5b57\u6216\u8005\u76f4\u5230\u6570\u7ec4\u8fb9\u754c\uff0c\u6784\u6210\u77e9\u5f62\u7136\u540e\u8ba1\u7b97\u5bbd\u4e58\u4ee5\u9ad8\uff0c\u96be\u70b9\u5c31\u662f\u8ba1\u7b97\u9762\u79ef\u6d89\u53ca\u4e00\u4e9b\u8fb9\u754c\u7684\u5224\u65ad\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int largestRectangleArea(int&#91;] heights) {\n        Deque&lt;Integer&gt; stack = new ArrayDeque&lt;&gt;();\n        int n = heights.length;\n        int result = 0;\n        \n        for (int i = 0; i &lt;= n; i++) {\n            \/\/ \u5f53\u524d\u9ad8\u5ea6\uff0c\u904d\u5386\u5230n\u65f6\u9ad8\u5ea6\u4e3a0\uff0c\u786e\u4fdd\u6808\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u88ab\u5f39\u51fa\u8ba1\u7b97\n            int h = (i == n) ? 0 : heights&#91;i];\/*\u8fd9\u91cci=n\u7684\u601d\u8def\u505a\u9898\u65f6\u4e00\u76f4\u6ca1\u60f3\u5230\uff0c\u770b\u4e86\u89e3\u6790\u624d\u610f\u8bc6\u5230\u4f5c\u7528\uff0c\u5148\u8bb0\u4f4f\u5427*\/\n            while (!stack.isEmpty() &amp;&amp; h &lt; heights&#91;stack.peek()]) {\n                int height = heights&#91;stack.pop()];\n                int left = stack.isEmpty() ? 0 : stack.peek() + 1;\/\/\u5de6\u8fb9\u754c\n                int width = i - left;\n                result = Math.max(result, height * width);\n            }\n            stack.push(i);\n        }\n        return result;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">(\u4e8c) \u961f\u5217<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/number-of-students-unable-to-eat-lunch\/\">Q1. \u65e0\u6cd5\u5403\u5348\u9910\u7684\u5b66\u751f\u6570\u91cf<\/a><\/h4>\n\n\n\n<p>\u5b66\u6821\u7684\u81ea\u52a9\u5348\u9910\u63d0\u4f9b\u5706\u5f62\u548c\u65b9\u5f62\u7684\u4e09\u660e\u6cbb\uff0c\u5206\u522b\u7528\u6570\u5b57&nbsp;<code>0<\/code>&nbsp;\u548c&nbsp;<code>1<\/code>&nbsp;\u8868\u793a\u3002\u6240\u6709\u5b66\u751f\u7ad9\u5728\u4e00\u4e2a\u961f\u5217\u91cc\uff0c\u6bcf\u4e2a\u5b66\u751f\u8981\u4e48\u559c\u6b22\u5706\u5f62\u7684\u8981\u4e48\u559c\u6b22\u65b9\u5f62\u7684\u3002<br>\u9910\u5385\u91cc\u4e09\u660e\u6cbb\u7684\u6570\u91cf\u4e0e\u5b66\u751f\u7684\u6570\u91cf\u76f8\u540c\u3002\u6240\u6709\u4e09\u660e\u6cbb\u90fd\u653e\u5728\u4e00\u4e2a&nbsp;<strong>\u6808<\/strong>&nbsp;\u91cc\uff0c\u6bcf\u4e00\u8f6e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u961f\u5217\u6700\u524d\u9762\u7684\u5b66\u751f&nbsp;<strong>\u559c\u6b22<\/strong>&nbsp;\u6808\u9876\u7684\u4e09\u660e\u6cbb\uff0c\u90a3\u4e48\u4f1a&nbsp;<strong>\u62ff\u8d70\u5b83<\/strong>&nbsp;\u5e76\u79bb\u5f00\u961f\u5217\u3002<\/li>\n\n\n\n<li>\u5426\u5219\uff0c\u8fd9\u540d\u5b66\u751f\u4f1a&nbsp;<strong>\u653e\u5f03\u8fd9\u4e2a\u4e09\u660e\u6cbb<\/strong>&nbsp;\u5e76\u56de\u5230\u961f\u5217\u7684\u5c3e\u90e8\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u4e00\u76f4\u6301\u7eed\u5230\u961f\u5217\u91cc\u6240\u6709\u5b66\u751f\u90fd\u4e0d\u559c\u6b22\u6808\u9876\u7684\u4e09\u660e\u6cbb\u4e3a\u6b62\u3002<\/p>\n\n\n\n<p>\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>students<\/code>&nbsp;\u548c&nbsp;<code>sandwiches<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>sandwiches[i]<\/code>&nbsp;\u662f\u6808\u91cc\u9762\u7b2c&nbsp;<code>i<sup>\u200b\u200b\u200b\u200b\u200b\u200b<\/sup><\/code>&nbsp;\u4e2a\u4e09\u660e\u6cbb\u7684\u7c7b\u578b\uff08<code>i = 0<\/code>&nbsp;\u662f\u6808\u7684\u9876\u90e8\uff09\uff0c&nbsp;<code>students[j]<\/code>&nbsp;\u662f\u521d\u59cb\u961f\u5217\u91cc\u7b2c&nbsp;<code>j<sup>\u200b\u200b\u200b\u200b\u200b\u200b<\/sup><\/code>&nbsp;\u540d\u5b66\u751f\u5bf9\u4e09\u660e\u6cbb\u7684\u559c\u597d\uff08<code>j = 0<\/code>&nbsp;\u662f\u961f\u5217\u7684\u6700\u5f00\u59cb\u4f4d\u7f6e\uff09\u3002\u8bf7\u4f60\u8fd4\u56de\u65e0\u6cd5\u5403\u5348\u9910\u7684\u5b66\u751f\u6570\u91cf\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>students = [1,1,0,0], sandwiches = [0,1,0,1]<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>0<strong> <\/strong><\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<p>&#8211; \u6700\u524d\u9762\u7684\u5b66\u751f\u653e\u5f03\u6700\u9876\u4e0a\u7684\u4e09\u660e\u6cbb\uff0c\u5e76\u56de\u5230\u961f\u5217\u7684\u672b\u5c3e\uff0c\u5b66\u751f\u961f\u5217\u53d8\u4e3a students = [1,0,0,1]\u3002<br>&#8211; \u6700\u524d\u9762\u7684\u5b66\u751f\u653e\u5f03\u6700\u9876\u4e0a\u7684\u4e09\u660e\u6cbb\uff0c\u5e76\u56de\u5230\u961f\u5217\u7684\u672b\u5c3e\uff0c\u5b66\u751f\u961f\u5217\u53d8\u4e3a students = [0,0,1,1]\u3002<br>&#8211; \u6700\u524d\u9762\u7684\u5b66\u751f\u62ff\u8d70\u6700\u9876\u4e0a\u7684\u4e09\u660e\u6cbb\uff0c\u5269\u4f59\u5b66\u751f\u961f\u5217\u4e3a students = [0,1,1]\uff0c\u4e09\u660e\u6cbb\u6808\u4e3a sandwiches = [1,0,1]\u3002<br>&#8211; \u6700\u524d\u9762\u7684\u5b66\u751f\u653e\u5f03\u6700\u9876\u4e0a\u7684\u4e09\u660e\u6cbb\uff0c\u5e76\u56de\u5230\u961f\u5217\u7684\u672b\u5c3e\uff0c\u5b66\u751f\u961f\u5217\u53d8\u4e3a students = [1,1,0]\u3002<br>&#8211; \u6700\u524d\u9762\u7684\u5b66\u751f\u62ff\u8d70\u6700\u9876\u4e0a\u7684\u4e09\u660e\u6cbb\uff0c\u5269\u4f59\u5b66\u751f\u961f\u5217\u4e3a students = [1,0]\uff0c\u4e09\u660e\u6cbb\u6808\u4e3a sandwiches = [0,1]\u3002<br>&#8211; \u6700\u524d\u9762\u7684\u5b66\u751f\u653e\u5f03\u6700\u9876\u4e0a\u7684\u4e09\u660e\u6cbb\uff0c\u5e76\u56de\u5230\u961f\u5217\u7684\u672b\u5c3e\uff0c\u5b66\u751f\u961f\u5217\u53d8\u4e3a students = [0,1]\u3002<br>&#8211; \u6700\u524d\u9762\u7684\u5b66\u751f\u62ff\u8d70\u6700\u9876\u4e0a\u7684\u4e09\u660e\u6cbb\uff0c\u5269\u4f59\u5b66\u751f\u961f\u5217\u4e3a students = [1]\uff0c\u4e09\u660e\u6cbb\u6808\u4e3a sandwiches = [1]\u3002<br>&#8211; \u6700\u524d\u9762\u7684\u5b66\u751f\u62ff\u8d70\u6700\u9876\u4e0a\u7684\u4e09\u660e\u6cbb\uff0c\u5269\u4f59\u5b66\u751f\u961f\u5217\u4e3a students = []\uff0c\u4e09\u660e\u6cbb\u6808\u4e3a sandwiches = []\u3002<br>\u6240\u4ee5\u6240\u6709\u5b66\u751f\u90fd\u6709\u4e09\u660e\u6cbb\u5403\u3002<br><\/p>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]\n<strong>\u8f93\u51fa\uff1a<\/strong>3\n<\/p>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= students.length, sandwiches.length &lt;= 100<\/code><\/li>\n\n\n\n<li><code>students.length == sandwiches.length<\/code><\/li>\n\n\n\n<li><code>sandwiches[i]<\/code>&nbsp;\u8981\u4e48\u662f&nbsp;<code>0<\/code>&nbsp;\uff0c\u8981\u4e48\u662f&nbsp;<code>1<\/code>&nbsp;\u3002<\/li>\n\n\n\n<li><code>students[i]<\/code>&nbsp;\u8981\u4e48\u662f&nbsp;<code>0<\/code>&nbsp;\uff0c\u8981\u4e48\u662f&nbsp;<code>1<\/code>&nbsp;\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u6790<\/strong>\uff1a\u7b2c\u4e00\u60f3\u6cd5\u5c31\u662fnew\u4e00\u4e2a\u7279\u522b\u957f\u7684\u6570\u7ec4\uff0c\u7ed9students\u6570\u7ec4\u586b\u8fdb\u53bb\uff0c\u7136\u540e\u7528\u5934\u7d22\u5f15\u548c\u5c3e\u7d22\u5f15\u6765\u64cd\u4f5c\u66b4\u529b\u6a21\u62df\u961f\u5217\uff0c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int countStudents(int&#91;] students, int&#91;] sandwiches) {\n        int j=0;\n        int n=students.length;\n        int nn=students.length;\/\/\u8d85\u7edd\u968f\u610f\u547d\u540d\n        int&#91;] a=new int&#91;10000];\n        for(int i=0;i&lt;n;i++){\n            a&#91;i]=students&#91;i];\n        }\n        int i=0;\n        \/\/\u6dfb\u52a0\u4e00\u4e2a\u8ba1\u6570\u5668\uff0c\u8bb0\u5f55\u5f53\u524d\u8f6e\u6b21\u68c0\u67e5\u4e86\u591a\u5c11\u5b66\u751f\n        int checkedCount = 0;\n        \n        while(j != sandwiches.length){\n            if(i &gt;= nn&amp;&amp;checkedCount == (n - i)){\n                \/\/ \u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u904d\u5386\u4e86\u6240\u6709\u5b66\u751f\n                break;\n            }\n            if(a&#91;i]==sandwiches&#91;j]){\n                j++;\n                i++;\n                checkedCount = 0;  \/\/ \u91cd\u7f6e\u8ba1\u6570\u5668\n            }else{\n                a&#91;n++]=a&#91;i];\n                i++;\n                checkedCount++;  \/\/ \u8ba1\u6570\u5668\u589e\u52a0\n            }\n        } \n        int result;\n        return result=n-i;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u800c\u770b\u5b98\u89e3\uff0c\u672c\u9898\u53ea\u9700\u8981\u8fd4\u56de\u65e0\u6cd5\u5403\u5230\u4e09\u660e\u6cbb\u7684\u4eba\u6570\uff0c\u4e09\u660e\u6cbb\u987a\u5e8f\u56fa\u5b9a\u800c\u5b66\u751f\u53ef\u4ee5\u65e0\u9650\u6392\u961f\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u628a\u6240\u6709\u5b66\u751f\u4e2d\u559c\u6b221\u62160\u7684\u603b\u6570\uff0c\u548c\u5f53\u524d\u4e09\u660e\u6cbb\u7684\u79cd\u7c7b\u6765\u5224\u65ad\u5c31\u597d\u4e86\u3002\uff08\u5509\u5509\u6211\u600e\u4e48\u5c31\u6ca1\u60f3\u5230\u5462\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int countStudents(int&#91;] students, int&#91;] sandwiches) {\n        int s1 = Arrays.stream(students).sum();\/\/\u8ba1\u7b97students\u7684\u548c\uff0c\u76f8\u5f53\u4e8e\u559c\u6b221\u7684\u4eba\u6570\n        int s0 = students.length - s1;\/\/\u559c\u6b220\u7684\u4eba\u6570\n        for (int i = 0; i &lt; sandwiches.length; i++) {\n            if (sandwiches&#91;i] == 0 &amp;&amp; s0 &gt; 0) {\n                s0--;\n            } else if (sandwiches&#91;i] == 1 &amp;&amp; s1 &gt; 0) {\n                s1--;\n            } else {\n                break;\/\/\u90fd\u4e0d\u559c\u6b22\u5c31\u7ed3\u675f\n            }\n        }\n        return s0 + s1;\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/time-needed-to-buy-tickets\/\">Q2. \u4e70\u7968\u9700\u8981\u7684\u65f6\u95f4<\/a><\/h4>\n\n\n\n<p>\u6709&nbsp;<code>n<\/code>&nbsp;\u4e2a\u4eba\u524d\u6765\u6392\u961f\u4e70\u7968\uff0c\u5176\u4e2d\u7b2c&nbsp;<code>0<\/code>&nbsp;\u4eba\u7ad9\u5728\u961f\u4f0d&nbsp;<strong>\u6700\u524d\u65b9<\/strong>&nbsp;\uff0c\u7b2c&nbsp;<code>(n - 1)<\/code>&nbsp;\u4eba\u7ad9\u5728\u961f\u4f0d&nbsp;<strong>\u6700\u540e\u65b9<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u4e0b\u6807\u4ece&nbsp;<strong>0<\/strong>&nbsp;\u5f00\u59cb\u7684\u6574\u6570\u6570\u7ec4&nbsp;<code>tickets<\/code>&nbsp;\uff0c\u6570\u7ec4\u957f\u5ea6\u4e3a&nbsp;<code>n<\/code>&nbsp;\uff0c\u5176\u4e2d\u7b2c&nbsp;<code>i<\/code>&nbsp;\u4eba\u60f3\u8981\u8d2d\u4e70\u7684\u7968\u6570\u4e3a&nbsp;<code>tickets[i]<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u4eba\u4e70\u7968\u90fd\u9700\u8981\u7528\u6389&nbsp;<strong>\u6070\u597d 1 \u79d2<\/strong>&nbsp;\u3002\u4e00\u4e2a\u4eba&nbsp;<strong>\u4e00\u6b21\u53ea\u80fd\u4e70\u4e00\u5f20\u7968<\/strong>&nbsp;\uff0c\u5982\u679c\u9700\u8981\u8d2d\u4e70\u66f4\u591a\u7968\uff0c\u4ed6\u5fc5\u987b\u8d70\u5230&nbsp;&nbsp;<strong>\u961f\u5c3e<\/strong>&nbsp;\u91cd\u65b0\u6392\u961f\uff08<strong>\u77ac\u95f4&nbsp;<\/strong>\u53d1\u751f\uff0c\u4e0d\u8ba1\u65f6\u95f4\uff09\u3002\u5982\u679c\u4e00\u4e2a\u4eba\u6ca1\u6709\u5269\u4e0b\u9700\u8981\u4e70\u7684\u7968\uff0c\u90a3\u4ed6\u5c06\u4f1a&nbsp;<strong>\u79bb\u5f00<\/strong>&nbsp;\u961f\u4f0d\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u4f4d\u4e8e\u4f4d\u7f6e&nbsp;<code>k<\/code>\uff08\u4e0b\u6807\u4ece&nbsp;<strong>0<\/strong>&nbsp;\u5f00\u59cb\uff09\u7684\u4eba\u5b8c\u6210\u4e70\u7968\u9700\u8981\u7684\u65f6\u95f4\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>tickets = [2,3,2], k = 2<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>6<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u961f\u4f0d\u4e00\u5f00\u59cb\u4e3a [2,3,2]\uff0c\u7b2c k \u4e2a\u4eba\u4ee5\u4e0b\u5212\u7ebf\u6807\u8bc6\u3002<\/li>\n\n\n\n<li>\u5728\u6700\u524d\u9762\u7684\u4eba\u4e70\u5b8c\u7968\u540e\uff0c\u961f\u4f0d\u5728\u7b2c 1 \u79d2\u53d8\u6210 [3,2,1]\u3002<\/li>\n\n\n\n<li>\u7ee7\u7eed\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u961f\u4f0d\u5728\u7b2c 2 \u79d2\u53d8\u4e3a[2,1,2]\u3002<\/li>\n\n\n\n<li>\u7ee7\u7eed\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u961f\u4f0d\u5728\u7b2c 3 \u79d2\u53d8\u4e3a[1,2,1]\u3002<\/li>\n\n\n\n<li>\u7ee7\u7eed\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u961f\u4f0d\u5728\u7b2c 4 \u79d2\u53d8\u4e3a[2,1]\u3002<\/li>\n\n\n\n<li>\u7ee7\u7eed\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u961f\u4f0d\u5728\u7b2c 5 \u79d2\u53d8\u4e3a[1,1]\u3002<\/li>\n\n\n\n<li>\u7ee7\u7eed\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u961f\u4f0d\u5728\u7b2c 6 \u79d2\u53d8\u4e3a[1]\u3002\u7b2c k \u4e2a\u4eba\u5b8c\u6210\u4e70\u7968\uff0c\u6240\u4ee5\u8fd4\u56de 6\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>tickets = [5,1,1,1], k = 0<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>8<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u961f\u4f0d\u4e00\u5f00\u59cb\u4e3a [5,1,1,1]\uff0c\u7b2c k \u4e2a\u4eba\u4ee5\u4e0b\u5212\u7ebf\u6807\u8bc6\u3002<\/li>\n\n\n\n<li>\u5728\u6700\u524d\u9762\u7684\u4eba\u4e70\u5b8c\u7968\u540e\uff0c\u961f\u4f0d\u5728\u7b2c 1 \u79d2\u53d8\u6210 [1,1,1,4]\u3002<\/li>\n\n\n\n<li>\u7ee7\u7eed\u8fd9\u4e2a\u8fc7\u7a0b 3 \u79d2\uff0c\u961f\u4f0d\u5728\u7b2c 4&nbsp;\u79d2\u53d8\u4e3a[4]\u3002<\/li>\n\n\n\n<li>\u7ee7\u7eed\u8fd9\u4e2a\u8fc7\u7a0b 4 \u79d2\uff0c\u961f\u4f0d\u5728\u7b2c 8&nbsp;\u79d2\u53d8\u4e3a[]\u3002\u7b2c k \u4e2a\u4eba\u5b8c\u6210\u4e70\u7968\uff0c\u6240\u4ee5\u8fd4\u56de 8\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>n == tickets.length<\/code><\/li>\n\n\n\n<li><code>1 &lt;= n &lt;= 100<\/code><\/li>\n\n\n\n<li><code>1 &lt;= tickets[i] &lt;= 100<\/code><\/li>\n\n\n\n<li><code>0 &lt;= k &lt; n<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u6790<\/strong>\uff1a\u7b2c\u4e00\u65b9\u6cd5\u8fd8\u662f\u548c\u4e0a\u4e00\u9898\u7c7b\u4f3c\u53ef\u4ee5\u7528\u6570\u7ec4\u6a21\u62df\u961f\u5217\u4e0d\u518d\u8d58\u8ff0\u3002\u5206\u6790\u9898\u76ee\u4e0d\u96be\u770b\u51fa\uff0c\u4e70\u7968\u8f6e\u6d41\u6765\uff0c\u76f4\u5230\u7b2ck\u4e2a\u4eba\u4e70\u5b8c\u65f6\u5df2\u7ecf\u5b8c\u6574\u7ecf\u8fc7k-1\u8f6e\u518d\u52a0\u4e0ak\u524d\u9762\u7684\u4eba\uff0c\u4e5f\u5c31\u662f\u8bf4\u6b64\u65f6k\u524d\u9762\u7684\u4eba\u5e94\u8be5\u4e5f\u90fd\u4e70\u4e86k\u6b21\uff0ck\u540e\u9762\u7684\u4eba\u4e5f\u90fd\u4e70\u4e86k-1\u6b21\uff0c\u5728\u6b64\u57fa\u7840\u4e0a\u989d\u5916\u5224\u65ad\u4e00\u4e0b\u6b32\u4e70\u7968\u6570\u6bd4tickets[k]\u5c11\u7684\u64cd\u4f5c\u4e00\u4e0b\u7136\u540e\u5168\u52a0\u8d77\u6765\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int timeRequiredToBuy(int&#91;] tickets, int k) {\n        int n = tickets.length;\n        int t = tickets&#91;k];\n        int result = 0;\n        \n        for (int i = 0; i &lt; k; i++) {\n            result += Math.min(tickets&#91;i], t);\n        }\/\/\u524d\u9762\n        \n        for (int i = k + 1; i &lt; n; i++) {\n            result += Math.min(tickets&#91;i], t - 1);\n        }\/\/\u540e\u9762\n        \n        result += t;\/\/\u81ea\u5df1\n        return result;\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/implement-queue-using-stacks\/\">Q3. \u7528\u6808\u5b9e\u73b0\u961f\u5217<\/a><\/h4>\n\n\n\n<p>\u8bf7\u4f60\u4ec5\u4f7f\u7528\u4e24\u4e2a\u6808\u5b9e\u73b0\u5148\u5165\u5148\u51fa\u961f\u5217\u3002\u961f\u5217\u5e94\u5f53\u652f\u6301\u4e00\u822c\u961f\u5217\u652f\u6301\u7684\u6240\u6709\u64cd\u4f5c\uff08<code>push<\/code>\u3001<code>pop<\/code>\u3001<code>peek<\/code>\u3001<code>empty<\/code>\uff09\uff1a<\/p>\n\n\n\n<p>\u5b9e\u73b0&nbsp;<code>MyQueue<\/code>&nbsp;\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>void push(int x)<\/code>&nbsp;\u5c06\u5143\u7d20 x \u63a8\u5230\u961f\u5217\u7684\u672b\u5c3e<\/li>\n\n\n\n<li><code>int pop()<\/code>&nbsp;\u4ece\u961f\u5217\u7684\u5f00\u5934\u79fb\u9664\u5e76\u8fd4\u56de\u5143\u7d20<\/li>\n\n\n\n<li><code>int peek()<\/code>&nbsp;\u8fd4\u56de\u961f\u5217\u5f00\u5934\u7684\u5143\u7d20<\/li>\n\n\n\n<li><code>boolean empty()<\/code>&nbsp;\u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u8fd4\u56de&nbsp;<code>true<\/code>&nbsp;\uff1b\u5426\u5219\uff0c\u8fd4\u56de&nbsp;<code>false<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f60&nbsp;<strong>\u53ea\u80fd<\/strong>&nbsp;\u4f7f\u7528\u6807\u51c6\u7684\u6808\u64cd\u4f5c \u2014\u2014 \u4e5f\u5c31\u662f\u53ea\u6709&nbsp;<code>push to top<\/code>,&nbsp;<code>peek\/pop from top<\/code>,&nbsp;<code>size<\/code>, \u548c&nbsp;<code>is empty<\/code>&nbsp;\u64cd\u4f5c\u662f\u5408\u6cd5\u7684\u3002<\/li>\n\n\n\n<li>\u4f60\u6240\u4f7f\u7528\u7684\u8bed\u8a00\u4e5f\u8bb8\u4e0d\u652f\u6301\u6808\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 list \u6216\u8005 deque\uff08\u53cc\u7aef\u961f\u5217\uff09\u6765\u6a21\u62df\u4e00\u4e2a\u6808\uff0c\u53ea\u8981\u662f\u6807\u51c6\u7684\u6808\u64cd\u4f5c\u5373\u53ef\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>\n[&#8220;MyQueue&#8221;, &#8220;push&#8221;, &#8220;push&#8221;, &#8220;peek&#8221;, &#8220;pop&#8221;, &#8220;empty&#8221;]\n[[], [1], [2], [], [], []]\n<strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<p>[null, null, null, 1, 1, false]<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong> MyQueue myQueue = new MyQueue(); <\/p>\n\n\n\n<p>myQueue.push(1); \/\/ queue is: [1] <\/p>\n\n\n\n<p>myQueue.push(2); \/\/ queue is: [1, 2] (leftmost is front of the queue) <\/p>\n\n\n\n<p>myQueue.peek(); \/\/ return 1 <\/p>\n\n\n\n<p>myQueue.pop(); \/\/return 1, queue is [2] <\/p>\n\n\n\n<p>myQueue.empty(); \/\/ return false<\/p>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= x &lt;= 9<\/code><\/li>\n\n\n\n<li>\u6700\u591a\u8c03\u7528&nbsp;<code>100<\/code>&nbsp;\u6b21&nbsp;<code>push<\/code>\u3001<code>pop<\/code>\u3001<code>peek<\/code>&nbsp;\u548c&nbsp;<code>empty<\/code><\/li>\n\n\n\n<li>\u5047\u8bbe\u6240\u6709\u64cd\u4f5c\u90fd\u662f\u6709\u6548\u7684 \uff08\u4f8b\u5982\uff0c\u4e00\u4e2a\u7a7a\u7684\u961f\u5217\u4e0d\u4f1a\u8c03\u7528&nbsp;<code>pop<\/code>&nbsp;\u6216\u8005&nbsp;<code>peek<\/code>&nbsp;\u64cd\u4f5c\uff09<\/li>\n<\/ul>\n\n\n\n<p><strong>\u8fdb\u9636\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f60\u80fd\u5426\u5b9e\u73b0\u6bcf\u4e2a\u64cd\u4f5c\u5747\u644a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(1)<\/code>&nbsp;\u7684\u961f\u5217\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u6267\u884c&nbsp;<code>n<\/code>&nbsp;\u4e2a\u64cd\u4f5c\u7684\u603b\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(n)<\/code>&nbsp;\uff0c\u5373\u4f7f\u5176\u4e2d\u4e00\u4e2a\u64cd\u4f5c\u53ef\u80fd\u82b1\u8d39\u8f83\u957f\u65f6\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u6790<\/strong>\uff1a\u5bf9\u4e8ejava\uff0c\u672c\u8eab\u5c31\u6709\u53cc\u7aef\u961f\u5217Deque\uff0c\u76f4\u63a5deque.addLast\uff1bdeque.removeFirst\u7b49\u5373\u53ef\u3002\u5982\u679c\u6309\u7167\u9898\u610f\u7528\u4e24\u4e2a\u6808\uff0c\u5c31\u9700\u8981\u4e00\u4e2ain\u6808\u653e\u63a5\u53d7\u7684\u65b0\u6570\u636e\uff0c\u4e00\u4e2aout\u6808\u653e\u5f85\u8f93\u51fa\u7684\u6570\u636e\u3002\u5b98\u89e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyQueue {\n    Deque&lt;Integer&gt; inStack;\n    Deque&lt;Integer&gt; outStack;\n\n    public MyQueue() {\n        inStack = new ArrayDeque&lt;Integer&gt;();\n        outStack = new ArrayDeque&lt;Integer&gt;();\n    }\n\n    public void push(int x) {\n        inStack.push(x);\/\/\u76f4\u63a5push\n    }\n\n    public int pop() {\n        if (outStack.isEmpty()) {\n            in2out();\/\/\u5982\u679cout\u4e3a\u7a7a\uff0c\u8c03\u7528\u628ain\u5012\u8fdbout\u7684\u65b9\u6cd5\n        }\n        return outStack.pop();\n    }\n\n    public int peek() {\n        if (outStack.isEmpty()) {\n            in2out();\n        }\n        return outStack.peek();\n    }\n\n    public boolean empty() {\n        return inStack.isEmpty() &amp;&amp; outStack.isEmpty();\n    }\n\n    private void in2out() {\n        while (!inStack.isEmpty()) {\n            outStack.push(inStack.pop());\/*\u5faa\u73af\u628ain\u6808\u5168\u5012\u8fc7\u6765\u653e\u8fdbout\u6808\uff0c\u8fd9\u6837in\u7684\u6700\u5e95\u4f4d\u5728out\u91cc\u5c31\u662f\u6700\u9ad8\u4f4d\uff0c\u5b9e\u73b0\u5148\u8fdb\u5148\u51fa*\/\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">(\u4e09) \u5806<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/last-stone-weight\/\">Q1. \u6700\u540e\u4e00\u5757\u77f3\u5934\u7684\u91cd\u91cf<\/a><\/h4>\n\n\n\n<p>\u6709\u4e00\u5806\u77f3\u5934\uff0c\u6bcf\u5757\u77f3\u5934\u7684\u91cd\u91cf\u90fd\u662f\u6b63\u6574\u6570\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e00\u56de\u5408\uff0c\u4ece\u4e2d\u9009\u51fa\u4e24\u5757<strong>&nbsp;\u6700\u91cd\u7684<\/strong>&nbsp;\u77f3\u5934\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u4e00\u8d77\u7c89\u788e\u3002\u5047\u8bbe\u77f3\u5934\u7684\u91cd\u91cf\u5206\u522b\u4e3a&nbsp;<code>x<\/code>&nbsp;\u548c&nbsp;<code>y<\/code>\uff0c\u4e14&nbsp;<code>x &lt;= y<\/code>\u3002\u90a3\u4e48\u7c89\u788e\u7684\u53ef\u80fd\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c&nbsp;<code>x == y<\/code>\uff0c\u90a3\u4e48\u4e24\u5757\u77f3\u5934\u90fd\u4f1a\u88ab\u5b8c\u5168\u7c89\u788e\uff1b<\/li>\n\n\n\n<li>\u5982\u679c&nbsp;<code>x != y<\/code>\uff0c\u90a3\u4e48\u91cd\u91cf\u4e3a&nbsp;<code>x<\/code>&nbsp;\u7684\u77f3\u5934\u5c06\u4f1a\u5b8c\u5168\u7c89\u788e\uff0c\u800c\u91cd\u91cf\u4e3a&nbsp;<code>y<\/code>&nbsp;\u7684\u77f3\u5934\u65b0\u91cd\u91cf\u4e3a&nbsp;<code>y-x<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6700\u540e\uff0c\u6700\u591a\u53ea\u4f1a\u5269\u4e0b\u4e00\u5757\u77f3\u5934\u3002\u8fd4\u56de\u6b64\u77f3\u5934\u7684\u91cd\u91cf\u3002\u5982\u679c\u6ca1\u6709\u77f3\u5934\u5269\u4e0b\uff0c\u5c31\u8fd4\u56de&nbsp;<code>0<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>[2,7,4,1,8,1]\n<strong>\u8f93\u51fa\uff1a<\/strong>1\n<strong>\u89e3\u91ca\uff1a<\/strong>\n\u5148\u9009\u51fa 7 \u548c 8\uff0c\u5f97\u5230 1\uff0c\u6240\u4ee5\u6570\u7ec4\u8f6c\u6362\u4e3a [2,4,1,1,1]\uff0c\n\u518d\u9009\u51fa 2 \u548c 4\uff0c\u5f97\u5230 2\uff0c\u6240\u4ee5\u6570\u7ec4\u8f6c\u6362\u4e3a [2,1,1,1]\uff0c\n\u63a5\u7740\u662f 2 \u548c 1\uff0c\u5f97\u5230 1\uff0c\u6240\u4ee5\u6570\u7ec4\u8f6c\u6362\u4e3a [1,1,1]\uff0c\n\u6700\u540e\u9009\u51fa 1 \u548c 1\uff0c\u5f97\u5230 0\uff0c\u6700\u7ec8\u6570\u7ec4\u8f6c\u6362\u4e3a [1]\uff0c\u8fd9\u5c31\u662f\u6700\u540e\u5269\u4e0b\u90a3\u5757\u77f3\u5934\u7684\u91cd\u91cf\u3002<\/p>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= stones.length &lt;= 30<\/code><\/li>\n\n\n\n<li><code>1 &lt;= stones[i] &lt;= 1000<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u6790<\/strong>\uff1a\u76f4\u63a5\u5b57\u9762\u610f\u601d\u53bb\u6a21\u62df\uff0c\u627e\u51fa\u6700\u5927\u503c\u548c\u7b2c\u4e8c\u5927\u503c\uff0c\u66f4\u65b0\u503c\u7136\u540e\u9012\u5f52\uff0c\u76f4\u5230\u6700\u540e\u4e00\u5757\u6216\u8005\u6ca1\u6709\u5c31\u7ed3\u675f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int lastStoneWeight(int&#91;] stones) {\n        int x = 0, y = 0;\n        int xx = 0, yy = 0;\n        int n = stones.length;\n        \/\/\u5148\u627e\u6700\u5927\u77f3\u5934 y\u4ee5\u53ca\u8bb0\u5f55y\u7684\u7d22\u5f15yy\n        y = stones&#91;0];\n        yy = 0;\n        for (int i = 1; i &lt; n; i++) {\n            if (stones&#91;i] &gt; y) {\n                y = stones&#91;i];\n                yy = i;\n            }\n        }\n        \/\/ \u518d\u627e\u7b2c\u4e8c\u5927\u77f3\u5934 x\n        \/\/ \u5148\u521d\u59cb\u5316x\u4e3a-1\u8868\u793a\u6ca1\u627e\u5230\n        x = -1;\n        xx = -1;\n        for (int i = 0; i &lt; n; i++) {\n            if (i == yy) continue; \/\/ \u8df3\u8fc7\u6700\u5927\u77f3\u5934\n            if (stones&#91;i] &gt; 0) { \/\/ \u6392\u9664\u6ca1\u77f3\u5934\u60c5\u51b5\n                if (stones&#91;i] &gt; x) {\n                    x = stones&#91;i];\n                    xx = i;\n                }\n            }\n        }\n        \/\/ \u5224\u65ad\u4e00\u4e0b\u5982\u679c\u6ca1\u627e\u5230\u7b2c\u4e8c\u5927\u77f3\u5934\uff0c\u8bf4\u660e\u53ea\u6709\u4e00\u4e2a\u77f3\u5934\u4e86\n        if (x == -1) {\n            return y; \/\/ \u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u77f3\u5934\n        }\n        \/\/ \u66f4\u65b0\u77f3\u5934\u503c\n        if (y &gt; x) {\n            stones&#91;yy] = y - x;\n            stones&#91;xx] = 0;\n        } else {\n            stones&#91;yy] = 0;\n            stones&#91;xx] = 0;\n        }\n        \/\/ \u7edf\u8ba1\u5269\u4e0b\u7684\u77f3\u5934\u6570\u91cf\n        int count = 0;\n        int lastValue = 0;\n        for (int i = 0; i &lt; n; i++) {\n            if (stones&#91;i] &gt; 0) {\n                count++;\n                lastValue = stones&#91;i];\n            }\n        }\n        \/\/ \u5224\u65ad\u4e00\u4e0b\u6570\u91cf\n        if (count == 0) {\n            return 0;\n        } else if (count == 1) {\n            return lastValue;\n        } else {\n            \/\/ \u9012\u5f52\u518d\u53bb\u5904\u7406\u5269\u4e0b\u7684\u77f3\u5934\n            return lastStoneWeight(stones);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u60f3\u5230\u4e86\u66f4\u7b80\u5355\u7684\u601d\u8def\uff0c\u987a\u5e8f\u4e0d\u91cd\u8981\u6240\u4ee5\u7528\u4e0d\u7740\u6a21\u62df\uff0c\u76f4\u63a5\u5728while\u5faa\u73af\u91cc\u7528Arrays.sort\uff08\uff09\u6392\u5e8f\u627e\u6700\u5927\u503c\u7b2c\u4e8c\u5927\u503c\u518d\u64cd\u4f5c\u5c31\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int lastStoneWeight(int&#91;] stones) \n        \/\/ \u7b80\u5355\u5224\u65ad\n        if (stones.length == 0) return 0;\n        if (stones.length == 1) return stones&#91;0];\n        int n = stones.length;\n        \n        while (stones&#91;n-2]!=0) {\/\/\u53ea\u8981\u7b2c\u4e8c\u5927\u503c\u4e3a0\uff0c\u5c31\u8bf4\u660e\u8981\u4e48\u53ea\u5269\u4e00\u4e2a\u8981\u4e48\u5168\u6ca1\u4e86\uff0c\u7ed3\u675f\u5faa\u73af\u76f4\u63a5\u8fd4\u56de\u6700\u5927\u503c\n            Arrays.sort(stones);\n            stones&#91;n-1]=stones&#91;n-1]-stones&#91;n-2];\n            stones&#91;n-2]=0;\n            Arrays.sort(stones);\n        }\n        return stones&#91;n-1];\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u518d\u770b\u5b98\u89e3\uff0c\u5b66\u5230\u4e86PriorityQueue\u4f18\u5148\u961f\u5217\uff0c\u76f4\u63a5\u53ef\u4ee5poll\u6700\u5c0f\u503c\uff08\u6700\u5927\u503c\uff09\uff0c\u5176\u5b9e\u601d\u8def\u548c\u4e0a\u9762\u5dee\u4e0d\u591a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int lastStoneWeight(int&#91;] stones) {\n        PriorityQueue&lt;Integer> pq = new PriorityQueue&lt;Integer>((a, b) -> b - a);\/\/\u62ec\u53f7\u91cc\u4e3a\u7a7a\u5c31\u662f\u6700\u5c0f\u4f18\u5148\u961f\u5217\n        for (int stone : stones) {\n            pq.offer(stone);\/\/offer\u5b58\u8fdb\u53bb\u81ea\u52a8\u5c31\u6392\u597d\u4e86\n        }\n\n        while (pq.size() > 1) {\/\/\u76f4\u5230\u53ea\u5269\u4e00\u4e2a\u503c\u7ed3\u675f\n            int a = pq.poll();\/\/poll\u53d6\u51fa\u6700\u5927\u503c\n            int b = pq.poll();\/\/\u7b2c\u4e8c\u5927\u503c\n            if (a > b) {\n                pq.offer(a - b);\/\/\u65b0\u503c\u518d\u653e\u8fdb\u53bb\u6392\u5217\n            } \/\/else\u6bd4\u5982a == b\uff0c\u4e24\u4e2a\u90fd\u4e0d\u505a\u5904\u7406\uff0c\u76f8\u5f53\u4e8e\u4e22\u6389\n        }\n        return pq.isEmpty() ? 0 : pq.poll();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 \u524d\u9762\u7684\u6ca1\u8bb0\u5f55\u5c31\u8df3\u8fc7\u5427 (\u4e00) \u5355\u8c03\u6808 Q1. \u5546\u54c1\u6298\u6263\u540e\u7684\u6700\u7ec8\u4ef7\u683c \u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4&#038;nbsp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[12,10],"class_list":["post-374","post","type-post","status-publish","format-standard","hentry","category-java_study","tag-java","tag-10"],"_links":{"self":[{"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/posts\/374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/comments?post=374"}],"version-history":[{"count":22,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/posts\/374\/revisions\/484"}],"wp:attachment":[{"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}