diff --git a/404.html b/404.html index de41c5df8..88b8ebbba 100644 --- a/404.html +++ b/404.html @@ -1864,14 +1864,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1892,14 +1884,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2319,14 +2303,6 @@ 第 12 章   分治 - - - - - - - - @@ -2358,14 +2334,6 @@ 12.1   分治算法 - - - - - - - - @@ -2386,14 +2354,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2414,14 +2374,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2442,14 +2394,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2470,14 +2414,6 @@ 12.5   小结 - - - - - - - - @@ -2709,14 +2645,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2748,14 +2676,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2776,14 +2696,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2804,14 +2716,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2832,14 +2736,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2860,14 +2756,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2888,14 +2776,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2916,14 +2796,6 @@ 14.7   小结 - - - - - - - - @@ -2982,14 +2854,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3021,14 +2885,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3049,14 +2905,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3077,14 +2925,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3105,14 +2945,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3133,14 +2965,6 @@ 15.5   小结 - - - - - - - - @@ -3387,10 +3211,15 @@ diff --git a/assets/javascripts/glightbox.min.js b/assets/javascripts/glightbox.min.js new file mode 100644 index 000000000..614fb1888 --- /dev/null +++ b/assets/javascripts/glightbox.min.js @@ -0,0 +1 @@ +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).GLightbox=t()}(this,(function(){"use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var i=0;i1&&void 0!==arguments[1]?arguments[1]:null,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n=e[s]=e[s]||[],l={all:n,evt:null,found:null};return t&&i&&P(n)>0&&o(n,(function(e,n){if(e.eventName==t&&e.fn.toString()==i.toString())return l.found=!0,l.evt=n,!1})),l}function a(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=t.onElement,n=t.withCallback,s=t.avoidDuplicate,l=void 0===s||s,a=t.once,h=void 0!==a&&a,d=t.useCapture,c=void 0!==d&&d,u=arguments.length>2?arguments[2]:void 0,g=i||[];function v(e){T(n)&&n.call(u,e,this),h&&v.destroy()}return C(g)&&(g=document.querySelectorAll(g)),v.destroy=function(){o(g,(function(t){var i=r(t,e,v);i.found&&i.all.splice(i.evt,1),t.removeEventListener&&t.removeEventListener(e,v,c)}))},o(g,(function(t){var i=r(t,e,v);(t.addEventListener&&l&&!i.found||!l)&&(t.addEventListener(e,v,c),i.all.push({eventName:e,fn:v}))})),v}function h(e,t){o(t.split(" "),(function(t){return e.classList.add(t)}))}function d(e,t){o(t.split(" "),(function(t){return e.classList.remove(t)}))}function c(e,t){return e.classList.contains(t)}function u(e,t){for(;e!==document.body;){if(!(e=e.parentElement))return!1;if("function"==typeof e.matches?e.matches(t):e.msMatchesSelector(t))return e}}function g(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||""===t)return!1;if("none"==t)return T(i)&&i(),!1;var n=x(),s=t.split(" ");o(s,(function(t){h(e,"g"+t)})),a(n,{onElement:e,avoidDuplicate:!1,once:!0,withCallback:function(e,t){o(s,(function(e){d(t,"g"+e)})),T(i)&&i()}})}function v(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";if(""==t)return e.style.webkitTransform="",e.style.MozTransform="",e.style.msTransform="",e.style.OTransform="",e.style.transform="",!1;e.style.webkitTransform=t,e.style.MozTransform=t,e.style.msTransform=t,e.style.OTransform=t,e.style.transform=t}function f(e){e.style.display="block"}function p(e){e.style.display="none"}function m(e){var t=document.createDocumentFragment(),i=document.createElement("div");for(i.innerHTML=e;i.firstChild;)t.appendChild(i.firstChild);return t}function y(){return{width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}}function x(){var e,t=document.createElement("fakeelement"),i={animation:"animationend",OAnimation:"oAnimationEnd",MozAnimation:"animationend",WebkitAnimation:"webkitAnimationEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}function b(e,t,i,n){if(e())t();else{var s;i||(i=100);var l=setInterval((function(){e()&&(clearInterval(l),s&&clearTimeout(s),t())}),i);n&&(s=setTimeout((function(){clearInterval(l)}),n))}}function S(e,t,i){if(I(e))console.error("Inject assets error");else if(T(t)&&(i=t,t=!1),C(t)&&t in window)T(i)&&i();else{var n;if(-1!==e.indexOf(".css")){if((n=document.querySelectorAll('link[href="'+e+'"]'))&&n.length>0)return void(T(i)&&i());var s=document.getElementsByTagName("head")[0],l=s.querySelectorAll('link[rel="stylesheet"]'),o=document.createElement("link");return o.rel="stylesheet",o.type="text/css",o.href=e,o.media="all",l?s.insertBefore(o,l[0]):s.appendChild(o),void(T(i)&&i())}if((n=document.querySelectorAll('script[src="'+e+'"]'))&&n.length>0){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}}else{var r=document.createElement("script");r.type="text/javascript",r.src=e,r.onload=function(){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}},document.body.appendChild(r)}}}function w(){return"navigator"in window&&window.navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i)}function T(e){return"function"==typeof e}function C(e){return"string"==typeof e}function k(e){return!(!e||!e.nodeType||1!=e.nodeType)}function E(e){return Array.isArray(e)}function A(e){return e&&e.length&&isFinite(e.length)}function L(t){return"object"===e(t)&&null!=t&&!T(t)&&!E(t)}function I(e){return null==e}function O(e,t){return null!==e&&hasOwnProperty.call(e,t)}function P(e){if(L(e)){if(e.keys)return e.keys().length;var t=0;for(var i in e)O(e,i)&&t++;return t}return e.length}function M(e){return!isNaN(parseFloat(e))&&isFinite(e)}function z(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1,t=document.querySelectorAll(".gbtn[data-taborder]:not(.disabled)");if(!t.length)return!1;if(1==t.length)return t[0];"string"==typeof e&&(e=parseInt(e));var i=[];o(t,(function(e){i.push(e.getAttribute("data-taborder"))}));var n=Math.max.apply(Math,i.map((function(e){return parseInt(e)}))),s=e<0?1:e+1;s>n&&(s="1");var l=i.filter((function(e){return e>=parseInt(s)})),r=l.sort()[0];return document.querySelector('.gbtn[data-taborder="'.concat(r,'"]'))}function X(e){if(e.events.hasOwnProperty("keyboard"))return!1;e.events.keyboard=a("keydown",{onElement:window,withCallback:function(t,i){var n=(t=t||window.event).keyCode;if(9==n){var s=document.querySelector(".gbtn.focused");if(!s){var l=!(!document.activeElement||!document.activeElement.nodeName)&&document.activeElement.nodeName.toLocaleLowerCase();if("input"==l||"textarea"==l||"button"==l)return}t.preventDefault();var o=document.querySelectorAll(".gbtn[data-taborder]");if(!o||o.length<=0)return;if(!s){var r=z();return void(r&&(r.focus(),h(r,"focused")))}var a=z(s.getAttribute("data-taborder"));d(s,"focused"),a&&(a.focus(),h(a,"focused"))}39==n&&e.nextSlide(),37==n&&e.prevSlide(),27==n&&e.close()}})}function Y(e){return Math.sqrt(e.x*e.x+e.y*e.y)}function q(e,t){var i=function(e,t){var i=Y(e)*Y(t);if(0===i)return 0;var n=function(e,t){return e.x*t.x+e.y*t.y}(e,t)/i;return n>1&&(n=1),Math.acos(n)}(e,t);return function(e,t){return e.x*t.y-t.x*e.y}(e,t)>0&&(i*=-1),180*i/Math.PI}var N=function(){function e(i){t(this,e),this.handlers=[],this.el=i}return n(e,[{key:"add",value:function(e){this.handlers.push(e)}},{key:"del",value:function(e){e||(this.handlers=[]);for(var t=this.handlers.length;t>=0;t--)this.handlers[t]===e&&this.handlers.splice(t,1)}},{key:"dispatch",value:function(){for(var e=0,t=this.handlers.length;e=0)console.log("ignore drag for this touched element",e.target.nodeName.toLowerCase());else{this.now=Date.now(),this.x1=e.touches[0].pageX,this.y1=e.touches[0].pageY,this.delta=this.now-(this.last||this.now),this.touchStart.dispatch(e,this.element),null!==this.preTapPosition.x&&(this.isDoubleTap=this.delta>0&&this.delta<=250&&Math.abs(this.preTapPosition.x-this.x1)<30&&Math.abs(this.preTapPosition.y-this.y1)<30,this.isDoubleTap&&clearTimeout(this.singleTapTimeout)),this.preTapPosition.x=this.x1,this.preTapPosition.y=this.y1,this.last=this.now;var t=this.preV;if(e.touches.length>1){this._cancelLongTap(),this._cancelSingleTap();var i={x:e.touches[1].pageX-this.x1,y:e.touches[1].pageY-this.y1};t.x=i.x,t.y=i.y,this.pinchStartLen=Y(t),this.multipointStart.dispatch(e,this.element)}this._preventTap=!1,this.longTapTimeout=setTimeout(function(){this.longTap.dispatch(e,this.element),this._preventTap=!0}.bind(this),750)}}}},{key:"move",value:function(e){if(e.touches){var t=this.preV,i=e.touches.length,n=e.touches[0].pageX,s=e.touches[0].pageY;if(this.isDoubleTap=!1,i>1){var l=e.touches[1].pageX,o=e.touches[1].pageY,r={x:e.touches[1].pageX-n,y:e.touches[1].pageY-s};null!==t.x&&(this.pinchStartLen>0&&(e.zoom=Y(r)/this.pinchStartLen,this.pinch.dispatch(e,this.element)),e.angle=q(r,t),this.rotate.dispatch(e,this.element)),t.x=r.x,t.y=r.y,null!==this.x2&&null!==this.sx2?(e.deltaX=(n-this.x2+l-this.sx2)/2,e.deltaY=(s-this.y2+o-this.sy2)/2):(e.deltaX=0,e.deltaY=0),this.twoFingerPressMove.dispatch(e,this.element),this.sx2=l,this.sy2=o}else{if(null!==this.x2){e.deltaX=n-this.x2,e.deltaY=s-this.y2;var a=Math.abs(this.x1-this.x2),h=Math.abs(this.y1-this.y2);(a>10||h>10)&&(this._preventTap=!0)}else e.deltaX=0,e.deltaY=0;this.pressMove.dispatch(e,this.element)}this.touchMove.dispatch(e,this.element),this._cancelLongTap(),this.x2=n,this.y2=s,i>1&&e.preventDefault()}}},{key:"end",value:function(e){if(e.changedTouches){this._cancelLongTap();var t=this;e.touches.length<2&&(this.multipointEnd.dispatch(e,this.element),this.sx2=this.sy2=null),this.x2&&Math.abs(this.x1-this.x2)>30||this.y2&&Math.abs(this.y1-this.y2)>30?(e.direction=this._swipeDirection(this.x1,this.x2,this.y1,this.y2),this.swipeTimeout=setTimeout((function(){t.swipe.dispatch(e,t.element)}),0)):(this.tapTimeout=setTimeout((function(){t._preventTap||t.tap.dispatch(e,t.element),t.isDoubleTap&&(t.doubleTap.dispatch(e,t.element),t.isDoubleTap=!1)}),0),t.isDoubleTap||(t.singleTapTimeout=setTimeout((function(){t.singleTap.dispatch(e,t.element)}),250))),this.touchEnd.dispatch(e,this.element),this.preV.x=0,this.preV.y=0,this.zoom=1,this.pinchStartLen=null,this.x1=this.x2=this.y1=this.y2=null}}},{key:"cancelAll",value:function(){this._preventTap=!0,clearTimeout(this.singleTapTimeout),clearTimeout(this.tapTimeout),clearTimeout(this.longTapTimeout),clearTimeout(this.swipeTimeout)}},{key:"cancel",value:function(e){this.cancelAll(),this.touchCancel.dispatch(e,this.element)}},{key:"_cancelLongTap",value:function(){clearTimeout(this.longTapTimeout)}},{key:"_cancelSingleTap",value:function(){clearTimeout(this.singleTapTimeout)}},{key:"_swipeDirection",value:function(e,t,i,n){return Math.abs(e-t)>=Math.abs(i-n)?e-t>0?"Left":"Right":i-n>0?"Up":"Down"}},{key:"on",value:function(e,t){this[e]&&this[e].add(t)}},{key:"off",value:function(e,t){this[e]&&this[e].del(t)}},{key:"destroy",value:function(){return this.singleTapTimeout&&clearTimeout(this.singleTapTimeout),this.tapTimeout&&clearTimeout(this.tapTimeout),this.longTapTimeout&&clearTimeout(this.longTapTimeout),this.swipeTimeout&&clearTimeout(this.swipeTimeout),this.element.removeEventListener("touchstart",this.start),this.element.removeEventListener("touchmove",this.move),this.element.removeEventListener("touchend",this.end),this.element.removeEventListener("touchcancel",this.cancel),this.rotate.del(),this.touchStart.del(),this.multipointStart.del(),this.multipointEnd.del(),this.pinch.del(),this.swipe.del(),this.tap.del(),this.doubleTap.del(),this.longTap.del(),this.singleTap.del(),this.pressMove.del(),this.twoFingerPressMove.del(),this.touchMove.del(),this.touchEnd.del(),this.touchCancel.del(),this.preV=this.pinchStartLen=this.zoom=this.isDoubleTap=this.delta=this.last=this.now=this.tapTimeout=this.singleTapTimeout=this.longTapTimeout=this.swipeTimeout=this.x1=this.x2=this.y1=this.y2=this.preTapPosition=this.rotate=this.touchStart=this.multipointStart=this.multipointEnd=this.pinch=this.swipe=this.tap=this.doubleTap=this.longTap=this.singleTap=this.pressMove=this.touchMove=this.touchEnd=this.touchCancel=this.twoFingerPressMove=null,window.removeEventListener("scroll",this._cancelAllHandler),null}}]),e}();function W(e){var t=function(){var e,t=document.createElement("fakeelement"),i={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}(),i=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,n=c(e,"gslide-media")?e:e.querySelector(".gslide-media"),s=u(n,".ginner-container"),l=e.querySelector(".gslide-description");i>769&&(n=s),h(n,"greset"),v(n,"translate3d(0, 0, 0)"),a(t,{onElement:n,once:!0,withCallback:function(e,t){d(n,"greset")}}),n.style.opacity="",l&&(l.style.opacity="")}function B(e){if(e.events.hasOwnProperty("touch"))return!1;var t,i,n,s=y(),l=s.width,o=s.height,r=!1,a=null,g=null,f=null,p=!1,m=1,x=1,b=!1,S=!1,w=null,T=null,C=null,k=null,E=0,A=0,L=!1,I=!1,O={},P={},M=0,z=0,X=document.getElementById("glightbox-slider"),Y=document.querySelector(".goverlay"),q=new _(X,{touchStart:function(t){if(r=!0,(c(t.targetTouches[0].target,"ginner-container")||u(t.targetTouches[0].target,".gslide-desc")||"a"==t.targetTouches[0].target.nodeName.toLowerCase())&&(r=!1),u(t.targetTouches[0].target,".gslide-inline")&&!c(t.targetTouches[0].target.parentNode,"gslide-inline")&&(r=!1),r){if(P=t.targetTouches[0],O.pageX=t.targetTouches[0].pageX,O.pageY=t.targetTouches[0].pageY,M=t.targetTouches[0].clientX,z=t.targetTouches[0].clientY,a=e.activeSlide,g=a.querySelector(".gslide-media"),n=a.querySelector(".gslide-inline"),f=null,c(g,"gslide-image")&&(f=g.querySelector("img")),(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth)>769&&(g=a.querySelector(".ginner-container")),d(Y,"greset"),t.pageX>20&&t.pageXo){var a=O.pageX-P.pageX;if(Math.abs(a)<=13)return!1}p=!0;var h,d=s.targetTouches[0].clientX,c=s.targetTouches[0].clientY,u=M-d,m=z-c;if(Math.abs(u)>Math.abs(m)?(L=!1,I=!0):(I=!1,L=!0),t=P.pageX-O.pageX,E=100*t/l,i=P.pageY-O.pageY,A=100*i/o,L&&f&&(h=1-Math.abs(i)/o,Y.style.opacity=h,e.settings.touchFollowAxis&&(E=0)),I&&(h=1-Math.abs(t)/l,g.style.opacity=h,e.settings.touchFollowAxis&&(A=0)),!f)return v(g,"translate3d(".concat(E,"%, 0, 0)"));v(g,"translate3d(".concat(E,"%, ").concat(A,"%, 0)"))}},touchEnd:function(){if(r){if(p=!1,S||b)return C=w,void(k=T);var t=Math.abs(parseInt(A)),i=Math.abs(parseInt(E));if(!(t>29&&f))return t<29&&i<25?(h(Y,"greset"),Y.style.opacity=1,W(g)):void 0;e.close()}},multipointEnd:function(){setTimeout((function(){b=!1}),50)},multipointStart:function(){b=!0,m=x||1},pinch:function(e){if(!f||p)return!1;b=!0,f.scaleX=f.scaleY=m*e.zoom;var t=m*e.zoom;if(S=!0,t<=1)return S=!1,t=1,k=null,C=null,w=null,T=null,void f.setAttribute("style","");t>4.5&&(t=4.5),f.style.transform="scale3d(".concat(t,", ").concat(t,", 1)"),x=t},pressMove:function(e){if(S&&!b){var t=P.pageX-O.pageX,i=P.pageY-O.pageY;C&&(t+=C),k&&(i+=k),w=t,T=i;var n="translate3d(".concat(t,"px, ").concat(i,"px, 0)");x&&(n+=" scale3d(".concat(x,", ").concat(x,", 1)")),v(f,n)}},swipe:function(t){if(!S)if(b)b=!1;else{if("Left"==t.direction){if(e.index==e.elements.length-1)return W(g);e.nextSlide()}if("Right"==t.direction){if(0==e.index)return W(g);e.prevSlide()}}}});e.events.touch=q}var H=function(){function e(i,n){var s=this,l=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if(t(this,e),this.img=i,this.slide=n,this.onclose=l,this.img.setZoomEvents)return!1;this.active=!1,this.zoomedIn=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.img.addEventListener("mousedown",(function(e){return s.dragStart(e)}),!1),this.img.addEventListener("mouseup",(function(e){return s.dragEnd(e)}),!1),this.img.addEventListener("mousemove",(function(e){return s.drag(e)}),!1),this.img.addEventListener("click",(function(e){return s.slide.classList.contains("dragging-nav")?(s.zoomOut(),!1):s.zoomedIn?void(s.zoomedIn&&!s.dragging&&s.zoomOut()):s.zoomIn()}),!1),this.img.setZoomEvents=!0}return n(e,[{key:"zoomIn",value:function(){var e=this.widowWidth();if(!(this.zoomedIn||e<=768)){var t=this.img;if(t.setAttribute("data-style",t.getAttribute("style")),t.style.maxWidth=t.naturalWidth+"px",t.style.maxHeight=t.naturalHeight+"px",t.naturalWidth>e){var i=e/2-t.naturalWidth/2;this.setTranslate(this.img.parentNode,i,0)}this.slide.classList.add("zoomed"),this.zoomedIn=!0}}},{key:"zoomOut",value:function(){this.img.parentNode.setAttribute("style",""),this.img.setAttribute("style",this.img.getAttribute("data-style")),this.slide.classList.remove("zoomed"),this.zoomedIn=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.onclose&&"function"==typeof this.onclose&&this.onclose()}},{key:"dragStart",value:function(e){e.preventDefault(),this.zoomedIn?("touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset),e.target===this.img&&(this.active=!0,this.img.classList.add("dragging"))):this.active=!1}},{key:"dragEnd",value:function(e){var t=this;e.preventDefault(),this.initialX=this.currentX,this.initialY=this.currentY,this.active=!1,setTimeout((function(){t.dragging=!1,t.img.isDragging=!1,t.img.classList.remove("dragging")}),100)}},{key:"drag",value:function(e){this.active&&(e.preventDefault(),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.img.isDragging=!0,this.dragging=!0,this.setTranslate(this.img,this.currentX,this.currentY))}},{key:"onMove",value:function(e){if(this.zoomedIn){var t=e.clientX-this.img.naturalWidth/2,i=e.clientY-this.img.naturalHeight/2;this.setTranslate(this.img,t,i)}}},{key:"setTranslate",value:function(e,t,i){e.style.transform="translate3d("+t+"px, "+i+"px, 0)"}},{key:"widowWidth",value:function(){return window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth}}]),e}(),V=function(){function e(){var i=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e);var s=n.dragEl,l=n.toleranceX,o=void 0===l?40:l,r=n.toleranceY,a=void 0===r?65:r,h=n.slide,d=void 0===h?null:h,c=n.instance,u=void 0===c?null:c;this.el=s,this.active=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.direction=null,this.lastDirection=null,this.toleranceX=o,this.toleranceY=a,this.toleranceReached=!1,this.dragContainer=this.el,this.slide=d,this.instance=u,this.el.addEventListener("mousedown",(function(e){return i.dragStart(e)}),!1),this.el.addEventListener("mouseup",(function(e){return i.dragEnd(e)}),!1),this.el.addEventListener("mousemove",(function(e){return i.drag(e)}),!1)}return n(e,[{key:"dragStart",value:function(e){if(this.slide.classList.contains("zoomed"))this.active=!1;else{"touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset);var t=e.target.nodeName.toLowerCase();e.target.classList.contains("nodrag")||u(e.target,".nodrag")||-1!==["input","select","textarea","button","a"].indexOf(t)?this.active=!1:(e.preventDefault(),(e.target===this.el||"img"!==t&&u(e.target,".gslide-inline"))&&(this.active=!0,this.el.classList.add("dragging"),this.dragContainer=u(e.target,".ginner-container")))}}},{key:"dragEnd",value:function(e){var t=this;e&&e.preventDefault(),this.initialX=0,this.initialY=0,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.active=!1,this.doSlideChange&&(this.instance.preventOutsideClick=!0,"right"==this.doSlideChange&&this.instance.prevSlide(),"left"==this.doSlideChange&&this.instance.nextSlide()),this.doSlideClose&&this.instance.close(),this.toleranceReached||this.setTranslate(this.dragContainer,0,0,!0),setTimeout((function(){t.instance.preventOutsideClick=!1,t.toleranceReached=!1,t.lastDirection=null,t.dragging=!1,t.el.isDragging=!1,t.el.classList.remove("dragging"),t.slide.classList.remove("dragging-nav"),t.dragContainer.style.transform="",t.dragContainer.style.transition=""}),100)}},{key:"drag",value:function(e){if(this.active){e.preventDefault(),this.slide.classList.add("dragging-nav"),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.el.isDragging=!0,this.dragging=!0,this.doSlideChange=!1,this.doSlideClose=!1;var t=Math.abs(this.currentX),i=Math.abs(this.currentY);if(t>0&&t>=Math.abs(this.currentY)&&(!this.lastDirection||"x"==this.lastDirection)){this.yOffset=0,this.lastDirection="x",this.setTranslate(this.dragContainer,this.currentX,0);var n=this.shouldChange();if(!this.instance.settings.dragAutoSnap&&n&&(this.doSlideChange=n),this.instance.settings.dragAutoSnap&&n)return this.instance.preventOutsideClick=!0,this.toleranceReached=!0,this.active=!1,this.instance.preventOutsideClick=!0,this.dragEnd(null),"right"==n&&this.instance.prevSlide(),void("left"==n&&this.instance.nextSlide())}if(this.toleranceY>0&&i>0&&i>=t&&(!this.lastDirection||"y"==this.lastDirection)){this.xOffset=0,this.lastDirection="y",this.setTranslate(this.dragContainer,0,this.currentY);var s=this.shouldClose();return!this.instance.settings.dragAutoSnap&&s&&(this.doSlideClose=!0),void(this.instance.settings.dragAutoSnap&&s&&this.instance.close())}}}},{key:"shouldChange",value:function(){var e=!1;if(Math.abs(this.currentX)>=this.toleranceX){var t=this.currentX>0?"right":"left";("left"==t&&this.slide!==this.slide.parentNode.lastChild||"right"==t&&this.slide!==this.slide.parentNode.firstChild)&&(e=t)}return e}},{key:"shouldClose",value:function(){var e=!1;return Math.abs(this.currentY)>=this.toleranceY&&(e=!0),e}},{key:"setTranslate",value:function(e,t,i){var n=arguments.length>3&&void 0!==arguments[3]&&arguments[3];e.style.transition=n?"all .2s ease":"",e.style.transform="translate3d(".concat(t,"px, ").concat(i,"px, 0)")}}]),e}();function j(e,t,i,n){var s=e.querySelector(".gslide-media"),l=new Image,o="gSlideTitle_"+i,r="gSlideDesc_"+i;l.addEventListener("load",(function(){T(n)&&n()}),!1),l.src=t.href,""!=t.sizes&&""!=t.srcset&&(l.sizes=t.sizes,l.srcset=t.srcset),l.alt="",I(t.alt)||""===t.alt||(l.alt=t.alt),""!==t.title&&l.setAttribute("aria-labelledby",o),""!==t.description&&l.setAttribute("aria-describedby",r),t.hasOwnProperty("_hasCustomWidth")&&t._hasCustomWidth&&(l.style.width=t.width),t.hasOwnProperty("_hasCustomHeight")&&t._hasCustomHeight&&(l.style.height=t.height),s.insertBefore(l,s.firstChild)}function F(e,t,i,n){var s=this,l=e.querySelector(".ginner-container"),o="gvideo"+i,r=e.querySelector(".gslide-media"),a=this.getAllPlayers();h(l,"gvideo-container"),r.insertBefore(m('
'),r.firstChild);var d=e.querySelector(".gvideo-wrapper");S(this.settings.plyr.css,"Plyr");var c=t.href,u=location.protocol.replace(":",""),g="",v="",f=!1;"file"==u&&(u="http"),r.style.maxWidth=t.width,S(this.settings.plyr.js,"Plyr",(function(){if(c.match(/vimeo\.com\/([0-9]*)/)){var l=/vimeo.*\/(\d+)/i.exec(c);g="vimeo",v=l[1]}if(c.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||c.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||c.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/)){var r=function(e){var t="";t=void 0!==(e=e.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/))[2]?(t=e[2].split(/[^0-9a-z_\-]/i))[0]:e;return t}(c);g="youtube",v=r}if(null!==c.match(/\.(mp4|ogg|webm|mov)$/)){g="local";var u='")}var w=f||m('
'));h(d,"".concat(g,"-video gvideo")),d.appendChild(w),d.setAttribute("data-id",o),d.setAttribute("data-index",i);var C=O(s.settings.plyr,"config")?s.settings.plyr.config:{},k=new Plyr("#"+o,C);k.on("ready",(function(e){var t=e.detail.plyr;a[o]=t,T(n)&&n()})),b((function(){return e.querySelector("iframe")&&"true"==e.querySelector("iframe").dataset.ready}),(function(){s.resize(e)})),k.on("enterfullscreen",R),k.on("exitfullscreen",R)}))}function R(e){var t=u(e.target,".gslide-media");"enterfullscreen"==e.type&&h(t,"fullscreen"),"exitfullscreen"==e.type&&d(t,"fullscreen")}function G(e,t,i,n){var s,l=this,o=e.querySelector(".gslide-media"),r=!(!O(t,"href")||!t.href)&&t.href.split("#").pop().trim(),d=!(!O(t,"content")||!t.content)&&t.content;if(d&&(C(d)&&(s=m('
'.concat(d,"
"))),k(d))){"none"==d.style.display&&(d.style.display="block");var c=document.createElement("div");c.className="ginlined-content",c.appendChild(d),s=c}if(r){var u=document.getElementById(r);if(!u)return!1;var g=u.cloneNode(!0);g.style.height=t.height,g.style.maxWidth=t.width,h(g,"ginlined-content"),s=g}if(!s)return console.error("Unable to append inline slide content",t),!1;o.style.height=t.height,o.style.width=t.width,o.appendChild(s),this.events["inlineclose"+r]=a("click",{onElement:o.querySelectorAll(".gtrigger-close"),withCallback:function(e){e.preventDefault(),l.close()}}),T(n)&&n()}function Z(e,t,i,n){var s=e.querySelector(".gslide-media"),l=function(e){var t=e.url,i=e.allow,n=e.callback,s=e.appendTo,l=document.createElement("iframe");return l.className="vimeo-video gvideo",l.src=t,l.style.width="100%",l.style.height="100%",i&&l.setAttribute("allow",i),l.onload=function(){h(l,"node-ready"),T(n)&&n()},s&&s.appendChild(l),l}({url:t.href,callback:n});s.parentNode.style.maxWidth=t.width,s.parentNode.style.height=t.height,s.appendChild(l)}var $=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.defaults={href:"",sizes:"",srcset:"",title:"",type:"",description:"",alt:"",descPosition:"bottom",effect:"",width:"",height:"",content:!1,zoomable:!0,draggable:!0},L(i)&&(this.defaults=l(this.defaults,i))}return n(e,[{key:"sourceType",value:function(e){var t=e;if(null!==(e=e.toLowerCase()).match(/\.(jpeg|jpg|jpe|gif|png|apn|webp|avif|svg)/))return"image";if(e.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||e.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||e.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/))return"video";if(e.match(/vimeo\.com\/([0-9]*)/))return"video";if(null!==e.match(/\.(mp4|ogg|webm|mov)/))return"video";if(null!==e.match(/\.(mp3|wav|wma|aac|ogg)/))return"audio";if(e.indexOf("#")>-1&&""!==t.split("#").pop().trim())return"inline";return e.indexOf("goajax=true")>-1?"ajax":"external"}},{key:"parseConfig",value:function(e,t){var i=this,n=l({descPosition:t.descPosition},this.defaults);if(L(e)&&!k(e)){O(e,"type")||(O(e,"content")&&e.content?e.type="inline":O(e,"href")&&(e.type=this.sourceType(e.href)));var s=l(n,e);return this.setSize(s,t),s}var r="",a=e.getAttribute("data-glightbox"),h=e.nodeName.toLowerCase();if("a"===h&&(r=e.href),"img"===h&&(r=e.src,n.alt=e.alt),n.href=r,o(n,(function(s,l){O(t,l)&&"width"!==l&&(n[l]=t[l]);var o=e.dataset[l];I(o)||(n[l]=i.sanitizeValue(o))})),n.content&&(n.type="inline"),!n.type&&r&&(n.type=this.sourceType(r)),I(a)){if(!n.title&&"a"==h){var d=e.title;I(d)||""===d||(n.title=d)}if(!n.title&&"img"==h){var c=e.alt;I(c)||""===c||(n.title=c)}}else{var u=[];o(n,(function(e,t){u.push(";\\s?"+t)})),u=u.join("\\s?:|"),""!==a.trim()&&o(n,(function(e,t){var s=a,l=new RegExp("s?"+t+"s?:s?(.*?)("+u+"s?:|$)"),o=s.match(l);if(o&&o.length&&o[1]){var r=o[1].trim().replace(/;\s*$/,"");n[t]=i.sanitizeValue(r)}}))}if(n.description&&"."===n.description.substring(0,1)){var g;try{g=document.querySelector(n.description).innerHTML}catch(e){if(!(e instanceof DOMException))throw e}g&&(n.description=g)}if(!n.description){var v=e.querySelector(".glightbox-desc");v&&(n.description=v.innerHTML)}return this.setSize(n,t,e),this.slideConfig=n,n}},{key:"setSize",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n="video"==e.type?this.checkSize(t.videosWidth):this.checkSize(t.width),s=this.checkSize(t.height);return e.width=O(e,"width")&&""!==e.width?this.checkSize(e.width):n,e.height=O(e,"height")&&""!==e.height?this.checkSize(e.height):s,i&&"image"==e.type&&(e._hasCustomWidth=!!i.dataset.width,e._hasCustomHeight=!!i.dataset.height),e}},{key:"checkSize",value:function(e){return M(e)?"".concat(e,"px"):e}},{key:"sanitizeValue",value:function(e){return"true"!==e&&"false"!==e?e:"true"===e}}]),e}(),U=function(){function e(i,n,s){t(this,e),this.element=i,this.instance=n,this.index=s}return n(e,[{key:"setContent",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(c(t,"loaded"))return!1;var n=this.instance.settings,s=this.slideConfig,l=w();T(n.beforeSlideLoad)&&n.beforeSlideLoad({index:this.index,slide:t,player:!1});var o=s.type,r=s.descPosition,a=t.querySelector(".gslide-media"),d=t.querySelector(".gslide-title"),u=t.querySelector(".gslide-desc"),g=t.querySelector(".gdesc-inner"),v=i,f="gSlideTitle_"+this.index,p="gSlideDesc_"+this.index;if(T(n.afterSlideLoad)&&(v=function(){T(i)&&i(),n.afterSlideLoad({index:e.index,slide:t,player:e.instance.getSlidePlayerInstance(e.index)})}),""==s.title&&""==s.description?g&&g.parentNode.parentNode.removeChild(g.parentNode):(d&&""!==s.title?(d.id=f,d.innerHTML=s.title):d.parentNode.removeChild(d),u&&""!==s.description?(u.id=p,l&&n.moreLength>0?(s.smallDescription=this.slideShortDesc(s.description,n.moreLength,n.moreText),u.innerHTML=s.smallDescription,this.descriptionEvents(u,s)):u.innerHTML=s.description):u.parentNode.removeChild(u),h(a.parentNode,"desc-".concat(r)),h(g.parentNode,"description-".concat(r))),h(a,"gslide-".concat(o)),h(t,"loaded"),"video"!==o){if("external"!==o)return"inline"===o?(G.apply(this.instance,[t,s,this.index,v]),void(s.draggable&&new V({dragEl:t.querySelector(".gslide-inline"),toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:this.instance}))):void("image"!==o?T(v)&&v():j(t,s,this.index,(function(){var i=t.querySelector("img");s.draggable&&new V({dragEl:i,toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:e.instance}),s.zoomable&&i.naturalWidth>i.offsetWidth&&(h(i,"zoomable"),new H(i,t,(function(){e.instance.resize()}))),T(v)&&v()})));Z.apply(this,[t,s,this.index,v])}else F.apply(this.instance,[t,s,this.index,v])}},{key:"slideShortDesc",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:50,i=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=document.createElement("div");n.innerHTML=e;var s=n.innerText,l=i;if((e=s.trim()).length<=t)return e;var o=e.substr(0,t-1);return l?(n=null,o+'... '+i+""):o}},{key:"descriptionEvents",value:function(e,t){var i=this,n=e.querySelector(".desc-more");if(!n)return!1;a("click",{onElement:n,withCallback:function(e,n){e.preventDefault();var s=document.body,l=u(n,".gslide-desc");if(!l)return!1;l.innerHTML=t.description,h(s,"gdesc-open");var o=a("click",{onElement:[s,u(l,".gslide-description")],withCallback:function(e,n){"a"!==e.target.nodeName.toLowerCase()&&(d(s,"gdesc-open"),h(s,"gdesc-closed"),l.innerHTML=t.smallDescription,i.descriptionEvents(l,t),setTimeout((function(){d(s,"gdesc-closed")}),400),o.destroy())}})}})}},{key:"create",value:function(){return m(this.instance.settings.slideHTML)}},{key:"getConfig",value:function(){k(this.element)||this.element.hasOwnProperty("draggable")||(this.element.draggable=this.instance.settings.draggable);var e=new $(this.instance.settings.slideExtraAttributes);return this.slideConfig=e.parseConfig(this.element,this.instance.settings),this.slideConfig}}]),e}(),J=w(),K=null!==w()||void 0!==document.createTouch||"ontouchstart"in window||"onmsgesturechange"in window||navigator.msMaxTouchPoints,Q=document.getElementsByTagName("html")[0],ee={selector:".glightbox",elements:null,skin:"clean",theme:"clean",closeButton:!0,startAt:null,autoplayVideos:!0,autofocusVideos:!0,descPosition:"bottom",width:"900px",height:"506px",videosWidth:"960px",beforeSlideChange:null,afterSlideChange:null,beforeSlideLoad:null,afterSlideLoad:null,slideInserted:null,slideRemoved:null,slideExtraAttributes:null,onOpen:null,onClose:null,loop:!1,zoomable:!0,draggable:!0,dragAutoSnap:!1,dragToleranceX:40,dragToleranceY:65,preload:!0,oneSlidePerOpen:!1,touchNavigation:!0,touchFollowAxis:!0,keyboardNavigation:!0,closeOnOutsideClick:!0,plugins:!1,plyr:{css:"https://cdn.plyr.io/3.6.8/plyr.css",js:"https://cdn.plyr.io/3.6.8/plyr.js",config:{ratio:"16:9",fullscreen:{enabled:!0,iosNative:!0},youtube:{noCookie:!0,rel:0,showinfo:0,iv_load_policy:3},vimeo:{byline:!1,portrait:!1,title:!1,transparent:!1}}},openEffect:"zoom",closeEffect:"zoom",slideEffect:"slide",moreText:"See more",moreLength:60,cssEfects:{fade:{in:"fadeIn",out:"fadeOut"},zoom:{in:"zoomIn",out:"zoomOut"},slide:{in:"slideInRight",out:"slideOutLeft"},slideBack:{in:"slideInLeft",out:"slideOutRight"},none:{in:"none",out:"none"}},svg:{close:'',next:' ',prev:''},slideHTML:'
\n
\n
\n
\n
\n
\n
\n

\n
\n
\n
\n
\n
\n
',lightboxHTML:''},te=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.customOptions=i,this.settings=l(ee,i),this.effectsClasses=this.getAnimationClasses(),this.videoPlayers={},this.apiEvents=[],this.fullElementsList=!1}return n(e,[{key:"init",value:function(){var e=this,t=this.getSelector();t&&(this.baseEvents=a("click",{onElement:t,withCallback:function(t,i){t.preventDefault(),e.open(i)}})),this.elements=this.getElements()}},{key:"open",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(0==this.elements.length)return!1;this.activeSlide=null,this.prevActiveSlideIndex=null,this.prevActiveSlide=null;var i=M(t)?t:this.settings.startAt;if(k(e)){var n=e.getAttribute("data-gallery");n&&(this.fullElementsList=this.elements,this.elements=this.getGalleryElements(this.elements,n)),I(i)&&(i=this.getElementIndex(e))<0&&(i=0)}M(i)||(i=0),this.build(),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.in);var s=document.body,l=window.innerWidth-document.documentElement.clientWidth;if(l>0){var o=document.createElement("style");o.type="text/css",o.className="gcss-styles",o.innerText=".gscrollbar-fixer {margin-right: ".concat(l,"px}"),document.head.appendChild(o),h(s,"gscrollbar-fixer")}h(s,"glightbox-open"),h(Q,"glightbox-open"),J&&(h(document.body,"glightbox-mobile"),this.settings.slideEffect="slide"),this.showSlide(i,!0),1==this.elements.length?(h(this.prevButton,"glightbox-button-hidden"),h(this.nextButton,"glightbox-button-hidden")):(d(this.prevButton,"glightbox-button-hidden"),d(this.nextButton,"glightbox-button-hidden")),this.lightboxOpen=!0,this.trigger("open"),T(this.settings.onOpen)&&this.settings.onOpen(),K&&this.settings.touchNavigation&&B(this),this.settings.keyboardNavigation&&X(this)}},{key:"openAt",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;this.open(null,e)}},{key:"showSlide",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];f(this.loader),this.index=parseInt(t);var n=this.slidesContainer.querySelector(".current");n&&d(n,"current"),this.slideAnimateOut();var s=this.slidesContainer.querySelectorAll(".gslide")[t];if(c(s,"loaded"))this.slideAnimateIn(s,i),p(this.loader);else{f(this.loader);var l=this.elements[t],o={index:this.index,slide:s,slideNode:s,slideConfig:l.slideConfig,slideIndex:this.index,trigger:l.node,player:null};this.trigger("slide_before_load",o),l.instance.setContent(s,(function(){p(e.loader),e.resize(),e.slideAnimateIn(s,i),e.trigger("slide_after_load",o)}))}this.slideDescription=s.querySelector(".gslide-description"),this.slideDescriptionContained=this.slideDescription&&c(this.slideDescription.parentNode,"gslide-media"),this.settings.preload&&(this.preloadSlide(t+1),this.preloadSlide(t-1)),this.updateNavigationClasses(),this.activeSlide=s}},{key:"preloadSlide",value:function(e){var t=this;if(e<0||e>this.elements.length-1)return!1;if(I(this.elements[e]))return!1;var i=this.slidesContainer.querySelectorAll(".gslide")[e];if(c(i,"loaded"))return!1;var n=this.elements[e],s=n.type,l={index:e,slide:i,slideNode:i,slideConfig:n.slideConfig,slideIndex:e,trigger:n.node,player:null};this.trigger("slide_before_load",l),"video"==s||"external"==s?setTimeout((function(){n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}),200):n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}},{key:"prevSlide",value:function(){this.goToSlide(this.index-1)}},{key:"nextSlide",value:function(){this.goToSlide(this.index+1)}},{key:"goToSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(this.prevActiveSlide=this.activeSlide,this.prevActiveSlideIndex=this.index,!this.loop()&&(e<0||e>this.elements.length-1))return!1;e<0?e=this.elements.length-1:e>=this.elements.length&&(e=0),this.showSlide(e)}},{key:"insertSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1;t<0&&(t=this.elements.length);var i=new U(e,this,t),n=i.getConfig(),s=l({},n),o=i.create(),r=this.elements.length-1;s.index=t,s.node=!1,s.instance=i,s.slideConfig=n,this.elements.splice(t,0,s);var a=null,h=null;if(this.slidesContainer){if(t>r)this.slidesContainer.appendChild(o);else{var d=this.slidesContainer.querySelectorAll(".gslide")[t];this.slidesContainer.insertBefore(o,d)}(this.settings.preload&&0==this.index&&0==t||this.index-1==t||this.index+1==t)&&this.preloadSlide(t),0==this.index&&0==t&&(this.index=1),this.updateNavigationClasses(),a=this.slidesContainer.querySelectorAll(".gslide")[t],h=this.getSlidePlayerInstance(t),s.slideNode=a}this.trigger("slide_inserted",{index:t,slide:a,slideNode:a,slideConfig:n,slideIndex:t,trigger:null,player:h}),T(this.settings.slideInserted)&&this.settings.slideInserted({index:t,slide:a,player:h})}},{key:"removeSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1;if(e<0||e>this.elements.length-1)return!1;var t=this.slidesContainer&&this.slidesContainer.querySelectorAll(".gslide")[e];t&&(this.getActiveSlideIndex()==e&&(e==this.elements.length-1?this.prevSlide():this.nextSlide()),t.parentNode.removeChild(t)),this.elements.splice(e,1),this.trigger("slide_removed",e),T(this.settings.slideRemoved)&&this.settings.slideRemoved(e)}},{key:"slideAnimateIn",value:function(e,t){var i=this,n=e.querySelector(".gslide-media"),s=e.querySelector(".gslide-description"),l={index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlide,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},o={index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideConfig:this.elements[this.index].slideConfig,slideIndex:this.index,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)};if(n.offsetWidth>0&&s&&(p(s),s.style.display=""),d(e,this.effectsClasses),t)g(e,this.settings.cssEfects[this.settings.openEffect].in,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}));else{var r=this.settings.slideEffect,a="none"!==r?this.settings.cssEfects[r].in:r;this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(a=this.settings.cssEfects.slideBack.in),g(e,a,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}))}setTimeout((function(){i.resize(e)}),100),h(e,"current")}},{key:"slideAnimateOut",value:function(){if(!this.prevActiveSlide)return!1;var e=this.prevActiveSlide;d(e,this.effectsClasses),h(e,"prev");var t=this.settings.slideEffect,i="none"!==t?this.settings.cssEfects[t].out:t;this.slidePlayerPause(e),this.trigger("slide_before_change",{prev:{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlideIndex,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},current:{index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideIndex:this.index,slideConfig:this.elements[this.index].slideConfig,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)}}),T(this.settings.beforeSlideChange)&&this.settings.beforeSlideChange.apply(this,[{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},{index:this.index,slide:this.activeSlide,player:this.getSlidePlayerInstance(this.index)}]),this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(i=this.settings.cssEfects.slideBack.out),g(e,i,(function(){var t=e.querySelector(".ginner-container"),i=e.querySelector(".gslide-media"),n=e.querySelector(".gslide-description");t.style.transform="",i.style.transform="",d(i,"greset"),i.style.opacity="",n&&(n.style.opacity=""),d(e,"prev")}))}},{key:"getAllPlayers",value:function(){return this.videoPlayers}},{key:"getSlidePlayerInstance",value:function(e){var t="gvideo"+e,i=this.getAllPlayers();return!(!O(i,t)||!i[t])&&i[t]}},{key:"stopSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("stopSlideVideo is deprecated, use slidePlayerPause");var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"slidePlayerPause",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"playSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("playSlideVideo is deprecated, use slidePlayerPlay");var i=this.getSlidePlayerInstance(e);i&&!i.playing&&i.play()}},{key:"slidePlayerPlay",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&!i.playing&&(i.play(),this.settings.autofocusVideos&&i.elements.container.focus())}},{key:"setElements",value:function(e){var t=this;this.settings.elements=!1;var i=[];e&&e.length&&o(e,(function(e,n){var s=new U(e,t,n),o=s.getConfig(),r=l({},o);r.slideConfig=o,r.instance=s,r.index=n,i.push(r)})),this.elements=i,this.lightboxOpen&&(this.slidesContainer.innerHTML="",this.elements.length&&(o(this.elements,(function(){var e=m(t.settings.slideHTML);t.slidesContainer.appendChild(e)})),this.showSlide(0,!0)))}},{key:"getElementIndex",value:function(e){var t=!1;return o(this.elements,(function(i,n){if(O(i,"node")&&i.node==e)return t=n,!0})),t}},{key:"getElements",value:function(){var e=this,t=[];this.elements=this.elements?this.elements:[],!I(this.settings.elements)&&E(this.settings.elements)&&this.settings.elements.length&&o(this.settings.elements,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=!1,r.index=n,r.instance=s,r.slideConfig=o,t.push(r)}));var i=!1;return this.getSelector()&&(i=document.querySelectorAll(this.getSelector())),i?(o(i,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=i,r.index=n,r.instance=s,r.slideConfig=o,r.gallery=i.getAttribute("data-gallery"),t.push(r)})),t):t}},{key:"getGalleryElements",value:function(e,t){return e.filter((function(e){return e.gallery==t}))}},{key:"getSelector",value:function(){return!this.settings.elements&&(this.settings.selector&&"data-"==this.settings.selector.substring(0,5)?"*[".concat(this.settings.selector,"]"):this.settings.selector)}},{key:"getActiveSlide",value:function(){return this.slidesContainer.querySelectorAll(".gslide")[this.index]}},{key:"getActiveSlideIndex",value:function(){return this.index}},{key:"getAnimationClasses",value:function(){var e=[];for(var t in this.settings.cssEfects)if(this.settings.cssEfects.hasOwnProperty(t)){var i=this.settings.cssEfects[t];e.push("g".concat(i.in)),e.push("g".concat(i.out))}return e.join(" ")}},{key:"build",value:function(){var e=this;if(this.built)return!1;var t=document.body.childNodes,i=[];o(t,(function(e){e.parentNode==document.body&&"#"!==e.nodeName.charAt(0)&&e.hasAttribute&&!e.hasAttribute("aria-hidden")&&(i.push(e),e.setAttribute("aria-hidden","true"))}));var n=O(this.settings.svg,"next")?this.settings.svg.next:"",s=O(this.settings.svg,"prev")?this.settings.svg.prev:"",l=O(this.settings.svg,"close")?this.settings.svg.close:"",r=this.settings.lightboxHTML;r=m(r=(r=(r=r.replace(/{nextSVG}/g,n)).replace(/{prevSVG}/g,s)).replace(/{closeSVG}/g,l)),document.body.appendChild(r);var d=document.getElementById("glightbox-body");this.modal=d;var g=d.querySelector(".gclose");this.prevButton=d.querySelector(".gprev"),this.nextButton=d.querySelector(".gnext"),this.overlay=d.querySelector(".goverlay"),this.loader=d.querySelector(".gloader"),this.slidesContainer=document.getElementById("glightbox-slider"),this.bodyHiddenChildElms=i,this.events={},h(this.modal,"glightbox-"+this.settings.skin),this.settings.closeButton&&g&&(this.events.close=a("click",{onElement:g,withCallback:function(t,i){t.preventDefault(),e.close()}})),g&&!this.settings.closeButton&&g.parentNode.removeChild(g),this.nextButton&&(this.events.next=a("click",{onElement:this.nextButton,withCallback:function(t,i){t.preventDefault(),e.nextSlide()}})),this.prevButton&&(this.events.prev=a("click",{onElement:this.prevButton,withCallback:function(t,i){t.preventDefault(),e.prevSlide()}})),this.settings.closeOnOutsideClick&&(this.events.outClose=a("click",{onElement:d,withCallback:function(t,i){e.preventOutsideClick||c(document.body,"glightbox-mobile")||u(t.target,".ginner-container")||u(t.target,".gbtn")||c(t.target,"gnext")||c(t.target,"gprev")||e.close()}})),o(this.elements,(function(t,i){e.slidesContainer.appendChild(t.instance.create()),t.slideNode=e.slidesContainer.querySelectorAll(".gslide")[i]})),K&&h(document.body,"glightbox-touch"),this.events.resize=a("resize",{onElement:window,withCallback:function(){e.resize()}}),this.built=!0}},{key:"resize",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;if((e=e||this.activeSlide)&&!c(e,"zoomed")){var t=y(),i=e.querySelector(".gvideo-wrapper"),n=e.querySelector(".gslide-image"),s=this.slideDescription,l=t.width,o=t.height;if(l<=768?h(document.body,"glightbox-mobile"):d(document.body,"glightbox-mobile"),i||n){var r=!1;if(s&&(c(s,"description-bottom")||c(s,"description-top"))&&!c(s,"gabsolute")&&(r=!0),n)if(l<=768)n.querySelector("img");else if(r){var a=s.offsetHeight,u=n.querySelector("img");u.setAttribute("style","max-height: calc(100vh - ".concat(a,"px)")),s.setAttribute("style","max-width: ".concat(u.offsetWidth,"px;"))}if(i){var g=O(this.settings.plyr.config,"ratio")?this.settings.plyr.config.ratio:"";if(!g){var v=i.clientWidth,f=i.clientHeight,p=v/f;g="".concat(v/p,":").concat(f/p)}var m=g.split(":"),x=this.settings.videosWidth,b=this.settings.videosWidth,S=(b=M(x)||-1!==x.indexOf("px")?parseInt(x):-1!==x.indexOf("vw")?l*parseInt(x)/100:-1!==x.indexOf("vh")?o*parseInt(x)/100:-1!==x.indexOf("%")?l*parseInt(x)/100:parseInt(i.clientWidth))/(parseInt(m[0])/parseInt(m[1]));if(S=Math.floor(S),r&&(o-=s.offsetHeight),b>l||S>o||ob){var w=i.offsetWidth,T=i.offsetHeight,C=o/T,k={width:w*C,height:T*C};i.parentNode.setAttribute("style","max-width: ".concat(k.width,"px")),r&&s.setAttribute("style","max-width: ".concat(k.width,"px;"))}else i.parentNode.style.maxWidth="".concat(x),r&&s.setAttribute("style","max-width: ".concat(x,";"))}}}}},{key:"reload",value:function(){this.init()}},{key:"updateNavigationClasses",value:function(){var e=this.loop();d(this.nextButton,"disabled"),d(this.prevButton,"disabled"),0==this.index&&this.elements.length-1==0?(h(this.prevButton,"disabled"),h(this.nextButton,"disabled")):0!==this.index||e?this.index!==this.elements.length-1||e||h(this.nextButton,"disabled"):h(this.prevButton,"disabled")}},{key:"loop",value:function(){var e=O(this.settings,"loopAtEnd")?this.settings.loopAtEnd:null;return e=O(this.settings,"loop")?this.settings.loop:e,e}},{key:"close",value:function(){var e=this;if(!this.lightboxOpen){if(this.events){for(var t in this.events)this.events.hasOwnProperty(t)&&this.events[t].destroy();this.events=null}return!1}if(this.closing)return!1;this.closing=!0,this.slidePlayerPause(this.activeSlide),this.fullElementsList&&(this.elements=this.fullElementsList),this.bodyHiddenChildElms.length&&o(this.bodyHiddenChildElms,(function(e){e.removeAttribute("aria-hidden")})),h(this.modal,"glightbox-closing"),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.out),g(this.activeSlide,this.settings.cssEfects[this.settings.closeEffect].out,(function(){if(e.activeSlide=null,e.prevActiveSlideIndex=null,e.prevActiveSlide=null,e.built=!1,e.events){for(var t in e.events)e.events.hasOwnProperty(t)&&e.events[t].destroy();e.events=null}var i=document.body;d(Q,"glightbox-open"),d(i,"glightbox-open touching gdesc-open glightbox-touch glightbox-mobile gscrollbar-fixer"),e.modal.parentNode.removeChild(e.modal),e.trigger("close"),T(e.settings.onClose)&&e.settings.onClose();var n=document.querySelector(".gcss-styles");n&&n.parentNode.removeChild(n),e.lightboxOpen=!1,e.closing=null}))}},{key:"destroy",value:function(){this.close(),this.clearAllEvents(),this.baseEvents&&this.baseEvents.destroy()}},{key:"on",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||!T(t))throw new TypeError("Event name and callback must be defined");this.apiEvents.push({evt:e,once:i,callback:t})}},{key:"once",value:function(e,t){this.on(e,t,!0)}},{key:"trigger",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=[];o(this.apiEvents,(function(t,s){var l=t.evt,o=t.once,r=t.callback;l==e&&(r(i),o&&n.push(s))})),n.length&&o(n,(function(e){return t.apiEvents.splice(e,1)}))}},{key:"clearAllEvents",value:function(){this.apiEvents.splice(0,this.apiEvents.length)}},{key:"version",value:function(){return"3.1.1"}}]),e}();return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=new te(e);return t.init(),t}})); diff --git a/assets/stylesheets/glightbox.min.css b/assets/stylesheets/glightbox.min.css new file mode 100644 index 000000000..3c9ff8775 --- /dev/null +++ b/assets/stylesheets/glightbox.min.css @@ -0,0 +1 @@ +.glightbox-container{width:100%;height:100%;position:fixed;top:0;left:0;z-index:999999!important;overflow:hidden;-ms-touch-action:none;touch-action:none;-webkit-text-size-adjust:100%;-moz-text-size-adjust:100%;-ms-text-size-adjust:100%;text-size-adjust:100%;-webkit-backface-visibility:hidden;backface-visibility:hidden;outline:0}.glightbox-container.inactive{display:none}.glightbox-container .gcontainer{position:relative;width:100%;height:100%;z-index:9999;overflow:hidden}.glightbox-container .gslider{-webkit-transition:-webkit-transform .4s ease;transition:-webkit-transform .4s ease;transition:transform .4s ease;transition:transform .4s ease,-webkit-transform .4s ease;height:100%;left:0;top:0;width:100%;position:relative;overflow:hidden;display:-webkit-box!important;display:-ms-flexbox!important;display:flex!important;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}.glightbox-container .gslide{width:100%;position:absolute;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;opacity:0}.glightbox-container .gslide.current{opacity:1;z-index:99999;position:relative}.glightbox-container .gslide.prev{opacity:1;z-index:9999}.glightbox-container .gslide-inner-content{width:100%}.glightbox-container .ginner-container{position:relative;width:100%;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;max-width:100%;margin:auto;height:100vh}.glightbox-container .ginner-container.gvideo-container{width:100%}.glightbox-container .ginner-container.desc-bottom,.glightbox-container .ginner-container.desc-top{-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.glightbox-container .ginner-container.desc-left,.glightbox-container .ginner-container.desc-right{max-width:100%!important}.gslide iframe,.gslide video{outline:0!important;border:none;min-height:165px;-webkit-overflow-scrolling:touch;-ms-touch-action:auto;touch-action:auto}.gslide:not(.current){pointer-events:none}.gslide-image{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.gslide-image img{max-height:100vh;display:block;padding:0;float:none;outline:0;border:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;max-width:100vw;width:auto;height:auto;-o-object-fit:cover;object-fit:cover;-ms-touch-action:none;touch-action:none;margin:auto;min-width:200px}.desc-bottom .gslide-image img,.desc-top .gslide-image img{width:auto}.desc-left .gslide-image img,.desc-right .gslide-image img{width:auto;max-width:100%}.gslide-image img.zoomable{position:relative}.gslide-image img.dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.gslide-video{position:relative;max-width:100vh;width:100%!important}.gslide-video .plyr__poster-enabled.plyr--loading .plyr__poster{display:none}.gslide-video .gvideo-wrapper{width:100%;margin:auto}.gslide-video::before{content:'';position:absolute;width:100%;height:100%;background:rgba(255,0,0,.34);display:none}.gslide-video.playing::before{display:none}.gslide-video.fullscreen{max-width:100%!important;min-width:100%;height:75vh}.gslide-video.fullscreen video{max-width:100%!important;width:100%!important}.gslide-inline{background:#fff;text-align:left;max-height:calc(100vh - 40px);overflow:auto;max-width:100%;margin:auto}.gslide-inline .ginlined-content{padding:20px;width:100%}.gslide-inline .dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.ginlined-content{overflow:auto;display:block!important;opacity:1}.gslide-external{display:-webkit-box;display:-ms-flexbox;display:flex;width:100%;min-width:100%;background:#fff;padding:0;overflow:auto;max-height:75vh;height:100%}.gslide-media{display:-webkit-box;display:-ms-flexbox;display:flex;width:auto}.zoomed .gslide-media{-webkit-box-shadow:none!important;box-shadow:none!important}.desc-bottom .gslide-media,.desc-top .gslide-media{margin:0 auto;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gslide-description{position:relative;-webkit-box-flex:1;-ms-flex:1 0 100%;flex:1 0 100%}.gslide-description.description-left,.gslide-description.description-right{max-width:100%}.gslide-description.description-bottom,.gslide-description.description-top{margin:0 auto;width:100%}.gslide-description p{margin-bottom:12px}.gslide-description p:last-child{margin-bottom:0}.zoomed .gslide-description{display:none}.glightbox-button-hidden{display:none}.glightbox-mobile .glightbox-container .gslide-description{height:auto!important;width:100%;position:absolute;bottom:0;padding:19px 11px;max-width:100vw!important;-webkit-box-ordinal-group:3!important;-ms-flex-order:2!important;order:2!important;max-height:78vh;overflow:auto!important;background:-webkit-gradient(linear,left top,left bottom,from(rgba(0,0,0,0)),to(rgba(0,0,0,.75)));background:linear-gradient(to bottom,rgba(0,0,0,0) 0,rgba(0,0,0,.75) 100%);-webkit-transition:opacity .3s linear;transition:opacity .3s linear;padding-bottom:50px}.glightbox-mobile .glightbox-container .gslide-title{color:#fff;font-size:1em}.glightbox-mobile .glightbox-container .gslide-desc{color:#a1a1a1}.glightbox-mobile .glightbox-container .gslide-desc a{color:#fff;font-weight:700}.glightbox-mobile .glightbox-container .gslide-desc *{color:inherit}.glightbox-mobile .glightbox-container .gslide-desc .desc-more{color:#fff;opacity:.4}.gdesc-open .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:.4}.gdesc-open .gdesc-inner{padding-bottom:30px}.gdesc-closed .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:1}.greset{-webkit-transition:all .3s ease;transition:all .3s ease}.gabsolute{position:absolute}.grelative{position:relative}.glightbox-desc{display:none!important}.glightbox-open{overflow:hidden}.gloader{height:25px;width:25px;-webkit-animation:lightboxLoader .8s infinite linear;animation:lightboxLoader .8s infinite linear;border:2px solid #fff;border-right-color:transparent;border-radius:50%;position:absolute;display:block;z-index:9999;left:0;right:0;margin:0 auto;top:47%}.goverlay{width:100%;height:calc(100vh + 1px);position:fixed;top:-1px;left:0;background:#000;will-change:opacity}.glightbox-mobile .goverlay{background:#000}.gclose,.gnext,.gprev{z-index:99999;cursor:pointer;width:26px;height:44px;border:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gclose svg,.gnext svg,.gprev svg{display:block;width:25px;height:auto;margin:0;padding:0}.gclose.disabled,.gnext.disabled,.gprev.disabled{opacity:.1}.gclose .garrow,.gnext .garrow,.gprev .garrow{stroke:#fff}.gbtn.focused{outline:2px solid #0f3d81}iframe.wait-autoplay{opacity:0}.glightbox-closing .gclose,.glightbox-closing .gnext,.glightbox-closing .gprev{opacity:0!important}.glightbox-clean .gslide-description{background:#fff}.glightbox-clean .gdesc-inner{padding:22px 20px}.glightbox-clean .gslide-title{font-size:1em;font-weight:400;font-family:arial;color:#000;margin-bottom:19px;line-height:1.4em}.glightbox-clean .gslide-desc{font-size:.86em;margin-bottom:0;font-family:arial;line-height:1.4em}.glightbox-clean .gslide-video{background:#000}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.75);border-radius:4px}.glightbox-clean .gclose path,.glightbox-clean .gnext path,.glightbox-clean .gprev path{fill:#fff}.glightbox-clean .gprev{position:absolute;top:-100%;left:30px;width:40px;height:50px}.glightbox-clean .gnext{position:absolute;top:-100%;right:30px;width:40px;height:50px}.glightbox-clean .gclose{width:35px;height:35px;top:15px;right:10px;position:absolute}.glightbox-clean .gclose svg{width:18px;height:auto}.glightbox-clean .gclose:hover{opacity:1}.gfadeIn{-webkit-animation:gfadeIn .5s ease;animation:gfadeIn .5s ease}.gfadeOut{-webkit-animation:gfadeOut .5s ease;animation:gfadeOut .5s ease}.gslideOutLeft{-webkit-animation:gslideOutLeft .3s ease;animation:gslideOutLeft .3s ease}.gslideInLeft{-webkit-animation:gslideInLeft .3s ease;animation:gslideInLeft .3s ease}.gslideOutRight{-webkit-animation:gslideOutRight .3s ease;animation:gslideOutRight .3s ease}.gslideInRight{-webkit-animation:gslideInRight .3s ease;animation:gslideInRight .3s ease}.gzoomIn{-webkit-animation:gzoomIn .5s ease;animation:gzoomIn .5s ease}.gzoomOut{-webkit-animation:gzoomOut .5s ease;animation:gzoomOut .5s ease}@-webkit-keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@-webkit-keyframes gfadeIn{from{opacity:0}to{opacity:1}}@keyframes gfadeIn{from{opacity:0}to{opacity:1}}@-webkit-keyframes gfadeOut{from{opacity:1}to{opacity:0}}@keyframes gfadeOut{from{opacity:1}to{opacity:0}}@-webkit-keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@-webkit-keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@-webkit-keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@-webkit-keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@media (min-width:769px){.glightbox-container .ginner-container{width:auto;height:auto;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row}.glightbox-container .ginner-container.desc-top .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-top .gslide-image,.glightbox-container .ginner-container.desc-top .gslide-image img{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.glightbox-container .ginner-container.desc-left .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-left .gslide-image{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.gslide-image img{max-height:97vh;max-width:100%}.gslide-image img.zoomable{cursor:-webkit-zoom-in;cursor:zoom-in}.zoomed .gslide-image img.zoomable{cursor:-webkit-grab;cursor:grab}.gslide-inline{max-height:95vh}.gslide-external{max-height:100vh}.gslide-description.description-left,.gslide-description.description-right{max-width:275px}.glightbox-open{height:auto}.goverlay{background:rgba(0,0,0,.92)}.glightbox-clean .gslide-media{-webkit-box-shadow:1px 2px 9px 0 rgba(0,0,0,.65);box-shadow:1px 2px 9px 0 rgba(0,0,0,.65)}.glightbox-clean .description-left .gdesc-inner,.glightbox-clean .description-right .gdesc-inner{position:absolute;height:100%;overflow-y:auto}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.32)}.glightbox-clean .gclose:hover,.glightbox-clean .gnext:hover,.glightbox-clean .gprev:hover{background-color:rgba(0,0,0,.7)}.glightbox-clean .gprev{top:45%}.glightbox-clean .gnext{top:45%}}@media (min-width:992px){.glightbox-clean .gclose{opacity:.7;right:20px}}@media screen and (max-height:420px){.goverlay{background:#000}} \ No newline at end of file diff --git a/chapter_appendix/contribution/index.html b/chapter_appendix/contribution/index.html index ddfc295c1..b6bd47556 100644 --- a/chapter_appendix/contribution/index.html +++ b/chapter_appendix/contribution/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2993,14 +2876,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3032,14 +2907,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3060,14 +2927,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3088,14 +2947,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3116,14 +2967,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3144,14 +2987,6 @@ 15.5   小结 - - - - - - - - @@ -3502,7 +3337,7 @@
  • 修改 Markdown 源文件内容,检查内容的正确性,并尽量保持排版格式的统一。
  • 在页面底部填写修改说明,然后点击“Propose file change”按钮。页面跳转后,点击“Create pull request”按钮即可发起拉取请求。
  • -

    页面编辑按键

    +

    页面编辑按键

    图 16-1   页面编辑按键

    图片无法直接修改,需要通过新建 Issue 或评论留言来描述问题,我们会尽快重新绘制并替换图片。

    @@ -3685,10 +3520,15 @@ aria-label="页脚" @@ -3757,5 +3597,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_appendix/index.html b/chapter_appendix/index.html index 0bbfeacca..d3de15332 100644 --- a/chapter_appendix/index.html +++ b/chapter_appendix/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2993,14 +2876,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3032,14 +2907,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3060,14 +2927,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3088,14 +2947,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3116,14 +2967,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3144,14 +2987,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 16 章   附录

    -

    附录

    +

    附录

    本章内容

      @@ -3584,10 +3419,15 @@ aria-label="页脚" @@ -3656,5 +3496,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_appendix/installation/index.html b/chapter_appendix/installation/index.html index 3fa6b0219..1e26a8435 100644 --- a/chapter_appendix/installation/index.html +++ b/chapter_appendix/installation/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2993,14 +2876,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3032,14 +2907,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3060,14 +2927,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3088,14 +2947,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3116,14 +2967,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3144,14 +2987,6 @@ 15.5   小结 - - - - - - - - @@ -3801,10 +3636,15 @@ aria-label="页脚" @@ -3873,5 +3713,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_array_and_linkedlist/array/index.html b/chapter_array_and_linkedlist/array/index.html index 51d8855be..2054c41fe 100644 --- a/chapter_array_and_linkedlist/array/index.html +++ b/chapter_array_and_linkedlist/array/index.html @@ -60,7 +60,18 @@ - + @@ -1993,14 +2004,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2021,14 +2024,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2448,14 +2443,6 @@ 第 12 章   分治 - - - - - - - - @@ -2487,14 +2474,6 @@ 12.1   分治算法 - - - - - - - - @@ -2515,14 +2494,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2543,14 +2514,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2571,14 +2534,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2599,14 +2554,6 @@ 12.5   小结 - - - - - - - - @@ -2838,14 +2785,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2877,14 +2816,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2905,14 +2836,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2933,14 +2856,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2961,14 +2876,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2989,14 +2896,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3017,14 +2916,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3045,14 +2936,6 @@ 14.7   小结 - - - - - - - - @@ -3111,14 +2994,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3150,14 +3025,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3178,14 +3045,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3206,14 +3065,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3234,14 +3085,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3262,14 +3105,6 @@ 15.5   小结 - - - - - - - - @@ -3599,7 +3434,7 @@

      4.1   数组

      「数组 array」是一种线性数据结构,其将相同类型元素存储在连续的内存空间中。我们将元素在数组中的位置称为该元素的「索引 index」。图 4-1 展示了数组的主要术语和概念。

      -

      数组定义与存储方式

      +

      数组定义与存储方式

      图 4-1   数组定义与存储方式

      4.1.1   数组常用操作

      @@ -3689,7 +3524,7 @@

      2.   访问元素

      数组元素被存储在连续的内存空间中,这意味着计算数组元素的内存地址非常容易。给定数组内存地址(即首元素内存地址)和某个元素的索引,我们可以使用图 4-2 所示的公式计算得到该元素的内存地址,从而直接访问此元素。

      -

      数组元素的内存地址计算

      +

      数组元素的内存地址计算

      图 4-2   数组元素的内存地址计算

      观察图 4-2 ,我们发现数组首个元素的索引为 \(0\) ,这似乎有些反直觉,因为从 \(1\) 开始计数会更自然。但从地址计算公式的角度看,索引的含义本质上是内存地址的偏移量。首个元素的地址偏移量是 \(0\) ,因此它的索引为 \(0\) 也是合理的。

      @@ -3832,7 +3667,7 @@

      3.   插入元素

      数组元素在内存中是“紧挨着的”,它们之间没有空间再存放任何数据。如图 4-3 所示,如果想要在数组中间插入一个元素,则需要将该元素之后的所有元素都向后移动一位,之后再把元素赋值给该索引。

      -

      数组插入元素示例

      +

      数组插入元素示例

      图 4-3   数组插入元素示例

      值得注意的是,由于数组的长度是固定的,因此插入一个元素必定会导致数组尾部元素的“丢失”。我们将这个问题的解决方案留在列表章节中讨论。

      @@ -3985,7 +3820,7 @@

      4.   删除元素

      同理,如图 4-4 所示,若想要删除索引 \(i\) 处的元素,则需要把索引 \(i\) 之后的元素都向前移动一位。

      -

      数组删除元素示例

      +

      数组删除元素示例

      图 4-4   数组删除元素示例

      请注意,删除元素完成后,原先末尾的元素变得“无意义”了,所以我们无须特意去修改它。

      @@ -4812,10 +4647,15 @@ aria-label="页脚" @@ -4884,5 +4724,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_array_and_linkedlist/index.html b/chapter_array_and_linkedlist/index.html index 53d577c17..015aa1075 100644 --- a/chapter_array_and_linkedlist/index.html +++ b/chapter_array_and_linkedlist/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

      第 4 章   数组与链表

      -

      数组与链表

      +

      数组与链表

      Abstract

      @@ -3591,10 +3426,15 @@ aria-label="页脚" @@ -3663,5 +3503,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_array_and_linkedlist/linked_list/index.html b/chapter_array_and_linkedlist/linked_list/index.html index 94d54e27f..4aedbaea3 100644 --- a/chapter_array_and_linkedlist/linked_list/index.html +++ b/chapter_array_and_linkedlist/linked_list/index.html @@ -60,7 +60,18 @@ - + @@ -1986,14 +1997,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2014,14 +2017,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2441,14 +2436,6 @@ 第 12 章   分治 - - - - - - - - @@ -2480,14 +2467,6 @@ 12.1   分治算法 - - - - - - - - @@ -2508,14 +2487,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2536,14 +2507,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2564,14 +2527,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2592,14 +2547,6 @@ 12.5   小结 - - - - - - - - @@ -2831,14 +2778,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2870,14 +2809,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2898,14 +2829,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2926,14 +2849,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2954,14 +2869,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2982,14 +2889,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3010,14 +2909,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3038,14 +2929,6 @@ 14.7   小结 - - - - - - - - @@ -3104,14 +2987,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3143,14 +3018,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3171,14 +3038,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3199,14 +3058,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3227,14 +3078,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3255,14 +3098,6 @@ 15.5   小结 - - - - - - - - @@ -3587,7 +3422,7 @@

      内存空间是所有程序的公共资源,在一个复杂的系统运行环境下,空闲的内存空间可能散落在内存各处。我们知道,存储数组的内存空间必须是连续的,而当数组非常大时,内存可能无法提供如此大的连续空间。此时链表的灵活性优势就体现出来了。

      「链表 linked list」是一种线性数据结构,其中的每个元素都是一个节点对象,各个节点通过“引用”相连接。引用记录了下一个节点的内存地址,通过它可以从当前节点访问到下一个节点。

      链表的设计使得各个节点可以被分散存储在内存各处,它们的内存地址是无须连续的。

      -

      链表定义与存储方式

      +

      链表定义与存储方式

      图 4-5   链表定义与存储方式

      观察图 4-5 ,链表的组成单位是「节点 node」对象。每个节点都包含两项数据:节点的“值”和指向下一节点的“引用”。

      @@ -3937,7 +3772,7 @@

      2.   插入节点

      在链表中插入节点非常容易。如图 4-6 所示,假设我们想在相邻的两个节点 n0n1 之间插入一个新节点 P则只需要改变两个节点引用(指针)即可,时间复杂度为 \(O(1)\)

      相比之下,在数组中插入元素的时间复杂度为 \(O(n)\) ,在大数据量下的效率较低。

      -

      链表插入节点示例

      +

      链表插入节点示例

      图 4-6   链表插入节点示例

      @@ -4055,7 +3890,7 @@

      3.   删除节点

      如图 4-7 所示,在链表中删除节点也非常方便,只需改变一个节点的引用(指针)即可

      请注意,尽管在删除操作完成后节点 P 仍然指向 n1 ,但实际上遍历此链表已经无法访问到 P ,这意味着 P 已经不再属于该链表了。

      -

      链表删除节点

      +

      链表删除节点

      图 4-7   链表删除节点

      @@ -4775,7 +4610,7 @@
      -

      常见链表种类

      +

      常见链表种类

      图 4-8   常见链表种类

      4.2.4   链表典型应用

      @@ -4959,10 +4794,15 @@ aria-label="页脚" @@ -5031,5 +4871,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_array_and_linkedlist/list/index.html b/chapter_array_and_linkedlist/list/index.html index 3c46cafa8..af207b437 100644 --- a/chapter_array_and_linkedlist/list/index.html +++ b/chapter_array_and_linkedlist/list/index.html @@ -60,7 +60,18 @@ - + @@ -1979,14 +1990,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2007,14 +2010,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2434,14 +2429,6 @@ 第 12 章   分治 - - - - - - - - @@ -2473,14 +2460,6 @@ 12.1   分治算法 - - - - - - - - @@ -2501,14 +2480,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2529,14 +2500,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2557,14 +2520,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2585,14 +2540,6 @@ 12.5   小结 - - - - - - - - @@ -2824,14 +2771,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2863,14 +2802,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2891,14 +2822,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2919,14 +2842,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2947,14 +2862,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2975,14 +2882,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3003,14 +2902,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3031,14 +2922,6 @@ 14.7   小结 - - - - - - - - @@ -3097,14 +2980,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3136,14 +3011,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3164,14 +3031,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3192,14 +3051,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3220,14 +3071,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3248,14 +3091,6 @@ 15.5   小结 - - - - - - - - @@ -5688,10 +5523,15 @@ aria-label="页脚" @@ -5760,5 +5600,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_array_and_linkedlist/summary/index.html b/chapter_array_and_linkedlist/summary/index.html index 458cb3375..be7834cd3 100644 --- a/chapter_array_and_linkedlist/summary/index.html +++ b/chapter_array_and_linkedlist/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3700,10 +3535,15 @@ aria-label="页脚" @@ -3772,5 +3612,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_backtracking/backtracking_algorithm/index.html b/chapter_backtracking/backtracking_algorithm/index.html index e7f26a9f0..9310c0110 100644 --- a/chapter_backtracking/backtracking_algorithm/index.html +++ b/chapter_backtracking/backtracking_algorithm/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2804,14 +2751,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2843,14 +2782,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2871,14 +2802,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2899,14 +2822,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2927,14 +2842,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2955,14 +2862,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2983,14 +2882,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3011,14 +2902,6 @@ 14.7   小结 - - - - - - - - @@ -3077,14 +2960,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3116,14 +2991,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3144,14 +3011,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3172,14 +3031,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3200,14 +3051,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3228,14 +3071,6 @@ 15.5   小结 - - - - - - - - @@ -3698,7 +3533,7 @@ -

      在前序遍历中搜索节点

      +

      在前序遍历中搜索节点

      图 13-1   在前序遍历中搜索节点

      13.1.1   尝试与回退

      @@ -3946,37 +3781,37 @@
      -

      尝试与回退

      +

      尝试与回退

      -

      preorder_find_paths_step2

      +

      preorder_find_paths_step2

      -

      preorder_find_paths_step3

      +

      preorder_find_paths_step3

      -

      preorder_find_paths_step4

      +

      preorder_find_paths_step4

      -

      preorder_find_paths_step5

      +

      preorder_find_paths_step5

      -

      preorder_find_paths_step6

      +

      preorder_find_paths_step6

      -

      preorder_find_paths_step7

      +

      preorder_find_paths_step7

      -

      preorder_find_paths_step8

      +

      preorder_find_paths_step8

      -

      preorder_find_paths_step9

      +

      preorder_find_paths_step9

      -

      preorder_find_paths_step10

      +

      preorder_find_paths_step10

      -

      preorder_find_paths_step11

      +

      preorder_find_paths_step11

      @@ -4232,7 +4067,7 @@

      剪枝是一个非常形象的名词。如图 13-3 所示,在搜索过程中,我们“剪掉”了不满足约束条件的搜索分支,避免许多无意义的尝试,从而提高了搜索效率。

      -

      根据约束条件剪枝

      +

      根据约束条件剪枝

      图 13-3   根据约束条件剪枝

      13.1.3   框架代码

      @@ -5046,7 +4881,7 @@

      根据题意,我们在找到值为 \(7\) 的节点后应该继续搜索,因此需要将记录解之后的 return 语句删除。图 13-4 对比了保留或删除 return 语句的搜索过程。

      -

      保留与删除 return 的搜索过程对比

      +

      保留与删除 return 的搜索过程对比

      图 13-4   保留与删除 return 的搜索过程对比

      相比基于前序遍历的代码实现,基于回溯算法框架的代码实现虽然显得啰嗦,但通用性更好。实际上,许多回溯问题都可以在该框架下解决。我们只需根据具体问题来定义 statechoices ,并实现框架中的各个方法即可。

      @@ -5302,10 +5137,15 @@ aria-label="页脚" @@ -5374,5 +5214,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_backtracking/index.html b/chapter_backtracking/index.html index dcf841a6e..f800d308a 100644 --- a/chapter_backtracking/index.html +++ b/chapter_backtracking/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

      第 13 章   回溯

      -

      回溯

      +

      回溯

      Abstract

      @@ -3592,10 +3427,15 @@ aria-label="页脚" @@ -3664,5 +3504,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_backtracking/n_queens_problem/index.html b/chapter_backtracking/n_queens_problem/index.html index a287728f9..72a282584 100644 --- a/chapter_backtracking/n_queens_problem/index.html +++ b/chapter_backtracking/n_queens_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3493,18 +3328,18 @@

      根据国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。给定 \(n\) 个皇后和一个 \(n \times n\) 大小的棋盘,寻找使得所有皇后之间无法相互攻击的摆放方案。

      如图 13-15 所示,当 \(n = 4\) 时,共可以找到两个解。从回溯算法的角度看,\(n \times n\) 大小的棋盘共有 \(n^2\) 个格子,给出了所有的选择 choices 。在逐个放置皇后的过程中,棋盘状态在不断地变化,每个时刻的棋盘就是状态 state

      -

      4 皇后问题的解

      +

      4 皇后问题的解

      图 13-15   4 皇后问题的解

      图 13-16 展示了本题的三个约束条件:多个皇后不能在同一行、同一列、同一对角线。值得注意的是,对角线分为主对角线 \ 和次对角线 / 两种。

      -

      n 皇后问题的约束条件

      +

      n 皇后问题的约束条件

      图 13-16   n 皇后问题的约束条件

      1.   逐行放置策略

      皇后的数量和棋盘的行数都为 \(n\) ,因此我们容易得到一个推论:棋盘每行都允许且只允许放置一个皇后

      也就是说,我们可以采取逐行放置策略:从第一行开始,在每行放置一个皇后,直至最后一行结束。

      如图 13-17 所示,为 \(4\) 皇后问题的逐行放置过程。受画幅限制,图 13-17 仅展开了第一行的其中一个搜索分支,并且将不满足列约束和对角线约束的方案都进行了剪枝。

      -

      逐行放置策略

      +

      逐行放置策略

      图 13-17   逐行放置策略

      本质上看,逐行放置策略起到了剪枝的作用,它避免了同一行出现多个皇后的所有搜索分支。

      @@ -3513,7 +3348,7 @@

      那么,如何处理对角线约束呢?设棋盘中某个格子的行列索引为 \((row, col)\) ,选定矩阵中的某条主对角线,我们发现该对角线上所有格子的行索引减列索引都相等,即对角线上所有格子的 \(row - col\) 为恒定值

      也就是说,如果两个格子满足 \(row_1 - col_1 = row_2 - col_2\) ,则它们一定处在同一条主对角线上。利用该规律,我们可以借助图 13-18 所示的数组 diag1 ,记录每条主对角线上是否有皇后。

      同理,次对角线上的所有格子的 \(row + col\) 是恒定值。我们同样也可以借助数组 diag2 来处理次对角线约束。

      -

      处理列约束和对角线约束

      +

      处理列约束和对角线约束

      图 13-18   处理列约束和对角线约束

      3.   代码实现

      @@ -4225,10 +4060,15 @@ aria-label="页脚" @@ -4297,5 +4137,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_backtracking/permutations_problem/index.html b/chapter_backtracking/permutations_problem/index.html index 5dbc5514d..c3412a324 100644 --- a/chapter_backtracking/permutations_problem/index.html +++ b/chapter_backtracking/permutations_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2823,14 +2770,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2862,14 +2801,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2890,14 +2821,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2918,14 +2841,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2946,14 +2861,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2974,14 +2881,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3002,14 +2901,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3030,14 +2921,6 @@ 14.7   小结 - - - - - - - - @@ -3096,14 +2979,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3135,14 +3010,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3163,14 +3030,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3191,14 +3050,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3219,14 +3070,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3247,14 +3090,6 @@ 15.5   小结 - - - - - - - - @@ -3604,7 +3439,7 @@

      从回溯算法的角度看,我们可以把生成排列的过程想象成一系列选择的结果。假设输入数组为 \([1, 2, 3]\) ,如果我们先选择 \(1\)、再选择 \(3\)、最后选择 \(2\) ,则获得排列 \([1, 3, 2]\) 。回退表示撤销一个选择,之后继续尝试其他选择。

      从回溯代码的角度看,候选集合 choices 是输入数组中的所有元素,状态 state 是直至目前已被选择的元素。请注意,每个元素只允许被选择一次,因此 state 中的所有元素都应该是唯一的

      如图 13-5 所示,我们可以将搜索过程展开成一个递归树,树中的每个节点代表当前状态 state 。从根节点开始,经过三轮选择后到达叶节点,每个叶节点都对应一个排列。

      -

      全排列的递归树

      +

      全排列的递归树

      图 13-5   全排列的递归树

      1.   重复选择剪枝

      @@ -3614,7 +3449,7 @@
    • 遍历选择列表 choices 时,跳过所有已被选择过的节点,即剪枝。

    如图 13-6 所示,假设我们第一轮选择 1 ,第二轮选择 3 ,第三轮选择 2 ,则需要在第二轮剪掉元素 1 的分支,在第三轮剪掉元素 1 和元素 3 的分支。

    -

    全排列剪枝示例

    +

    全排列剪枝示例

    图 13-6   全排列剪枝示例

    观察图 13-6 发现,该剪枝操作将搜索空间大小从 \(O(n^n)\) 降低至 \(O(n!)\)

    @@ -4025,7 +3860,7 @@

    假设输入数组为 \([1, 1, 2]\) 。为了方便区分两个重复元素 \(1\) ,我们将第二个 \(1\) 记为 \(\hat{1}\)

    如图 13-7 所示,上述方法生成的排列有一半都是重复的。

    -

    重复排列

    +

    重复排列

    图 13-7   重复排列

    那么如何去除重复的排列呢?最直接地,考虑借助一个哈希表,直接对排列结果进行去重。然而这样做不够优雅,因为生成重复排列的搜索分支是没有必要的,应当被提前识别并剪枝,这样可以进一步提升算法效率。

    @@ -4033,7 +3868,7 @@

    观察图 13-8 ,在第一轮中,选择 \(1\) 或选择 \(\hat{1}\) 是等价的,在这两个选择之下生成的所有排列都是重复的。因此应该把 \(\hat{1}\) 剪枝掉。

    同理,在第一轮选择 \(2\) 之后,第二轮选择中的 \(1\)\(\hat{1}\) 也会产生重复分支,因此也应将第二轮的 \(\hat{1}\) 剪枝。

    本质上看,我们的目标是在某一轮选择中,保证多个相等的元素仅被选择一次

    -

    重复排列剪枝

    +

    重复排列剪枝

    图 13-8   重复排列剪枝

    2.   代码实现

    @@ -4423,7 +4258,7 @@
  • 相等元素剪枝:每轮选择(即每个开启的 backtrack 函数)都包含一个 duplicated 。它记录的是在遍历中哪些元素已被选择过,作用是保证相等元素只被选择一次。
  • 图 13-9 展示了两个剪枝条件的生效范围。注意,树中的每个节点代表一个选择,从根节点到叶节点的路径上的各个节点构成一个排列。

    -

    两种剪枝条件的作用范围

    +

    两种剪枝条件的作用范围

    图 13-9   两种剪枝条件的作用范围

    @@ -4588,10 +4423,15 @@ aria-label="页脚" @@ -4660,5 +4500,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_backtracking/subset_sum_problem/index.html b/chapter_backtracking/subset_sum_problem/index.html index 978e5b3fc..e180cb3f4 100644 --- a/chapter_backtracking/subset_sum_problem/index.html +++ b/chapter_backtracking/subset_sum_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2823,14 +2770,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2862,14 +2801,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2890,14 +2821,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2918,14 +2841,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2946,14 +2861,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2974,14 +2881,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3002,14 +2901,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3030,14 +2921,6 @@ 14.7   小结 - - - - - - - - @@ -3096,14 +2979,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3135,14 +3010,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3163,14 +3030,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3191,14 +3050,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3219,14 +3070,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3247,14 +3090,6 @@ 15.5   小结 - - - - - - - - @@ -3974,7 +3809,7 @@

    向以上代码输入数组 \([3, 4, 5]\) 和目标元素 \(9\) ,输出结果为 \([3, 3, 3], [4, 5], [5, 4]\)虽然成功找出了所有和为 \(9\) 的子集,但其中存在重复的子集 \([4, 5]\)\([5, 4]\)

    这是因为搜索过程是区分选择顺序的,然而子集不区分选择顺序。如图 13-10 所示,先选 \(4\) 后选 \(5\) 与先选 \(5\) 后选 \(4\) 是两个不同的分支,但两者对应同一个子集。

    -

    子集搜索与越界剪枝

    +

    子集搜索与越界剪枝

    图 13-10   子集搜索与越界剪枝

    为了去除重复子集,一种直接的思路是对结果列表进行去重。但这个方法效率很低,有两方面原因。

    @@ -3994,7 +3829,7 @@
  • 前两轮选择 \(4\)\(5\) ,生成子集 \([4, 5, \dots]\)
  • 若第一轮选择 \(5\)则第二轮应该跳过 \(3\)\(4\) ,因为子集 \([5, 3, \dots]\)\([5, 4, \dots]\) 与第 1.2. 步中描述的子集完全重复。
  • -

    不同选择顺序导致的重复子集

    +

    不同选择顺序导致的重复子集

    图 13-11   不同选择顺序导致的重复子集

    总结来看,给定输入数组 \([x_1, x_2, \dots, x_n]\) ,设搜索过程中的选择序列为 \([x_{i_1}, x_{i_2}, \dots, x_{i_m}]\) ,则该选择序列需要满足 \(i_1 \leq i_2 \leq \dots \leq i_m\)不满足该条件的选择序列都会造成重复,应当剪枝

    @@ -4425,7 +4260,7 @@

    如图 13-12 所示,为将数组 \([3, 4, 5]\) 和目标元素 \(9\) 输入到以上代码后的整体回溯过程。

    -

    子集和 I 回溯过程

    +

    子集和 I 回溯过程

    图 13-12   子集和 I 回溯过程

    13.3.2   考虑重复元素的情况

    @@ -4435,7 +4270,7 @@

    相比于上题,本题的输入数组可能包含重复元素,这引入了新的问题。例如,给定数组 \([4, \hat{4}, 5]\) 和目标元素 \(9\) ,则现有代码的输出结果为 \([4, 5], [\hat{4}, 5]\) ,出现了重复子集。

    造成这种重复的原因是相等元素在某轮中被多次选择。在图 13-13 中,第一轮共有三个选择,其中两个都为 \(4\) ,会产生两个重复的搜索分支,从而输出重复子集;同理,第二轮的两个 \(4\) 也会产生重复子集。

    -

    相等元素导致的重复子集

    +

    相等元素导致的重复子集

    图 13-13   相等元素导致的重复子集

    1.   相等元素剪枝

    @@ -4917,7 +4752,7 @@

    图 13-14 展示了数组 \([4, 4, 5]\) 和目标元素 \(9\) 的回溯过程,共包含四种剪枝操作。请你将图示与代码注释相结合,理解整个搜索过程,以及每种剪枝操作是如何工作的。

    -

    子集和 II 回溯过程

    +

    子集和 II 回溯过程

    图 13-14   子集和 II 回溯过程

    @@ -5082,10 +4917,15 @@ aria-label="页脚" @@ -5154,5 +4994,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_backtracking/summary/index.html b/chapter_backtracking/summary/index.html index 2bdea29fc..3b72d775b 100644 --- a/chapter_backtracking/summary/index.html +++ b/chapter_backtracking/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3659,10 +3494,15 @@ aria-label="页脚" @@ -3731,5 +3571,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_computational_complexity/index.html b/chapter_computational_complexity/index.html index 595162801..5fe217247 100644 --- a/chapter_computational_complexity/index.html +++ b/chapter_computational_complexity/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 2 章   复杂度分析

    -

    复杂度分析

    +

    复杂度分析

    Abstract

    @@ -3592,10 +3427,15 @@ aria-label="页脚" @@ -3664,5 +3504,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_computational_complexity/iteration_and_recursion/index.html b/chapter_computational_complexity/iteration_and_recursion/index.html index acb2bfd20..435fe901b 100644 --- a/chapter_computational_complexity/iteration_and_recursion/index.html +++ b/chapter_computational_complexity/iteration_and_recursion/index.html @@ -60,7 +60,18 @@ - + @@ -2000,14 +2011,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2028,14 +2031,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2455,14 +2450,6 @@ 第 12 章   分治 - - - - - - - - @@ -2494,14 +2481,6 @@ 12.1   分治算法 - - - - - - - - @@ -2522,14 +2501,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2550,14 +2521,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2578,14 +2541,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2606,14 +2561,6 @@ 12.5   小结 - - - - - - - - @@ -2845,14 +2792,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2884,14 +2823,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2912,14 +2843,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2940,14 +2863,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2968,14 +2883,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2996,14 +2903,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3024,14 +2923,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3052,14 +2943,6 @@ 14.7   小结 - - - - - - - - @@ -3118,14 +3001,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3157,14 +3032,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3185,14 +3052,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3213,14 +3072,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3241,14 +3092,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3269,14 +3112,6 @@ 15.5   小结 - - - - - - - - @@ -3749,7 +3584,7 @@

    图 2-1 展示了该求和函数的流程框图。

    -

    求和函数的流程框图

    +

    求和函数的流程框图

    图 2-1   求和函数的流程框图

    此求和函数的操作数量与输入数据大小 \(n\) 成正比,或者说成“线性关系”。实际上,时间复杂度描述的就是这个“线性关系”。相关内容将会在下一节中详细介绍。

    @@ -4281,7 +4116,7 @@

    图 2-2 给出了该嵌套循环的流程框图。

    -

    嵌套循环的流程框图

    +

    嵌套循环的流程框图

    图 2-2   嵌套循环的流程框图

    在这种情况下,函数的操作数量与 \(n^2\) 成正比,或者说算法运行时间和输入数据大小 \(n\) 成“平方关系”。

    @@ -4450,7 +4285,7 @@

    图 2-3 展示了该函数的递归过程。

    -

    求和函数的递归过程

    +

    求和函数的递归过程

    图 2-3   求和函数的递归过程

    虽然从计算角度看,迭代与递归可以得到相同的结果,但它们代表了两种完全不同的思考和解决问题的范式

    @@ -4470,7 +4305,7 @@
  • 递归调用函数会产生额外的开销。因此递归通常比循环的时间效率更低
  • 如图 2-4 所示,在触发终止条件前,同时存在 \(n\) 个未返回的递归函数,递归深度为 \(n\)

    -

    递归调用深度

    +

    递归调用深度

    图 2-4   递归调用深度

    在实际中,编程语言允许的递归深度通常是有限的,过深的递归可能导致栈溢出报错。

    @@ -4614,7 +4449,7 @@
  • 普通递归:求和操作是在“归”的过程中执行的,每层返回后都要再执行一次求和操作。
  • 尾递归:求和操作是在“递”的过程中执行的,“归”的过程只需层层返回。
  • -

    尾递归过程

    +

    尾递归过程

    图 2-5   尾递归过程

    @@ -4784,7 +4619,7 @@

    观察以上代码,我们在函数内递归调用了两个函数,这意味着从一个调用产生了两个调用分支。如图 2-6 所示,这样不断递归调用下去,最终将产生一个层数为 \(n\) 的「递归树 recursion tree」。

    -

    斐波那契数列的递归树

    +

    斐波那契数列的递归树

    图 2-6   斐波那契数列的递归树

    本质上看,递归体现“将问题分解为更小子问题”的思维范式,这种分治策略是至关重要的。

    @@ -5110,10 +4945,15 @@ aria-label="页脚" @@ -5182,5 +5022,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_computational_complexity/performance_evaluation/index.html b/chapter_computational_complexity/performance_evaluation/index.html index eefc10e55..fdc4ed897 100644 --- a/chapter_computational_complexity/performance_evaluation/index.html +++ b/chapter_computational_complexity/performance_evaluation/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3673,10 +3508,15 @@ aria-label="页脚" @@ -3745,5 +3585,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_computational_complexity/space_complexity/index.html b/chapter_computational_complexity/space_complexity/index.html index 62d6eddf2..9c433e46e 100644 --- a/chapter_computational_complexity/space_complexity/index.html +++ b/chapter_computational_complexity/space_complexity/index.html @@ -60,7 +60,18 @@ - + @@ -1986,14 +1997,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2014,14 +2017,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2441,14 +2436,6 @@ 第 12 章   分治 - - - - - - - - @@ -2480,14 +2467,6 @@ 12.1   分治算法 - - - - - - - - @@ -2508,14 +2487,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2536,14 +2507,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2564,14 +2527,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2592,14 +2547,6 @@ 12.5   小结 - - - - - - - - @@ -2831,14 +2778,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2870,14 +2809,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2898,14 +2829,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2926,14 +2849,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2954,14 +2869,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2982,14 +2889,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3010,14 +2909,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3038,14 +2929,6 @@ 14.7   小结 - - - - - - - - @@ -3104,14 +2987,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3143,14 +3018,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3171,14 +3038,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3199,14 +3058,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3227,14 +3078,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3255,14 +3098,6 @@ 15.5   小结 - - - - - - - - @@ -3600,7 +3435,7 @@
  • 指令空间:用于保存编译后的程序指令,在实际统计中通常忽略不计。
  • 在分析一段程序的空间复杂度时,我们通常统计暂存数据、栈帧空间和输出数据三部分

    -

    算法使用的相关空间

    +

    算法使用的相关空间

    图 2-15   算法使用的相关空间

    @@ -4226,7 +4061,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline \text{常数阶} < \text{对数阶} < \text{线性阶} < \text{平方阶} < \text{指数阶} \end{aligned} \]
    -

    常见的空间复杂度类型

    +

    常见的空间复杂度类型

    图 2-16   常见的空间复杂度类型

    1.   常数阶 \(O(1)\)

    @@ -4903,7 +4738,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline -

    递归函数产生的线性阶空间复杂度

    +

    递归函数产生的线性阶空间复杂度

    图 2-17   递归函数产生的线性阶空间复杂度

    3.   平方阶 \(O(n^2)\)

    @@ -5238,7 +5073,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline -

    递归函数产生的平方阶空间复杂度

    +

    递归函数产生的平方阶空间复杂度

    图 2-18   递归函数产生的平方阶空间复杂度

    4.   指数阶 \(O(2^n)\)

    @@ -5387,7 +5222,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline -

    满二叉树产生的指数阶空间复杂度

    +

    满二叉树产生的指数阶空间复杂度

    图 2-19   满二叉树产生的指数阶空间复杂度

    5.   对数阶 \(O(\log n)\)

    @@ -5560,10 +5395,15 @@ aria-label="页脚" @@ -5632,5 +5472,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_computational_complexity/summary/index.html b/chapter_computational_complexity/summary/index.html index 286b6db53..455c25c85 100644 --- a/chapter_computational_complexity/summary/index.html +++ b/chapter_computational_complexity/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3681,10 +3516,15 @@ aria-label="页脚" @@ -3753,5 +3593,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_computational_complexity/time_complexity/index.html b/chapter_computational_complexity/time_complexity/index.html index 4b3caff89..ba421aafb 100644 --- a/chapter_computational_complexity/time_complexity/index.html +++ b/chapter_computational_complexity/time_complexity/index.html @@ -60,7 +60,18 @@ - + @@ -2027,14 +2038,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2055,14 +2058,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2482,14 +2477,6 @@ 第 12 章   分治 - - - - - - - - @@ -2521,14 +2508,6 @@ 12.1   分治算法 - - - - - - - - @@ -2549,14 +2528,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2577,14 +2548,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2605,14 +2568,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2633,14 +2588,6 @@ 12.5   小结 - - - - - - - - @@ -2872,14 +2819,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2911,14 +2850,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2939,14 +2870,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2967,14 +2890,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2995,14 +2910,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -3023,14 +2930,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3051,14 +2950,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3079,14 +2970,6 @@ 14.7   小结 - - - - - - - - @@ -3145,14 +3028,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3184,14 +3059,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3212,14 +3079,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3240,14 +3099,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3268,14 +3119,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3296,14 +3139,6 @@ 15.5   小结 - - - - - - - - @@ -4050,7 +3885,7 @@
  • 算法 B 中的打印操作需要循环 \(n\) 次,算法运行时间随着 \(n\) 增大呈线性增长。此算法的时间复杂度被称为“线性阶”。
  • 算法 C 中的打印操作需要循环 \(1000000\) 次,虽然运行时间很长,但它与输入数据大小 \(n\) 无关。因此 C 的时间复杂度和 A 相同,仍为“常数阶”。
  • -

    算法 A、B 和 C 的时间增长趋势

    +

    算法 A、B 和 C 的时间增长趋势

    图 2-7   算法 A、B 和 C 的时间增长趋势

    相较于直接统计算法运行时间,时间复杂度分析有哪些特点呢?

    @@ -4212,7 +4047,7 @@ T(n) = 3 + 2n

    若存在正实数 \(c\) 和实数 \(n_0\) ,使得对于所有的 \(n > n_0\) ,均有 \(T(n) \leq c \cdot f(n)\) ,则可认为 \(f(n)\) 给出了 \(T(n)\) 的一个渐近上界,记为 \(T(n) = O(f(n))\)

    如图 2-8 所示,计算渐近上界就是寻找一个函数 \(f(n)\) ,使得当 \(n\) 趋向于无穷大时,\(T(n)\)\(f(n)\) 处于相同的增长级别,仅相差一个常数项 \(c\) 的倍数。

    -

    函数的渐近上界

    +

    函数的渐近上界

    图 2-8   函数的渐近上界

    2.3.3   推算方法

    @@ -4472,7 +4307,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n! \text{常数阶} < \text{对数阶} < \text{线性阶} < \text{线性对数阶} < \text{平方阶} < \text{指数阶} < \text{阶乘阶} \end{aligned} \] -

    常见的时间复杂度类型

    +

    常见的时间复杂度类型

    图 2-9   常见的时间复杂度类型

    1.   常数阶 \(O(1)\)

    @@ -5073,7 +4908,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!

    图 2-10 对比了常数阶、线性阶和平方阶三种时间复杂度。

    -

    常数阶、线性阶和平方阶的时间复杂度

    +

    常数阶、线性阶和平方阶的时间复杂度

    图 2-10   常数阶、线性阶和平方阶的时间复杂度

    以冒泡排序为例,外层循环执行 \(n - 1\) 次,内层循环执行 \(n-1\)\(n-2\)\(\dots\)\(2\)\(1\) 次,平均为 \(n / 2\) 次,因此时间复杂度为 \(O((n - 1) n / 2) = O(n^2)\)

    @@ -5534,7 +5369,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n! -

    指数阶的时间复杂度

    +

    指数阶的时间复杂度

    图 2-11   指数阶的时间复杂度

    在实际算法中,指数阶常出现于递归函数中。例如在以下代码中,其递归地一分为二,经过 \(n\) 次分裂后停止:

    @@ -5800,7 +5635,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n! -

    对数阶的时间复杂度

    +

    对数阶的时间复杂度

    图 2-12   对数阶的时间复杂度

    与指数阶类似,对数阶也常出现于递归函数中。以下代码形成了一个高度为 \(\log_2 n\) 的递归树:

    @@ -6087,7 +5922,7 @@ O(\log_m n) = O(\log_k n / \log_k m) = O(\log_k n)

    图 2-13 展示了线性对数阶的生成方式。二叉树的每一层的操作总数都为 \(n\) ,树共有 \(\log_2 n + 1\) 层,因此时间复杂度为 \(O(n \log n)\)

    -

    线性对数阶的时间复杂度

    +

    线性对数阶的时间复杂度

    图 2-13   线性对数阶的时间复杂度

    主流排序算法的时间复杂度通常为 \(O(n \log n)\) ,例如快速排序、归并排序、堆排序等。

    @@ -6265,7 +6100,7 @@ n! = n \times (n - 1) \times (n - 2) \times \dots \times 2 \times 1 -

    阶乘阶的时间复杂度

    +

    阶乘阶的时间复杂度

    图 2-14   阶乘阶的时间复杂度

    请注意,因为当 \(n \geq 4\) 时恒有 \(n! > 2^n\) ,所以阶乘阶比指数阶增长得更快,在 \(n\) 较大时也是不可接受的。

    @@ -6777,10 +6612,15 @@ aria-label="页脚" @@ -6849,5 +6689,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_data_structure/basic_data_types/index.html b/chapter_data_structure/basic_data_types/index.html index 42ff7b03c..81745172f 100644 --- a/chapter_data_structure/basic_data_types/index.html +++ b/chapter_data_structure/basic_data_types/index.html @@ -60,7 +60,18 @@ - + @@ -1887,14 +1898,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1915,14 +1918,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2342,14 +2337,6 @@ 第 12 章   分治 - - - - - - - - @@ -2381,14 +2368,6 @@ 12.1   分治算法 - - - - - - - - @@ -2409,14 +2388,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2437,14 +2408,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2465,14 +2428,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2493,14 +2448,6 @@ 12.5   小结 - - - - - - - - @@ -2732,14 +2679,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2771,14 +2710,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2799,14 +2730,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2827,14 +2750,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2855,14 +2770,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2883,14 +2790,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2911,14 +2810,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2939,14 +2830,6 @@ 14.7   小结 - - - - - - - - @@ -3005,14 +2888,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3044,14 +2919,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3072,14 +2939,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3100,14 +2959,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3128,14 +2979,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3156,14 +2999,6 @@ 15.5   小结 - - - - - - - - @@ -3768,10 +3603,15 @@ aria-label="页脚" @@ -3840,5 +3680,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_data_structure/character_encoding/index.html b/chapter_data_structure/character_encoding/index.html index 9b8ee9eef..38b7032c9 100644 --- a/chapter_data_structure/character_encoding/index.html +++ b/chapter_data_structure/character_encoding/index.html @@ -60,7 +60,18 @@ - + @@ -1952,14 +1963,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1980,14 +1983,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2407,14 +2402,6 @@ 第 12 章   分治 - - - - - - - - @@ -2446,14 +2433,6 @@ 12.1   分治算法 - - - - - - - - @@ -2474,14 +2453,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2502,14 +2473,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2530,14 +2493,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2558,14 +2513,6 @@ 12.5   小结 - - - - - - - - @@ -2797,14 +2744,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2836,14 +2775,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2864,14 +2795,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2892,14 +2815,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2920,14 +2835,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2948,14 +2855,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2976,14 +2875,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3004,14 +2895,6 @@ 14.7   小结 - - - - - - - - @@ -3070,14 +2953,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3109,14 +2984,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3137,14 +3004,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3165,14 +3024,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3193,14 +3044,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3221,14 +3064,6 @@ 15.5   小结 - - - - - - - - @@ -3519,7 +3354,7 @@

    在计算机中,所有数据都是以二进制数的形式存储的,字符 char 也不例外。为了表示字符,我们需要建立一套“字符集”,规定每个字符和二进制数之间的一一对应关系。有了字符集之后,计算机就可以通过查表完成二进制数到字符的转换。

    3.4.1   ASCII 字符集

    「ASCII 码」是最早出现的字符集,全称为“美国标准信息交换代码”。它使用 7 位二进制数(即一个字节的低 7 位)表示一个字符,最多能够表示 128 个不同的字符。如图 3-6 所示,ASCII 码包括英文字母的大小写、数字 0 ~ 9、一些标点符号,以及一些控制字符(如换行符和制表符)。

    -

    ASCII 码

    +

    ASCII 码

    图 3-6   ASCII 码

    然而,ASCII 码仅能够表示英文。随着计算机的全球化,诞生了一种能够表示更多语言的字符集「EASCII」。它在 ASCII 的 7 位基础上扩展到 8 位,能够表示 256 个不同的字符。

    @@ -3534,7 +3369,7 @@

    自 1991 年发布以来,Unicode 不断扩充新的语言与字符。截止 2022 年 9 月,Unicode 已经包含 149186 个字符,包括各种语言的字符、符号、甚至是表情符号等。在庞大的 Unicode 字符集中,常用的字符占用 2 字节,有些生僻的字符占 3 字节甚至 4 字节。

    Unicode 是一种字符集标准,本质上是给每个字符分配一个编号(称为“码点”),但它并没有规定在计算机中如何存储这些字符码点。我们不禁会问:当多种长度的 Unicode 码点同时出现在同一个文本中时,系统如何解析字符?例如给定一个长度为 2 字节的编码,系统如何确认它是一个 2 字节的字符还是两个 1 字节的字符?

    对于以上问题,一种直接的解决方案是将所有字符存储为等长的编码。如图 3-7 所示,“Hello”中的每个字符占用 1 字节,“算法”中的每个字符占用 2 字节。我们可以通过高位填 0 ,将“Hello 算法”中的所有字符都编码为 2 字节长度。这样系统就可以每隔 2 字节解析一个字符,恢复出这个短语的内容了。

    -

    Unicode 编码示例

    +

    Unicode 编码示例

    图 3-7   Unicode 编码示例

    然而 ASCII 码已经向我们证明,编码英文只需要 1 字节。若采用上述方案,英文文本占用空间的大小将会是 ASCII 编码下大小的两倍,非常浪费内存空间。因此,我们需要一种更加高效的 Unicode 编码方法。

    @@ -3548,7 +3383,7 @@

    图 3-8 展示了“Hello算法”对应的 UTF-8 编码。观察发现,由于最高 \(n\) 位都被设置为 \(1\) ,因此系统可以通过读取最高位 \(1\) 的个数来解析出字符的长度为 \(n\)

    但为什么要将其余所有字节的高 2 位都设置为 \(10\) 呢?实际上,这个 \(10\) 能够起到校验符的作用。假设系统从一个错误的字节开始解析文本,字节头部的 \(10\) 能够帮助系统快速的判断出异常。

    之所以将 \(10\) 当作校验符,是因为在 UTF-8 编码规则下,不可能有字符的最高两位是 \(10\) 。这个结论可以用反证法来证明:假设一个字符的最高两位是 \(10\) ,说明该字符的长度为 \(1\) ,对应 ASCII 码。而 ASCII 码的最高位应该是 \(0\) ,与假设矛盾。

    -

    UTF-8 编码示例

    +

    UTF-8 编码示例

    图 3-8   UTF-8 编码示例

    除了 UTF-8 之外,常见的编码方式还包括以下两种。

    @@ -3742,10 +3577,15 @@ aria-label="页脚" @@ -3814,5 +3654,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_data_structure/classification_of_data_structure/index.html b/chapter_data_structure/classification_of_data_structure/index.html index 579b7c855..79304dd74 100644 --- a/chapter_data_structure/classification_of_data_structure/index.html +++ b/chapter_data_structure/classification_of_data_structure/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3482,7 +3317,7 @@
  • 线性数据结构:数组、链表、栈、队列、哈希表。
  • 非线性数据结构:树、堆、图、哈希表。
  • -

    线性与非线性数据结构

    +

    线性与非线性数据结构

    图 3-1   线性与非线性数据结构

    非线性数据结构可以进一步被划分为树形结构和网状结构。

    @@ -3495,12 +3330,12 @@

    在计算机中,内存和硬盘是两种主要的存储硬件设备。硬盘主要用于长期存储数据,容量较大(通常可达到 TB 级别)、速度较慢。内存用于运行程序时暂存数据,速度较快,但容量较小(通常为 GB 级别)。

    在算法运行过程中,相关数据都存储在内存中。图 3-2 展示了一个计算机内存条,其中每个黑色方块都包含一块内存空间。我们可以将内存想象成一个巨大的 Excel 表格,其中每个单元格都可以存储一定大小的数据,在算法运行时,所有数据都被存储在这些单元格中。

    系统通过内存地址来访问目标位置的数据。如图 3-2 所示,计算机根据特定规则为表格中的每个单元格分配编号,确保每个内存空间都有唯一的内存地址。有了这些地址,程序便可以访问内存中的数据。

    -

    内存条、内存空间、内存地址

    +

    内存条、内存空间、内存地址

    图 3-2   内存条、内存空间、内存地址

    内存是所有程序的共享资源,当某块内存被某个程序占用时,则无法被其他程序同时使用了。因此在数据结构与算法的设计中,内存资源是一个重要的考虑因素。比如,算法所占用的内存峰值不应超过系统剩余空闲内存;如果缺少连续大块的内存空间,那么所选用的数据结构必须能够存储在离散的内存空间内。

    如图 3-3 所示,物理结构反映了数据在计算机内存中的存储方式,可分为连续空间存储(数组)和离散空间存储(链表)。物理结构从底层决定了数据的访问、更新、增删等操作方法,同时在时间效率和空间效率方面呈现出互补的特点。

    -

    连续空间存储与离散空间存储

    +

    连续空间存储与离散空间存储

    图 3-3   连续空间存储与离散空间存储

    值得说明的是,所有数据结构都是基于数组、链表或二者的组合实现的。例如,栈和队列既可以使用数组实现,也可以使用链表实现;而哈希表的实现可能同时包含数组和链表。

    @@ -3676,10 +3511,15 @@ aria-label="页脚" @@ -3748,5 +3588,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_data_structure/index.html b/chapter_data_structure/index.html index 24c67e45a..38f9792dd 100644 --- a/chapter_data_structure/index.html +++ b/chapter_data_structure/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 3 章   数据结构

    -

    数据结构

    +

    数据结构

    Abstract

    @@ -3592,10 +3427,15 @@ aria-label="页脚" @@ -3664,5 +3504,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_data_structure/number_encoding/index.html b/chapter_data_structure/number_encoding/index.html index a87b73280..f3f1b0146 100644 --- a/chapter_data_structure/number_encoding/index.html +++ b/chapter_data_structure/number_encoding/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3487,7 +3322,7 @@
  • 补码:正数的补码与其原码相同,负数的补码是在其反码的基础上加 \(1\)
  • 图 3-4 展示了原码、反码和补码之间的转换方法。

    -

    原码、反码与补码之间的相互转换

    +

    原码、反码与补码之间的相互转换

    图 3-4   原码、反码与补码之间的相互转换

    「原码 true form」虽然最直观,但存在一些局限性。一方面,负数的原码不能直接用于运算。例如在原码下计算 \(1 + (-2)\) ,得到的结果是 \(-3\) ,这显然是不对的。

    @@ -3569,7 +3404,7 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0 (1 + \mathrm{N}) = & (1 + \sum_{i=1}^{23} b_{23-i} 2^{-i}) \subset [1, 2 - 2^{-23}] \end{aligned} \]
    -

    IEEE 754 标准下的 float 的计算示例

    +

    IEEE 754 标准下的 float 的计算示例

    图 3-5   IEEE 754 标准下的 float 的计算示例

    观察图 3-5 ,给定一个示例数据 \(\mathrm{S} = 0\)\(\mathrm{E} = 124\)\(\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\) ,则有:

    @@ -3778,10 +3613,15 @@ aria-label="页脚" @@ -3850,5 +3690,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_data_structure/summary/index.html b/chapter_data_structure/summary/index.html index 45159f2b2..f1d70ba76 100644 --- a/chapter_data_structure/summary/index.html +++ b/chapter_data_structure/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3659,10 +3494,15 @@ aria-label="页脚" @@ -3731,5 +3571,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_divide_and_conquer/binary_search_recur/index.html b/chapter_divide_and_conquer/binary_search_recur/index.html index c8a701f88..89f55ce99 100644 --- a/chapter_divide_and_conquer/binary_search_recur/index.html +++ b/chapter_divide_and_conquer/binary_search_recur/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2408,14 +2387,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2482,14 +2445,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2510,14 +2465,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2538,14 +2485,6 @@ 12.5   小结 - - - - - - - - @@ -2777,14 +2716,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2816,14 +2747,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2844,14 +2767,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2872,14 +2787,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2900,14 +2807,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2928,14 +2827,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2956,14 +2847,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2984,14 +2867,6 @@ 14.7   小结 - - - - - - - - @@ -3050,14 +2925,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3089,14 +2956,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3117,14 +2976,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3145,14 +2996,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3173,14 +3016,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3201,14 +3036,6 @@ 15.5   小结 - - - - - - - - @@ -3499,7 +3326,7 @@
  • 循环第 1.2. 步,直至找到 target 或区间为空时返回。
  • 图 12-4 展示了在数组中二分查找元素 \(6\) 的分治过程。

    -

    二分查找的分治过程

    +

    二分查找的分治过程

    图 12-4   二分查找的分治过程

    在实现代码中,我们声明一个递归函数 dfs() 来求解问题 \(f(i, j)\)

    @@ -3967,10 +3794,15 @@ aria-label="页脚" @@ -4039,5 +3871,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_divide_and_conquer/build_binary_tree_problem/index.html b/chapter_divide_and_conquer/build_binary_tree_problem/index.html index 009bbacb1..50ae85db6 100644 --- a/chapter_divide_and_conquer/build_binary_tree_problem/index.html +++ b/chapter_divide_and_conquer/build_binary_tree_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2436,14 +2407,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2531,14 +2486,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2559,14 +2506,6 @@ 12.5   小结 - - - - - - - - @@ -2798,14 +2737,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2837,14 +2768,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2865,14 +2788,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2893,14 +2808,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2921,14 +2828,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2949,14 +2848,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2977,14 +2868,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3005,14 +2888,6 @@ 14.7   小结 - - - - - - - - @@ -3071,14 +2946,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3110,14 +2977,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3138,14 +2997,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3166,14 +3017,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3194,14 +3037,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3222,14 +3057,6 @@ 15.5   小结 - - - - - - - - @@ -3514,7 +3341,7 @@

    Question

    给定一个二叉树的前序遍历 preorder 和中序遍历 inorder ,请从中构建二叉树,返回二叉树的根节点。

    -

    构建二叉树的示例数据

    +

    构建二叉树的示例数据

    图 12-5   构建二叉树的示例数据

    1.   判断是否为分治问题

    @@ -3537,7 +3364,7 @@
  • 查找根节点 3 在 inorder 中的索引,利用该索引可将 inorder 划分为 [ 9 | 3 | 1 2 7 ]
  • 根据 inorder 划分结果,易得左子树和右子树的节点数量分别为 1 和 3 ,从而可将 preorder 划分为 [ 3 | 9 | 2 1 7 ]
  • -

    在前序和中序遍历中划分子树

    +

    在前序和中序遍历中划分子树

    图 12-6   在前序和中序遍历中划分子树

    3.   基于变量描述子树区间

    @@ -3579,7 +3406,7 @@

    请注意,右子树根节点索引中的 \((m-l)\) 的含义是“左子树的节点数量”,建议配合图 12-7 理解。

    -

    根节点和左右子树的索引区间表示

    +

    根节点和左右子树的索引区间表示

    图 12-7   根节点和左右子树的索引区间表示

    4.   代码实现

    @@ -3914,38 +3741,38 @@
    -

    构建二叉树的递归过程

    +

    构建二叉树的递归过程

    -

    built_tree_step2

    +

    built_tree_step2

    -

    built_tree_step3

    +

    built_tree_step3

    -

    built_tree_step4

    +

    built_tree_step4

    -

    built_tree_step5

    +

    built_tree_step5

    -

    built_tree_step6

    +

    built_tree_step6

    -

    built_tree_step7

    +

    built_tree_step7

    -

    built_tree_step8

    +

    built_tree_step8

    -

    built_tree_step9

    +

    built_tree_step9

    图 12-8   构建二叉树的递归过程

    每个递归函数内的前序遍历 preorder 和中序遍历 inorder 的划分结果如图 12-9 所示。

    -

    每个递归函数中的划分结果

    +

    每个递归函数中的划分结果

    图 12-9   每个递归函数中的划分结果

    设树的节点数量为 \(n\) ,初始化每一个节点(执行一个递归函数 dfs() )使用 \(O(1)\) 时间。因此总体时间复杂度为 \(O(n)\)

    @@ -4113,10 +3940,15 @@ aria-label="页脚" @@ -4185,5 +4017,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_divide_and_conquer/divide_and_conquer/index.html b/chapter_divide_and_conquer/divide_and_conquer/index.html index e0fa608c8..ccb47fcbd 100644 --- a/chapter_divide_and_conquer/divide_and_conquer/index.html +++ b/chapter_divide_and_conquer/divide_and_conquer/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2380,14 +2367,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.1   分治算法 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2572,14 +2519,6 @@ 12.5   小结 - - - - - - - - @@ -2811,14 +2750,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3018,14 +2901,6 @@ 14.7   小结 - - - - - - - - @@ -3084,14 +2959,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3235,14 +3070,6 @@ 15.5   小结 - - - - - - - - @@ -3546,7 +3373,7 @@
  • :递归地将原数组(原问题)划分为两个子数组(子问题),直到子数组只剩一个元素(最小子问题)。
  • :从底至顶地将有序的子数组(子问题的解)进行合并,从而得到有序的原数组(原问题的解)。
  • -

    归并排序的分治策略

    +

    归并排序的分治策略

    图 12-1   归并排序的分治策略

    12.1.1   如何判断分治问题

    @@ -3570,7 +3397,7 @@
    \[ O(n + (\frac{n}{2})^2 \times 2 + n) = O(\frac{n^2}{2} + 2n) \]
    -

    划分数组前后的冒泡排序

    +

    划分数组前后的冒泡排序

    图 12-2   划分数组前后的冒泡排序

    接下来,我们计算以下不等式,其左边和右边分别为划分前和划分后的操作总数:

    @@ -3588,7 +3415,7 @@ n(n - 4) & > 0

    我们知道,分治生成的子问题是相互独立的,因此通常可以并行解决。也就是说,分治不仅可以降低算法的时间复杂度,还有利于操作系统的并行优化

    并行优化在多核或多处理器的环境中尤其有效,因为系统可以同时处理多个子问题,更加充分地利用计算资源,从而显著减少总体的运行时间。

    比如在图 12-3 所示的“桶排序”中,我们将海量的数据平均分配到各个桶中,则可所有桶的排序任务分散到各个计算单元,完成后再进行结果合并。

    -

    桶排序的并行计算

    +

    桶排序的并行计算

    图 12-3   桶排序的并行计算

    12.1.3   分治常见应用

    @@ -3774,10 +3601,15 @@ aria-label="页脚" @@ -3846,5 +3678,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_divide_and_conquer/hanota_problem/index.html b/chapter_divide_and_conquer/hanota_problem/index.html index 01fe76ee0..0764cd586 100644 --- a/chapter_divide_and_conquer/hanota_problem/index.html +++ b/chapter_divide_and_conquer/hanota_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2464,14 +2427,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2552,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2791,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2830,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2858,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2886,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2914,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2942,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2970,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2998,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3064,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3103,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3131,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3159,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3187,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3215,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3506,7 +3333,7 @@
  • 小圆盘必须时刻位于大圆盘之上。
  • -

    汉诺塔问题示例

    +

    汉诺塔问题示例

    图 12-10   汉诺塔问题示例

    我们将规模为 \(i\) 的汉诺塔问题记做 \(f(i)\) 。例如 \(f(3)\) 代表将 \(3\) 个圆盘从 A 移动至 C 的汉诺塔问题。

    @@ -3515,10 +3342,10 @@
    -

    规模为 1 问题的解

    +

    规模为 1 问题的解

    -

    hanota_f1_step2

    +

    hanota_f1_step2

    @@ -3533,16 +3360,16 @@
    -

    规模为 2 问题的解

    +

    规模为 2 问题的解

    -

    hanota_f2_step2

    +

    hanota_f2_step2

    -

    hanota_f2_step3

    +

    hanota_f2_step3

    -

    hanota_f2_step4

    +

    hanota_f2_step4

    @@ -3560,16 +3387,16 @@
    -

    规模为 3 问题的解

    +

    规模为 3 问题的解

    -

    hanota_f3_step2

    +

    hanota_f3_step2

    -

    hanota_f3_step3

    +

    hanota_f3_step3

    -

    hanota_f3_step4

    +

    hanota_f3_step4

    @@ -3583,7 +3410,7 @@
  • \(n-1\) 个圆盘借助 AB 移至 C
  • 对于这两个子问题 \(f(n-1)\)可以通过相同的方式进行递归划分,直至达到最小子问题 \(f(1)\) 。而 \(f(1)\) 的解是已知的,只需一次移动操作即可。

    -

    汉诺塔问题的分治策略

    +

    汉诺塔问题的分治策略

    图 12-14   汉诺塔问题的分治策略

    3.   代码实现

    @@ -3930,7 +3757,7 @@

    如图 12-15 所示,汉诺塔问题形成一个高度为 \(n\) 的递归树,每个节点代表一个子问题、对应一个开启的 dfs() 函数,因此时间复杂度为 \(O(2^n)\) ,空间复杂度为 \(O(n)\)

    -

    汉诺塔问题的递归树

    +

    汉诺塔问题的递归树

    图 12-15   汉诺塔问题的递归树

    @@ -4101,10 +3928,15 @@ aria-label="页脚" @@ -4173,5 +4005,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_divide_and_conquer/index.html b/chapter_divide_and_conquer/index.html index 11c296d68..37b3d1955 100644 --- a/chapter_divide_and_conquer/index.html +++ b/chapter_divide_and_conquer/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 12 章   分治

    -

    分治

    +

    分治

    Abstract

    @@ -3592,10 +3427,15 @@ aria-label="页脚" @@ -3664,5 +3504,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_divide_and_conquer/summary/index.html b/chapter_divide_and_conquer/summary/index.html index 477ef45eb..93c19aea0 100644 --- a/chapter_divide_and_conquer/summary/index.html +++ b/chapter_divide_and_conquer/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2492,14 +2447,6 @@ 12.5   小结 - - - - - - - - @@ -2732,14 +2679,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2771,14 +2710,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2799,14 +2730,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2827,14 +2750,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2855,14 +2770,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2883,14 +2790,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2911,14 +2810,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2939,14 +2830,6 @@ 14.7   小结 - - - - - - - - @@ -3005,14 +2888,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3044,14 +2919,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3072,14 +2939,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3100,14 +2959,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3128,14 +2979,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3156,14 +2999,6 @@ 15.5   小结 - - - - - - - - @@ -3582,10 +3417,15 @@ aria-label="页脚" @@ -3654,5 +3494,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_dynamic_programming/dp_problem_features/index.html b/chapter_dynamic_programming/dp_problem_features/index.html index b43ebd693..083ac695d 100644 --- a/chapter_dynamic_programming/dp_problem_features/index.html +++ b/chapter_dynamic_programming/dp_problem_features/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2798,14 +2729,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2879,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2907,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2935,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2963,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2991,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3057,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3096,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3124,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3152,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3180,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3208,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3496,7 +3323,7 @@

    给定一个楼梯,你每步可以上 \(1\) 阶或者 \(2\) 阶,每一阶楼梯上都贴有一个非负整数,表示你在该台阶所需要付出的代价。给定一个非负整数数组 \(cost\) ,其中 \(cost[i]\) 表示在第 \(i\) 个台阶需要付出的代价,\(cost[0]\) 为地面起始点。请计算最少需要付出多少代价才能到达顶部?

    如图 14-6 所示,若第 \(1\)\(2\)\(3\) 阶的代价分别为 \(1\)\(10\)\(1\) ,则从地面爬到第 \(3\) 阶的最小代价为 \(2\)

    -

    爬到第 3 阶的最小代价

    +

    爬到第 3 阶的最小代价

    图 14-6   爬到第 3 阶的最小代价

    \(dp[i]\) 为爬到第 \(i\) 阶累计付出的代价,由于第 \(i\) 阶只可能从 \(i - 1\) 阶或 \(i - 2\) 阶走来,因此 \(dp[i]\) 只可能等于 \(dp[i - 1] + cost[i]\)\(dp[i - 2] + cost[i]\) 。为了尽可能减少代价,我们应该选择两者中较小的那一个:

    @@ -3725,7 +3552,7 @@ dp[i] = \min(dp[i-1], dp[i-2]) + cost[i]

    图 14-7 展示了以上代码的动态规划过程。

    -

    爬楼梯最小代价的动态规划过程

    +

    爬楼梯最小代价的动态规划过程

    图 14-7   爬楼梯最小代价的动态规划过程

    本题也可以进行空间优化,将一维压缩至零维,使得空间复杂度从 \(O(n)\) 降低至 \(O(1)\)

    @@ -3925,7 +3752,7 @@ dp[i] = \min(dp[i-1], dp[i-2]) + cost[i]

    给定一个共有 \(n\) 阶的楼梯,你每步可以上 \(1\) 阶或者 \(2\) 阶,但不能连续两轮跳 \(1\),请问有多少种方案可以爬到楼顶。

    例如图 14-8 ,爬上第 \(3\) 阶仅剩 \(2\) 种可行方案,其中连续三次跳 \(1\) 阶的方案不满足约束条件,因此被舍弃。

    -

    带约束爬到第 3 阶的方案数量

    +

    带约束爬到第 3 阶的方案数量

    图 14-8   带约束爬到第 3 阶的方案数量

    在该问题中,如果上一轮是跳 \(1\) 阶上来的,那么下一轮就必须跳 \(2\) 阶。这意味着,下一步选择不能由当前状态(当前楼梯阶数)独立决定,还和前一个状态(上轮楼梯阶数)有关

    @@ -3942,7 +3769,7 @@ dp[i, 1] = dp[i-1, 2] \\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \end{cases} \] -

    考虑约束下的递推关系

    +

    考虑约束下的递推关系

    图 14-9   考虑约束下的递推关系

    最终,返回 \(dp[n, 1] + dp[n, 2]\) 即可,两者之和代表爬到第 \(n\) 阶的方案总数。

    @@ -4359,10 +4186,15 @@ aria-label="页脚" @@ -4431,5 +4263,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_dynamic_programming/dp_solution_pipeline/index.html b/chapter_dynamic_programming/dp_solution_pipeline/index.html index 0891d3ff9..27f1a336a 100644 --- a/chapter_dynamic_programming/dp_solution_pipeline/index.html +++ b/chapter_dynamic_programming/dp_solution_pipeline/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2826,14 +2749,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2941,14 +2848,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2969,14 +2868,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2997,14 +2888,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3025,14 +2908,6 @@ 14.7   小结 - - - - - - - - @@ -3091,14 +2966,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3130,14 +2997,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3158,14 +3017,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3186,14 +3037,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3214,14 +3057,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3242,14 +3077,6 @@ 15.5   小结 - - - - - - - - @@ -3578,14 +3405,14 @@

    给定一个 \(n \times m\) 的二维网格 grid ,网格中的每个单元格包含一个非负整数,表示该单元格的代价。机器人以左上角单元格为起始点,每次只能向下或者向右移动一步,直至到达右下角单元格。请返回从左上角到右下角的最小路径和。

    图 14-10 展示了一个例子,给定网格的最小路径和为 \(13\)

    -

    最小路径和示例数据

    +

    最小路径和示例数据

    图 14-10   最小路径和示例数据

    第一步:思考每轮的决策,定义状态,从而得到 \(dp\)

    本题的每一轮的决策就是从当前格子向下或向右一步。设当前格子的行列索引为 \([i, j]\) ,则向下或向右走一步后,索引变为 \([i+1, j]\)\([i, j+1]\) 。因此,状态应包含行索引和列索引两个变量,记为 \([i, j]\)

    状态 \([i, j]\) 对应的子问题为:从起始点 \([0, 0]\) 走到 \([i, j]\) 的最小路径和,解记为 \(dp[i, j]\)

    至此,我们就得到了图 14-11 所示的二维 \(dp\) 矩阵,其尺寸与输入网格 \(grid\) 相同。

    -

    状态定义与 dp 表

    +

    状态定义与 dp 表

    图 14-11   状态定义与 dp 表

    @@ -3599,7 +3426,7 @@
    \[ dp[i, j] = \min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \]
    -

    最优子结构与状态转移方程

    +

    最优子结构与状态转移方程

    图 14-12   最优子结构与状态转移方程

    @@ -3610,7 +3437,7 @@ dp[i, j] = \min(dp[i-1, j], dp[i, j-1]) + grid[i, j]

    第三步:确定边界条件和状态转移顺序

    在本题中,首行的状态只能从其左边的状态得来,首列的状态只能从其上边的状态得来,因此首行 \(i = 0\) 和首列 \(j = 0\) 是边界条件。

    如图 14-13 所示,由于每个格子是由其左方格子和上方格子转移而来,因此我们使用采用循环来遍历矩阵,外循环遍历各行、内循环遍历各列。

    -

    边界条件与状态转移顺序

    +

    边界条件与状态转移顺序

    图 14-13   边界条件与状态转移顺序

    @@ -3848,7 +3675,7 @@ dp[i, j] = \min(dp[i-1, j], dp[i, j-1]) + grid[i, j]

    图 14-14 给出了以 \(dp[2, 1]\) 为根节点的递归树,其中包含一些重叠子问题,其数量会随着网格 grid 的尺寸变大而急剧增多。

    本质上看,造成重叠子问题的原因为:存在多条路径可以从左上角到达某一单元格

    -

    暴力搜索递归树

    +

    暴力搜索递归树

    图 14-14   暴力搜索递归树

    每个状态都有向下和向右两种选择,从左上角走到右下角总共需要 \(m + n - 2\) 步,所以最差时间复杂度为 \(O(2^{m + n})\) 。请注意,这种计算方式未考虑临近网格边界的情况,当到达网络边界时只剩下一种选择。因此实际的路径数量会少一些。

    @@ -4132,7 +3959,7 @@ dp[i, j] = \min(dp[i-1, j], dp[i, j-1]) + grid[i, j]

    如图 14-15 所示,在引入记忆化后,所有子问题的解只需计算一次,因此时间复杂度取决于状态总数,即网格尺寸 \(O(nm)\)

    -

    记忆化搜索递归树

    +

    记忆化搜索递归树

    图 14-15   记忆化搜索递归树

    3.   方法三:动态规划

    @@ -4431,40 +4258,40 @@ dp[i, j] = \min(dp[i-1, j], dp[i, j-1]) + grid[i, j]
    -

    最小路径和的动态规划过程

    +

    最小路径和的动态规划过程

    -

    min_path_sum_dp_step2

    +

    min_path_sum_dp_step2

    -

    min_path_sum_dp_step3

    +

    min_path_sum_dp_step3

    -

    min_path_sum_dp_step4

    +

    min_path_sum_dp_step4

    -

    min_path_sum_dp_step5

    +

    min_path_sum_dp_step5

    -

    min_path_sum_dp_step6

    +

    min_path_sum_dp_step6

    -

    min_path_sum_dp_step7

    +

    min_path_sum_dp_step7

    -

    min_path_sum_dp_step8

    +

    min_path_sum_dp_step8

    -

    min_path_sum_dp_step9

    +

    min_path_sum_dp_step9

    -

    min_path_sum_dp_step10

    +

    min_path_sum_dp_step10

    -

    min_path_sum_dp_step11

    +

    min_path_sum_dp_step11

    -

    min_path_sum_dp_step12

    +

    min_path_sum_dp_step12

    @@ -4906,10 +4733,15 @@ aria-label="页脚" @@ -4978,5 +4810,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_dynamic_programming/edit_distance_problem/index.html b/chapter_dynamic_programming/edit_distance_problem/index.html index ab51ebfb8..3147635da 100644 --- a/chapter_dynamic_programming/edit_distance_problem/index.html +++ b/chapter_dynamic_programming/edit_distance_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2910,14 +2809,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2998,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3064,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3103,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3131,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3159,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3187,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3215,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3503,13 +3330,13 @@

    你可以在一个字符串中进行三种编辑操作:插入一个字符、删除一个字符、替换字符为任意一个字符。

    如图 14-27 所示,将 kitten 转换为 sitting 需要编辑 3 步,包括 2 次替换操作与 1 次添加操作;将 hello 转换为 algo 需要 3 步,包括 2 次替换操作和 1 次删除操作。

    -

    编辑距离的示例数据

    +

    编辑距离的示例数据

    图 14-27   编辑距离的示例数据

    编辑距离问题可以很自然地用决策树模型来解释。字符串对应树节点,一轮决策(一次编辑操作)对应树的一条边。

    如图 14-28 所示,在不限制操作的情况下,每个节点都可以派生出许多条边,每条边对应一种操作,这意味着从 hello 转换到 algo 有许多种可能的路径。

    从决策树的角度看,本题的目标是求解节点 hello 和节点 algo 之间的最短路径。

    -

    基于决策树模型表示编辑距离问题

    +

    基于决策树模型表示编辑距离问题

    图 14-28   基于决策树模型表示编辑距离问题

    1.   动态规划思路

    @@ -3530,7 +3357,7 @@
  • 删除 \(s[i-1]\) ,则剩余子问题 \(dp[i-1, j]\)
  • \(s[i-1]\) 替换为 \(t[j-1]\) ,则剩余子问题 \(dp[i-1, j-1]\)
  • -

    编辑距离的状态转移

    +

    编辑距离的状态转移

    图 14-29   编辑距离的状态转移

    根据以上分析,可得最优子结构:\(dp[i, j]\) 的最少编辑步数等于 \(dp[i, j-1]\)\(dp[i-1, j]\)\(dp[i-1, j-1]\) 三者中的最少编辑步数,再加上本次的编辑步数 \(1\) 。对应的状态转移方程为:

    @@ -3877,49 +3704,49 @@ dp[i, j] = dp[i-1, j-1]
    -

    编辑距离的动态规划过程

    +

    编辑距离的动态规划过程

    -

    edit_distance_dp_step2

    +

    edit_distance_dp_step2

    -

    edit_distance_dp_step3

    +

    edit_distance_dp_step3

    -

    edit_distance_dp_step4

    +

    edit_distance_dp_step4

    -

    edit_distance_dp_step5

    +

    edit_distance_dp_step5

    -

    edit_distance_dp_step6

    +

    edit_distance_dp_step6

    -

    edit_distance_dp_step7

    +

    edit_distance_dp_step7

    -

    edit_distance_dp_step8

    +

    edit_distance_dp_step8

    -

    edit_distance_dp_step9

    +

    edit_distance_dp_step9

    -

    edit_distance_dp_step10

    +

    edit_distance_dp_step10

    -

    edit_distance_dp_step11

    +

    edit_distance_dp_step11

    -

    edit_distance_dp_step12

    +

    edit_distance_dp_step12

    -

    edit_distance_dp_step13

    +

    edit_distance_dp_step13

    -

    edit_distance_dp_step14

    +

    edit_distance_dp_step14

    -

    edit_distance_dp_step15

    +

    edit_distance_dp_step15

    @@ -4440,10 +4267,15 @@ aria-label="页脚" @@ -4512,5 +4344,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_dynamic_programming/index.html b/chapter_dynamic_programming/index.html index f38c9fc30..24568d043 100644 --- a/chapter_dynamic_programming/index.html +++ b/chapter_dynamic_programming/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 14 章   动态规划

    -

    动态规划

    +

    动态规划

    Abstract

    @@ -3594,10 +3429,15 @@ aria-label="页脚" @@ -3666,5 +3506,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_dynamic_programming/intro_to_dynamic_programming/index.html b/chapter_dynamic_programming/intro_to_dynamic_programming/index.html index 6393a48f2..40a45fb7f 100644 --- a/chapter_dynamic_programming/intro_to_dynamic_programming/index.html +++ b/chapter_dynamic_programming/intro_to_dynamic_programming/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2770,14 +2709,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2865,14 +2788,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2893,14 +2808,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2921,14 +2828,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2949,14 +2848,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2977,14 +2868,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3005,14 +2888,6 @@ 14.7   小结 - - - - - - - - @@ -3071,14 +2946,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3110,14 +2977,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3138,14 +2997,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3166,14 +3017,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3194,14 +3037,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3222,14 +3057,6 @@ 15.5   小结 - - - - - - - - @@ -3517,7 +3344,7 @@

    给定一个共有 \(n\) 阶的楼梯,你每步可以上 \(1\) 阶或者 \(2\) 阶,请问有多少种方案可以爬到楼顶。

    如图 14-1 所示,对于一个 \(3\) 阶楼梯,共有 \(3\) 种方案可以爬到楼顶。

    -

    爬到第 3 阶的方案数量

    +

    爬到第 3 阶的方案数量

    图 14-1   爬到第 3 阶的方案数量

    本题的目标是求解方案数量,我们可以考虑通过回溯来穷举所有可能性。具体来说,将爬楼梯想象为一个多轮选择的过程:从地面出发,每轮选择上 \(1\) 阶或 \(2\) 阶,每当到达楼梯顶部时就将方案数量加 \(1\) ,当越过楼梯顶部时就将其剪枝。

    @@ -3852,7 +3679,7 @@ dp[i-1], dp[i-2], \dots, dp[2], dp[1] dp[i] = dp[i-1] + dp[i-2] \]

    这意味着在爬楼梯问题中,各个子问题之间存在递推关系,原问题的解可以由子问题的解构建得来。图 14-2 展示了该递推关系。

    -

    方案数量递推关系

    +

    方案数量递推关系

    图 14-2   方案数量递推关系

    我们可以根据递推公式得到暴力搜索解法。以 \(dp[n]\) 为起始点,递归地将一个较大问题拆解为两个较小问题的和,直至到达最小子问题 \(dp[1]\)\(dp[2]\) 时返回。其中,最小子问题的解是已知的,即 \(dp[1] = 1\)\(dp[2] = 2\) ,表示爬到第 \(1\)\(2\) 阶分别有 \(1\)\(2\) 种方案。

    @@ -4052,7 +3879,7 @@ dp[i] = dp[i-1] + dp[i-2]

    图 14-3 展示了暴力搜索形成的递归树。对于问题 \(dp[n]\) ,其递归树的深度为 \(n\) ,时间复杂度为 \(O(2^n)\) 。指数阶属于爆炸式增长,如果我们输入一个比较大的 \(n\) ,则会陷入漫长的等待之中。

    -

    爬楼梯对应递归树

    +

    爬楼梯对应递归树

    图 14-3   爬楼梯对应递归树

    观察图 14-3 ,指数阶的时间复杂度是由于“重叠子问题”导致的。例如 \(dp[9]\) 被分解为 \(dp[8]\)\(dp[7]\)\(dp[8]\) 被分解为 \(dp[7]\)\(dp[6]\) ,两者都包含子问题 \(dp[7]\)

    @@ -4339,7 +4166,7 @@ dp[i] = dp[i-1] + dp[i-2]

    观察图 14-4 ,经过记忆化处理后,所有重叠子问题都只需被计算一次,时间复杂度被优化至 \(O(n)\) ,这是一个巨大的飞跃。

    -

    记忆化搜索对应递归树

    +

    记忆化搜索对应递归树

    图 14-4   记忆化搜索对应递归树

    14.1.3   方法三:动态规划

    @@ -4551,7 +4378,7 @@ dp[i] = dp[i-1] + dp[i-2]

    图 14-5 模拟了以上代码的执行过程。

    -

    爬楼梯的动态规划过程

    +

    爬楼梯的动态规划过程

    图 14-5   爬楼梯的动态规划过程

    与回溯算法一样,动态规划也使用“状态”概念来表示问题求解的某个特定阶段,每个状态都对应一个子问题以及相应的局部最优解。例如,爬楼梯问题的状态定义为当前所在楼梯阶数 \(i\)

    @@ -4897,10 +4724,15 @@ aria-label="页脚" @@ -4969,5 +4801,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_dynamic_programming/knapsack_problem/index.html b/chapter_dynamic_programming/knapsack_problem/index.html index 83ab02045..17f6a914d 100644 --- a/chapter_dynamic_programming/knapsack_problem/index.html +++ b/chapter_dynamic_programming/knapsack_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2854,14 +2769,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2949,14 +2848,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2977,14 +2868,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3005,14 +2888,6 @@ 14.7   小结 - - - - - - - - @@ -3071,14 +2946,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3110,14 +2977,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3138,14 +2997,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3166,14 +3017,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3194,14 +3037,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3222,14 +3057,6 @@ 15.5   小结 - - - - - - - - @@ -3517,7 +3344,7 @@

    给定 \(n\) 个物品,第 \(i\) 个物品的重量为 \(wgt[i-1]\)、价值为 \(val[i-1]\) ,和一个容量为 \(cap\) 的背包。每个物品只能选择一次,问在不超过背包容量下能放入物品的最大价值。

    观察图 14-17 ,由于物品编号 \(i\)\(1\) 开始计数,数组索引从 \(0\) 开始计数,因此物品 \(i\) 对应重量 \(wgt[i-1]\) 和价值 \(val[i-1]\)

    -

    0-1 背包的示例数据

    +

    0-1 背包的示例数据

    图 14-17   0-1 背包的示例数据

    我们可以将 0-1 背包问题看作是一个由 \(n\) 轮决策组成的过程,每个物体都有不放入和放入两种决策,因此该问题是满足决策树模型的。

    @@ -3770,7 +3597,7 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])

    如图 14-18 所示,由于每个物品都会产生不选和选两条搜索分支,因此时间复杂度为 \(O(2^n)\)

    观察递归树,容易发现其中存在重叠子问题,例如 \(dp[1, 10]\) 等。而当物品较多、背包容量较大,尤其是相同重量的物品较多时,重叠子问题的数量将会大幅增多。

    -

    0-1 背包的暴力搜索递归树

    +

    0-1 背包的暴力搜索递归树

    图 14-18   0-1 背包的暴力搜索递归树

    2.   方法二:记忆化搜索

    @@ -4061,7 +3888,7 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])

    图 14-19 展示了在记忆化递归中被剪掉的搜索分支。

    -

    0-1 背包的记忆化搜索递归树

    +

    0-1 背包的记忆化搜索递归树

    图 14-19   0-1 背包的记忆化搜索递归树

    3.   方法三:动态规划

    @@ -4333,46 +4160,46 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
    -

    0-1 背包的动态规划过程

    +

    0-1 背包的动态规划过程

    -

    knapsack_dp_step2

    +

    knapsack_dp_step2

    -

    knapsack_dp_step3

    +

    knapsack_dp_step3

    -

    knapsack_dp_step4

    +

    knapsack_dp_step4

    -

    knapsack_dp_step5

    +

    knapsack_dp_step5

    -

    knapsack_dp_step6

    +

    knapsack_dp_step6

    -

    knapsack_dp_step7

    +

    knapsack_dp_step7

    -

    knapsack_dp_step8

    +

    knapsack_dp_step8

    -

    knapsack_dp_step9

    +

    knapsack_dp_step9

    -

    knapsack_dp_step10

    +

    knapsack_dp_step10

    -

    knapsack_dp_step11

    +

    knapsack_dp_step11

    -

    knapsack_dp_step12

    +

    knapsack_dp_step12

    -

    knapsack_dp_step13

    +

    knapsack_dp_step13

    -

    knapsack_dp_step14

    +

    knapsack_dp_step14

    @@ -4389,22 +4216,22 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
    -

    0-1 背包的空间优化后的动态规划过程

    +

    0-1 背包的空间优化后的动态规划过程

    -

    knapsack_dp_comp_step2

    +

    knapsack_dp_comp_step2

    -

    knapsack_dp_comp_step3

    +

    knapsack_dp_comp_step3

    -

    knapsack_dp_comp_step4

    +

    knapsack_dp_comp_step4

    -

    knapsack_dp_comp_step5

    +

    knapsack_dp_comp_step5

    -

    knapsack_dp_comp_step6

    +

    knapsack_dp_comp_step6

    @@ -4809,10 +4636,15 @@ aria-label="页脚" @@ -4881,5 +4713,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_dynamic_programming/summary/index.html b/chapter_dynamic_programming/summary/index.html index 61a7429c1..1b52c2ae8 100644 --- a/chapter_dynamic_programming/summary/index.html +++ b/chapter_dynamic_programming/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2938,14 +2829,6 @@ 14.7   小结 - - - - - - - - @@ -3005,14 +2888,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3044,14 +2919,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3072,14 +2939,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3100,14 +2959,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3128,14 +2979,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3156,14 +2999,6 @@ 15.5   小结 - - - - - - - - @@ -3594,10 +3429,15 @@ aria-label="页脚" @@ -3666,5 +3506,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_dynamic_programming/unbounded_knapsack_problem/index.html b/chapter_dynamic_programming/unbounded_knapsack_problem/index.html index c789349b5..59b7cc3dd 100644 --- a/chapter_dynamic_programming/unbounded_knapsack_problem/index.html +++ b/chapter_dynamic_programming/unbounded_knapsack_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2882,14 +2789,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3051,14 +2942,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3079,14 +2962,6 @@ 14.7   小结 - - - - - - - - @@ -3145,14 +3020,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3184,14 +3051,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3212,14 +3071,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3240,14 +3091,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3268,14 +3111,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3296,14 +3131,6 @@ 15.5   小结 - - - - - - - - @@ -3664,7 +3491,7 @@

    Question

    给定 \(n\) 个物品,第 \(i\) 个物品的重量为 \(wgt[i-1]\)、价值为 \(val[i-1]\) ,和一个容量为 \(cap\) 的背包。每个物品可以重复选取,问在不超过背包容量下能放入物品的最大价值。

    -

    完全背包问题的示例数据

    +

    完全背包问题的示例数据

    图 14-22   完全背包问题的示例数据

    1.   动态规划思路

    @@ -3953,22 +3780,22 @@ dp[i, c] = \max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1])
    -

    完全背包的空间优化后的动态规划过程

    +

    完全背包的空间优化后的动态规划过程

    -

    unbounded_knapsack_dp_comp_step2

    +

    unbounded_knapsack_dp_comp_step2

    -

    unbounded_knapsack_dp_comp_step3

    +

    unbounded_knapsack_dp_comp_step3

    -

    unbounded_knapsack_dp_comp_step4

    +

    unbounded_knapsack_dp_comp_step4

    -

    unbounded_knapsack_dp_comp_step5

    +

    unbounded_knapsack_dp_comp_step5

    -

    unbounded_knapsack_dp_comp_step6

    +

    unbounded_knapsack_dp_comp_step6

    @@ -4232,7 +4059,7 @@ dp[i, c] = \max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1])

    Question

    给定 \(n\) 种硬币,第 \(i\) 种硬币的面值为 \(coins[i - 1]\) ,目标金额为 \(amt\)每种硬币可以重复选取,问能够凑出目标金额的最少硬币个数。如果无法凑出目标金额则返回 \(-1\)

    -

    零钱兑换问题的示例数据

    +

    零钱兑换问题的示例数据

    图 14-24   零钱兑换问题的示例数据

    1.   动态规划思路

    @@ -4579,49 +4406,49 @@ dp[i, a] = \min(dp[i-1, a], dp[i, a - coins[i-1]] + 1)
    -

    零钱兑换问题的动态规划过程

    +

    零钱兑换问题的动态规划过程

    -

    coin_change_dp_step2

    +

    coin_change_dp_step2

    -

    coin_change_dp_step3

    +

    coin_change_dp_step3

    -

    coin_change_dp_step4

    +

    coin_change_dp_step4

    -

    coin_change_dp_step5

    +

    coin_change_dp_step5

    -

    coin_change_dp_step6

    +

    coin_change_dp_step6

    -

    coin_change_dp_step7

    +

    coin_change_dp_step7

    -

    coin_change_dp_step8

    +

    coin_change_dp_step8

    -

    coin_change_dp_step9

    +

    coin_change_dp_step9

    -

    coin_change_dp_step10

    +

    coin_change_dp_step10

    -

    coin_change_dp_step11

    +

    coin_change_dp_step11

    -

    coin_change_dp_step12

    +

    coin_change_dp_step12

    -

    coin_change_dp_step13

    +

    coin_change_dp_step13

    -

    coin_change_dp_step14

    +

    coin_change_dp_step14

    -

    coin_change_dp_step15

    +

    coin_change_dp_step15

    @@ -4917,7 +4744,7 @@ dp[i, a] = \min(dp[i-1, a], dp[i, a - coins[i-1]] + 1)

    Question

    给定 \(n\) 种硬币,第 \(i\) 种硬币的面值为 \(coins[i - 1]\) ,目标金额为 \(amt\) ,每种硬币可以重复选取,问在凑出目标金额的硬币组合数量

    -

    零钱兑换问题 II 的示例数据

    +

    零钱兑换问题 II 的示例数据

    图 14-26   零钱兑换问题 II 的示例数据

    1.   动态规划思路

    @@ -5648,10 +5475,15 @@ aria-label="页脚" @@ -5720,5 +5552,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_graph/graph/index.html b/chapter_graph/graph/index.html index 952eb250f..e56789152 100644 --- a/chapter_graph/graph/index.html +++ b/chapter_graph/graph/index.html @@ -60,7 +60,18 @@ - + @@ -1958,14 +1969,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1986,14 +1989,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2413,14 +2408,6 @@ 第 12 章   分治 - - - - - - - - @@ -2452,14 +2439,6 @@ 12.1   分治算法 - - - - - - - - @@ -2480,14 +2459,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2508,14 +2479,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2536,14 +2499,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2564,14 +2519,6 @@ 12.5   小结 - - - - - - - - @@ -2803,14 +2750,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2842,14 +2781,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2870,14 +2801,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2898,14 +2821,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2926,14 +2841,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2954,14 +2861,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2982,14 +2881,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3010,14 +2901,6 @@ 14.7   小结 - - - - - - - - @@ -3076,14 +2959,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3115,14 +2990,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3143,14 +3010,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3171,14 +3030,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3199,14 +3050,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3227,14 +3070,6 @@ 15.5   小结 - - - - - - - - @@ -3537,7 +3372,7 @@ G & = \{ V, E \} \newline \end{aligned} \]

    如果将顶点看作节点,将边看作连接各个节点的引用(指针),我们就可以将图看作是一种从链表拓展而来的数据结构。如图 9-1 所示,相较于线性关系(链表)和分治关系(树),网络关系(图)的自由度更高,从而更为复杂。

    -

    链表、树、图之间的关系

    +

    链表、树、图之间的关系

    图 9-1   链表、树、图之间的关系

    9.1.1   图常见类型与术语

    @@ -3546,7 +3381,7 @@ G & = \{ V, E \} \newline
  • 在无向图中,边表示两顶点之间的“双向”连接关系,例如微信或 QQ 中的“好友关系”。
  • 在有向图中,边具有方向性,即 \(A \rightarrow B\)\(A \leftarrow B\) 两个方向的边是相互独立的,例如微博或抖音上的“关注”与“被关注”关系。
  • -

    有向图与无向图

    +

    有向图与无向图

    图 9-2   有向图与无向图

    根据所有顶点是否连通,可分为图 9-3 所示的「连通图 connected graph」和「非连通图 disconnected graph」。

    @@ -3554,11 +3389,11 @@ G & = \{ V, E \} \newline
  • 对于连通图,从某个顶点出发,可以到达其余任意顶点。
  • 对于非连通图,从某个顶点出发,至少有一个顶点无法到达。
  • -

    连通图与非连通图

    +

    连通图与非连通图

    图 9-3   连通图与非连通图

    我们还可以为边添加“权重”变量,从而得到图 9-4 所示的「有权图 weighted graph」。例如在王者荣耀等手游中,系统会根据共同游戏时间来计算玩家之间的“亲密度”,这种亲密度网络就可以用有权图来表示。

    -

    有权图与无权图

    +

    有权图与无权图

    图 9-4   有权图与无权图

    图数据结构包含以下常用术语。

    @@ -3572,7 +3407,7 @@ G & = \{ V, E \} \newline

    1.   邻接矩阵

    设图的顶点数量为 \(n\) ,「邻接矩阵 adjacency matrix」使用一个 \(n \times n\) 大小的矩阵来表示图,每一行(列)代表一个顶点,矩阵元素代表边,用 \(1\)\(0\) 表示两个顶点之间是否存在边。

    如图 9-5 所示,设邻接矩阵为 \(M\)、顶点列表为 \(V\) ,那么矩阵元素 \(M[i, j] = 1\) 表示顶点 \(V[i]\) 到顶点 \(V[j]\) 之间存在边,反之 \(M[i, j] = 0\) 表示两顶点之间无边。

    -

    图的邻接矩阵表示

    +

    图的邻接矩阵表示

    图 9-5   图的邻接矩阵表示

    邻接矩阵具有以下特性。

    @@ -3584,7 +3419,7 @@ G & = \{ V, E \} \newline

    使用邻接矩阵表示图时,我们可以直接访问矩阵元素以获取边,因此增删查操作的效率很高,时间复杂度均为 \(O(1)\) 。然而,矩阵的空间复杂度为 \(O(n^2)\) ,内存占用较多。

    2.   邻接表

    「邻接表 adjacency list」使用 \(n\) 个链表来表示图,链表节点表示顶点。第 \(i\) 条链表对应顶点 \(i\) ,其中存储了该顶点的所有邻接顶点(即与该顶点相连的顶点)。图 9-6 展示了一个使用邻接表存储的图的示例。

    -

    图的邻接表表示

    +

    图的邻接表表示

    图 9-6   图的邻接表表示

    邻接表仅存储实际存在的边,而边的总数通常远小于 \(n^2\) ,因此它更加节省空间。然而,在邻接表中需要通过遍历链表来查找边,因此其时间效率不如邻接矩阵。

    @@ -3788,10 +3623,15 @@ aria-label="页脚" @@ -3860,5 +3700,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_graph/graph_operations/index.html b/chapter_graph/graph_operations/index.html index 1bae3435a..852d4d61f 100644 --- a/chapter_graph/graph_operations/index.html +++ b/chapter_graph/graph_operations/index.html @@ -60,7 +60,18 @@ - + @@ -1938,14 +1949,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1966,14 +1969,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3500,19 +3335,19 @@
    -

    邻接矩阵的初始化、增删边、增删顶点

    +

    邻接矩阵的初始化、增删边、增删顶点

    -

    adjacency_matrix_add_edge

    +

    adjacency_matrix_add_edge

    -

    adjacency_matrix_remove_edge

    +

    adjacency_matrix_remove_edge

    -

    adjacency_matrix_add_vertex

    +

    adjacency_matrix_add_vertex

    -

    adjacency_matrix_remove_vertex

    +

    adjacency_matrix_remove_vertex

    @@ -4599,19 +4434,19 @@
    -

    邻接表的初始化、增删边、增删顶点

    +

    邻接表的初始化、增删边、增删顶点

    -

    adjacency_list_add_edge

    +

    adjacency_list_add_edge

    -

    adjacency_list_remove_edge

    +

    adjacency_list_remove_edge

    -

    adjacency_list_add_vertex

    +

    adjacency_list_add_vertex

    -

    adjacency_list_remove_vertex

    +

    adjacency_list_remove_vertex

    @@ -5776,10 +5611,15 @@ aria-label="页脚" @@ -5848,5 +5688,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_graph/graph_traversal/index.html b/chapter_graph/graph_traversal/index.html index 245e2abd9..03eb058f6 100644 --- a/chapter_graph/graph_traversal/index.html +++ b/chapter_graph/graph_traversal/index.html @@ -60,7 +60,18 @@ - + @@ -1971,14 +1982,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1999,14 +2002,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2426,14 +2421,6 @@ 第 12 章   分治 - - - - - - - - @@ -2465,14 +2452,6 @@ 12.1   分治算法 - - - - - - - - @@ -2493,14 +2472,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2521,14 +2492,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2549,14 +2512,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2577,14 +2532,6 @@ 12.5   小结 - - - - - - - - @@ -2816,14 +2763,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2855,14 +2794,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2883,14 +2814,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2911,14 +2834,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2939,14 +2854,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2967,14 +2874,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2995,14 +2894,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3023,14 +2914,6 @@ 14.7   小结 - - - - - - - - @@ -3089,14 +2972,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3128,14 +3003,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3156,14 +3023,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3184,14 +3043,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3212,14 +3063,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3240,14 +3083,6 @@ 15.5   小结 - - - - - - - - @@ -3558,7 +3393,7 @@

    图和树都都需要应用搜索算法来实现遍历操作。图的遍历方式可分为两种:「广度优先遍历 breadth-first traversal」和「深度优先遍历 depth-first traversal」。它们也常被称为「广度优先搜索 breadth-first search」和「深度优先搜索 depth-first search」,简称 BFS 和 DFS 。

    9.3.1   广度优先遍历

    广度优先遍历是一种由近及远的遍历方式,从某个节点出发,始终优先访问距离最近的顶点,并一层层向外扩张。如图 9-9 所示,从左上角顶点出发,先遍历该顶点的所有邻接顶点,然后遍历下一个顶点的所有邻接顶点,以此类推,直至所有顶点访问完毕。

    -

    图的广度优先遍历

    +

    图的广度优先遍历

    图 9-9   图的广度优先遍历

    1.   算法实现

    @@ -3915,37 +3750,37 @@
    -

    图的广度优先遍历步骤

    +

    图的广度优先遍历步骤

    -

    graph_bfs_step2

    +

    graph_bfs_step2

    -

    graph_bfs_step3

    +

    graph_bfs_step3

    -

    graph_bfs_step4

    +

    graph_bfs_step4

    -

    graph_bfs_step5

    +

    graph_bfs_step5

    -

    graph_bfs_step6

    +

    graph_bfs_step6

    -

    graph_bfs_step7

    +

    graph_bfs_step7

    -

    graph_bfs_step8

    +

    graph_bfs_step8

    -

    graph_bfs_step9

    +

    graph_bfs_step9

    -

    graph_bfs_step10

    +

    graph_bfs_step10

    -

    graph_bfs_step11

    +

    graph_bfs_step11

    @@ -3960,7 +3795,7 @@

    空间复杂度: 列表 res ,哈希表 visited ,队列 que 中的顶点数量最多为 \(|V|\) ,使用 \(O(|V|)\) 空间。

    9.3.2   深度优先遍历

    深度优先遍历是一种优先走到底、无路可走再回头的遍历方式。如图 9-11 所示,从左上角顶点出发,访问当前顶点的某个邻接顶点,直到走到尽头时返回,再继续走到尽头并返回,以此类推,直至所有顶点遍历完成。

    -

    图的深度优先遍历

    +

    图的深度优先遍历

    图 9-11   图的深度优先遍历

    1.   算法实现

    @@ -4300,37 +4135,37 @@
    -

    图的深度优先遍历步骤

    +

    图的深度优先遍历步骤

    -

    graph_dfs_step2

    +

    graph_dfs_step2

    -

    graph_dfs_step3

    +

    graph_dfs_step3

    -

    graph_dfs_step4

    +

    graph_dfs_step4

    -

    graph_dfs_step5

    +

    graph_dfs_step5

    -

    graph_dfs_step6

    +

    graph_dfs_step6

    -

    graph_dfs_step7

    +

    graph_dfs_step7

    -

    graph_dfs_step8

    +

    graph_dfs_step8

    -

    graph_dfs_step9

    +

    graph_dfs_step9

    -

    graph_dfs_step10

    +

    graph_dfs_step10

    -

    graph_dfs_step11

    +

    graph_dfs_step11

    @@ -4507,10 +4342,15 @@ aria-label="页脚" @@ -4579,5 +4419,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_graph/index.html b/chapter_graph/index.html index 776fa26f7..1d038a20c 100644 --- a/chapter_graph/index.html +++ b/chapter_graph/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 9 章   图

    -

    图

    +

    图

    Abstract

    @@ -3591,10 +3426,15 @@ aria-label="页脚" @@ -3663,5 +3503,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_graph/summary/index.html b/chapter_graph/summary/index.html index 755381911..283a4b0ed 100644 --- a/chapter_graph/summary/index.html +++ b/chapter_graph/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3665,10 +3500,15 @@ aria-label="页脚" @@ -3737,5 +3577,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_greedy/fractional_knapsack_problem/index.html b/chapter_greedy/fractional_knapsack_problem/index.html index 2f3cfbf66..39bc6733b 100644 --- a/chapter_greedy/fractional_knapsack_problem/index.html +++ b/chapter_greedy/fractional_knapsack_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3071,14 +2938,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3159,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3187,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3215,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3500,7 +3327,7 @@

    Question

    给定 \(n\) 个物品,第 \(i\) 个物品的重量为 \(wgt[i-1]\)、价值为 \(val[i-1]\) ,和一个容量为 \(cap\) 的背包。每个物品只能选择一次,但可以选择物品的一部分,价值根据选择的重量比例计算,问在不超过背包容量下背包中物品的最大价值。

    -

    分数背包问题的示例数据

    +

    分数背包问题的示例数据

    图 15-3   分数背包问题的示例数据

    分数背包和 0-1 背包整体上非常相似,状态包含当前物品 \(i\) 和容量 \(c\) ,目标是求不超过背包容量下的最大价值。

    @@ -3509,7 +3336,7 @@
  • 对于物品 \(i\) ,它在单位重量下的价值为 \(val[i-1] / wgt[i-1]\) ,简称为单位价值。
  • 假设放入一部分物品 \(i\) ,重量为 \(w\) ,则背包增加的价值为 \(w \times val[i-1] / wgt[i-1]\)
  • -

    物品在单位重量下的价值

    +

    物品在单位重量下的价值

    图 15-4   物品在单位重量下的价值

    1.   贪心策略确定

    @@ -3519,7 +3346,7 @@
  • 遍历所有物品,每轮贪心地选择单位价值最高的物品
  • 若剩余背包容量不足,则使用当前物品的一部分填满背包即可。
  • -

    分数背包的贪心策略

    +

    分数背包的贪心策略

    图 15-5   分数背包的贪心策略

    2.   代码实现

    @@ -3943,7 +3770,7 @@

    现在从背包中拿出单位重量的任意物品,并替换为单位重量的物品 \(x\) 。由于物品 \(x\) 的单位价值最高,因此替换后的总价值一定大于 res这与 res 是最优解矛盾,说明最优解中必须包含物品 \(x\)

    对于该解中的其他物品,我们也可以构建出上述矛盾。总而言之,单位价值更大的物品总是更优选择,这说明贪心策略是有效的。

    如图 15-6 所示,如果将物品重量和物品单位价值分别看作一个 2D 图表的横轴和纵轴,则分数背包问题可被转化为“求在有限横轴区间下的最大围成面积”。这个类比可以帮助我们从几何角度理解贪心策略的有效性。

    -

    分数背包问题的几何表示

    +

    分数背包问题的几何表示

    图 15-6   分数背包问题的几何表示

    @@ -4108,10 +3935,15 @@ aria-label="页脚" @@ -4180,5 +4012,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_greedy/greedy_algorithm/index.html b/chapter_greedy/greedy_algorithm/index.html index cbabb7baf..b7bf55e10 100644 --- a/chapter_greedy/greedy_algorithm/index.html +++ b/chapter_greedy/greedy_algorithm/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3043,14 +2918,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3138,14 +2997,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3166,14 +3017,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3194,14 +3037,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3222,14 +3057,6 @@ 15.5   小结 - - - - - - - - @@ -3522,7 +3349,7 @@

    给定 \(n\) 种硬币,第 \(i\) 种硬币的面值为 \(coins[i - 1]\) ,目标金额为 \(amt\) ,每种硬币可以重复选取,问能够凑出目标金额的最少硬币个数。如果无法凑出目标金额则返回 \(-1\)

    本题的贪心策略如图 15-1 所示。给定目标金额,我们贪心地选择不大于且最接近它的硬币,不断循环该步骤,直至凑出目标金额为止。

    -

    零钱兑换的贪心策略

    +

    零钱兑换的贪心策略

    图 15-1   零钱兑换的贪心策略

    实现代码如下所示。你可能会不由地发出感叹:So Clean !贪心算法仅用十行代码就解决了零钱兑换问题。

    @@ -3778,7 +3605,7 @@
  • 反例 \(coins = [1, 20, 50]\):假设 \(amt = 60\) ,贪心算法只能找到 \(50 + 1 \times 10\) 的兑换组合,共计 \(11\) 枚硬币,但动态规划可以找到最优解 \(20 + 20 + 20\) ,仅需 \(3\) 枚硬币。
  • 反例 \(coins = [1, 49, 50]\):假设 \(amt = 98\) ,贪心算法只能找到 \(50 + 1 \times 48\) 的兑换组合,共计 \(49\) 枚硬币,但动态规划可以找到最优解 \(49 + 49\) ,仅需 \(2\) 枚硬币。
  • -

    贪心无法找出最优解的示例

    +

    贪心无法找出最优解的示例

    图 15-2   贪心无法找出最优解的示例

    也就是说,对于零钱兑换问题,贪心算法无法保证找到全局最优解,并且有可能找到非常差的解。它更适合用动态规划解决。

    @@ -3989,10 +3816,15 @@ aria-label="页脚" @@ -4061,5 +3893,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_greedy/index.html b/chapter_greedy/index.html index 820da4621..0645336c6 100644 --- a/chapter_greedy/index.html +++ b/chapter_greedy/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 15 章   贪心

    -

    贪心

    +

    贪心

    Abstract

    @@ -3592,10 +3427,15 @@ aria-label="页脚" @@ -3664,5 +3504,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_greedy/max_capacity_problem/index.html b/chapter_greedy/max_capacity_problem/index.html index f592d2d96..f671218bc 100644 --- a/chapter_greedy/max_capacity_problem/index.html +++ b/chapter_greedy/max_capacity_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3099,14 +2958,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3187,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3215,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3502,7 +3329,7 @@

    容器的容量等于高度和宽度的乘积(即面积),其中高度由较短的隔板决定,宽度是两个隔板的数组索引之差。

    请在数组中选择两个隔板,使得组成的容器的容量最大,返回最大容量。

    -

    最大容量问题的示例数据

    +

    最大容量问题的示例数据

    图 15-7   最大容量问题的示例数据

    容器由任意两个隔板围成,因此本题的状态为两个隔板的索引,记为 \([i, j]\)

    @@ -3513,16 +3340,16 @@ cap[i, j] = \min(ht[i], ht[j]) \times (j - i)

    设数组长度为 \(n\) ,两个隔板的组合数量(即状态总数)为 \(C_n^2 = \frac{n(n - 1)}{2}\) 个。最直接地,我们可以穷举所有状态,从而求得最大容量,时间复杂度为 \(O(n^2)\)

    1.   贪心策略确定

    这道题还有更高效率的解法。如图 15-8 所示,现选取一个状态 \([i, j]\) ,其满足索引 \(i < j\) 且高度 \(ht[i] < ht[j]\) ,即 \(i\) 为短板、\(j\) 为长板。

    -

    初始状态

    +

    初始状态

    图 15-8   初始状态

    如图 15-9 所示,若此时将长板 \(j\) 向短板 \(i\) 靠近,则容量一定变小

    这是因为在移动长板 \(j\) 后,宽度 \(j-i\) 肯定变小;而高度由短板决定,因此高度只可能不变( \(i\) 仍为短板)或变小(移动后的 \(j\) 成为短板)。

    -

    向内移动长板后的状态

    +

    向内移动长板后的状态

    图 15-9   向内移动长板后的状态

    反向思考,我们只有向内收缩短板 \(i\) ,才有可能使容量变大。因为虽然宽度一定变小,但高度可能会变大(移动后的短板 \(i\) 可能会变长)。例如在图 15-10 中,移动短板后面积变大。

    -

    向内移动短板后的状态

    +

    向内移动短板后的状态

    图 15-10   向内移动短板后的状态

    由此便可推出本题的贪心策略:初始化两指针分裂容器两端,每轮向内收缩短板对应的指针,直至两指针相遇。

    @@ -3536,31 +3363,31 @@ cap[i, j] = \min(ht[i], ht[j]) \times (j - i)
    -

    最大容量问题的贪心过程

    +

    最大容量问题的贪心过程

    -

    max_capacity_greedy_step2

    +

    max_capacity_greedy_step2

    -

    max_capacity_greedy_step3

    +

    max_capacity_greedy_step3

    -

    max_capacity_greedy_step4

    +

    max_capacity_greedy_step4

    -

    max_capacity_greedy_step5

    +

    max_capacity_greedy_step5

    -

    max_capacity_greedy_step6

    +

    max_capacity_greedy_step6

    -

    max_capacity_greedy_step7

    +

    max_capacity_greedy_step7

    -

    max_capacity_greedy_step8

    +

    max_capacity_greedy_step8

    -

    max_capacity_greedy_step9

    +

    max_capacity_greedy_step9

    @@ -3837,7 +3664,7 @@ cap[i, j] = \min(ht[i], ht[j]) \times (j - i)
    \[ cap[i, i+1], cap[i, i+2], \dots, cap[i, j-2], cap[i, j-1] \]
    -

    移动短板导致被跳过的状态

    +

    移动短板导致被跳过的状态

    图 15-12   移动短板导致被跳过的状态

    观察发现,这些被跳过的状态实际上就是将长板 \(j\) 向内移动的所有状态。而在第二步中,我们已经证明内移长板一定会导致容量变小。也就是说,被跳过的状态都不可能是最优解,跳过它们不会导致错过最优解

    @@ -4005,10 +3832,15 @@ aria-label="页脚" @@ -4077,5 +3909,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_greedy/max_product_cutting_problem/index.html b/chapter_greedy/max_product_cutting_problem/index.html index 2c666f95a..1573ea87b 100644 --- a/chapter_greedy/max_product_cutting_problem/index.html +++ b/chapter_greedy/max_product_cutting_problem/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3127,14 +2978,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3215,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3500,7 +3327,7 @@

    Question

    给定一个正整数 \(n\) ,将其切分为至少两个正整数的和,求切分后所有整数的乘积最大是多少。

    -

    最大切分乘积的问题定义

    +

    最大切分乘积的问题定义

    图 15-13   最大切分乘积的问题定义

    假设我们将 \(n\) 切分为 \(m\) 个整数因子,其中第 \(i\) 个因子记为 \(n_i\) ,即

    @@ -3523,13 +3350,13 @@ n & \geq 4 \]

    如图 15-14 所示,当 \(n \geq 4\) 时,切分出一个 \(2\) 后乘积会变大,这说明大于等于 \(4\) 的整数都应该被切分

    贪心策略一:如果切分方案中包含 \(\geq 4\) 的因子,那么它就应该被继续切分。最终的切分方案只应出现 \(1\)\(2\)\(3\) 这三种因子。

    -

    切分导致乘积变大

    +

    切分导致乘积变大

    图 15-14   切分导致乘积变大

    接下来思考哪个因子是最优的。在 \(1\)\(2\)\(3\) 这三个因子中,显然 \(1\) 是最差的,因为 \(1 \times (n-1) < n\) 恒成立,即切分出 \(1\) 反而会导致乘积减小。

    如图 15-15 所示,当 \(n = 6\) 时,有 \(3 \times 3 > 2 \times 2 \times 2\)这意味着切分出 \(3\) 比切分出 \(2\) 更优

    贪心策略二:在切分方案中,最多只应存在两个 \(2\) 。因为三个 \(2\) 总是可以被替换为两个 \(3\) ,从而获得更大乘积。

    -

    最优切分因子

    +

    最优切分因子

    图 15-15   最优切分因子

    总结以上,可推出以下贪心策略。

    @@ -3801,7 +3628,7 @@ n = 3 a + b -

    最大切分乘积的计算方法

    +

    最大切分乘积的计算方法

    图 15-16   最大切分乘积的计算方法

    时间复杂度取决于编程语言的幂运算的实现方法。以 Python 为例,常用的幂计算函数有三种。

    @@ -3980,10 +3807,15 @@ aria-label="页脚" @@ -4052,5 +3884,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_greedy/summary/index.html b/chapter_greedy/summary/index.html index b3977d005..f9d66879e 100644 --- a/chapter_greedy/summary/index.html +++ b/chapter_greedy/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3155,14 +2998,6 @@ 15.5   小结 - - - - - - - - @@ -3583,10 +3418,15 @@ aria-label="页脚" @@ -3655,5 +3495,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_hashing/hash_algorithm/index.html b/chapter_hashing/hash_algorithm/index.html index 47deb47fa..702234871 100644 --- a/chapter_hashing/hash_algorithm/index.html +++ b/chapter_hashing/hash_algorithm/index.html @@ -60,7 +60,18 @@ - + @@ -1945,14 +1956,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1973,14 +1976,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2400,14 +2395,6 @@ 第 12 章   分治 - - - - - - - - @@ -2439,14 +2426,6 @@ 12.1   分治算法 - - - - - - - - @@ -2467,14 +2446,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2495,14 +2466,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2523,14 +2486,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2551,14 +2506,6 @@ 12.5   小结 - - - - - - - - @@ -2790,14 +2737,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2829,14 +2768,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2857,14 +2788,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2885,14 +2808,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2913,14 +2828,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2941,14 +2848,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2969,14 +2868,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2997,14 +2888,6 @@ 14.7   小结 - - - - - - - - @@ -3063,14 +2946,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3102,14 +2977,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3130,14 +2997,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3158,14 +3017,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3186,14 +3037,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3214,14 +3057,6 @@ 15.5   小结 - - - - - - - - @@ -3504,7 +3339,7 @@

    6.3   哈希算法

    在上两节中,我们了解了哈希表的工作原理和哈希冲突的处理方法。然而无论是开放寻址还是链地址法,它们只能保证哈希表可以在发生冲突时正常工作,但无法减少哈希冲突的发生

    如果哈希冲突过于频繁,哈希表的性能则会急剧劣化。如图 6-8 所示,对于链地址哈希表,理想情况下键值对平均分布在各个桶中,达到最佳查询效率;最差情况下所有键值对都被存储到同一个桶中,时间复杂度退化至 \(O(n)\)

    -

    哈希冲突的最佳与最差情况

    +

    哈希冲突的最佳与最差情况

    图 6-8   哈希冲突的最佳与最差情况

    键值对的分布情况由哈希函数决定。回忆哈希函数的计算步骤,先计算哈希值,再对数组长度取模:

    @@ -4470,10 +4305,15 @@ aria-label="页脚" @@ -4542,5 +4382,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_hashing/hash_collision/index.html b/chapter_hashing/hash_collision/index.html index 51055509e..7ae7ea204 100644 --- a/chapter_hashing/hash_collision/index.html +++ b/chapter_hashing/hash_collision/index.html @@ -60,7 +60,18 @@ - + @@ -1965,14 +1976,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1993,14 +1996,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2420,14 +2415,6 @@ 第 12 章   分治 - - - - - - - - @@ -2459,14 +2446,6 @@ 12.1   分治算法 - - - - - - - - @@ -2487,14 +2466,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2515,14 +2486,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2543,14 +2506,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2571,14 +2526,6 @@ 12.5   小结 - - - - - - - - @@ -2810,14 +2757,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2849,14 +2788,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2877,14 +2808,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2905,14 +2828,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2933,14 +2848,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2961,14 +2868,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2989,14 +2888,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3017,14 +2908,6 @@ 14.7   小结 - - - - - - - - @@ -3083,14 +2966,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3122,14 +2997,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3150,14 +3017,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3178,14 +3037,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3206,14 +3057,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3234,14 +3077,6 @@ 15.5   小结 - - - - - - - - @@ -3551,7 +3386,7 @@

    哈希表的结构改良方法主要包括“链式地址”和“开放寻址”。

    6.2.1   链式地址

    在原始哈希表中,每个桶仅能存储一个键值对。「链式地址 separate chaining」将单个元素转换为链表,将键值对作为链表节点,将所有发生冲突的键值对都存储在同一链表中。图 6-5 展示了一个链式地址哈希表的例子。

    -

    链式地址哈希表

    +

    链式地址哈希表

    图 6-5   链式地址哈希表

    基于链式地址实现的哈希表的操作方法发生了以下变化。

    @@ -4854,12 +4689,12 @@
  • 查找元素:若发现哈希冲突,则使用相同步长向后线性遍历,直到找到对应元素,返回 value 即可;如果遇到空桶,说明目标元素不在哈希表中,返回 \(\text{None}\)
  • 图 6-6 展示了开放寻址(线性探测)哈希表的键值对分布。根据此哈希函数,最后两位相同的 key 都会被映射到相同的桶。而通过线性探测,它们被依次存储在该桶以及之下的桶中。

    -

    开放寻址和线性探测

    +

    开放寻址和线性探测

    图 6-6   开放寻址和线性探测

    然而,线性探测容易产生“聚集现象”。具体来说,数组中连续被占用的位置越长,这些连续位置发生哈希冲突的可能性越大,从而进一步促使该位置的聚堆生长,形成恶性循环,最终导致增删查改操作效率劣化。

    值得注意的是,我们不能在开放寻址哈希表中直接删除元素。这是因为删除元素会在数组内产生一个空桶 \(\text{None}\) ,而当查询元素时,线性探测到该空桶就会返回,因此在该空桶之下的元素都无法再被访问到,程序可能误判这些元素不存在。

    -

    在开放寻址中删除元素导致的查询问题

    +

    在开放寻址中删除元素导致的查询问题

    图 6-7   在开放寻址中删除元素导致的查询问题

    为了解决该问题,我们可以采用「懒删除 lazy deletion」机制:它不直接从哈希表中移除元素,而是利用一个常量 TOMBSTONE 来标记这个桶。在该机制下,\(\text{None}\)TOMBSTONE 都代表空桶,都可以放置键值对。但不同的是,线性探测到 TOMBSTONE 时应该继续遍历,因为其之下可能还存在键值对。

    @@ -6334,10 +6169,15 @@ aria-label="页脚" @@ -6406,5 +6246,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_hashing/hash_map/index.html b/chapter_hashing/hash_map/index.html index 493200271..23ab59dd6 100644 --- a/chapter_hashing/hash_map/index.html +++ b/chapter_hashing/hash_map/index.html @@ -60,7 +60,18 @@ - + @@ -1938,14 +1949,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1966,14 +1969,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3490,7 +3325,7 @@

    6.1   哈希表

    「哈希表 hash table」,又称「散列表」,其通过建立键 key 与值 value 之间的映射,实现高效的元素查询。具体而言,我们向哈希表输入一个键 key ,则可以在 \(O(1)\) 时间内获取对应的值 value

    如图 6-1 所示,给定 \(n\) 个学生,每个学生都有“姓名”和“学号”两项数据。假如我们希望实现“输入一个学号,返回对应的姓名”的查询功能,则可以采用图 6-1 所示的哈希表来实现。

    -

    哈希表的抽象表示

    +

    哈希表的抽象表示

    图 6-1   哈希表的抽象表示

    除哈希表外,数组和链表也可以实现查询功能,它们的效率对比如表 6-1 所示。

    @@ -3949,7 +3784,7 @@

    随后,我们就可以利用 index 在哈希表中访问对应的桶,从而获取 value

    设数组长度 capacity = 100、哈希算法 hash(key) = key ,易得哈希函数为 key % 100 。图 6-2 以 key 学号和 value 姓名为例,展示了哈希函数的工作原理。

    -

    哈希函数工作原理

    +

    哈希函数工作原理

    图 6-2   哈希函数工作原理

    以下代码实现了一个简单哈希表。其中,我们将 keyvalue 封装成一个类 Pair ,以表示键值对。

    @@ -4956,12 +4791,12 @@ 20336 % 100 = 36

    如图 6-3 所示,两个学号指向了同一个姓名,这显然是不对的。我们将这种多个输入对应同一输出的情况称为「哈希冲突 hash collision」。

    -

    哈希冲突示例

    +

    哈希冲突示例

    图 6-3   哈希冲突示例

    容易想到,哈希表容量 \(n\) 越大,多个 key 被分配到同一个桶中的概率就越低,冲突就越少。因此,我们可以通过扩容哈希表来减少哈希冲突

    如图 6-4 所示,扩容前键值对 (136, A)(236, D) 发生冲突,扩容后冲突消失。

    -

    哈希表扩容

    +

    哈希表扩容

    图 6-4   哈希表扩容

    类似于数组扩容,哈希表扩容需将所有键值对从原哈希表迁移至新哈希表,非常耗时。并且由于哈希表容量 capacity 改变,我们需要通过哈希函数来重新计算所有键值对的存储位置,这进一步提高了扩容过程的计算开销。为此,编程语言通常会预留足够大的哈希表容量,防止频繁扩容。

    @@ -5129,10 +4964,15 @@ aria-label="页脚" @@ -5201,5 +5041,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_hashing/index.html b/chapter_hashing/index.html index 22896f587..c3f8cff65 100644 --- a/chapter_hashing/index.html +++ b/chapter_hashing/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 6 章   哈希表

    -

    哈希表

    +

    哈希表

    Abstract

    @@ -3591,10 +3426,15 @@ aria-label="页脚" @@ -3663,5 +3503,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_hashing/summary/index.html b/chapter_hashing/summary/index.html index f223197ac..aeb92f7b8 100644 --- a/chapter_hashing/summary/index.html +++ b/chapter_hashing/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3680,10 +3515,15 @@ aria-label="页脚" @@ -3752,5 +3592,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_heap/build_heap/index.html b/chapter_heap/build_heap/index.html index c913c8e57..acb3723ad 100644 --- a/chapter_heap/build_heap/index.html +++ b/chapter_heap/build_heap/index.html @@ -60,7 +60,18 @@ - + @@ -1938,14 +1949,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1966,14 +1969,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3665,7 +3500,7 @@

    将上述两者相乘,可得到建堆过程的时间复杂度为 \(O(n \log n)\)但这个估算结果并不准确,因为我们没有考虑到二叉树底层节点数量远多于顶层节点的性质

    接下来我们来进行更为准确的计算。为了减小计算难度,假设给定一个节点数量为 \(n\) ,高度为 \(h\) 的“完美二叉树”,该假设不会影响计算结果的正确性。

    -

    完美二叉树的各层节点数量

    +

    完美二叉树的各层节点数量

    图 8-5   完美二叉树的各层节点数量

    如图 8-5 所示,节点“从顶至底堆化”的最大迭代次数等于该节点到叶节点的距离,而该距离正是“节点高度”。因此,我们可以将各层的“节点数量 \(\times\) 节点高度”求和,从而得到所有节点的堆化迭代次数的总和

    @@ -3855,10 +3690,15 @@ aria-label="页脚" @@ -3927,5 +3767,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_heap/heap/index.html b/chapter_heap/heap/index.html index da9ff1975..2a6cde6ed 100644 --- a/chapter_heap/heap/index.html +++ b/chapter_heap/heap/index.html @@ -60,7 +60,18 @@ - + @@ -1972,14 +1983,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2000,14 +2003,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2427,14 +2422,6 @@ 第 12 章   分治 - - - - - - - - @@ -2466,14 +2453,6 @@ 12.1   分治算法 - - - - - - - - @@ -2494,14 +2473,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2522,14 +2493,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2550,14 +2513,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2578,14 +2533,6 @@ 12.5   小结 - - - - - - - - @@ -2817,14 +2764,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2856,14 +2795,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2884,14 +2815,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2912,14 +2835,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2940,14 +2855,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2968,14 +2875,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2996,14 +2895,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3024,14 +2915,6 @@ 14.7   小结 - - - - - - - - @@ -3090,14 +2973,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3129,14 +3004,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3157,14 +3024,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3185,14 +3044,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3213,14 +3064,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3241,14 +3084,6 @@ 15.5   小结 - - - - - - - - @@ -3561,7 +3396,7 @@
  • 「大顶堆 max heap」:任意节点的值 \(\geq\) 其子节点的值。
  • 「小顶堆 min heap」:任意节点的值 \(\leq\) 其子节点的值。
  • -

    小顶堆与大顶堆

    +

    小顶堆与大顶堆

    图 8-1   小顶堆与大顶堆

    堆作为完全二叉树的一个特例,具有以下特性。

    @@ -3879,7 +3714,7 @@

    我们在二叉树章节中学习到,完全二叉树非常适合用数组来表示。由于堆正是一种完全二叉树,我们将采用数组来存储堆

    当使用数组表示二叉树时,元素代表节点值,索引代表节点在二叉树中的位置。节点指针通过索引映射公式来实现

    如图 8-2 所示,给定索引 \(i\) ,其左子节点索引为 \(2i + 1\) ,右子节点索引为 \(2i + 2\) ,父节点索引为 \((i - 1) / 2\)(向下取整)。当索引越界时,表示空节点或节点不存在。

    -

    堆的表示与存储

    +

    堆的表示与存储

    图 8-2   堆的表示与存储

    我们可以将索引映射公式封装成函数,方便后续使用。

    @@ -4185,31 +4020,31 @@
    -

    元素入堆步骤

    +

    元素入堆步骤

    -

    heap_push_step2

    +

    heap_push_step2

    -

    heap_push_step3

    +

    heap_push_step3

    -

    heap_push_step4

    +

    heap_push_step4

    -

    heap_push_step5

    +

    heap_push_step5

    -

    heap_push_step6

    +

    heap_push_step6

    -

    heap_push_step7

    +

    heap_push_step7

    -

    heap_push_step8

    +

    heap_push_step8

    -

    heap_push_step9

    +

    heap_push_step9

    @@ -4543,34 +4378,34 @@
    -

    堆顶元素出堆步骤

    +

    堆顶元素出堆步骤

    -

    heap_pop_step2

    +

    heap_pop_step2

    -

    heap_pop_step3

    +

    heap_pop_step3

    -

    heap_pop_step4

    +

    heap_pop_step4

    -

    heap_pop_step5

    +

    heap_pop_step5

    -

    heap_pop_step6

    +

    heap_pop_step6

    -

    heap_pop_step7

    +

    heap_pop_step7

    -

    heap_pop_step8

    +

    heap_pop_step8

    -

    heap_pop_step9

    +

    heap_pop_step9

    -

    heap_pop_step10

    +

    heap_pop_step10

    @@ -5197,10 +5032,15 @@ aria-label="页脚" @@ -5269,5 +5109,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_heap/index.html b/chapter_heap/index.html index 8948bb2f7..de6995e84 100644 --- a/chapter_heap/index.html +++ b/chapter_heap/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 8 章   堆

    -

    堆

    +

    堆

    Abstract

    @@ -3591,10 +3426,15 @@ aria-label="页脚" @@ -3663,5 +3503,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_heap/summary/index.html b/chapter_heap/summary/index.html index 3ec3a9082..4957b54af 100644 --- a/chapter_heap/summary/index.html +++ b/chapter_heap/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3652,10 +3487,15 @@ aria-label="页脚" @@ -3724,5 +3564,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_heap/top_k/index.html b/chapter_heap/top_k/index.html index 1a4ee9f51..38aad7bf1 100644 --- a/chapter_heap/top_k/index.html +++ b/chapter_heap/top_k/index.html @@ -60,7 +60,18 @@ - + @@ -1938,14 +1949,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1966,14 +1969,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3496,7 +3331,7 @@

    8.3.1   方法一:遍历选择

    我们可以进行图 8-6 所示的 \(k\) 轮遍历,分别在每轮中提取第 \(1\)\(2\)\(\dots\)\(k\) 大的元素,时间复杂度为 \(O(nk)\)

    此方法只适用于 \(k \ll n\) 的情况,因为当 \(k\)\(n\) 比较接近时,其时间复杂度趋向于 \(O(n^2)\) ,非常耗时。

    -

    遍历寻找最大的 k 个元素

    +

    遍历寻找最大的 k 个元素

    图 8-6   遍历寻找最大的 k 个元素

    @@ -3506,7 +3341,7 @@

    8.3.2   方法二:排序

    如图 8-7 所示,我们可以先对数组 nums 进行排序,再返回最右边的 \(k\) 个元素,时间复杂度为 \(O(n \log n)\)

    显然,该方法“超额”完成任务了,因为我们只需要找出最大的 \(k\) 个元素即可,而不需要排序其他元素。

    -

    排序寻找最大的 k 个元素

    +

    排序寻找最大的 k 个元素

    图 8-7   排序寻找最大的 k 个元素

    8.3.3   方法三:堆

    @@ -3520,31 +3355,31 @@
    -

    基于堆寻找最大的 k 个元素

    +

    基于堆寻找最大的 k 个元素

    -

    top_k_heap_step2

    +

    top_k_heap_step2

    -

    top_k_heap_step3

    +

    top_k_heap_step3

    -

    top_k_heap_step4

    +

    top_k_heap_step4

    -

    top_k_heap_step5

    +

    top_k_heap_step5

    -

    top_k_heap_step6

    +

    top_k_heap_step6

    -

    top_k_heap_step7

    +

    top_k_heap_step7

    -

    top_k_heap_step8

    +

    top_k_heap_step8

    -

    top_k_heap_step9

    +

    top_k_heap_step9

    @@ -3925,10 +3760,15 @@ aria-label="页脚" @@ -3997,5 +3837,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_introduction/algorithms_are_everywhere/index.html b/chapter_introduction/algorithms_are_everywhere/index.html index 5b95ca0b4..9351b9af5 100644 --- a/chapter_introduction/algorithms_are_everywhere/index.html +++ b/chapter_introduction/algorithms_are_everywhere/index.html @@ -60,7 +60,18 @@ - + @@ -1887,14 +1898,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1915,14 +1918,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2342,14 +2337,6 @@ 第 12 章   分治 - - - - - - - - @@ -2381,14 +2368,6 @@ 12.1   分治算法 - - - - - - - - @@ -2409,14 +2388,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2437,14 +2408,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2465,14 +2428,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2493,14 +2448,6 @@ 12.5   小结 - - - - - - - - @@ -2732,14 +2679,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2771,14 +2710,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2799,14 +2730,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2827,14 +2750,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2855,14 +2770,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2883,14 +2790,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2911,14 +2810,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2939,14 +2830,6 @@ 14.7   小结 - - - - - - - - @@ -3005,14 +2888,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3044,14 +2919,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3072,14 +2939,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3100,14 +2959,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3128,14 +2979,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3156,14 +2999,6 @@ 15.5   小结 - - - - - - - - @@ -3419,19 +3254,19 @@
    -

    查字典步骤

    +

    查字典步骤

    -

    binary_search_dictionary_step2

    +

    binary_search_dictionary_step2

    -

    binary_search_dictionary_step3

    +

    binary_search_dictionary_step3

    -

    binary_search_dictionary_step4

    +

    binary_search_dictionary_step4

    -

    binary_search_dictionary_step5

    +

    binary_search_dictionary_step5

    @@ -3444,7 +3279,7 @@
  • 在无序部分抽出一张扑克牌,插入至有序部分的正确位置;完成后最左 2 张扑克已经有序。
  • 不断循环步骤 2. ,每一轮将一张扑克牌从无序部分插入至有序部分,直至所有扑克牌都有序。
  • -

    扑克排序步骤

    +

    扑克排序步骤

    图 1-2   扑克排序步骤

    上述整理扑克牌的方法本质上是“插入排序”算法,它在处理小型数据集时非常高效。许多编程语言的排序库函数中都存在插入排序的身影。

    @@ -3456,7 +3291,7 @@
  • 从剩余可选项中拿出最大的 \(1\) 元,剩余 \(1 - 1 = 0\) 元。
  • 完成找零,方案为 \(20 + 10 + 1 = 31\) 元。
  • -

    货币找零过程

    +

    货币找零过程

    图 1-3   货币找零过程

    在以上步骤中,我们每一步都采取当前看来最好的选择(尽可能用大面额的货币),最终得到了可行的找零方案。从数据结构与算法的角度看,这种方法本质上是“贪心”算法。

    @@ -3628,10 +3463,15 @@ aria-label="页脚" @@ -3700,5 +3540,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_introduction/index.html b/chapter_introduction/index.html index 7b6a2a7d6..f86100ce3 100644 --- a/chapter_introduction/index.html +++ b/chapter_introduction/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 1 章   初识算法

    -

    初识算法

    +

    初识算法

    Abstract

    @@ -3590,10 +3425,15 @@ aria-label="页脚" @@ -3662,5 +3502,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_introduction/summary/index.html b/chapter_introduction/summary/index.html index 06fdc6c60..c27e64a6b 100644 --- a/chapter_introduction/summary/index.html +++ b/chapter_introduction/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1887,14 +1898,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1915,14 +1918,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2342,14 +2337,6 @@ 第 12 章   分治 - - - - - - - - @@ -2381,14 +2368,6 @@ 12.1   分治算法 - - - - - - - - @@ -2409,14 +2388,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2437,14 +2408,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2465,14 +2428,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2493,14 +2448,6 @@ 12.5   小结 - - - - - - - - @@ -2732,14 +2679,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2771,14 +2710,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2799,14 +2730,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2827,14 +2750,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2855,14 +2770,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2883,14 +2790,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2911,14 +2810,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2939,14 +2830,6 @@ 14.7   小结 - - - - - - - - @@ -3005,14 +2888,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3044,14 +2919,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3072,14 +2939,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3100,14 +2959,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3128,14 +2979,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3156,14 +2999,6 @@ 15.5   小结 - - - - - - - - @@ -3580,10 +3415,15 @@ aria-label="页脚" @@ -3652,5 +3492,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_introduction/what_is_dsa/index.html b/chapter_introduction/what_is_dsa/index.html index edf617cdf..2846883f2 100644 --- a/chapter_introduction/what_is_dsa/index.html +++ b/chapter_introduction/what_is_dsa/index.html @@ -60,7 +60,18 @@ - + @@ -1938,14 +1949,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1966,14 +1969,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3514,11 +3349,11 @@
  • 算法是数据结构发挥作用的舞台。数据结构本身仅存储数据信息,结合算法才能解决特定问题。
  • 算法通常可以基于不同的数据结构进行实现,但执行效率可能相差很大,选择合适的数据结构是关键。
  • -

    数据结构与算法的关系

    +

    数据结构与算法的关系

    图 1-4   数据结构与算法的关系

    数据结构与算法犹如图 1-5 所示的拼装积木。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。

    -

    拼装积木

    +

    拼装积木

    图 1-5   拼装积木

    两者的详细对应关系如表 1-1 所示。

    @@ -3720,10 +3555,15 @@ aria-label="页脚" @@ -3792,5 +3632,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_preface/about_the_book/index.html b/chapter_preface/about_the_book/index.html index c60b758ab..f719ac03b 100644 --- a/chapter_preface/about_the_book/index.html +++ b/chapter_preface/about_the_book/index.html @@ -60,7 +60,18 @@ - + @@ -1938,14 +1949,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1966,14 +1969,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3509,7 +3344,7 @@
  • 数据结构:基本数据类型,数据结构的分类方法。数组、链表、栈、队列、哈希表、树、堆、图等数据结构的定义、优缺点、常用操作、常见类型、典型应用、实现方法等。
  • 算法:搜索、排序、分治、回溯、动态规划、贪心等算法的定义、优缺点、效率、应用场景、解题步骤、示例题目等。
  • -

    Hello 算法内容结构

    +

    Hello 算法内容结构

    图 0-1   Hello 算法内容结构

    0.1.3   致谢

    @@ -3687,10 +3522,15 @@ aria-label="页脚" @@ -3759,5 +3599,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_preface/index.html b/chapter_preface/index.html index 82153dc24..009f29c82 100644 --- a/chapter_preface/index.html +++ b/chapter_preface/index.html @@ -58,7 +58,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2330,14 +2325,6 @@ 第 12 章   分治 - - - - - - - - @@ -2369,14 +2356,6 @@ 12.1   分治算法 - - - - - - - - @@ -2397,14 +2376,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2425,14 +2396,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2453,14 +2416,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2481,14 +2436,6 @@ 12.5   小结 - - - - - - - - @@ -2720,14 +2667,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2759,14 +2698,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2787,14 +2718,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2815,14 +2738,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2843,14 +2758,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2871,14 +2778,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2899,14 +2798,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2927,14 +2818,6 @@ 14.7   小结 - - - - - - - - @@ -2993,14 +2876,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3032,14 +2907,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3060,14 +2927,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3088,14 +2947,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3116,14 +2967,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3144,14 +2987,6 @@ 15.5   小结 - - - - - - - - @@ -3412,7 +3247,7 @@

    第 0 章   前言

    -

    前言

    +

    前言

    Abstract

    @@ -3567,10 +3402,15 @@ aria-label="页脚" @@ -3639,5 +3479,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_preface/suggestions/index.html b/chapter_preface/suggestions/index.html index 5add11d85..6b6921a08 100644 --- a/chapter_preface/suggestions/index.html +++ b/chapter_preface/suggestions/index.html @@ -60,7 +60,18 @@ - + @@ -1952,14 +1963,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1980,14 +1983,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2407,14 +2402,6 @@ 第 12 章   分治 - - - - - - - - @@ -2446,14 +2433,6 @@ 12.1   分治算法 - - - - - - - - @@ -2474,14 +2453,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2502,14 +2473,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2530,14 +2493,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2558,14 +2513,6 @@ 12.5   小结 - - - - - - - - @@ -2797,14 +2744,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2836,14 +2775,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2864,14 +2795,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2892,14 +2815,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2920,14 +2835,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2948,14 +2855,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2976,14 +2875,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3004,14 +2895,6 @@ 14.7   小结 - - - - - - - - @@ -3070,14 +2953,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3109,14 +2984,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3137,14 +3004,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3165,14 +3024,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3193,14 +3044,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3221,14 +3064,6 @@ 15.5   小结 - - - - - - - - @@ -3666,14 +3501,14 @@

    0.2.2   在动画图解中高效学习

    相较于文字,视频和图片具有更高的信息密度和结构化程度,更易于理解。在本书中,重点和难点知识将主要通过动画和图解形式展示,而文字则作为动画和图片的解释与补充。

    如果你在阅读本书时,发现某段内容提供了图 0-2 所示的动画或图解,请以图为主、以文字为辅,综合两者来理解内容。

    -

    动画图解示例

    +

    动画图解示例

    图 0-2   动画图解示例

    0.2.3   在代码实践中加深理解

    本书的配套代码被托管在 GitHub 仓库。如图 0-3 所示,源代码附有测试样例,可一键运行

    如果时间允许,建议你参照代码自行敲一遍。如果学习时间有限,请至少通读并运行所有代码。

    与阅读代码相比,编写代码的过程往往能带来更多收获。动手学,才是真的学

    -

    运行代码示例

    +

    运行代码示例

    图 0-3   运行代码示例

    运行代码的前置工作主要分为三步。

    @@ -3682,17 +3517,17 @@
    git clone https://github.com/krahets/hello-algo.git
     

    当然,你也可以在图 0-4 所示的位置,点击“Download ZIP”直接下载代码压缩包,然后在本地解压即可。

    -

    克隆仓库与下载代码

    +

    克隆仓库与下载代码

    图 0-4   克隆仓库与下载代码

    第三步:运行源代码。如图 0-5 所示,对于顶部标有文件名称的代码块,我们可以在仓库的 codes 文件夹内找到对应的源代码文件。源代码文件可一键运行,将帮助你节省不必要的调试时间,让你能够专注于学习内容。

    -

    代码块与对应的源代码文件

    +

    代码块与对应的源代码文件

    图 0-5   代码块与对应的源代码文件

    0.2.4   在提问讨论中共同成长

    在阅读本书时,请不要轻易跳过那些没学明白的知识点。欢迎在评论区提出你的问题,我和小伙伴们将竭诚为你解答,一般情况下可在两天内回复。

    如图 0-6 所示,每篇文章的底部都配有评论区。希望你能多关注评论区的内容。一方面,你可以了解大家遇到的问题,从而查漏补缺,激发更深入的思考。另一方面,期待你能慷慨地回答其他小伙伴的问题,分享您的见解,帮助他人进步。

    -

    评论区示例

    +

    评论区示例

    图 0-6   评论区示例

    0.2.5   算法学习路线

    @@ -3703,7 +3538,7 @@
  • 搭建知识体系。在学习方面,我们可以阅读算法专栏文章、解题框架和算法教材,以不断丰富知识体系。在刷题方面,可以尝试采用进阶刷题策略,如按专题分类、一题多解、一解多题等,相关的刷题心得可以在各个社区找到。
  • 如图 0-7 所示,本书内容主要涵盖“第一阶段”,旨在帮助你更高效地展开第二和第三阶段的学习。

    -

    算法学习路线

    +

    算法学习路线

    图 0-7   算法学习路线

    @@ -3868,10 +3703,15 @@ aria-label="页脚" @@ -3940,5 +3780,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_preface/summary/index.html b/chapter_preface/summary/index.html index 11b0d6225..a3a3f104a 100644 --- a/chapter_preface/summary/index.html +++ b/chapter_preface/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1887,14 +1898,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1915,14 +1918,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2342,14 +2337,6 @@ 第 12 章   分治 - - - - - - - - @@ -2381,14 +2368,6 @@ 12.1   分治算法 - - - - - - - - @@ -2409,14 +2388,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2437,14 +2408,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2465,14 +2428,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2493,14 +2448,6 @@ 12.5   小结 - - - - - - - - @@ -2732,14 +2679,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2771,14 +2710,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2799,14 +2730,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2827,14 +2750,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2855,14 +2770,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2883,14 +2790,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2911,14 +2810,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2939,14 +2830,6 @@ 14.7   小结 - - - - - - - - @@ -3005,14 +2888,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3044,14 +2919,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3072,14 +2939,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3100,14 +2959,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3128,14 +2979,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3156,14 +2999,6 @@ 15.5   小结 - - - - - - - - @@ -3579,10 +3414,15 @@ aria-label="页脚" @@ -3651,5 +3491,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_reference/index.html b/chapter_reference/index.html index 9a3436d48..8dcaf0fba 100644 --- a/chapter_reference/index.html +++ b/chapter_reference/index.html @@ -58,7 +58,18 @@ - + @@ -1873,14 +1884,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1901,14 +1904,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2328,14 +2323,6 @@ 第 12 章   分治 - - - - - - - - @@ -2367,14 +2354,6 @@ 12.1   分治算法 - - - - - - - - @@ -2395,14 +2374,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2423,14 +2394,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2451,14 +2414,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2479,14 +2434,6 @@ 12.5   小结 - - - - - - - - @@ -2718,14 +2665,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2757,14 +2696,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2785,14 +2716,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2813,14 +2736,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2841,14 +2756,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2869,14 +2776,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2897,14 +2796,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2925,14 +2816,6 @@ 14.7   小结 - - - - - - - - @@ -2991,14 +2874,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3030,14 +2905,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3058,14 +2925,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3086,14 +2945,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3114,14 +2965,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3142,14 +2985,6 @@ 15.5   小结 - - - - - - - - @@ -3498,10 +3333,15 @@ aria-label="页脚" @@ -3570,5 +3410,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_searching/binary_search/index.html b/chapter_searching/binary_search/index.html index b2bfc37b7..882eb06cf 100644 --- a/chapter_searching/binary_search/index.html +++ b/chapter_searching/binary_search/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3479,7 +3314,7 @@

    Question

    给定一个长度为 \(n\) 的数组 nums ,元素按从小到大的顺序排列,数组不包含重复元素。请查找并返回元素 target 在该数组中的索引。若数组不包含该元素,则返回 \(-1\)

    -

    二分查找示例数据

    +

    二分查找示例数据

    图 10-1   二分查找示例数据

    如图 10-2 所示,我们先初始化指针 \(i = 0\)\(j = n - 1\) ,分别指向数组首元素和尾元素,代表搜索区间 \([0, n - 1]\) 。请注意,中括号表示闭区间,其包含边界值本身。

    @@ -3497,25 +3332,25 @@
    -

    二分查找流程

    +

    二分查找流程

    -

    binary_search_step2

    +

    binary_search_step2

    -

    binary_search_step3

    +

    binary_search_step3

    -

    binary_search_step4

    +

    binary_search_step4

    -

    binary_search_step5

    +

    binary_search_step5

    -

    binary_search_step6

    +

    binary_search_step6

    -

    binary_search_step7

    +

    binary_search_step7

    @@ -4051,7 +3886,7 @@

    如图 10-3 所示,在两种区间表示下,二分查找算法的初始化、循环条件和缩小区间操作皆有所不同。

    由于“双闭区间”表示中的左右边界都被定义为闭区间,因此指针 \(i\)\(j\) 缩小区间操作也是对称的。这样更不容易出错,因此一般建议采用“双闭区间”的写法

    -

    两种区间定义

    +

    两种区间定义

    图 10-3   两种区间定义

    10.1.2   优点与局限性

    @@ -4229,10 +4064,15 @@ aria-label="页脚" @@ -4301,5 +4141,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_searching/binary_search_edge/index.html b/chapter_searching/binary_search_edge/index.html index b24464ba3..c3e81263e 100644 --- a/chapter_searching/binary_search_edge/index.html +++ b/chapter_searching/binary_search_edge/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1914,14 +1917,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -1933,14 +1928,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2414,14 +2401,6 @@ 第 12 章   分治 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.1   分治算法 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2565,14 +2512,6 @@ 12.5   小结 - - - - - - - - @@ -2804,14 +2743,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3011,14 +2894,6 @@ 14.7   小结 - - - - - - - - @@ -3077,14 +2952,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3228,14 +3063,6 @@ 15.5   小结 - - - - - - - - @@ -3700,7 +3527,7 @@

    1.   复用查找左边界

    实际上,我们可以利用查找最左元素的函数来查找最右元素,具体方法为:将查找最右一个 target 转化为查找最左一个 target + 1

    如图 10-7 所示,查找完成后,指针 \(i\) 指向最左一个 target + 1(如果存在),而 \(j\) 指向最右一个 target因此返回 \(j\) 即可

    -

    将查找右边界转化为查找左边界

    +

    将查找右边界转化为查找左边界

    图 10-7   将查找右边界转化为查找左边界

    请注意,返回的插入点是 \(i\) ,因此需要将其减 \(1\) ,从而获得 \(j\)

    @@ -3893,7 +3720,7 @@
  • 查找最左一个 target :可以转化为查找 target - 0.5 ,并返回指针 \(i\)
  • 查找最右一个 target :可以转化为查找 target + 0.5 ,并返回指针 \(j\)
  • -

    将查找边界转化为查找元素

    +

    将查找边界转化为查找元素

    图 10-8   将查找边界转化为查找元素

    代码在此省略,值得注意以下两点。

    @@ -4064,10 +3891,15 @@ aria-label="页脚" @@ -4136,5 +3968,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_searching/binary_search_insertion/index.html b/chapter_searching/binary_search_insertion/index.html index 8bec0a5e5..aaa1cf8f8 100644 --- a/chapter_searching/binary_search_insertion/index.html +++ b/chapter_searching/binary_search_insertion/index.html @@ -60,7 +60,18 @@ - + @@ -1886,14 +1897,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1967,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2394,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2433,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2461,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2489,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2517,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2545,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2784,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2823,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2851,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2879,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2907,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2935,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2963,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2991,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3057,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3096,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3124,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3152,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3180,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3208,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3488,7 +3315,7 @@

    Question

    给定一个长度为 \(n\) 的有序数组 nums 和一个元素 target ,数组不存在重复元素。现将 target 插入到数组 nums 中,并保持其有序性。若数组中已存在元素 target ,则插入到其左方。请返回插入后 target 在数组中的索引。

    -

    二分查找插入点示例数据

    +

    二分查找插入点示例数据

    图 10-4   二分查找插入点示例数据

    如果想要复用上节的二分查找代码,则需要回答以下两个问题。

    @@ -3732,7 +3559,7 @@
  • 执行二分查找,得到任意一个 target 的索引,记为 \(k\)
  • 从索引 \(k\) 开始,向左进行线性遍历,当找到最左边的 target 时返回。
  • -

    线性查找重复元素的插入点

    +

    线性查找重复元素的插入点

    图 10-5   线性查找重复元素的插入点

    此方法虽然可用,但其包含线性查找,因此时间复杂度为 \(O(n)\) 。当数组中存在很多重复的 target 时,该方法效率很低。

    @@ -3745,28 +3572,28 @@
    -

    二分查找重复元素的插入点的步骤

    +

    二分查找重复元素的插入点的步骤

    -

    binary_search_insertion_step2

    +

    binary_search_insertion_step2

    -

    binary_search_insertion_step3

    +

    binary_search_insertion_step3

    -

    binary_search_insertion_step4

    +

    binary_search_insertion_step4

    -

    binary_search_insertion_step5

    +

    binary_search_insertion_step5

    -

    binary_search_insertion_step6

    +

    binary_search_insertion_step6

    -

    binary_search_insertion_step7

    +

    binary_search_insertion_step7

    -

    binary_search_insertion_step8

    +

    binary_search_insertion_step8

    @@ -4164,10 +3991,15 @@ aria-label="页脚" @@ -4236,5 +4068,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_searching/index.html b/chapter_searching/index.html index 17e651886..39219b582 100644 --- a/chapter_searching/index.html +++ b/chapter_searching/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 10 章   搜索

    -

    搜索

    +

    搜索

    Abstract

    @@ -3593,10 +3428,15 @@ aria-label="页脚" @@ -3665,5 +3505,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_searching/replace_linear_by_hashing/index.html b/chapter_searching/replace_linear_by_hashing/index.html index a5f6bec72..da28adc51 100644 --- a/chapter_searching/replace_linear_by_hashing/index.html +++ b/chapter_searching/replace_linear_by_hashing/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3481,7 +3316,7 @@

    10.4.1   线性查找:以时间换空间

    考虑直接遍历所有可能的组合。如图 10-9 所示,我们开启一个两层循环,在每轮中判断两个整数的和是否为 target ,若是则返回它们的索引。

    -

    线性查找求解两数之和

    +

    线性查找求解两数之和

    图 10-9   线性查找求解两数之和

    @@ -3683,13 +3518,13 @@
    -

    辅助哈希表求解两数之和

    +

    辅助哈希表求解两数之和

    -

    two_sum_hashtable_step2

    +

    two_sum_hashtable_step2

    -

    two_sum_hashtable_step3

    +

    two_sum_hashtable_step3

    @@ -4095,10 +3930,15 @@ aria-label="页脚" @@ -4167,5 +4007,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_searching/searching_algorithm_revisited/index.html b/chapter_searching/searching_algorithm_revisited/index.html index d418e4af1..7985696d2 100644 --- a/chapter_searching/searching_algorithm_revisited/index.html +++ b/chapter_searching/searching_algorithm_revisited/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3518,7 +3353,7 @@

    10.5.3   搜索方法选取

    给定大小为 \(n\) 的一组数据,我们可以使用线性搜索、二分查找、树查找、哈希查找等多种方法在该数据中搜索目标元素。各个方法的工作原理如图 10-11 所示。

    -

    多种搜索策略

    +

    多种搜索策略

    图 10-11   多种搜索策略

    上述几种方法的操作效率与特性如表 10-1 所示。

    @@ -3771,10 +3606,15 @@ aria-label="页脚" @@ -3843,5 +3683,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_searching/summary/index.html b/chapter_searching/summary/index.html index c861ba3a2..69be77f2b 100644 --- a/chapter_searching/summary/index.html +++ b/chapter_searching/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2342,14 +2337,6 @@ 第 12 章   分治 - - - - - - - - @@ -2381,14 +2368,6 @@ 12.1   分治算法 - - - - - - - - @@ -2409,14 +2388,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2437,14 +2408,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2465,14 +2428,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2493,14 +2448,6 @@ 12.5   小结 - - - - - - - - @@ -2732,14 +2679,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2771,14 +2710,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2799,14 +2730,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2827,14 +2750,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2855,14 +2770,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2883,14 +2790,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2911,14 +2810,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2939,14 +2830,6 @@ 14.7   小结 - - - - - - - - @@ -3005,14 +2888,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3044,14 +2919,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3072,14 +2939,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3100,14 +2959,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3128,14 +2979,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3156,14 +2999,6 @@ 15.5   小结 - - - - - - - - @@ -3579,10 +3414,15 @@ aria-label="页脚" @@ -3651,5 +3491,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/bubble_sort/index.html b/chapter_sorting/bubble_sort/index.html index 7b90e2c16..424774861 100644 --- a/chapter_sorting/bubble_sort/index.html +++ b/chapter_sorting/bubble_sort/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3493,25 +3328,25 @@
    -

    利用元素交换操作模拟冒泡

    +

    利用元素交换操作模拟冒泡

    -

    bubble_operation_step2

    +

    bubble_operation_step2

    -

    bubble_operation_step3

    +

    bubble_operation_step3

    -

    bubble_operation_step4

    +

    bubble_operation_step4

    -

    bubble_operation_step5

    +

    bubble_operation_step5

    -

    bubble_operation_step6

    +

    bubble_operation_step6

    -

    bubble_operation_step7

    +

    bubble_operation_step7

    @@ -3525,7 +3360,7 @@
  • 以此类推,经过 \(n - 1\) 轮“冒泡”后,\(n - 1\) 大的元素都被交换至正确位置
  • 仅剩的一个元素必定是最小元素,无须排序,因此数组排序完成。
  • -

    冒泡排序流程

    +

    冒泡排序流程

    图 11-5   冒泡排序流程

    @@ -4169,10 +4004,15 @@ aria-label="页脚" @@ -4241,5 +4081,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/bucket_sort/index.html b/chapter_sorting/bucket_sort/index.html index 7d46e2dd4..df44a5fe9 100644 --- a/chapter_sorting/bucket_sort/index.html +++ b/chapter_sorting/bucket_sort/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3497,7 +3332,7 @@
  • 对每个桶分别执行排序(本文采用编程语言的内置排序函数)。
  • 按照桶的从小到大的顺序,合并结果。
  • -

    桶排序算法流程

    +

    桶排序算法流程

    图 11-13   桶排序算法流程

    @@ -3864,12 +3699,12 @@

    桶排序的时间复杂度理论上可以达到 \(O(n)\)关键在于将元素均匀分配到各个桶中,因为实际数据往往不是均匀分布的。例如,我们想要将淘宝上的所有商品按价格范围平均分配到 10 个桶中,但商品价格分布不均,低于 100 元的非常多,高于 1000 元的非常少。若将价格区间平均划分为 10 份,各个桶中的商品数量差距会非常大。

    为实现平均分配,我们可以先设定一个大致的分界线,将数据粗略地分到 3 个桶中。分配完毕后,再将商品较多的桶继续划分为 3 个桶,直至所有桶中的元素数量大致相等

    如图 11-14 所示,这种方法本质上是创建一个递归树,目标是让叶节点的值尽可能平均。当然,不一定要每轮将数据划分为 3 个桶,具体划分方式可根据数据特点灵活选择。

    -

    递归划分桶

    +

    递归划分桶

    图 11-14   递归划分桶

    如果我们提前知道商品价格的概率分布,则可以根据数据概率分布设置每个桶的价格分界线。值得注意的是,数据分布并不一定需要特意统计,也可以根据数据特点采用某种概率模型进行近似。

    如图 11-15 所示,我们假设商品价格服从正态分布,这样就可以合理地设定价格区间,从而将商品平均分配到各个桶中。

    -

    根据概率分布划分桶

    +

    根据概率分布划分桶

    图 11-15   根据概率分布划分桶

    @@ -4034,10 +3869,15 @@ aria-label="页脚" @@ -4106,5 +3946,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/counting_sort/index.html b/chapter_sorting/counting_sort/index.html index 5f2e271e2..d40eb668a 100644 --- a/chapter_sorting/counting_sort/index.html +++ b/chapter_sorting/counting_sort/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2400,14 +2395,6 @@ 第 12 章   分治 - - - - - - - - @@ -2439,14 +2426,6 @@ 12.1   分治算法 - - - - - - - - @@ -2467,14 +2446,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2495,14 +2466,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2523,14 +2486,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2551,14 +2506,6 @@ 12.5   小结 - - - - - - - - @@ -2790,14 +2737,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2829,14 +2768,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2857,14 +2788,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2885,14 +2808,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2913,14 +2828,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2941,14 +2848,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2969,14 +2868,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2997,14 +2888,6 @@ 14.7   小结 - - - - - - - - @@ -3063,14 +2946,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3102,14 +2977,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3130,14 +2997,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3158,14 +3017,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3186,14 +3037,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3214,14 +3057,6 @@ 15.5   小结 - - - - - - - - @@ -3510,7 +3345,7 @@
  • 借助 counter 统计 nums 中各数字的出现次数,其中 counter[num] 对应数字 num 的出现次数。统计方法很简单,只需遍历 nums(设当前数字为 num),每轮将 counter[num] 增加 \(1\) 即可。
  • 由于 counter 的各个索引天然有序,因此相当于所有数字已经被排序好了。接下来,我们遍历 counter ,根据各数字的出现次数,将它们按从小到大的顺序填入 nums 即可。
  • -

    计数排序流程

    +

    计数排序流程

    图 11-16   计数排序流程

    @@ -3811,28 +3646,28 @@
    -

    计数排序步骤

    +

    计数排序步骤

    -

    counting_sort_step2

    +

    counting_sort_step2

    -

    counting_sort_step3

    +

    counting_sort_step3

    -

    counting_sort_step4

    +

    counting_sort_step4

    -

    counting_sort_step5

    +

    counting_sort_step5

    -

    counting_sort_step6

    +

    counting_sort_step6

    -

    counting_sort_step7

    +

    counting_sort_step7

    -

    counting_sort_step8

    +

    counting_sort_step8

    @@ -4398,10 +4233,15 @@ aria-label="页脚" @@ -4470,5 +4310,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/heap_sort/index.html b/chapter_sorting/heap_sort/index.html index fe84236d8..2051888fc 100644 --- a/chapter_sorting/heap_sort/index.html +++ b/chapter_sorting/heap_sort/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3499,40 +3334,40 @@
    -

    堆排序步骤

    +

    堆排序步骤

    -

    heap_sort_step2

    +

    heap_sort_step2

    -

    heap_sort_step3

    +

    heap_sort_step3

    -

    heap_sort_step4

    +

    heap_sort_step4

    -

    heap_sort_step5

    +

    heap_sort_step5

    -

    heap_sort_step6

    +

    heap_sort_step6

    -

    heap_sort_step7

    +

    heap_sort_step7

    -

    heap_sort_step8

    +

    heap_sort_step8

    -

    heap_sort_step9

    +

    heap_sort_step9

    -

    heap_sort_step10

    +

    heap_sort_step10

    -

    heap_sort_step11

    +

    heap_sort_step11

    -

    heap_sort_step12

    +

    heap_sort_step12

    @@ -4162,10 +3997,15 @@ aria-label="页脚" @@ -4234,5 +4074,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/index.html b/chapter_sorting/index.html index 8c566ba09..cd8b78517 100644 --- a/chapter_sorting/index.html +++ b/chapter_sorting/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 11 章   排序

    -

    排序

    +

    排序

    Abstract

    @@ -3598,10 +3433,15 @@ aria-label="页脚" @@ -3670,5 +3510,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/insertion_sort/index.html b/chapter_sorting/insertion_sort/index.html index 7868f345b..82b0f2dd1 100644 --- a/chapter_sorting/insertion_sort/index.html +++ b/chapter_sorting/insertion_sort/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3491,7 +3326,7 @@

    「插入排序 insertion sort」是一种简单的排序算法,它的工作原理与手动整理一副牌的过程非常相似。

    具体来说,我们在未排序区间选择一个基准元素,将该元素与其左侧已排序区间的元素逐一比较大小,并将该元素插入到正确的位置。

    图 11-6 展示了数组插入元素的操作流程。设基准元素为 base ,我们需要将从目标索引到 base 之间的所有元素向右移动一位,然后再将 base 赋值给目标索引。

    -

    单次插入操作

    +

    单次插入操作

    图 11-6   单次插入操作

    11.4.1   算法流程

    @@ -3502,7 +3337,7 @@
  • 选取第 3 个元素作为 base ,将其插入到正确位置后,数组的前 3 个元素已排序
  • 以此类推,在最后一轮中,选取最后一个元素作为 base ,将其插入到正确位置后,所有元素均已排序
  • -

    插入排序流程

    +

    插入排序流程

    图 11-7   插入排序流程

    @@ -3885,10 +3720,15 @@ aria-label="页脚" @@ -3957,5 +3797,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/merge_sort/index.html b/chapter_sorting/merge_sort/index.html index ba54ab7c5..5e8a89a0d 100644 --- a/chapter_sorting/merge_sort/index.html +++ b/chapter_sorting/merge_sort/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3493,7 +3328,7 @@
  • 划分阶段:通过递归不断地将数组从中点处分开,将长数组的排序问题转换为短数组的排序问题。
  • 合并阶段:当子数组长度为 1 时终止划分,开始合并,持续地将左右两个较短的有序数组合并为一个较长的有序数组,直至结束。
  • -

    归并排序的划分与合并阶段

    +

    归并排序的划分与合并阶段

    图 11-10   归并排序的划分与合并阶段

    11.6.1   算法流程

    @@ -3506,34 +3341,34 @@
    -

    归并排序步骤

    +

    归并排序步骤

    -

    merge_sort_step2

    +

    merge_sort_step2

    -

    merge_sort_step3

    +

    merge_sort_step3

    -

    merge_sort_step4

    +

    merge_sort_step4

    -

    merge_sort_step5

    +

    merge_sort_step5

    -

    merge_sort_step6

    +

    merge_sort_step6

    -

    merge_sort_step7

    +

    merge_sort_step7

    -

    merge_sort_step8

    +

    merge_sort_step8

    -

    merge_sort_step9

    +

    merge_sort_step9

    -

    merge_sort_step10

    +

    merge_sort_step10

    @@ -4266,10 +4101,15 @@ aria-label="页脚" @@ -4338,5 +4178,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/quick_sort/index.html b/chapter_sorting/quick_sort/index.html index ed8289a70..71ce76783 100644 --- a/chapter_sorting/quick_sort/index.html +++ b/chapter_sorting/quick_sort/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2407,14 +2402,6 @@ 第 12 章   分治 - - - - - - - - @@ -2446,14 +2433,6 @@ 12.1   分治算法 - - - - - - - - @@ -2474,14 +2453,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2502,14 +2473,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2530,14 +2493,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2558,14 +2513,6 @@ 12.5   小结 - - - - - - - - @@ -2797,14 +2744,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2836,14 +2775,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2864,14 +2795,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2892,14 +2815,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2920,14 +2835,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2948,14 +2855,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2976,14 +2875,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3004,14 +2895,6 @@ 14.7   小结 - - - - - - - - @@ -3070,14 +2953,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3109,14 +2984,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3137,14 +3004,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3165,14 +3024,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3193,14 +3044,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3221,14 +3064,6 @@ 15.5   小结 - - - - - - - - @@ -3526,31 +3361,31 @@
    -

    哨兵划分步骤

    +

    哨兵划分步骤

    -

    pivot_division_step2

    +

    pivot_division_step2

    -

    pivot_division_step3

    +

    pivot_division_step3

    -

    pivot_division_step4

    +

    pivot_division_step4

    -

    pivot_division_step5

    +

    pivot_division_step5

    -

    pivot_division_step6

    +

    pivot_division_step6

    -

    pivot_division_step7

    +

    pivot_division_step7

    -

    pivot_division_step8

    +

    pivot_division_step8

    -

    pivot_division_step9

    +

    pivot_division_step9

    @@ -3861,7 +3696,7 @@
  • 然后,对左子数组和右子数组分别递归执行“哨兵划分”。
  • 持续递归,直至子数组长度为 1 时终止,从而完成整个数组的排序。
  • -

    快速排序流程

    +

    快速排序流程

    图 11-9   快速排序流程

    @@ -4901,10 +4736,15 @@ aria-label="页脚" @@ -4973,5 +4813,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/radix_sort/index.html b/chapter_sorting/radix_sort/index.html index 0d32c7c2a..98724edd9 100644 --- a/chapter_sorting/radix_sort/index.html +++ b/chapter_sorting/radix_sort/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3483,7 +3318,7 @@
  • 对学号的第 \(k\) 位执行“计数排序”。完成后,数据会根据第 \(k\) 位从小到大排序。
  • \(k\) 增加 \(1\) ,然后返回步骤 2. 继续迭代,直到所有位都排序完成后结束。
  • -

    基数排序算法流程

    +

    基数排序算法流程

    图 11-18   基数排序算法流程

    下面来剖析代码实现。对于一个 \(d\) 进制的数字 \(x\) ,要获取其第 \(k\)\(x_k\) ,可以使用以下计算公式:

    @@ -4300,10 +4135,15 @@ aria-label="页脚" @@ -4372,5 +4212,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/selection_sort/index.html b/chapter_sorting/selection_sort/index.html index 819667950..ddc4f3366 100644 --- a/chapter_sorting/selection_sort/index.html +++ b/chapter_sorting/selection_sort/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2379,14 +2374,6 @@ 第 12 章   分治 - - - - - - - - @@ -2418,14 +2405,6 @@ 12.1   分治算法 - - - - - - - - @@ -2446,14 +2425,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2474,14 +2445,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2502,14 +2465,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2530,14 +2485,6 @@ 12.5   小结 - - - - - - - - @@ -2769,14 +2716,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2808,14 +2747,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2836,14 +2767,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2864,14 +2787,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2892,14 +2807,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2920,14 +2827,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2948,14 +2847,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2976,14 +2867,6 @@ 14.7   小结 - - - - - - - - @@ -3042,14 +2925,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3081,14 +2956,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3109,14 +2976,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3137,14 +2996,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3165,14 +3016,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3193,14 +3036,6 @@ 15.5   小结 - - - - - - - - @@ -3472,37 +3307,37 @@
    -

    选择排序步骤

    +

    选择排序步骤

    -

    selection_sort_step2

    +

    selection_sort_step2

    -

    selection_sort_step3

    +

    selection_sort_step3

    -

    selection_sort_step4

    +

    selection_sort_step4

    -

    selection_sort_step5

    +

    selection_sort_step5

    -

    selection_sort_step6

    +

    selection_sort_step6

    -

    selection_sort_step7

    +

    selection_sort_step7

    -

    selection_sort_step8

    +

    selection_sort_step8

    -

    selection_sort_step9

    +

    selection_sort_step9

    -

    selection_sort_step10

    +

    selection_sort_step10

    -

    selection_sort_step11

    +

    selection_sort_step11

    @@ -3728,7 +3563,7 @@
  • 空间复杂度 \(O(1)\)、原地排序:指针 \(i\)\(j\) 使用常数大小的额外空间。
  • 非稳定排序:如图 11-3 所示,元素 nums[i] 有可能被交换至与其相等的元素的右边,导致两者相对顺序发生改变。
  • -

    选择排序非稳定示例

    +

    选择排序非稳定示例

    图 11-3   选择排序非稳定示例

    @@ -3893,10 +3728,15 @@ aria-label="页脚" @@ -3965,5 +3805,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/sorting_algorithm/index.html b/chapter_sorting/sorting_algorithm/index.html index ef0aa111b..7b22132cd 100644 --- a/chapter_sorting/sorting_algorithm/index.html +++ b/chapter_sorting/sorting_algorithm/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3476,7 +3311,7 @@

    11.1   排序算法

    「排序算法 sorting algorithm」用于对一组数据按照特定顺序进行排列。排序算法有着广泛的应用,因为有序数据通常能够被更有效地查找、分析和处理。

    如图 11-1 所示,排序算法中的数据类型可以是整数、浮点数、字符或字符串等。排序的判断规则可根据需求设定,如数字大小、字符 ASCII 码顺序或自定义规则。

    -

    数据类型和判断规则示例

    +

    数据类型和判断规则示例

    图 11-1   数据类型和判断规则示例

    11.1.1   评价维度

    @@ -3670,10 +3505,15 @@ aria-label="页脚" @@ -3742,5 +3582,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_sorting/summary/index.html b/chapter_sorting/summary/index.html index 88a0edeed..0cccb8565 100644 --- a/chapter_sorting/summary/index.html +++ b/chapter_sorting/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1875,14 +1886,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1903,14 +1906,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3486,7 +3321,7 @@
  • 总的来说,我们希望找到一种排序算法,具有高效率、稳定、原地以及正向自适应性等优点。然而,正如其他数据结构和算法一样,没有一种排序算法能够同时满足所有这些条件。在实际应用中,我们需要根据数据的特性来选择合适的排序算法。
  • 图 11-19 对比了主流排序算法的效率、稳定性、就地性和自适应性等。
  • -

    排序算法对比

    +

    排序算法对比

    图 11-19   排序算法对比

    2.   Q & A

    @@ -3679,10 +3514,15 @@ aria-label="页脚" @@ -3751,5 +3591,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_stack_and_queue/deque/index.html b/chapter_stack_and_queue/deque/index.html index b15911330..c4f95e675 100644 --- a/chapter_stack_and_queue/deque/index.html +++ b/chapter_stack_and_queue/deque/index.html @@ -60,7 +60,18 @@ - + @@ -1958,14 +1969,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1986,14 +1989,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2413,14 +2408,6 @@ 第 12 章   分治 - - - - - - - - @@ -2452,14 +2439,6 @@ 12.1   分治算法 - - - - - - - - @@ -2480,14 +2459,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2508,14 +2479,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2536,14 +2499,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2564,14 +2519,6 @@ 12.5   小结 - - - - - - - - @@ -2803,14 +2750,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2842,14 +2781,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2870,14 +2801,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2898,14 +2821,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2926,14 +2841,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2954,14 +2861,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2982,14 +2881,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3010,14 +2901,6 @@ 14.7   小结 - - - - - - - - @@ -3076,14 +2959,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3115,14 +2990,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3143,14 +3010,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3171,14 +3030,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3199,14 +3050,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3227,14 +3070,6 @@ 15.5   小结 - - - - - - - - @@ -3529,7 +3364,7 @@

    5.3   双向队列

    在队列中,我们仅能在头部删除或在尾部添加元素。如图 5-7 所示,「双向队列 deque」提供了更高的灵活性,允许在头部和尾部执行元素的添加或删除操作。

    -

    双向队列的操作

    +

    双向队列的操作

    图 5-7   双向队列的操作

    5.3.1   双向队列常用操作

    @@ -3890,19 +3725,19 @@
    -

    基于链表实现双向队列的入队出队操作

    +

    基于链表实现双向队列的入队出队操作

    -

    linkedlist_deque_push_last

    +

    linkedlist_deque_push_last

    -

    linkedlist_deque_push_first

    +

    linkedlist_deque_push_first

    -

    linkedlist_deque_pop_last

    +

    linkedlist_deque_pop_last

    -

    linkedlist_deque_pop_first

    +

    linkedlist_deque_pop_first

    @@ -5518,19 +5353,19 @@
    -

    基于数组实现双向队列的入队出队操作

    +

    基于数组实现双向队列的入队出队操作

    -

    array_deque_push_last

    +

    array_deque_push_last

    -

    array_deque_push_first

    +

    array_deque_push_first

    -

    array_deque_pop_last

    +

    array_deque_pop_last

    -

    array_deque_pop_first

    +

    array_deque_pop_first

    @@ -6895,10 +6730,15 @@ aria-label="页脚" @@ -6967,5 +6807,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_stack_and_queue/index.html b/chapter_stack_and_queue/index.html index 983303784..66f4249e2 100644 --- a/chapter_stack_and_queue/index.html +++ b/chapter_stack_and_queue/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 5 章   栈与队列

    -

    栈与队列

    +

    栈与队列

    Abstract

    @@ -3591,10 +3426,15 @@ aria-label="页脚" @@ -3663,5 +3503,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_stack_and_queue/queue/index.html b/chapter_stack_and_queue/queue/index.html index f3a513bcd..9926325a3 100644 --- a/chapter_stack_and_queue/queue/index.html +++ b/chapter_stack_and_queue/queue/index.html @@ -60,7 +60,18 @@ - + @@ -1958,14 +1969,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1986,14 +1989,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2413,14 +2408,6 @@ 第 12 章   分治 - - - - - - - - @@ -2452,14 +2439,6 @@ 12.1   分治算法 - - - - - - - - @@ -2480,14 +2459,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2508,14 +2479,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2536,14 +2499,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2564,14 +2519,6 @@ 12.5   小结 - - - - - - - - @@ -2803,14 +2750,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2842,14 +2781,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2870,14 +2801,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2898,14 +2821,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2926,14 +2841,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2954,14 +2861,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2982,14 +2881,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3010,14 +2901,6 @@ 14.7   小结 - - - - - - - - @@ -3076,14 +2959,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3115,14 +2990,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3143,14 +3010,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3171,14 +3030,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3199,14 +3050,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3227,14 +3070,6 @@ 15.5   小结 - - - - - - - - @@ -3530,7 +3365,7 @@

    5.2   队列

    「队列 queue」是一种遵循先入先出规则的线性数据结构。顾名思义,队列模拟了排队现象,即新来的人不断加入队列的尾部,而位于队列头部的人逐个离开。

    如图 5-4 所示,我们将队列的头部称为“队首”,尾部称为“队尾”,将把元素加入队尾的操作称为“入队”,删除队首元素的操作称为“出队”。

    -

    队列的先入先出规则

    +

    队列的先入先出规则

    图 5-4   队列的先入先出规则

    5.2.1   队列常用操作

    @@ -3839,13 +3674,13 @@
    -

    基于链表实现队列的入队出队操作

    +

    基于链表实现队列的入队出队操作

    -

    linkedlist_queue_push

    +

    linkedlist_queue_push

    -

    linkedlist_queue_pop

    +

    linkedlist_queue_pop

    @@ -4711,13 +4546,13 @@
    -

    基于数组实现队列的入队出队操作

    +

    基于数组实现队列的入队出队操作

    -

    array_queue_push

    +

    array_queue_push

    -

    array_queue_pop

    +

    array_queue_pop

    @@ -5768,10 +5603,15 @@ aria-label="页脚" @@ -5840,5 +5680,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_stack_and_queue/stack/index.html b/chapter_stack_and_queue/stack/index.html index 6ae027874..d05719170 100644 --- a/chapter_stack_and_queue/stack/index.html +++ b/chapter_stack_and_queue/stack/index.html @@ -60,7 +60,18 @@ - + @@ -1965,14 +1976,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1993,14 +1996,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2420,14 +2415,6 @@ 第 12 章   分治 - - - - - - - - @@ -2459,14 +2446,6 @@ 12.1   分治算法 - - - - - - - - @@ -2487,14 +2466,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2515,14 +2486,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2543,14 +2506,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2571,14 +2526,6 @@ 12.5   小结 - - - - - - - - @@ -2810,14 +2757,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2849,14 +2788,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2877,14 +2808,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2905,14 +2828,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2933,14 +2848,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2961,14 +2868,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2989,14 +2888,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3017,14 +2908,6 @@ 14.7   小结 - - - - - - - - @@ -3083,14 +2966,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3122,14 +2997,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3150,14 +3017,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3178,14 +3037,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3206,14 +3057,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3234,14 +3077,6 @@ 15.5   小结 - - - - - - - - @@ -3545,7 +3380,7 @@

    「栈 stack」是一种遵循先入后出的逻辑的线性数据结构。

    我们可以将栈类比为桌面上的一摞盘子,如果需要拿出底部的盘子,则需要先将上面的盘子依次取出。我们将盘子替换为各种类型的元素(如整数、字符、对象等),就得到了栈数据结构。

    如图 5-1 所示,我们把堆叠元素的顶部称为“栈顶”,底部称为“栈底”。将把元素添加到栈顶的操作叫做“入栈”,删除栈顶元素的操作叫做“出栈”。

    -

    栈的先入后出规则

    +

    栈的先入后出规则

    图 5-1   栈的先入后出规则

    5.1.1   栈常用操作

    @@ -3854,13 +3689,13 @@
    -

    基于链表实现栈的入栈出栈操作

    +

    基于链表实现栈的入栈出栈操作

    -

    linkedlist_stack_push

    +

    linkedlist_stack_push

    -

    linkedlist_stack_pop

    +

    linkedlist_stack_pop

    @@ -4600,13 +4435,13 @@
    -

    基于数组实现栈的入栈出栈操作

    +

    基于数组实现栈的入栈出栈操作

    -

    array_stack_push

    +

    array_stack_push

    -

    array_stack_pop

    +

    array_stack_pop

    @@ -5364,10 +5199,15 @@ aria-label="页脚" @@ -5436,5 +5276,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_stack_and_queue/summary/index.html b/chapter_stack_and_queue/summary/index.html index 77f115186..f63856710 100644 --- a/chapter_stack_and_queue/summary/index.html +++ b/chapter_stack_and_queue/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3667,10 +3502,15 @@ aria-label="页脚" @@ -3739,5 +3579,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_tree/array_representation_of_tree/index.html b/chapter_tree/array_representation_of_tree/index.html index b307eb1af..35d1361e2 100644 --- a/chapter_tree/array_representation_of_tree/index.html +++ b/chapter_tree/array_representation_of_tree/index.html @@ -60,7 +60,18 @@ - + @@ -1938,14 +1949,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1966,14 +1969,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2393,14 +2388,6 @@ 第 12 章   分治 - - - - - - - - @@ -2432,14 +2419,6 @@ 12.1   分治算法 - - - - - - - - @@ -2460,14 +2439,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2488,14 +2459,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2516,14 +2479,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2544,14 +2499,6 @@ 12.5   小结 - - - - - - - - @@ -2783,14 +2730,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2822,14 +2761,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2850,14 +2781,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2878,14 +2801,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2906,14 +2821,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2934,14 +2841,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2962,14 +2861,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2990,14 +2881,6 @@ 14.7   小结 - - - - - - - - @@ -3056,14 +2939,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3095,14 +2970,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3123,14 +2990,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3151,14 +3010,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3179,14 +3030,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3207,14 +3050,6 @@ 15.5   小结 - - - - - - - - @@ -3493,14 +3328,14 @@

    7.3.1   表示完美二叉树

    先分析一个简单案例。给定一个完美二叉树,我们将所有节点按照层序遍历的顺序存储在一个数组中,则每个节点都对应唯一的数组索引。

    根据层序遍历的特性,我们可以推导出父节点索引与子节点索引之间的“映射公式”:若节点的索引为 \(i\) ,则该节点的左子节点索引为 \(2i + 1\) ,右子节点索引为 \(2i + 2\) 。图 7-12 展示了各个节点索引之间的映射关系。

    -

    完美二叉树的数组表示

    +

    完美二叉树的数组表示

    图 7-12   完美二叉树的数组表示

    映射公式的角色相当于链表中的指针。给定数组中的任意一个节点,我们都可以通过映射公式来访问它的左(右)子节点。

    7.3.2   表示任意二叉树

    完美二叉树是一个特例,在二叉树的中间层通常存在许多 \(\text{None}\) 。由于层序遍历序列并不包含这些 \(\text{None}\) ,因此我们无法仅凭该序列来推测 \(\text{None}\) 的数量和分布位置。这意味着存在多种二叉树结构都符合该层序遍历序列

    如图 7-13 所示,给定一个非完美二叉树,上述的数组表示方法已经失效。

    -

    层序遍历序列对应多种二叉树可能性

    +

    层序遍历序列对应多种二叉树可能性

    图 7-13   层序遍历序列对应多种二叉树可能性

    为了解决此问题,我们可以考虑在层序遍历序列中显式地写出所有 \(\text{None}\) 。如图 7-14 所示,这样处理后,层序遍历序列就可以唯一表示二叉树了。

    @@ -3576,12 +3411,12 @@
    -

    任意类型二叉树的数组表示

    +

    任意类型二叉树的数组表示

    图 7-14   任意类型二叉树的数组表示

    值得说明的是,完全二叉树非常适合使用数组来表示。回顾完全二叉树的定义,\(\text{None}\) 只出现在最底层且靠右的位置,因此所有 \(\text{None}\) 一定出现在层序遍历序列的末尾

    这意味着使用数组表示完全二叉树时,可以省略存储所有 \(\text{None}\) ,非常方便。图 7-15 给出了一个例子。

    -

    完全二叉树的数组表示

    +

    完全二叉树的数组表示

    图 7-15   完全二叉树的数组表示

    以下代码实现了一个基于数组表示的二叉树,包括以下几种操作。

    @@ -4761,10 +4596,15 @@ aria-label="页脚" @@ -4833,5 +4673,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_tree/avl_tree/index.html b/chapter_tree/avl_tree/index.html index cd52ddaa1..d048bb761 100644 --- a/chapter_tree/avl_tree/index.html +++ b/chapter_tree/avl_tree/index.html @@ -60,7 +60,18 @@ - + @@ -2033,14 +2044,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2061,14 +2064,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2488,14 +2483,6 @@ 第 12 章   分治 - - - - - - - - @@ -2527,14 +2514,6 @@ 12.1   分治算法 - - - - - - - - @@ -2555,14 +2534,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2583,14 +2554,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2611,14 +2574,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2639,14 +2594,6 @@ 12.5   小结 - - - - - - - - @@ -2878,14 +2825,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2917,14 +2856,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2945,14 +2876,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2973,14 +2896,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -3001,14 +2916,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -3029,14 +2936,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3057,14 +2956,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3085,14 +2976,6 @@ 14.7   小结 - - - - - - - - @@ -3151,14 +3034,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3190,14 +3065,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3218,14 +3085,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3246,14 +3105,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3274,14 +3125,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3302,14 +3145,6 @@ 15.5   小结 - - - - - - - - @@ -3680,11 +3515,11 @@

    7.5   AVL 树 *

    在二叉搜索树章节中,我们提到了在多次插入和删除操作后,二叉搜索树可能退化为链表。这种情况下,所有操作的时间复杂度将从 \(O(\log n)\) 恶化为 \(O(n)\)

    如图 7-24 所示,经过两次删除节点操作,这个二叉搜索树便会退化为链表。

    -

    AVL 树在删除节点后发生退化

    +

    AVL 树在删除节点后发生退化

    图 7-24   AVL 树在删除节点后发生退化

    再例如,在图 7-25 的完美二叉树中插入两个节点后,树将严重向左倾斜,查找操作的时间复杂度也随之恶化。

    -

    AVL 树在插入节点后发生退化

    +

    AVL 树在插入节点后发生退化

    图 7-25   AVL 树在插入节点后发生退化

    G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorithm for the organization of information" 中提出了「AVL 树」。论文中详细描述了一系列操作,确保在持续添加和删除节点后,AVL 树不会退化,从而使得各种操作的时间复杂度保持在 \(O(\log n)\) 级别。换句话说,在需要频繁进行增删查改操作的场景中,AVL 树能始终保持高效的数据操作性能,具有很好的应用价值。

    @@ -4189,23 +4024,23 @@
    -

    右旋操作步骤

    +

    右旋操作步骤

    -

    avltree_right_rotate_step2

    +

    avltree_right_rotate_step2

    -

    avltree_right_rotate_step3

    +

    avltree_right_rotate_step3

    -

    avltree_right_rotate_step4

    +

    avltree_right_rotate_step4

    图 7-26   右旋操作步骤

    如图 7-27 所示,当节点 child 有右子节点(记为 grandChild )时,需要在右旋中添加一步:将 grandChild 作为 node 的左子节点。

    -

    有 grandChild 的右旋操作

    +

    有 grandChild 的右旋操作

    图 7-27   有 grandChild 的右旋操作

    “向右旋转”是一种形象化的说法,实际上需要通过修改节点指针来实现,代码如下所示。

    @@ -4412,11 +4247,11 @@

    2.   左旋

    相应的,如果考虑上述失衡二叉树的“镜像”,则需要执行图 7-28 所示的“左旋”操作。

    -

    左旋操作

    +

    左旋操作

    图 7-28   左旋操作

    同理,如图 7-29 所示,当节点 child 有左子节点(记为 grandChild )时,需要在左旋中添加一步:将 grandChild 作为 node 的右子节点。

    -

    有 grandChild 的左旋操作

    +

    有 grandChild 的左旋操作

    图 7-29   有 grandChild 的左旋操作

    可以观察到,右旋和左旋操作在逻辑上是镜像对称的,它们分别解决的两种失衡情况也是对称的。基于对称性,我们只需将右旋的实现代码中的所有的 left 替换为 right ,将所有的 right 替换为 left ,即可得到左旋的实现代码。

    @@ -4623,17 +4458,17 @@

    3.   先左旋后右旋

    对于图 7-30 中的失衡节点 3 ,仅使用左旋或右旋都无法使子树恢复平衡。此时需要先对 child 执行“左旋”,再对 node 执行“右旋”。

    -

    先左旋后右旋

    +

    先左旋后右旋

    图 7-30   先左旋后右旋

    4.   先右旋后左旋

    如图 7-31 所示,对于上述失衡二叉树的镜像情况,需要先对 child 执行“右旋”,然后对 node 执行“左旋”。

    -

    先右旋后左旋

    +

    先右旋后左旋

    图 7-31   先右旋后左旋

    5.   旋转的选择

    图 7-32 展示的四种失衡情况与上述案例逐个对应,分别需要采用右旋、左旋、先右后左、先左后右的旋转操作。

    -

    AVL 树的四种旋转情况

    +

    AVL 树的四种旋转情况

    图 7-32   AVL 树的四种旋转情况

    如下表所示,我们通过判断失衡节点的平衡因子以及较高一侧子节点的平衡因子的正负号,来确定失衡节点属于图 7-32 中的哪种情况。

    @@ -6101,10 +5936,15 @@ aria-label="页脚" @@ -6173,5 +6013,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_tree/binary_search_tree/index.html b/chapter_tree/binary_search_tree/index.html index 2b626832d..c14ed0a98 100644 --- a/chapter_tree/binary_search_tree/index.html +++ b/chapter_tree/binary_search_tree/index.html @@ -60,7 +60,18 @@ - + @@ -1972,14 +1983,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2000,14 +2003,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2427,14 +2422,6 @@ 第 12 章   分治 - - - - - - - - @@ -2466,14 +2453,6 @@ 12.1   分治算法 - - - - - - - - @@ -2494,14 +2473,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2522,14 +2493,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2550,14 +2513,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2578,14 +2533,6 @@ 12.5   小结 - - - - - - - - @@ -2817,14 +2764,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2856,14 +2795,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2884,14 +2815,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2912,14 +2835,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2940,14 +2855,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2968,14 +2875,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2996,14 +2895,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3024,14 +2915,6 @@ 14.7   小结 - - - - - - - - @@ -3090,14 +2973,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3129,14 +3004,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3157,14 +3024,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3185,14 +3044,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3213,14 +3064,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3241,14 +3084,6 @@ 15.5   小结 - - - - - - - - @@ -3561,7 +3396,7 @@
  • 对于根节点,左子树中所有节点的值 \(<\) 根节点的值 \(<\) 右子树中所有节点的值。
  • 任意节点的左、右子树也是二叉搜索树,即同样满足条件 1.
  • -

    二叉搜索树

    +

    二叉搜索树

    图 7-16   二叉搜索树

    7.4.1   二叉搜索树的操作

    @@ -3576,16 +3411,16 @@
    -

    二叉搜索树查找节点示例

    +

    二叉搜索树查找节点示例

    -

    bst_search_step2

    +

    bst_search_step2

    -

    bst_search_step3

    +

    bst_search_step3

    -

    bst_search_step4

    +

    bst_search_step4

    @@ -3855,7 +3690,7 @@
  • 查找插入位置:与查找操作相似,从根节点出发,根据当前节点值和 num 的大小关系循环向下搜索,直到越过叶节点(遍历至 \(\text{None}\) )时跳出循环。
  • 在该位置插入节点:初始化节点 num ,将该节点置于 \(\text{None}\) 的位置。
  • -

    在二叉搜索树中插入节点

    +

    在二叉搜索树中插入节点

    图 7-18   在二叉搜索树中插入节点

    在代码实现中,需要注意以下两点。

    @@ -4256,11 +4091,11 @@

    与插入节点类似,我们需要保证在删除操作完成后,二叉搜索树的“左子树 < 根节点 < 右子树”的性质仍然满足。

    因此,我们需要根据目标节点的子节点数量,共分为 0、1 和 2 这三种情况,执行对应的删除节点操作。

    如图 7-19 所示,当待删除节点的度为 \(0\) 时,表示该节点是叶节点,可以直接删除。

    -

    在二叉搜索树中删除节点(度为 0 )

    +

    在二叉搜索树中删除节点(度为 0 )

    图 7-19   在二叉搜索树中删除节点(度为 0 )

    如图 7-20 所示,当待删除节点的度为 \(1\) 时,将待删除节点替换为其子节点即可。

    -

    在二叉搜索树中删除节点(度为 1 )

    +

    在二叉搜索树中删除节点(度为 1 )

    图 7-20   在二叉搜索树中删除节点(度为 1 )

    当待删除节点的度为 \(2\) 时,我们无法直接删除它,而需要使用一个节点替换该节点。由于要保持二叉搜索树“左 \(<\)\(<\) 右”的性质,因此这个节点可以是右子树的最小节点或左子树的最大节点

    @@ -4272,16 +4107,16 @@
    -

    在二叉搜索树中删除节点(度为 2 )

    +

    在二叉搜索树中删除节点(度为 2 )

    -

    bst_remove_case3_step2

    +

    bst_remove_case3_step2

    -

    bst_remove_case3_step3

    +

    bst_remove_case3_step3

    -

    bst_remove_case3_step4

    +

    bst_remove_case3_step4

    @@ -4942,7 +4777,7 @@

    如图 7-22 所示,二叉树的中序遍历遵循“左 \(\rightarrow\)\(\rightarrow\) 右”的遍历顺序,而二叉搜索树满足“左子节点 \(<\) 根节点 \(<\) 右子节点”的大小关系。

    这意味着在二叉搜索树中进行中序遍历时,总是会优先遍历下一个最小节点,从而得出一个重要性质:二叉搜索树的中序遍历序列是升序的

    利用中序遍历升序的性质,我们在二叉搜索树中获取有序数据仅需 \(O(n)\) 时间,无须进行额外的排序操作,非常高效。

    -

    二叉搜索树的中序遍历序列

    +

    二叉搜索树的中序遍历序列

    图 7-22   二叉搜索树的中序遍历序列

    7.4.2   二叉搜索树的效率

    @@ -4979,7 +4814,7 @@

    在理想情况下,二叉搜索树是“平衡”的,这样就可以在 \(\log n\) 轮循环内查找任意节点。

    然而,如果我们在二叉搜索树中不断地插入和删除节点,可能导致二叉树退化为图 7-23 所示的链表,这时各种操作的时间复杂度也会退化为 \(O(n)\)

    -

    二叉搜索树的退化

    +

    二叉搜索树的退化

    图 7-23   二叉搜索树的退化

    7.4.3   二叉搜索树常见应用

    @@ -5151,10 +4986,15 @@ aria-label="页脚" @@ -5223,5 +5063,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_tree/binary_tree/index.html b/chapter_tree/binary_tree/index.html index 6d8dc83b9..e2c01e2ae 100644 --- a/chapter_tree/binary_tree/index.html +++ b/chapter_tree/binary_tree/index.html @@ -60,7 +60,18 @@ - + @@ -1999,14 +2010,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -2027,14 +2030,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2454,14 +2449,6 @@ 第 12 章   分治 - - - - - - - - @@ -2493,14 +2480,6 @@ 12.1   分治算法 - - - - - - - - @@ -2521,14 +2500,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2549,14 +2520,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2577,14 +2540,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2605,14 +2560,6 @@ 12.5   小结 - - - - - - - - @@ -2844,14 +2791,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2883,14 +2822,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2911,14 +2842,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2939,14 +2862,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2967,14 +2882,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2995,14 +2902,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -3023,14 +2922,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3051,14 +2942,6 @@ 14.7   小结 - - - - - - - - @@ -3117,14 +3000,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3156,14 +3031,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3184,14 +3051,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3212,14 +3071,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3240,14 +3091,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3268,14 +3111,6 @@ 15.5   小结 - - - - - - - - @@ -3752,7 +3587,7 @@

    每个节点都有两个引用(指针),分别指向「左子节点 left-child node」和「右子节点 right-child node」,该节点被称为这两个子节点的「父节点 parent node」。当给定一个二叉树的节点时,我们将该节点的左子节点及其以下节点形成的树称为该节点的「左子树 left subtree」,同理可得「右子树 right subtree」。

    在二叉树中,除叶节点外,其他所有节点都包含子节点和非空子树。如图 7-1 所示,如果将“节点 2”视为父节点,则其左子节点和右子节点分别是“节点 4”和“节点 5”,左子树是“节点 4 及其以下节点形成的树”,右子树是“节点 5 及其以下节点形成的树”。

    -

    父节点、子节点、子树

    +

    父节点、子节点、子树

    图 7-1   父节点、子节点、子树

    7.1.1   二叉树常见术语

    @@ -3767,7 +3602,7 @@
  • 节点的「深度 depth」:从根节点到该节点所经过的边的数量。
  • 节点的「高度 height」:从最远叶节点到该节点所经过的边的数量。
  • -

    二叉树的常用术语

    +

    二叉树的常用术语

    图 7-2   二叉树的常用术语

    @@ -3939,7 +3774,7 @@

    2.   插入与删除节点

    与链表类似,在二叉树中插入与删除节点可以通过修改指针来实现。图 7-3 给出了一个示例。

    -

    在二叉树中插入与删除节点

    +

    在二叉树中插入与删除节点

    图 7-3   在二叉树中插入与删除节点

    @@ -4063,22 +3898,22 @@

    Tip

    请注意,在中文社区中,完美二叉树常被称为「满二叉树」。

    -

    完美二叉树

    +

    完美二叉树

    图 7-4   完美二叉树

    2.   完全二叉树

    如图 7-5 所示,「完全二叉树 complete binary tree」只有最底层的节点未被填满,且最底层节点尽量靠左填充。

    -

    完全二叉树

    +

    完全二叉树

    图 7-5   完全二叉树

    3.   完满二叉树

    如图 7-6 所示,「完满二叉树 full binary tree」除了叶节点之外,其余所有节点都有两个子节点。

    -

    完满二叉树

    +

    完满二叉树

    图 7-6   完满二叉树

    4.   平衡二叉树

    如图 7-7 所示,「平衡二叉树 balanced binary tree」中任意节点的左子树和右子树的高度之差的绝对值不超过 1 。

    -

    平衡二叉树

    +

    平衡二叉树

    图 7-7   平衡二叉树

    7.1.4   二叉树的退化

    @@ -4087,7 +3922,7 @@
  • 完美二叉树是理想情况,可以充分发挥二叉树“分治”的优势。
  • 链表则是另一个极端,各项操作都变为线性操作,时间复杂度退化至 \(O(n)\)
  • -

    二叉树的最佳与最差结构

    +

    二叉树的最佳与最差结构

    图 7-8   二叉树的最佳与最差结构

    如表 7-1 所示,在最佳和最差结构下,二叉树的叶节点数量、节点总数、高度等达到极大或极小值。

    @@ -4289,10 +4124,15 @@ aria-label="页脚" @@ -4361,5 +4201,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_tree/binary_tree_traversal/index.html b/chapter_tree/binary_tree_traversal/index.html index c11ca79f3..6d994bd9c 100644 --- a/chapter_tree/binary_tree_traversal/index.html +++ b/chapter_tree/binary_tree_traversal/index.html @@ -60,7 +60,18 @@ - + @@ -1971,14 +1982,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1999,14 +2002,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2426,14 +2421,6 @@ 第 12 章   分治 - - - - - - - - @@ -2465,14 +2452,6 @@ 12.1   分治算法 - - - - - - - - @@ -2493,14 +2472,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2521,14 +2492,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2549,14 +2512,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2577,14 +2532,6 @@ 12.5   小结 - - - - - - - - @@ -2816,14 +2763,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2855,14 +2794,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2883,14 +2814,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2911,14 +2834,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2939,14 +2854,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2967,14 +2874,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2995,14 +2894,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -3023,14 +2914,6 @@ 14.7   小结 - - - - - - - - @@ -3089,14 +2972,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3128,14 +3003,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3156,14 +3023,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3184,14 +3043,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3212,14 +3063,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3240,14 +3083,6 @@ 15.5   小结 - - - - - - - - @@ -3559,7 +3394,7 @@

    7.2.1   层序遍历

    如图 7-9 所示,「层序遍历 level-order traversal」从顶部到底部逐层遍历二叉树,并在每一层按照从左到右的顺序访问节点。

    层序遍历本质上属于「广度优先遍历 breadth-first traversal」,它体现了一种“一圈一圈向外扩展”的逐层遍历方式。

    -

    二叉树的层序遍历

    +

    二叉树的层序遍历

    图 7-9   二叉树的层序遍历

    1.   代码实现

    @@ -3854,7 +3689,7 @@

    7.2.2   前序、中序、后序遍历

    相应地,前序、中序和后序遍历都属于「深度优先遍历 depth-first traversal」,它体现了一种“先走到尽头,再回溯继续”的遍历方式。

    图 7-10 展示了对二叉树进行深度优先遍历的工作原理。深度优先遍历就像是绕着整个二叉树的外围“走”一圈,在每个节点都会遇到三个位置,分别对应前序遍历、中序遍历和后序遍历。

    -

    二叉搜索树的前、中、后序遍历

    +

    二叉搜索树的前、中、后序遍历

    图 7-10   二叉搜索树的前、中、后序遍历

    1.   代码实现

    @@ -4262,37 +4097,37 @@
    -

    前序遍历的递归过程

    +

    前序遍历的递归过程

    -

    preorder_step2

    +

    preorder_step2

    -

    preorder_step3

    +

    preorder_step3

    -

    preorder_step4

    +

    preorder_step4

    -

    preorder_step5

    +

    preorder_step5

    -

    preorder_step6

    +

    preorder_step6

    -

    preorder_step7

    +

    preorder_step7

    -

    preorder_step8

    +

    preorder_step8

    -

    preorder_step9

    +

    preorder_step9

    -

    preorder_step10

    +

    preorder_step10

    -

    preorder_step11

    +

    preorder_step11

    @@ -4466,10 +4301,15 @@ aria-label="页脚" @@ -4538,5 +4378,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_tree/index.html b/chapter_tree/index.html index 400cf5264..b708add65 100644 --- a/chapter_tree/index.html +++ b/chapter_tree/index.html @@ -60,7 +60,18 @@ - + @@ -1877,14 +1888,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1905,14 +1908,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2332,14 +2327,6 @@ 第 12 章   分治 - - - - - - - - @@ -2371,14 +2358,6 @@ 12.1   分治算法 - - - - - - - - @@ -2399,14 +2378,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2427,14 +2398,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2455,14 +2418,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2483,14 +2438,6 @@ 12.5   小结 - - - - - - - - @@ -2722,14 +2669,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2761,14 +2700,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2789,14 +2720,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2817,14 +2740,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2845,14 +2760,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2873,14 +2780,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2901,14 +2800,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2929,14 +2820,6 @@ 14.7   小结 - - - - - - - - @@ -2995,14 +2878,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3034,14 +2909,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3062,14 +2929,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3090,14 +2949,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3118,14 +2969,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3146,14 +2989,6 @@ 15.5   小结 - - - - - - - - @@ -3414,7 +3249,7 @@

    第 7 章   树

    -

    树

    +

    树

    Abstract

    @@ -3593,10 +3428,15 @@ aria-label="页脚" @@ -3665,5 +3505,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/chapter_tree/summary/index.html b/chapter_tree/summary/index.html index 52691acc3..d8b0290db 100644 --- a/chapter_tree/summary/index.html +++ b/chapter_tree/summary/index.html @@ -60,7 +60,18 @@ - + @@ -1931,14 +1942,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1959,14 +1962,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2386,14 +2381,6 @@ 第 12 章   分治 - - - - - - - - @@ -2425,14 +2412,6 @@ 12.1   分治算法 - - - - - - - - @@ -2453,14 +2432,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2481,14 +2452,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2509,14 +2472,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2537,14 +2492,6 @@ 12.5   小结 - - - - - - - - @@ -2776,14 +2723,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2815,14 +2754,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2843,14 +2774,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2871,14 +2794,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2899,14 +2814,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2927,14 +2834,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2955,14 +2854,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2983,14 +2874,6 @@ 14.7   小结 - - - - - - - - @@ -3049,14 +2932,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3088,14 +2963,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3116,14 +2983,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3144,14 +3003,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3172,14 +3023,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3200,14 +3043,6 @@ 15.5   小结 - - - - - - - - @@ -3689,10 +3524,15 @@ aria-label="页脚" @@ -3761,5 +3601,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/index.html b/index.html index 6f8a03f83..2ee152d49 100644 --- a/index.html +++ b/index.html @@ -56,7 +56,18 @@ - + @@ -1868,14 +1879,6 @@ 10.2   二分查找插入点 - - - - - - - - @@ -1896,14 +1899,6 @@ 10.3   二分查找边界 - - - - - - - - @@ -2323,14 +2318,6 @@ 第 12 章   分治 - - - - - - - - @@ -2362,14 +2349,6 @@ 12.1   分治算法 - - - - - - - - @@ -2390,14 +2369,6 @@ 12.2   分治搜索策略 - - - - - - - - @@ -2418,14 +2389,6 @@ 12.3   构建树问题 - - - - - - - - @@ -2446,14 +2409,6 @@ 12.4   汉诺塔问题 - - - - - - - - @@ -2474,14 +2429,6 @@ 12.5   小结 - - - - - - - - @@ -2713,14 +2660,6 @@ 第 14 章   动态规划 - - - - - - - - @@ -2752,14 +2691,6 @@ 14.1   初探动态规划 - - - - - - - - @@ -2780,14 +2711,6 @@ 14.2   DP 问题特性 - - - - - - - - @@ -2808,14 +2731,6 @@ 14.3   DP 解题思路 - - - - - - - - @@ -2836,14 +2751,6 @@ 14.4   0-1 背包问题 - - - - - - - - @@ -2864,14 +2771,6 @@ 14.5   完全背包问题 - - - - - - - - @@ -2892,14 +2791,6 @@ 14.6   编辑距离问题 - - - - - - - - @@ -2920,14 +2811,6 @@ 14.7   小结 - - - - - - - - @@ -2986,14 +2869,6 @@ 第 15 章   贪心 - - - - - - - - @@ -3025,14 +2900,6 @@ 15.1   贪心算法 - - - - - - - - @@ -3053,14 +2920,6 @@ 15.2   分数背包问题 - - - - - - - - @@ -3081,14 +2940,6 @@ 15.3   最大容量问题 - - - - - - - - @@ -3109,14 +2960,6 @@ 15.4   最大切分乘积问题 - - - - - - - - @@ -3137,14 +2980,6 @@ 15.5   小结 - - - - - - - - @@ -3391,8 +3226,8 @@

    - - + +

    《 Hello 算法 》

    @@ -3401,11 +3236,11 @@

    - GitHub Repo stars + GitHub Repo stars   - GitHub contributors + GitHub contributors

    @@ -3427,7 +3262,7 @@

    内容清晰易懂、学习曲线平滑
    电脑、平板、手机全终端阅读

    -

    algorithm_animation

    +

    algorithm_animation

    "A picture is worth a thousand words."

    “一图胜千言”

    @@ -3437,7 +3272,7 @@

    提供各个算法与数据结构的简洁实现与测试样例,皆可直接运行
    支持 Java, C++, Python, Go, JS, TS, C#, Swift, Zig 等语言

    -

    running_code

    +

    running_code

    "Talk is cheap. Show me the code."

    “少吹牛,看代码”

    @@ -3447,7 +3282,7 @@

    鼓励小伙伴们互帮互助、共同成长
    提问与评论一般能在两日内得到回复

    -

    comment

    +

    comment

    “追风赶月莫停留,平芜尽处是春山”

    一起加油!

    @@ -3467,7 +3302,7 @@

    本书在开源社区众多贡献者的共同努力下不断成长。感谢每一位投入时间与精力的撰稿人,是他们的无私奉献使这本书变得更好,他们是(按照 GitHub 自动生成的顺序):

    - +

    @@ -3475,15 +3310,15 @@
    - - - - - - - - - + + + + + + + + +
    Gonglja
    Gonglja

    C, C++
    gvenusleo
    gvenusleo

    Dart
    hpstory
    hpstory

    C#
    justin-tse
    justin-tse

    JS, TS
    krahets
    krahets

    Java, Python
    night-cruise
    night-cruise

    Rust
    nuomi1
    nuomi1

    Swift
    Reanon
    Reanon

    Go, C
    sjinzh
    sjinzh

    Rust, Zig
    Gonglja
    Gonglja
    C, C++
    gvenusleo
    gvenusleo
    Dart
    hpstory
    hpstory
    C#
    justin-tse
    justin-tse
    JS, TS
    krahets
    krahets
    Java, Python
    night-cruise
    night-cruise
    Rust
    nuomi1
    nuomi1
    Swift
    Reanon
    Reanon
    Go, C
    sjinzh
    sjinzh
    Rust, Zig
    @@ -3612,10 +3447,15 @@ aria-label="页脚" @@ -3684,5 +3524,5 @@ aria-label="页脚" - + \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index aaf3fa025..0bf8b0afb 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,512 +2,512 @@ https://www.hello-algo.com/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_appendix/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_appendix/contribution/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_appendix/installation/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_array_and_linkedlist/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_array_and_linkedlist/array/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_array_and_linkedlist/list/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_array_and_linkedlist/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_backtracking/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_backtracking/backtracking_algorithm/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_backtracking/n_queens_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_backtracking/permutations_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_backtracking/subset_sum_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_backtracking/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_computational_complexity/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_computational_complexity/iteration_and_recursion/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_computational_complexity/space_complexity/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_computational_complexity/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_computational_complexity/time_complexity/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_data_structure/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_data_structure/basic_data_types/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_data_structure/character_encoding/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_data_structure/number_encoding/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_data_structure/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_divide_and_conquer/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_divide_and_conquer/binary_search_recur/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_divide_and_conquer/build_binary_tree_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_divide_and_conquer/divide_and_conquer/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_divide_and_conquer/hanota_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_divide_and_conquer/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_dynamic_programming/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_dynamic_programming/dp_problem_features/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_dynamic_programming/dp_solution_pipeline/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_dynamic_programming/edit_distance_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_dynamic_programming/intro_to_dynamic_programming/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_dynamic_programming/knapsack_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_dynamic_programming/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_dynamic_programming/unbounded_knapsack_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_graph/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_graph/graph/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_graph/graph_operations/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_graph/graph_traversal/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_graph/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_greedy/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_greedy/fractional_knapsack_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_greedy/greedy_algorithm/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_greedy/max_capacity_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_greedy/max_product_cutting_problem/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_greedy/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_hashing/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_hashing/hash_algorithm/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_hashing/hash_collision/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_hashing/hash_map/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_hashing/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_heap/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_heap/build_heap/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_heap/heap/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_heap/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_heap/top_k/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_introduction/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_introduction/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_introduction/what_is_dsa/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_preface/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_preface/about_the_book/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_preface/suggestions/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_preface/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_reference/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_searching/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_searching/binary_search/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_searching/binary_search_edge/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_searching/binary_search_insertion/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_searching/replace_linear_by_hashing/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_searching/searching_algorithm_revisited/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_searching/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/bubble_sort/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/bucket_sort/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/counting_sort/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/heap_sort/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/insertion_sort/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/merge_sort/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/quick_sort/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/radix_sort/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/selection_sort/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/sorting_algorithm/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_sorting/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_stack_and_queue/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_stack_and_queue/deque/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_stack_and_queue/queue/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_stack_and_queue/stack/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_stack_and_queue/summary/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_tree/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_tree/array_representation_of_tree/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_tree/avl_tree/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_tree/binary_search_tree/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_tree/binary_tree/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_tree/binary_tree_traversal/ - 2023-09-21 + 2023-09-22 daily https://www.hello-algo.com/chapter_tree/summary/ - 2023-09-21 + 2023-09-22 daily \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index 35289ef54..81f27aadc 100644 Binary files a/sitemap.xml.gz and b/sitemap.xml.gz differ