lq1995 发表于 2024-10-15 19:13:32

哔哩哔哩-万诺 coding 城市旅游规划笔试题解析,涵盖滑动窗口、二分查找等算法

计算机科学领域,数组处理技能被视为根本且关键的技巧之一。本文旨在对数组内滑动窗口与二分搜索技术进行深入剖析,这两项技术在处理复杂数据结构问题时展现出卓越的效能。通过对这两种技术的原理、适用场合及编程实现进行详尽阐述,旨在助力读者深入理解和熟练掌握这些核心技巧。

滑动窗口的基本概念

<p><pre>    <code class="prism language-python"><span class="token comment"># 输入处理</span>
N<span class="token punctuation">,</span> k <span class="token operator">=</span> <span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&#39; &#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
city_xy <span class="token operator">=</span> <span class="token builtin">dict</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>N<span class="token punctuation">)</span><span class="token punctuation">:</span>
    x<span class="token punctuation">,</span> y <span class="token operator">=</span> <span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&#39; &#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    city_xy<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> y
<span class="token comment"># 对键进行排序</span>
keys_sorted <span class="token operator">=</span> <span class="token builtin">sorted</span><span class="token punctuation">(</span>city_xy<span class="token punctuation">.</span>keys<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># 定义双指针</span>
left <span class="token operator">=</span> right <span class="token operator">=</span> <span class="token number">0</span>
happy_vals <span class="token operator">=</span> <span class="token number">0</span>
max_happy <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">while</span> right <span class="token operator"><</span> <span class="token builtin">len</span><span class="token punctuation">(</span>keys_sorted<span class="token punctuation">)</span><span class="token punctuation">:</span>
    happy_vals <span class="token operator">+=</span> city_xy<span class="token punctuation">[</span>keys_sorted<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">]</span>
    <span class="token comment"># 两个城市之间的花费差值不小于k</span>
    <span class="token comment"># 左指针就要右移,同时要减去对应的开心值</span>
    <span class="token keyword">if</span> keys_sorted<span class="token punctuation">[</span>right<span class="token punctuation">]</span> <span class="token operator">-</span> keys_sorted<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">>=</span> k<span class="token punctuation">:</span>
      happy_vals <span class="token operator">-=</span> city_xy<span class="token punctuation">[</span>keys_sorted<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">]</span>
      left <span class="token operator">+=</span> <span class="token number">1</span>
    <span class="token comment"># 取当前较大的开心值</span>
    max_happy <span class="token operator">=</span> <span class="token builtin">max</span><span class="token punctuation">(</span>max_happy<span class="token punctuation">,</span> happy_vals<span class="token punctuation">)</span>
    right <span class="token operator">+=</span> <span class="token number">1</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>max_happy<span class="token punctuation">)</span>
</code></pre></p>
滑动窗口技术是数组操作中的一种普遍应用手段,尤其擅长处理涉及连续子数组或子字符串的问题。其基本原理在于构建一个可移动的窗口,该窗口在数组中移动以高效解决相关任务。例如,在寻找特定和的连续子数组时,滑动窗口技术能有效降低计算复杂度。

<p><pre>    <code class="prism language-python">N<span class="token punctuation">,</span> M <span class="token operator">=</span> <span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&#39; &#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
room <span class="token operator">=</span> <span class="token builtin">list</span><span class="token punctuation">(</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&#39; &#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
room<span class="token punctuation">.</span>sort<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 判断在当前间隔下,是否满足放下M条猪的情况</span>
<span class="token keyword">def</span> <span class="token function">check</span><span class="token punctuation">(</span>mid<span class="token punctuation">)</span><span class="token punctuation">:</span>
    pre <span class="token operator">=</span> room<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
    count <span class="token operator">=</span> <span class="token number">1</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token builtin">len</span><span class="token punctuation">(</span>room<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
      <span class="token keyword">if</span> pre <span class="token operator">+</span> mid <span class="token operator"><=</span> room<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">:</span>
            count <span class="token operator">+=</span> <span class="token number">1</span>
            pre <span class="token operator">=</span> room<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
    <span class="token keyword">return</span> count <span class="token operator">>=</span> M
<span class="token comment"># 二分查找间隔</span>
maxGap <span class="token operator">=</span> <span class="token punctuation">(</span>room<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">-</span> room<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">//</span> <span class="token punctuation">(</span>M <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>
left<span class="token punctuation">,</span> right <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> maxGap
res <span class="token operator">=</span> maxGap
<span class="token keyword">while</span> left <span class="token operator"><=</span> right<span class="token punctuation">:</span>
    mid <span class="token operator">=</span> <span class="token punctuation">(</span>left <span class="token operator">+</span> right<span class="token punctuation">)</span> <span class="token operator">//</span> <span class="token number">2</span>
    <span class="token keyword">if</span> check<span class="token punctuation">(</span>mid<span class="token punctuation">)</span><span class="token punctuation">:</span>
      res <span class="token operator">=</span> mid
      left <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
      right <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
</code></pre></p>
滑动窗口技术通常依赖两个关键指针,分别定位窗口的起始端和终结端。通过灵活调整这两个指针的位置,能够实现对窗口尺寸及位置的实时调整。此策略在处理大规模数据集时展现出卓越的效率,因为它有效减少了冗余的计算步骤。

二分查找的原理与应用

<p><pre>    <code class="prism language-python">strInput <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 为了避免初始化,attack和defender都扩充了一个元素</span>
<span class="token comment"># attack扩充的是第一个元素</span>
<span class="token comment"># defender扩充的是最后一个元素</span>
attack <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>strInput<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
defender <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>strInput<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token comment"># 从前往后遍历</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token builtin">len</span><span class="token punctuation">(</span>strInput<span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 如果遇见0,则再前一个值的基础上加上当前下标</span>
    <span class="token keyword">if</span> strInput<span class="token punctuation">[</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&#39;0&#39;</span><span class="token punctuation">:</span>
      attack<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> attack<span class="token punctuation">[</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> i
    <span class="token keyword">else</span><span class="token punctuation">:</span>
      attack<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> attack<span class="token punctuation">[</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
<span class="token comment"># 从后往前遍历</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>strInput<span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 如果遇见1,则再前一个值得基础上加上前下标</span>
    <span class="token keyword">if</span> strInput<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&#39;1&#39;</span><span class="token punctuation">:</span>
      defender<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> defender<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> i<span class="token operator">+</span><span class="token number">1</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
      defender<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> defender<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span>
<span class="token comment"># 移除扩充元素</span>
attack <span class="token operator">=</span> attack<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
defender <span class="token operator">=</span> defender<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
res <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token string">&#39;inf&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>attack<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    res <span class="token operator">=</span> <span class="token builtin">min</span><span class="token punctuation">(</span>res<span class="token punctuation">,</span> <span class="token builtin">abs</span><span class="token punctuation">(</span>attack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">-</span>defender<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
</code></pre></p>
二分搜索算法作为一种高效的信息检索技术,特别适用于对有序数组进行操作。其核心机制在于通过连续缩小查找区间的方式,迅速锁定目标数据。尽管这一算法在表面上看似直观易懂,但在具体问题解决中,它往往深藏于复杂问题的内部,需经过细致剖析方能准确把握并有效运用。

二分查找在编程实践中广泛用于处理查找插入点、定位峰值等任务,通过精心设计的算法,它能在对数时间复杂度内完成搜索,显著提升了程序的执行效能。

滑动窗口与二分查找的结合应用

在处理某些繁复问题时,滑动窗口技术与二分查找法可相辅相成,以实现更佳的解决策略。以动态调整的数组为例,往往需兼顾窗口的移动与元素的定位。运用此两种方法融合,可构建出既高效又适应性强的算法模型。

<p><pre>    <code class="prism language-python"><span class="token keyword">def</span> <span class="token function">demo</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 最迟发生时间</span>
    ve <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token builtin">len</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span>
    que <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment"># 定义一个先进先出的队列</span>
    <span class="token keyword">while</span> que<span class="token punctuation">:</span>
      index <span class="token operator">=</span> que<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment"># 取出队列中第一个元素,即matrix中的第index行</span>
      <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment"># 遍历matrix中的第index行</span>
            <span class="token keyword">if</span> matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token comment"># 如果当前matrix元素不为零</span>
                <span class="token comment"># 判断从index到i的距离是否大于之前的距离</span>
                ve<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">max</span><span class="token punctuation">(</span>ve<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> ve<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token operator">+</span>matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token comment"># 将i节点添加到队列中</span>
                que<span class="token punctuation">.</span>append<span class="token punctuation">(</span>i<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;ve={}&#39;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>ve<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment"># 最早发生时间</span>
    vl <span class="token operator">=</span> <span class="token punctuation">[</span>ve<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token builtin">len</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span>
    <span class="token keyword">for</span> index <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment"># matrix从下往上遍历</span>
      <span class="token comment"># 再从左往右遍历</span>
      <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token comment"># 如果当前matrix元素不为零</span>
                <span class="token comment"># 判断从i到index的距离是否小于之前的距离</span>
                vl<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">min</span><span class="token punctuation">(</span>vl<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">,</span> vl<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">-</span>matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;vl={}&#39;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>vl<span class="token punctuation">)</span><span class="token punctuation">)</span>
    res <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>ve<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
      <span class="token keyword">if</span> ve<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> vl<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">:</span>
            res<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">"V"</span> <span class="token operator">+</span> <span class="token builtin">str</span><span class="token punctuation">(</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> res
matrix <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
keyPoint <span class="token operator">=</span> demo<span class="token punctuation">(</span>matrix<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;Key point: &#39;</span> <span class="token operator">+</span> <span class="token string">&#39; &#39;</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>keyPoint<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></p>
确保该应用融合的核心要素是妥善协调窗口滑动与搜索区间的关系。常规做法是,对数组进行预处理,优化其适用于二分搜索,并在滑动窗口操作中灵活调整搜索策略。

<p><pre>    <code class="prism language-python"><span class="token keyword">def</span> <span class="token function">demo</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span><span class="token punctuation">:</span>
    passed <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
    nopass <span class="token operator">=</span> <span class="token punctuation">[</span>i <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">if</span> i <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">]</span>
    maxDis <span class="token operator">=</span> matrix<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
    path <span class="token operator">=</span> <span class="token builtin">dict</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>maxDis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
      <span class="token keyword">if</span> maxDis<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
            path<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
    <span class="token keyword">while</span> nopass<span class="token punctuation">:</span>
      index <span class="token operator">=</span> nopass<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
      <span class="token keyword">for</span> k <span class="token keyword">in</span> nopass<span class="token punctuation">:</span>
            <span class="token keyword">if</span> maxDis<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">></span> maxDis<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">:</span> index <span class="token operator">=</span> k
      passed<span class="token punctuation">.</span>append<span class="token punctuation">(</span>index<span class="token punctuation">)</span>
      nopass<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>index<span class="token punctuation">)</span>
      <span class="token keyword">for</span> k <span class="token keyword">in</span> nopass<span class="token punctuation">:</span>
            <span class="token keyword">if</span> matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
                <span class="token keyword">if</span> maxDis<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">+</span> matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">></span> maxDis<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">:</span>
                  maxDis<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> maxDis<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">+</span> matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span>
                  path<span class="token punctuation">[</span>k<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> path<span class="token punctuation">[</span>index<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token punctuation">[</span>index<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;ve={}&#39;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>maxDis<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;path(1->9): {}&#39;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>path<span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> maxDis
    <span class="token comment"># ve=</span>
        <span class="token comment"># path(1->9): </span>
</code></pre></p>
代码实现与优化

在滑动窗口与二分查找算法中,代码的编写至关重要。对于程序员而言,掌握编写清晰、高效代码的能力是必备素养。针对滑动窗口,需特别注意边界条件的妥善处理及指针移动策略的合理运用;至于二分查找,必须保证每次搜索均能有效减少搜索区间。

<p><pre>    <code class="prism language-python"><span class="token keyword">def</span> <span class="token function">demo</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span><span class="token punctuation">:</span>
    passed <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment"># 默认从第0个节点开始,以邻接矩阵的第0行为准</span>
    nopass <span class="token operator">=</span> <span class="token punctuation">[</span>i <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">if</span> i <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">]</span>
    minDis <span class="token operator">=</span> matrix<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
    <span class="token comment"># 初始化起始路径</span>
    path <span class="token operator">=</span> <span class="token builtin">dict</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>minDis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
      <span class="token keyword">if</span> minDis <span class="token operator">!=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token string">&#39;inf&#39;</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            path<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
    <span class="token keyword">while</span> nopass<span class="token punctuation">:</span>
      <span class="token comment"># 取出nopass中在minDis中路径最小的节点</span>
      index <span class="token operator">=</span> nopass<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
      <span class="token keyword">for</span> i <span class="token keyword">in</span> nopass<span class="token punctuation">:</span>
            <span class="token keyword">if</span> minDis<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator"><</span> minDis<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">:</span> index <span class="token operator">=</span> i
      
      nopass<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>index<span class="token punctuation">)</span>
      passed<span class="token punctuation">.</span>append<span class="token punctuation">(</span>index<span class="token punctuation">)</span>
      <span class="token keyword">for</span> i <span class="token keyword">in</span> nopass<span class="token punctuation">:</span>
            <span class="token keyword">if</span> matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token string">&#39;inf&#39;</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token keyword">if</span> minDis<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">+</span> matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator"><</span> minDis<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">:</span>
                  minDis<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> minDis<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">+</span> matrix<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span>
                  path<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> path<span class="token punctuation">[</span>index<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token punctuation">[</span>index<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span>
      
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;minDis={}&#39;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>minDis<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;path(1->6): {}&#39;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>path<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> minDis
inf <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token string">&#39;inf&#39;</span><span class="token punctuation">)</span>
matrix <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> inf<span class="token punctuation">,</span> inf<span class="token punctuation">,</span> inf<span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span>inf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> inf<span class="token punctuation">,</span> inf<span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span>inf<span class="token punctuation">,</span> inf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> inf<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> inf<span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span>inf<span class="token punctuation">,</span> inf<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span>inf<span class="token punctuation">,</span> inf<span class="token punctuation">,</span> inf <span class="token punctuation">,</span>inf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span>inf<span class="token punctuation">,</span> inf<span class="token punctuation">,</span> inf<span class="token punctuation">,</span> inf <span class="token punctuation">,</span>inf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
keyPoint <span class="token operator">=</span> demo<span class="token punctuation">(</span>matrix<span class="token punctuation">)</span>
<span class="token comment"># minDis=</span>
<span class="token comment"># path(1->6): </span>
</code></pre></p>
代码的优化环节同样不容忽视。恰当运用数据结构与算法,可显著提升程序执行效率。以滑动窗口问题为例,运用双端队列可优化窗口内元素的管理;在二分查找过程中,通过预处理数组,可减少冗余的比较次数。

实际案例分析

为深入探究滑动窗口与二分查找技术的实践应用,我们可选取具体案例进行细致剖析。譬如,在股票交易平台中,常需运用滑动窗口技术对特定时段内的股价变动进行评估,并辅以二分查找法迅速锁定特定价位的相关交易数据。

这些案例表明,滑动窗口与二分查找算法不仅限于理论探讨,其在具体应用领域展现出显著的价值与重要性。

常见问题与解决方案

<p><pre>    <code class="prism language-python"><span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
      strInput <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> strInput<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&#39;/&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      res <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
      <span class="token keyword">while</span> a <span class="token operator">!=</span> <span class="token number">1</span> <span class="token keyword">and</span> b <span class="token operator">%</span> a <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
            q <span class="token operator">=</span> b <span class="token operator">//</span> a <span class="token operator">+</span> <span class="token number">1</span>
            mod <span class="token operator">=</span> b <span class="token operator">%</span> a
            res<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">&#39;1/&#39;</span><span class="token operator">+</span><span class="token builtin">str</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">)</span>
            a <span class="token operator">-=</span> mod
            b <span class="token operator">*=</span> q
      <span class="token keyword">if</span> a <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
            res<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">&#39;1/&#39;</span><span class="token operator">+</span><span class="token builtin">str</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token keyword">else</span><span class="token punctuation">:</span>
            res<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">&#39;1/&#39;</span><span class="token operator">+</span><span class="token builtin">str</span><span class="token punctuation">(</span>b<span class="token operator">//</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;+&#39;</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
      <span class="token keyword">break</span>
</code></pre></p>
在具体操作过程中,滑动窗口与二分搜索算法常遭遇诸多难题,诸如边界条件处理失当、搜索区间设定错误等问题。针对此类问题,我们必须透彻掌握算法的基本原理,并在编码阶段进行详尽的测试与调试。

在处理滑动窗口时,务必关注窗口尺寸的适时调整,以维护算法的准确性与运行效率;至于二分查找,关键在于每次操作均需准确减少搜索区间,防止发生无限循环。

<p><pre>    <code class="prism language-python"><span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
      strInput <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> strInput<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&#39;/&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      res <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
      <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> a <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
                res<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">&#39;1/&#39;</span><span class="token operator">+</span><span class="token builtin">str</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token keyword">break</span>
            <span class="token keyword">elif</span> b <span class="token operator">%</span> a <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
                res<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">&#39;1/&#39;</span><span class="token operator">+</span><span class="token builtin">str</span><span class="token punctuation">(</span>b<span class="token operator">//</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token keyword">break</span>
            <span class="token keyword">elif</span> a <span class="token operator">==</span> <span class="token number">3</span> <span class="token keyword">and</span> b <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
                res<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">&#39;1/&#39;</span><span class="token operator">+</span><span class="token builtin">str</span><span class="token punctuation">(</span>b<span class="token operator">//</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                res<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">&#39;1/&#39;</span><span class="token operator">+</span><span class="token builtin">str</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token keyword">break</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                q <span class="token operator">=</span> b <span class="token operator">//</span> a <span class="token operator">+</span> <span class="token number">1</span>
                a <span class="token operator">-=</span> b <span class="token operator">%</span> a
                b <span class="token operator">*=</span> q
                res<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">&#39;1/&#39;</span><span class="token operator">+</span><span class="token builtin">str</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;+&#39;</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
      <span class="token keyword">break</span>
</code></pre></p>
总结与展望

<p><pre>    <code class="prism language-python"><span class="token keyword">def</span> <span class="token function">convert</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> a <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
      <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;1/%d&#39;</span> <span class="token operator">%</span> b<span class="token punctuation">)</span>
    <span class="token keyword">elif</span> b <span class="token operator">%</span> a <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
      <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;1/%d&#39;</span> <span class="token operator">%</span> <span class="token punctuation">(</span>b<span class="token operator">//</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">elif</span> a <span class="token operator">==</span> <span class="token number">3</span> <span class="token keyword">and</span> b <span class="token operator">%</span> <span class="token number">2</span><span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
      <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;1/%d+1/%d&#39;</span> <span class="token operator">%</span> <span class="token punctuation">(</span>b<span class="token operator">//</span><span class="token number">2</span><span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
      q <span class="token operator">=</span> b <span class="token operator">//</span> a <span class="token operator">+</span> <span class="token number">1</span>
      <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;1/%d&#39;</span> <span class="token operator">%</span> q<span class="token punctuation">,</span> end<span class="token operator">=</span><span class="token string">&#39;+&#39;</span><span class="token punctuation">)</span>
      a <span class="token operator">-=</span> b <span class="token operator">%</span> a
      b <span class="token operator">*=</span> q
      convert<span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
      strInput <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> strInput<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&#39;/&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      convert<span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
      <span class="token keyword">break</span>
</code></pre></p>
滑动窗口与二分搜索构成处理数组问题的核心策略。对这两种方法进行深刻领悟与熟练运用,有助于我们在处理复杂数据结构时,实现更为自如与高效的应对。展望未来,伴随数据规模持续扩大与计算需求日益增长,这两种技术的战略地位将愈发显著。

针对编程实践,我们向公众提问:在处理复杂问题时,您是如何巧妙融合滑动窗口技术与二分查找算法的呢?热切期待您在评论区分享个人心得与观点,同时,请不要吝啬您的点赞与转发,以便让更多读者从中获益。

<p><pre>    <code class="prism language-python">nums <span class="token operator">=</span> <span class="token builtin">list</span><span class="token punctuation">(</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&#39; &#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
res <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>nums<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">:</span>
      <span class="token keyword">if</span> nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><=</span> nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">:</span>
            res <span class="token operator">+=</span> nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span>
out <span class="token operator">=</span> res <span class="token operator">%</span> <span class="token number">1000000007</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>out<span class="token punctuation">)</span>
</code></pre></p>
页: [1]
查看完整版本: 哔哩哔哩-万诺 coding 城市旅游规划笔试题解析,涵盖滑动窗口、二分查找等算法