From a9a65adeb92b08cbbb72be24051fc1f6543ff24b Mon Sep 17 00:00:00 2001 From: BohouZhang Date: Wed, 28 Apr 2021 01:31:29 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E3=80=90=E5=89=91?= =?UTF-8?q?=E6=8C=87Offer=E3=80=91=E7=AC=AC59=E9=A2=98=E9=A2=98=E8=A7=A3?= =?UTF-8?q?=EF=BC=9A=E9=98=9F=E5=88=97=E7=9A=84=E6=9C=80=E5=A4=A7=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/LeetCode/.DS_Store | Bin 0 -> 6148 bytes docs/LeetCode/1 - 100/_category_.json | 4 + docs/LeetCode/1 - 100/hello.md | 12 ++ docs/LeetCode/100 - 200/_category_.json | 4 + docs/LeetCode/100 - 200/hello 100.md | 12 ++ docs/LeetCode/StackAndQueue/.DS_Store | Bin 0 -> 6148 bytes .../LeetCode/StackAndQueue/MaxValueOfQueue.md | 183 ++++++++++++++++++ docs/LeetCode/_category_.json | 4 + docs/LeetCode/intro-docs.md | 35 ++++ 9 files changed, 254 insertions(+) create mode 100644 docs/LeetCode/.DS_Store create mode 100644 docs/LeetCode/1 - 100/_category_.json create mode 100644 docs/LeetCode/1 - 100/hello.md create mode 100644 docs/LeetCode/100 - 200/_category_.json create mode 100644 docs/LeetCode/100 - 200/hello 100.md create mode 100644 docs/LeetCode/StackAndQueue/.DS_Store create mode 100755 docs/LeetCode/StackAndQueue/MaxValueOfQueue.md create mode 100644 docs/LeetCode/_category_.json create mode 100644 docs/LeetCode/intro-docs.md diff --git a/docs/LeetCode/.DS_Store b/docs/LeetCode/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8015193188bb95fd6afb475f2eaa539b5c53e9de GIT binary patch literal 6148 zcmeHK%Z}496uq7)Oj=Y;7f8$kq-@x=RSPo=vp|GW7PA8d!2(bxX{t7m#8sM7l~U9_ zE0(}V@DZ%|0=@+{aQrCr!Hk#=NCU`uuCh9ZE8`QJj$p;UE-9|7%f$LN(0mqwO`EHbZdoOq>vJDQ{+}CK-mVb zN-+h?O!j(>>4N7W{~1P2z`aL9%mKc_{PO!ue>CX(k(})(zaNEztXBKXHa2r-&Td&& z!73CBPuw0KxtW&@2MsTMBx-lPtmo_aKIhSo*z+HRol&cF{x(lCFHAbAB!@u+FNAdW@ zX7fjlaHmfAiu;@k&n^Kjy(H z8U5aAX7t=mlZYo>%wn#W%SJ;0bzz$jo8SX4ml4>nw&qj9ECZXGD(6#$q+ zvoyrwr$KORjgH2dLiE6xq5>6_=_>|PbhO(#&(SzjsOZG><%8)bGkrs0^641gmg&SC zg{CwL7zLIU*i_Y$IREb)fB#=5nVM0+DDY1yz;aEu*}#(Y*}Aqkan{=KBe*b;H&ZAn kDD-hG1924Zz@?$j76H)FI8%rgnE4TqGMK_B@JAK+4tBi5jsO4v literal 0 HcmV?d00001 diff --git a/docs/LeetCode/1 - 100/_category_.json b/docs/LeetCode/1 - 100/_category_.json new file mode 100644 index 0000000..063c1af --- /dev/null +++ b/docs/LeetCode/1 - 100/_category_.json @@ -0,0 +1,4 @@ +{ + "label": "1 - 100", + "position": 2 +} diff --git a/docs/LeetCode/1 - 100/hello.md b/docs/LeetCode/1 - 100/hello.md new file mode 100644 index 0000000..803e48d --- /dev/null +++ b/docs/LeetCode/1 - 100/hello.md @@ -0,0 +1,12 @@ +--- +sidebar_position: 1 +--- + +# Hello 1 + + + +```shell +Hello 1 + +``` diff --git a/docs/LeetCode/100 - 200/_category_.json b/docs/LeetCode/100 - 200/_category_.json new file mode 100644 index 0000000..0e3c226 --- /dev/null +++ b/docs/LeetCode/100 - 200/_category_.json @@ -0,0 +1,4 @@ +{ + "label": "100 - 200", + "position": 3 +} diff --git a/docs/LeetCode/100 - 200/hello 100.md b/docs/LeetCode/100 - 200/hello 100.md new file mode 100644 index 0000000..db0897c --- /dev/null +++ b/docs/LeetCode/100 - 200/hello 100.md @@ -0,0 +1,12 @@ +--- +sidebar_position: 1 +--- + +# Hello 100 + + + +```shell +Hello 100 + +``` diff --git a/docs/LeetCode/StackAndQueue/.DS_Store b/docs/LeetCode/StackAndQueue/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b626063be09773267276f60ba7425b86a812df9c GIT binary patch literal 6148 zcmeHKJxc>o5S-N%0h?fD`F=sbe{hCaScr&Vtub1iqzYJG=g-#J55?mYtSn>}cIUml zo%eE6+-3pTYQH@N769gSN4$ENnm>1+*g<8CNaq>%=&{EmuJAOe{yyQ{V_cH?g4+@Q zI!^M_mUzbYqTfAi`rU^0{$tPR%eOGDWKuv1NC7Dz1*E`l74Y6mn;#`AN&zV#1wIw< z??a#g@BrRuGdm-AlR u=y&uFV{Mc(L@OpnE9S;q@yVCE=4(Fh3Wvm?Gaq!K{s_1(GAZyE3cLg2Fdeu6 literal 0 HcmV?d00001 diff --git a/docs/LeetCode/StackAndQueue/MaxValueOfQueue.md b/docs/LeetCode/StackAndQueue/MaxValueOfQueue.md new file mode 100755 index 0000000..8e1d761 --- /dev/null +++ b/docs/LeetCode/StackAndQueue/MaxValueOfQueue.md @@ -0,0 +1,183 @@ +# 队列\_剑指Offer59题\_队列的最大值问题 + +## 题目描述如下: + +请定义一个队列并实现函数max\_value得到队列里的最大值,要求函数max\_value、push\_back和pop\_front的均摊时间复杂度都是O(1)。若队列为空,pop\_front和max\_value需要返回-1。 + +示例1: + +~~~shell +输入: +["MaxQueue","push_back","push_back","max_value","pop_front","max_value"] +[[],[1],[2],[],[],[]] +输出: [null,null,null,2,1,2] +~~~ + +示例2: + +```shell +输入: +["MaxQueue","pop_front","max_value"] +[[],[],[]] +输出: [null,-1,-1] +``` + +限制: + +- `1 <= push_back,pop_front,max_value的总操作数 <= 10000` +- `1 <= value <= 10^5` + +代码: + +~~~java +class MaxQueue { + + public MaxQueue() { + + } + + public int max_value() { + + } + + public void push_back(int value) { + + } + + public int pop_front() { + + } +} + +/** + * Your MaxQueue object will be instantiated and called as such: + * MaxQueue obj = new MaxQueue(); + * int param_1 = obj.max_value(); + * obj.push_back(value); + * int param_3 = obj.pop_front(); + */ +~~~ + +## 思考思路 + +题目的描述中提到了3个方法,其中max\_value是求最大值的,**提到最大值,首先想到这里应该是用到了某种排序的手段**。剩下的两个方法分别是push和pop操作,这明显是栈、队列这块的操作。因此不难想象,这道题用到的数据结构包含某种队列,然后涉及到某种排序方法。 + +本着先实现,后优化的原则,最容易想到的解决方案是这样的: + +用一个队列保存所有的数据,这样保证了push和pop操作是正常执行的。然后将这个队列从大到小排列到另一个数据结构中,这样执行max_value方法时输出数组第一项即可。删除队列中数据的时候,用来排序的数据结构中的对应数据也应该删掉。 + +于是乎很容易写出这样的代码: + +~~~java +class MaxQueue { + + private Queue datas; + + // TODO:排序后的数据暂时用队列保存 + private Queue sortedDatas; + + public MaxQueue() { + this.datas = new LinkedList; + this.sortedDatas = new LinkedList; + } + + public int max_value() { + if (sortedDatas.isEmpty()) { + return -1; + } + return sortedDatas.peekFirst(); + } + + public void push_back(int value) { + datas.offer(value); + sortedDatas = addNewDataAndSortDatas(); + } + + public int pop_front() { + if (datas.isEmpty()) { + return -1; + } + int ans = datas.pool(); + deleteDataFromSortedDatas(ans); + return ans; + } + + private Queue addNewDataAndSortDatas() { + // TODO: 将所有数据排序,然后覆盖原先的sortedDatas + } + + private void deleteDataFromSortedDatas(int ans) { + // TODO: 将ans从sortedDatas队列中删除 + } +} +~~~ + +接下来考虑着三个TODO项如何去解决。首先要确定的是用什么数据结构保存sortedDatas。 + +| 数据结构 | 优劣分析 | 结论 | +| -------- | ------------------------------------------------------------ | ---------- | +| 数组 | 队列中元素个数能达到10000,定义10000长度的数组浪费内存,不定义这么长要考虑扩容,浪费性能。 | :no_entry: | +| 链表 | 不可能用这货排序。 | :no_entry: | +| 栈 | 将数据由小到大存入,能直接取到最大值。删除数据时要再用一个栈倒数据。**正常的排序方法难以实现均摊时间复杂度O(1)**。所以能实现但是时间复杂度不符合要求。 | | +| 队列 | 单调递减的队列。遇到的问题和栈一致。 | | +| 树 | 最大二叉树能实现,但是树在内存中也是以数组的形式保存的,问题和数组一致。 | :no_entry: | + +所以说,要么用栈,要么用数组来保存sortedDatas中的数据。所以决定采用双端队列来处理sortedDatas,因为**双端队列能同时满足栈、队列的所有操作**。下一个问题便是……如何给这个双端队列进行排序。 + +堆排序、快速排序的时间复杂度是O(n lg n);其他的线性排序算法时间复杂度是O(n)。这些都不太满足题目的要求。所以这里我想到了一种可能性:**有些数是不是没有参与排序**?如果队列中有n个数字,参与排序的数字小于n,那么排序的时间复杂度会更小的。那么,**什么样的数字可能不用参与排序呢**?max\_value方法要输出的是最大的数,所以最大的数字是一定参与排序的,即比较小的数字可以不用参与排序——我只管最大的数,其他的数字顺序对与否甚至数据的有无,都不会影响到最后的结果。 + +所以进一步的解决方案是这样的: + +每次有新的数据插入的时候,将sortedDatas中的数据从后往前跟入参比较,如果小于入参,就直接舍弃掉,直到遇到比入参大的数字时再将入参存入队列中。 + +这样的好处显而易见:小于入参的数字不会影响到最大值的输出。另一方面,队列中的数据来得比现在这个入参早,所以小于入参的数字会比这个入参更早的被删除掉。即,小于入参的数字永远不可能作为最大值背输出出来。 + +所以优化后的代码是这个样子的: + +~~~java +class MaxQueue { + Queue datas; + Deque sortedDatas; + + public MaxQueue() { + datas = new LinkedList(); + sortedDatas = new LinkedList(); + } + + public int max_value() { + if (sortedDatas.isEmpty()) { + return -1; + } + return sortedDatas.peekFirst(); + } + + public void push_back(int value) { + while (!sortedDatas.isEmpty() && sortedDatas.peekLast() < value) { + sortedDatas.pollLast(); + } + sortedDatas.offerLast(value); + datas.offer(value); + } + + public int pop_front() { + if (datas.isEmpty()) { + return -1; + } + int ans = datas.poll(); + if (ans == sortedDatas.peekFirst()) { + sortedDatas.pollFirst(); + } + return ans; + } +} + +/** + * Your MaxQueue object will be instantiated and called as such: + * MaxQueue obj = new MaxQueue(); + * int param_1 = obj.max_value(); + * obj.push_back(value); + * int param_3 = obj.pop_front(); + */ +~~~ + +此题得解。 \ No newline at end of file diff --git a/docs/LeetCode/_category_.json b/docs/LeetCode/_category_.json new file mode 100644 index 0000000..26c3473 --- /dev/null +++ b/docs/LeetCode/_category_.json @@ -0,0 +1,4 @@ +{ + "label": "LeetCode刷题", + "position": 3 +} diff --git a/docs/LeetCode/intro-docs.md b/docs/LeetCode/intro-docs.md new file mode 100644 index 0000000..6ecdb88 --- /dev/null +++ b/docs/LeetCode/intro-docs.md @@ -0,0 +1,35 @@ +--- +sidebar_position: 1 +--- + +# LeetCode 刷题介绍 + +Let's discover **Docusaurus in less than 5 minutes**. + +## Getting Started + +Get started by **creating a new site**. + +Or **try Docusaurus immediately** with **[new.docusaurus.io](https://new.docusaurus.io)**. + +## Generate a new site + +Generate a new Docusaurus site using the **classic template**: + +```shell +npx @docusaurus/init@latest init my-website classic +``` + +## Start your site + +Run the development server: + +```shell +cd my-website + +npx docusaurus start +``` + +Your site starts at `http://localhost:3000`. + +Open `docs/getting-started.md` and edit some lines: the site **reloads automatically** and display your changes. From 243bd660e01bd0f1c2263653edc8db2a429ee862 Mon Sep 17 00:00:00 2001 From: BohouZhang Date: Mon, 3 May 2021 10:48:10 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E3=80=90feat=E3=80=91:=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=B7=A5=E7=A8=8B=E7=BB=93=E6=9E=84=EF=BC=8C=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=9B=A0MacOS=E7=B3=BB=E7=BB=9F=E7=89=B9?= =?UTF-8?q?=E6=80=A7=E4=BA=A7=E7=94=9F=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 6148 bytes docs/.DS_Store | Bin 0 -> 6148 bytes docs/LeetCode刷题/1 - 100/_category_.json | 4 --- docs/LeetCode刷题/1 - 100/hello.md | 12 ------- docs/LeetCode刷题/100 - 200/_category_.json | 4 --- docs/LeetCode刷题/100 - 200/hello 100.md | 12 ------- docs/LeetCode刷题/intro-docs.md | 35 -------------------- 7 files changed, 67 deletions(-) create mode 100644 .DS_Store create mode 100644 docs/.DS_Store delete mode 100644 docs/LeetCode刷题/1 - 100/_category_.json delete mode 100644 docs/LeetCode刷题/1 - 100/hello.md delete mode 100644 docs/LeetCode刷题/100 - 200/_category_.json delete mode 100644 docs/LeetCode刷题/100 - 200/hello 100.md delete mode 100644 docs/LeetCode刷题/intro-docs.md diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..992daa19cc039c4e9e1e064a15fadeb5d7e08db7 GIT binary patch literal 6148 zcmeHK!EVz)5S>jz>!d(~REb`YeBqWxt!ODdAf;(?hy(~>1P4H^9jB?mwIkahDj_I$ zz5ovV0^h+G@GYF+&3cRMgj^v)JJRf1uiwntZ`ZqCA`;D^yF*kZA_LA?s-XCT@qP9= zE2x(1Kp}JZRHrV56wv7$TQ_J0v;xbf0Drsd^gqvfXU0m zA-w)m8fsWJ5b5Ka@6YhQ&hcEZy0iJjtwlU%6c1Ft-<*N(#mV;ecTp&ouB@yYM#ZRH ztGu$#!jYBO$uMr((F?wMY$s=qsy_`w|99PXo_W2|adqP;3=-Q5dXWtF+#V<|pL>BD zj+)^xaATRrN^clO*(e`ZH^<|>{oR_mb+9|Bnd5tR4{GNAoxRDVY}~xPbH8(Pe(`Sn ze)8cHD}vP!%b~P&g_rOdl@ci--da)Mhe020%->M$Ly9pRLyF*WWQg81`W_#m)@Z4V+w4^!Q^W>_`I0sGZgrFv;AG2$*>?!PMj~9 ziBqO0c*a9&(L-RJ&dJU9L_CGE5o^%HgGxO)VI!%|+ys>qDjQVtO;TfR`5sL_kybz}@Q)SX^}&QQHWW@% zluHK+c?1BK&@2sg{z>2%Q(;5lG)3Hji8K_Lh6;7W5NSBtO~q>{oTiwDlTe2bp?VhT zgd$|`$ZtwIiAIXL)Cy#=L2YG6aR%j%9_n;!U_T+%vfXY$%+jh!&Xs5s)(ILM!lB75D{zY4rO5 literal 0 HcmV?d00001 diff --git a/docs/.DS_Store b/docs/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..993e58af0f803924096e1016c3ba5555ea375358 GIT binary patch literal 6148 zcmeHK%Wl&^6ur|LIti+p1rpsL&4yiCTG7&Wfn?IMsKf%v2o``^JGL52uBVC}(h`Ea zW6>`tKhf{#7w|2t;LL+ZoFXI^2t{+FJLk^KJ!8#rX2wHAtTTw$iCRR&;R?Mbifc^u zE1$AC+p+*;dJIV`6qBSb6;s)+!71Pr_}>&zYj=@S%BiG2R_@RByGq9t?`N_bWimpR z+9DpqSHEgQq_%~pl;Bs=F1RrzwfxIJ+0_R{)@gW^IVohl1h=i@wFmAbMcTP=SWZ>=lColeX*=t`%6Q(#hoc~fnv^Z&{3@Bfn|S91zD1!hVC=IsT09Zbodty7azXRUxA r!j-AK%Als8u*b0u)KR<)*M>f!0-$fPGKdzK`w-AFxWXy$PZjtE%($A# literal 0 HcmV?d00001 diff --git a/docs/LeetCode刷题/1 - 100/_category_.json b/docs/LeetCode刷题/1 - 100/_category_.json deleted file mode 100644 index 063c1af..0000000 --- a/docs/LeetCode刷题/1 - 100/_category_.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "label": "1 - 100", - "position": 2 -} diff --git a/docs/LeetCode刷题/1 - 100/hello.md b/docs/LeetCode刷题/1 - 100/hello.md deleted file mode 100644 index 803e48d..0000000 --- a/docs/LeetCode刷题/1 - 100/hello.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -sidebar_position: 1 ---- - -# Hello 1 - - - -```shell -Hello 1 - -``` diff --git a/docs/LeetCode刷题/100 - 200/_category_.json b/docs/LeetCode刷题/100 - 200/_category_.json deleted file mode 100644 index 0e3c226..0000000 --- a/docs/LeetCode刷题/100 - 200/_category_.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "label": "100 - 200", - "position": 3 -} diff --git a/docs/LeetCode刷题/100 - 200/hello 100.md b/docs/LeetCode刷题/100 - 200/hello 100.md deleted file mode 100644 index db0897c..0000000 --- a/docs/LeetCode刷题/100 - 200/hello 100.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -sidebar_position: 1 ---- - -# Hello 100 - - - -```shell -Hello 100 - -``` diff --git a/docs/LeetCode刷题/intro-docs.md b/docs/LeetCode刷题/intro-docs.md deleted file mode 100644 index 6ecdb88..0000000 --- a/docs/LeetCode刷题/intro-docs.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -sidebar_position: 1 ---- - -# LeetCode 刷题介绍 - -Let's discover **Docusaurus in less than 5 minutes**. - -## Getting Started - -Get started by **creating a new site**. - -Or **try Docusaurus immediately** with **[new.docusaurus.io](https://new.docusaurus.io)**. - -## Generate a new site - -Generate a new Docusaurus site using the **classic template**: - -```shell -npx @docusaurus/init@latest init my-website classic -``` - -## Start your site - -Run the development server: - -```shell -cd my-website - -npx docusaurus start -``` - -Your site starts at `http://localhost:3000`. - -Open `docs/getting-started.md` and edit some lines: the site **reloads automatically** and display your changes. From 1774f5e8926020a6efe5ab35f159a3a6dd0d4638 Mon Sep 17 00:00:00 2001 From: "yaoyuan2.chu" Date: Tue, 4 May 2021 00:04:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E3=80=90feat=E3=80=91:=E5=89=91=E6=8C=87of?= =?UTF-8?q?fer=E7=9B=AE=E5=BD=95=E6=B7=BB=E5=8A=A0=EF=BC=8Cmd=E4=B8=ADjsx?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/LeetCode/1 - 100/_category_.json | 2 +- docs/LeetCode/100 - 200/_category_.json | 2 +- .../{ => CodingInterviews}/StackAndQueue/.DS_Store | Bin .../StackAndQueue/MaxValueOfQueue.md | 8 ++++---- docs/LeetCode/CodingInterviews/_category_.json | 4 ++++ docs/LeetCode/_category_.json | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) rename docs/LeetCode/{ => CodingInterviews}/StackAndQueue/.DS_Store (100%) rename docs/LeetCode/{ => CodingInterviews}/StackAndQueue/MaxValueOfQueue.md (89%) mode change 100755 => 100644 create mode 100644 docs/LeetCode/CodingInterviews/_category_.json diff --git a/docs/LeetCode/1 - 100/_category_.json b/docs/LeetCode/1 - 100/_category_.json index 063c1af..7d660de 100644 --- a/docs/LeetCode/1 - 100/_category_.json +++ b/docs/LeetCode/1 - 100/_category_.json @@ -1,4 +1,4 @@ { "label": "1 - 100", - "position": 2 + "position": 3 } diff --git a/docs/LeetCode/100 - 200/_category_.json b/docs/LeetCode/100 - 200/_category_.json index 0e3c226..6b996cf 100644 --- a/docs/LeetCode/100 - 200/_category_.json +++ b/docs/LeetCode/100 - 200/_category_.json @@ -1,4 +1,4 @@ { "label": "100 - 200", - "position": 3 + "position": 4 } diff --git a/docs/LeetCode/StackAndQueue/.DS_Store b/docs/LeetCode/CodingInterviews/StackAndQueue/.DS_Store similarity index 100% rename from docs/LeetCode/StackAndQueue/.DS_Store rename to docs/LeetCode/CodingInterviews/StackAndQueue/.DS_Store diff --git a/docs/LeetCode/StackAndQueue/MaxValueOfQueue.md b/docs/LeetCode/CodingInterviews/StackAndQueue/MaxValueOfQueue.md old mode 100755 new mode 100644 similarity index 89% rename from docs/LeetCode/StackAndQueue/MaxValueOfQueue.md rename to docs/LeetCode/CodingInterviews/StackAndQueue/MaxValueOfQueue.md index 8e1d761..ad89551 --- a/docs/LeetCode/StackAndQueue/MaxValueOfQueue.md +++ b/docs/LeetCode/CodingInterviews/StackAndQueue/MaxValueOfQueue.md @@ -64,7 +64,7 @@ class MaxQueue { 本着先实现,后优化的原则,最容易想到的解决方案是这样的: -用一个队列保存所有的数据,这样保证了push和pop操作是正常执行的。然后将这个队列从大到小排列到另一个数据结构中,这样执行max_value方法时输出数组第一项即可。删除队列中数据的时候,用来排序的数据结构中的对应数据也应该删掉。 +用一个队列保存所有的数据,这样保证了push和pop操作是正常执行的。然后将这个队列从大到小排列到另一个数据结构中,这样执行max_value方法时输出数组第一项即可。删除队列中数据的时候,用来排序的数据结构中的对应数据也应该删掉。 于是乎很容易写出这样的代码: @@ -112,7 +112,7 @@ class MaxQueue { } ~~~ -接下来考虑着三个TODO项如何去解决。首先要确定的是用什么数据结构保存sortedDatas。 +接下来考虑着三个TODO项如何去解决。首先要确定的是用什么数据结构保存sortedDatas。 | 数据结构 | 优劣分析 | 结论 | | -------- | ------------------------------------------------------------ | ---------- | @@ -122,13 +122,13 @@ class MaxQueue { | 队列 | 单调递减的队列。遇到的问题和栈一致。 | | | 树 | 最大二叉树能实现,但是树在内存中也是以数组的形式保存的,问题和数组一致。 | :no_entry: | -所以说,要么用栈,要么用数组来保存sortedDatas中的数据。所以决定采用双端队列来处理sortedDatas,因为**双端队列能同时满足栈、队列的所有操作**。下一个问题便是……如何给这个双端队列进行排序。 +所以说,要么用栈,要么用数组来保存sortedDatas中的数据。所以决定采用双端队列来处理sortedDatas,因为**双端队列能同时满足栈、队列的所有操作**。下一个问题便是……如何给这个双端队列进行排序。 堆排序、快速排序的时间复杂度是O(n lg n);其他的线性排序算法时间复杂度是O(n)。这些都不太满足题目的要求。所以这里我想到了一种可能性:**有些数是不是没有参与排序**?如果队列中有n个数字,参与排序的数字小于n,那么排序的时间复杂度会更小的。那么,**什么样的数字可能不用参与排序呢**?max\_value方法要输出的是最大的数,所以最大的数字是一定参与排序的,即比较小的数字可以不用参与排序——我只管最大的数,其他的数字顺序对与否甚至数据的有无,都不会影响到最后的结果。 所以进一步的解决方案是这样的: -每次有新的数据插入的时候,将sortedDatas中的数据从后往前跟入参比较,如果小于入参,就直接舍弃掉,直到遇到比入参大的数字时再将入参存入队列中。 +每次有新的数据插入的时候,将sortedDatas中的数据从后往前跟入参比较,如果小于入参,就直接舍弃掉,直到遇到比入参大的数字时再将入参存入队列中。 这样的好处显而易见:小于入参的数字不会影响到最大值的输出。另一方面,队列中的数据来得比现在这个入参早,所以小于入参的数字会比这个入参更早的被删除掉。即,小于入参的数字永远不可能作为最大值背输出出来。 diff --git a/docs/LeetCode/CodingInterviews/_category_.json b/docs/LeetCode/CodingInterviews/_category_.json new file mode 100644 index 0000000..c35d1bd --- /dev/null +++ b/docs/LeetCode/CodingInterviews/_category_.json @@ -0,0 +1,4 @@ +{ + "label": "剑指Offer", + "position": 2 +} diff --git a/docs/LeetCode/_category_.json b/docs/LeetCode/_category_.json index 26c3473..b3c750c 100644 --- a/docs/LeetCode/_category_.json +++ b/docs/LeetCode/_category_.json @@ -1,4 +1,4 @@ { "label": "LeetCode刷题", - "position": 3 + "position": 2 } From c22c77d3b73dbc48d37cc71e87b3aedbc2b8fe69 Mon Sep 17 00:00:00 2001 From: "yaoyuan2.chu" Date: Tue, 4 May 2021 00:11:24 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E3=80=90feat=E3=80=91:=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=94=B9=E8=8B=B1=E6=96=87=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/LeetCode刷题/_category_.json | 4 ---- .../SpringBoot系列 => backend-doc/SpringBoot}/_category_.json | 0 .../SpringBoot系列 => backend-doc/SpringBoot}/helloboot.md | 0 .../SpringCloud系列 => backend-doc/SpringCloud}/_category_.json | 0 .../SpringCloud系列 => backend-doc/SpringCloud}/hellocloud.md | 0 docs/{技术文档 => backend-doc}/_category_.json | 2 +- docs/{技术文档 => backend-doc}/intro-docs.md | 0 .../React系列 => frontend-doc/React}/_category_.json | 0 .../React系列 => frontend-doc/React}/helloreact.md | 0 .../Vue系列 => frontend-doc/Vue}/_category_.json | 0 docs/{技术文档前端/Vue系列 => frontend-doc/Vue}/hellovue.md | 0 docs/{技术文档前端 => frontend-doc}/_category_.json | 2 +- docs/{技术文档前端 => frontend-doc}/intro-docs.md | 0 13 files changed, 2 insertions(+), 6 deletions(-) delete mode 100644 docs/LeetCode刷题/_category_.json rename docs/{技术文档/SpringBoot系列 => backend-doc/SpringBoot}/_category_.json (100%) rename docs/{技术文档/SpringBoot系列 => backend-doc/SpringBoot}/helloboot.md (100%) rename docs/{技术文档/SpringCloud系列 => backend-doc/SpringCloud}/_category_.json (100%) rename docs/{技术文档/SpringCloud系列 => backend-doc/SpringCloud}/hellocloud.md (100%) rename docs/{技术文档 => backend-doc}/_category_.json (70%) rename docs/{技术文档 => backend-doc}/intro-docs.md (100%) rename docs/{技术文档前端/React系列 => frontend-doc/React}/_category_.json (100%) rename docs/{技术文档前端/React系列 => frontend-doc/React}/helloreact.md (100%) rename docs/{技术文档前端/Vue系列 => frontend-doc/Vue}/_category_.json (100%) rename docs/{技术文档前端/Vue系列 => frontend-doc/Vue}/hellovue.md (100%) rename docs/{技术文档前端 => frontend-doc}/_category_.json (70%) rename docs/{技术文档前端 => frontend-doc}/intro-docs.md (100%) diff --git a/docs/LeetCode刷题/_category_.json b/docs/LeetCode刷题/_category_.json deleted file mode 100644 index ceaadc7..0000000 --- a/docs/LeetCode刷题/_category_.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "label": "LeetCode刷题", - "position": 4 -} diff --git a/docs/技术文档/SpringBoot系列/_category_.json b/docs/backend-doc/SpringBoot/_category_.json similarity index 100% rename from docs/技术文档/SpringBoot系列/_category_.json rename to docs/backend-doc/SpringBoot/_category_.json diff --git a/docs/技术文档/SpringBoot系列/helloboot.md b/docs/backend-doc/SpringBoot/helloboot.md similarity index 100% rename from docs/技术文档/SpringBoot系列/helloboot.md rename to docs/backend-doc/SpringBoot/helloboot.md diff --git a/docs/技术文档/SpringCloud系列/_category_.json b/docs/backend-doc/SpringCloud/_category_.json similarity index 100% rename from docs/技术文档/SpringCloud系列/_category_.json rename to docs/backend-doc/SpringCloud/_category_.json diff --git a/docs/技术文档/SpringCloud系列/hellocloud.md b/docs/backend-doc/SpringCloud/hellocloud.md similarity index 100% rename from docs/技术文档/SpringCloud系列/hellocloud.md rename to docs/backend-doc/SpringCloud/hellocloud.md diff --git a/docs/技术文档/_category_.json b/docs/backend-doc/_category_.json similarity index 70% rename from docs/技术文档/_category_.json rename to docs/backend-doc/_category_.json index 8f4e774..fb17be5 100644 --- a/docs/技术文档/_category_.json +++ b/docs/backend-doc/_category_.json @@ -1,4 +1,4 @@ { "label": "技术文档(后端)", - "position": 2 + "position": 3 } diff --git a/docs/技术文档/intro-docs.md b/docs/backend-doc/intro-docs.md similarity index 100% rename from docs/技术文档/intro-docs.md rename to docs/backend-doc/intro-docs.md diff --git a/docs/技术文档前端/React系列/_category_.json b/docs/frontend-doc/React/_category_.json similarity index 100% rename from docs/技术文档前端/React系列/_category_.json rename to docs/frontend-doc/React/_category_.json diff --git a/docs/技术文档前端/React系列/helloreact.md b/docs/frontend-doc/React/helloreact.md similarity index 100% rename from docs/技术文档前端/React系列/helloreact.md rename to docs/frontend-doc/React/helloreact.md diff --git a/docs/技术文档前端/Vue系列/_category_.json b/docs/frontend-doc/Vue/_category_.json similarity index 100% rename from docs/技术文档前端/Vue系列/_category_.json rename to docs/frontend-doc/Vue/_category_.json diff --git a/docs/技术文档前端/Vue系列/hellovue.md b/docs/frontend-doc/Vue/hellovue.md similarity index 100% rename from docs/技术文档前端/Vue系列/hellovue.md rename to docs/frontend-doc/Vue/hellovue.md diff --git a/docs/技术文档前端/_category_.json b/docs/frontend-doc/_category_.json similarity index 70% rename from docs/技术文档前端/_category_.json rename to docs/frontend-doc/_category_.json index d1e0854..6940365 100644 --- a/docs/技术文档前端/_category_.json +++ b/docs/frontend-doc/_category_.json @@ -1,4 +1,4 @@ { "label": "技术文档(前端)", - "position": 3 + "position": 4 } diff --git a/docs/技术文档前端/intro-docs.md b/docs/frontend-doc/intro-docs.md similarity index 100% rename from docs/技术文档前端/intro-docs.md rename to docs/frontend-doc/intro-docs.md