<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[find max - algorithmus optimieren]]></title><description><![CDATA[<p>hi,</p>
<p>habe folgenden algo implementiert. dieser laueft in O(n * k) ?<br />
wie kann ich ihn optimieren?</p>
<pre><code>/*
Given an array and an integer k, find the maximum for each and every contiguous 
subarray of size k.

Examples:

Input :
arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}
k = 3
Output :
3 3 4 5 5 5 6
*/

#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;queue&gt;
#include &lt;stack&gt;
using namespace std;

class node {
public:
	int index;
	int value;

	node(int index_, int value_): index(index_), value(value_) {}
};

struct Comparator {
	bool operator()(const node &amp;lhs, const node &amp;rhs) {
		return lhs.value &lt; rhs.value;
	}
};

typedef priority_queue&lt;node, vector&lt;node&gt;, Comparator&gt; pq;

void removeIndex(pq &amp;maxHeap, int index) {
	stack&lt;node&gt; copy;

	while (!maxHeap.empty()) {
		node n = maxHeap.top();
		maxHeap.pop();

		if (n.index == index) {
			break;
		}
		else {
			copy.push(n);
		}
	}

	while (!copy.empty()) {
		 maxHeap.push(copy.top());	
		 copy.pop();
	}

}

vector&lt;int&gt; getMax(vector&lt;int&gt; &amp;vec, int k) {
	vector&lt;int&gt; out;
	pq maxHeap;

	for (int i = 0; i &lt; vec.size(); i++) {
		if (i &lt; k) {
			maxHeap.push(node(i, vec[i]));

			if (i == (k - 1)) {
				out.push_back(maxHeap.top().value);
			}
		}
		else {
			maxHeap.push(node(i, vec[i]));
			removeIndex(maxHeap, i - k);
			out.push_back(maxHeap.top().value);
		}			
	}

	return out;
}

int main() {
	vector&lt;int&gt; vec = {1,2,5,7,3,1,7,9,0};
    //k=2 [2,5,7,7,3,7,9,9]

    vector&lt;int&gt; out = getMax(vec, 2);

    for (auto e : out) {
    	cout &lt;&lt; e &lt;&lt; ' ';
    }

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/331041/find-max-algorithmus-optimieren</link><generator>RSS for Node</generator><lastBuildDate>Fri, 01 May 2026 21:01:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/331041.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 07 Feb 2015 12:48:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 12:48:28 GMT]]></title><description><![CDATA[<p>hi,</p>
<p>habe folgenden algo implementiert. dieser laueft in O(n * k) ?<br />
wie kann ich ihn optimieren?</p>
<pre><code>/*
Given an array and an integer k, find the maximum for each and every contiguous 
subarray of size k.

Examples:

Input :
arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}
k = 3
Output :
3 3 4 5 5 5 6
*/

#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;queue&gt;
#include &lt;stack&gt;
using namespace std;

class node {
public:
	int index;
	int value;

	node(int index_, int value_): index(index_), value(value_) {}
};

struct Comparator {
	bool operator()(const node &amp;lhs, const node &amp;rhs) {
		return lhs.value &lt; rhs.value;
	}
};

typedef priority_queue&lt;node, vector&lt;node&gt;, Comparator&gt; pq;

void removeIndex(pq &amp;maxHeap, int index) {
	stack&lt;node&gt; copy;

	while (!maxHeap.empty()) {
		node n = maxHeap.top();
		maxHeap.pop();

		if (n.index == index) {
			break;
		}
		else {
			copy.push(n);
		}
	}

	while (!copy.empty()) {
		 maxHeap.push(copy.top());	
		 copy.pop();
	}

}

vector&lt;int&gt; getMax(vector&lt;int&gt; &amp;vec, int k) {
	vector&lt;int&gt; out;
	pq maxHeap;

	for (int i = 0; i &lt; vec.size(); i++) {
		if (i &lt; k) {
			maxHeap.push(node(i, vec[i]));

			if (i == (k - 1)) {
				out.push_back(maxHeap.top().value);
			}
		}
		else {
			maxHeap.push(node(i, vec[i]));
			removeIndex(maxHeap, i - k);
			out.push_back(maxHeap.top().value);
		}			
	}

	return out;
}

int main() {
	vector&lt;int&gt; vec = {1,2,5,7,3,1,7,9,0};
    //k=2 [2,5,7,7,3,7,9,9]

    vector&lt;int&gt; out = getMax(vec, 2);

    for (auto e : out) {
    	cout &lt;&lt; e &lt;&lt; ' ';
    }

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2441655</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441655</guid><dc:creator><![CDATA[alen]]></dc:creator><pubDate>Sat, 07 Feb 2015 12:48:28 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 13:02:05 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;set&gt;

int main() {
  auto arr = std::vector&lt;int&gt;{1, 2, 3, 1, 4, 5, 2, 3, 6};
  auto k=size_t(3);

  std::cout &lt;&lt; &quot;Output:&quot;;

  auto active = std::multiset&lt;int, std::greater&lt;int&gt; &gt;();
  for (auto&amp; elem : arr) {
    active.insert(elem);
    if (active.size() &gt; k)
      active.erase(active.find(*(&amp;elem-k)));
    if (active.size() == k)
    std::cout &lt;&lt; ' ' &lt;&lt; *active.begin();
  }
  std::cout &lt;&lt; '\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2441657</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441657</guid><dc:creator><![CDATA[élève eleven]]></dc:creator><pubDate>Sat, 07 Feb 2015 13:02:05 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 13:08:34 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">auto k=size_t(3);
</code></pre>
<p>wirklich?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441660</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441660</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 07 Feb 2015 13:08:34 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 13:13:22 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">auto k=size_t(3);
</code></pre>
<p>wirklich?</p>
</blockquote>
<p>Ich nenne diesen Stil AAA: <a href="http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/" rel="nofollow">AAA</a> ad absurdum.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441661</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441661</guid><dc:creator><![CDATA[élève eleven]]></dc:creator><pubDate>Sat, 07 Feb 2015 13:13:22 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 13:50:23 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">auto k=size_t(3);
</code></pre>
<p>wirklich?</p>
</blockquote>
<p>Ich weiß! Ich dachte auch sofort an das fehlende <code>std::</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441668</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441668</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 07 Feb 2015 13:50:23 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 13:53:19 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">auto k=size_t(3);
</code></pre>
<p>wirklich?</p>
</blockquote>
<p>Kann doch mal passieren:</p>
<pre><code class="language-cpp">auto k=size_t{3};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2441669</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441669</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 07 Feb 2015 13:53:19 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 16:07:00 GMT]]></title><description><![CDATA[<p>welche laufzeit hat mein aktueller algo?</p>
<p>welche laufzeit hat dein algo welcher das multiset verwendet?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441698</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441698</guid><dc:creator><![CDATA[alen]]></dc:creator><pubDate>Sat, 07 Feb 2015 16:07:00 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 16:30:05 GMT]]></title><description><![CDATA[<p>alen schrieb:</p>
<blockquote>
<p>welche laufzeit hat mein aktueller algo?</p>
</blockquote>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mrow><mi mathvariant="script">O</mi></mrow><mo>(</mo><mi>n</mi><mi>k</mi><mi>log</mi><mi>k</mi><mo>)</mo></mrow><annotation encoding="application/x-tex">\mathcal{O}(nk\log k)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mord textstyle uncramped"><span class="mord mathcal" style="margin-right:0.02778em;">O</span></span><span class="mopen">(</span><span class="mord mathit">n</span><span class="mord mathit" style="margin-right:0.03148em;">k</span><span class="mop">lo<span style="margin-right:0.01389em;">g</span></span><span class="mord mathit" style="margin-right:0.03148em;">k</span><span class="mclose">)</span></span></span></span></p>
<p>alen schrieb:</p>
<blockquote>
<p>welche laufzeit hat dein algo welcher das multiset verwendet?</p>
</blockquote>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mrow><mi mathvariant="script">O</mi></mrow><mo>(</mo><mi>n</mi><mi>log</mi><mi>k</mi><mo>)</mo></mrow><annotation encoding="application/x-tex">\mathcal{O}(n\log k)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mord textstyle uncramped"><span class="mord mathcal" style="margin-right:0.02778em;">O</span></span><span class="mopen">(</span><span class="mord mathit">n</span><span class="mop">lo<span style="margin-right:0.01389em;">g</span></span><span class="mord mathit" style="margin-right:0.03148em;">k</span><span class="mclose">)</span></span></span></span></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441700</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441700</guid><dc:creator><![CDATA[élève eleven]]></dc:creator><pubDate>Sat, 07 Feb 2015 16:30:05 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 16:57:33 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstddef&gt;

int main() {
  int arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6, 11, 12, 13, 14, 15, 16, 15, 14, 13, 12, 11, 10, 9};
  constexpr std::size_t n = sizeof(arr)/sizeof(*arr);
  constexpr std::size_t k = 5;
  int result[n-k+1]={};

  for (auto&amp; elem : arr) {
    std::cout &lt;&lt; ' ' &lt;&lt; elem;
  }

  std::cout &lt;&lt; &quot;\nOutput:\n&quot;;

  std::size_t pos = 0, max_pos = 0;
// vorwärts
  for (; pos != k; ++pos) {
    if ( arr[pos] &gt;= arr[max_pos] )
        max_pos = pos;
  }
  for (; pos != n; ++pos) {
    result[pos-k] = arr[max_pos];
    if ( arr[pos] &gt;= arr[max_pos] || pos == max_pos + k )
      max_pos = pos;
  }
  result[pos-k] = arr[max_pos];
// rückwärts
  for (; pos != n - k; ) {
    --pos;
    if ( arr[pos] &gt;= arr[max_pos] )
        max_pos = pos;
  }
  for (; pos != 0; ) {
    result[pos] = std::max(result[pos],arr[max_pos]);
    --pos;
    if ( arr[pos] &gt;= arr[max_pos] || pos == max_pos - k )
      max_pos = pos;
  }
  result[pos] = std::max(result[pos],arr[max_pos]);

  for (auto&amp; elem : result) {
    std::cout &lt;&lt; ' ' &lt;&lt; elem;
  }
  std::cout &lt;&lt; '\n';
}
</code></pre>
<p>lineare Laufzeit. Schöner Aufschreiben überlasse ich anderen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441703</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441703</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 07 Feb 2015 16:57:33 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 17:05:36 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>:<br />
i optimized my function removeIndex, does it still run in O(k)?<br />
are there any tricks i could get the removeIndex to O(1) using a heap?</p>
<pre><code>void removeIndex(pq &amp;maxHeap, int index) {
	while (!maxHeap.empty()) {
		node n = maxHeap.top();

		if (n.index &gt; index) {
			break;
		} else {
			maxHeap.pop();
		}
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2441704</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441704</guid><dc:creator><![CDATA[alen]]></dc:creator><pubDate>Sat, 07 Feb 2015 17:05:36 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 17:21:37 GMT]]></title><description><![CDATA[<p>alen schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>:<br />
i optimized my function removeIndex, does it still run in O(k)?<br />
are there any tricks i could get the removeIndex to O(1) using a heap?</p>
<pre><code>void removeIndex(pq &amp;maxHeap, int index) {
	while (!maxHeap.empty()) {
		node n = maxHeap.top();
		
		if (n.index &gt; index) {
			break;
		} else {
			maxHeap.pop();
		}
	}
}
</code></pre>
</blockquote>
<p>looks like O(1), but push isn't. Just consider the worst case: elements sorted in descending order.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441706</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441706</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 07 Feb 2015 17:21:37 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 17:23:35 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>:<br />
why should it look like O(1) now?</p>
<p>i know push is log k</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441707</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441707</guid><dc:creator><![CDATA[alen]]></dc:creator><pubDate>Sat, 07 Feb 2015 17:23:35 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 17:26:50 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<pre><code class="language-cpp">auto k=size_t{3};
</code></pre>
</blockquote>
<pre><code class="language-cpp">auto k{ std::size_t{ 3 } };
</code></pre>
<p>gaah!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441708</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441708</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 07 Feb 2015 17:26:50 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 17:40:35 GMT]]></title><description><![CDATA[<p>alen schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>:<br />
why should it look like O(1) now?</p>
<p>i know push is log k</p>
</blockquote>
<p>armortized O(1) to be precise. Every element is pushed and poped exactly once. pop and top have constant complexity.</p>
<p>If you were to write</p>
<pre><code class="language-cpp">void removeIndex(pq &amp;maxHeap, int index) {
            maxHeap.pop();
}
</code></pre>
<p>the algorithm would be broken but its easy to see that the complexity wouldn't change. The loop and index-check do not change the total amount of pop, they only shift them around to another invocation of removeIndex.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441710</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441710</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 07 Feb 2015 17:40:35 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 18:04:41 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>: Dein Algorithmus hat einen Bug (Eingabe <code>{9,1,1,1,8,1,1,1,9}</code> ) und ich sehe auf die Schnelle nicht, wie man ihn reparieren könnte.</p>
<p>Daher hier meine O(n)-Version:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstddef&gt;
#include &lt;array&gt;

int main() {
  auto&amp;&amp; arr = std::remove_reference_t&lt;int[]&gt;{
    9,1,1,1,8,1,1,1,9,1,2,3,1,4,5,2,3,6,11,12,13,14,15,16,15,14,13,12,11,10,9};
  constexpr auto n = sizeof(arr)/sizeof(*arr);
  constexpr auto k = std::size_t(5); static_assert(k &lt; n, &quot;&quot;);
  auto forward = std::array&lt;int, n&gt;{};
  auto backward = std::array&lt;int, n&gt;{};

  for (auto i = std::size_t(0); i&lt;n; ++i)
    forward[i]  = i%k ? std::max(forward[i-1], arr[i]) : arr[i];
  backward[n-1] = arr[n-1];
  for (auto i = n-2; i --&gt; 0;)
    backward[i]  = i%k ? std::max(backward[i+1], arr[i]) : arr[i];

  std::cout &lt;&lt; &quot;Output:&quot;;
  for (auto i = std::size_t(0); i+k-1&lt;n; ++i)
    std::cout &lt;&lt; ' ' &lt;&lt; std::max(forward[i+k-1], backward[i]);
  std::cout &lt;&lt; '\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2441713</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441713</guid><dc:creator><![CDATA[élève eleven]]></dc:creator><pubDate>Sat, 07 Feb 2015 18:04:41 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 18:44:09 GMT]]></title><description><![CDATA[<p>élève eleven schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>: Dein Algorithmus hat einen Bug (Eingabe <code>{9,1,1,1,8,1,1,1,9}</code> ) und ich sehe auf die Schnelle nicht, wie man ihn reparieren könnte.</p>
</blockquote>
<p>Doofe off-by-1 Fehler</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstddef&gt;

int main() {
  int arr[] = {9,1,1,1,8,1,1,1,9,9,1,1,1,8,1,1,1,9,1,2,3,1,4,5,2,3,6,11,12,13,14,15,16,15,14,13,12,11,10,9};
  constexpr std::size_t n = sizeof(arr)/sizeof(*arr);
  constexpr std::size_t k = 5;
  int result[n-k+1]={};

  for (auto&amp; elem : arr) {
    std::cout &lt;&lt; ' ' &lt;&lt; elem;
  }

  std::cout &lt;&lt; &quot;\nOutput:\n&quot;;

  std::size_t pos = 0, max_pos = 0;
// forwärts
  for (; pos != k; ++pos) {
    if ( arr[pos] &gt;= arr[max_pos] )
        max_pos = pos;
  }
  for (; pos != n; ++pos) {
    result[pos-k] = arr[max_pos];
    if ( pos == max_pos + k )
      max_pos = pos - 1; // ++max_pos funktioniert genauso, max_pos muss nur in (pos-k,pos) liegen
    if ( arr[pos] &gt;= arr[max_pos] )
      max_pos = pos;
  }
  result[pos-k] = arr[max_pos];
// rückwärts
  for (; pos != n - k; ) {
    --pos;
    if ( arr[pos] &gt;= arr[max_pos] )
        max_pos = pos;
  }
  for (; pos != 0; ) {
    result[pos] = std::max(result[pos],arr[max_pos]);
    --pos;
    if ( pos == max_pos - k )
      max_pos = pos + 1;
    if ( arr[pos] &gt;= arr[max_pos] )
      max_pos = pos;
  }
  result[pos] = std::max(result[pos],arr[max_pos]);

  for (auto&amp; elem : result) {
    std::cout &lt;&lt; ' ' &lt;&lt; elem;
  }
  std::cout &lt;&lt; '\n';
}
</code></pre>
<p>élève eleven schrieb:</p>
<blockquote>
<p>Daher hier meine O(n)-Version:</p>
<pre><code class="language-cpp">...
</code></pre>
</blockquote>
<p>Clever. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441716</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441716</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 07 Feb 2015 18:44:09 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 19:49:10 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>élève eleven schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>: Dein Algorithmus hat einen Bug (Eingabe <code>{9,1,1,1,8,1,1,1,9}</code> ) und ich sehe auf die Schnelle nicht, wie man ihn reparieren könnte.</p>
</blockquote>
<p>Doofe off-by-1 Fehler</p>
</blockquote>
<p>Immer noch nicht ganz ( <code>{9,1,1,8,1,1,9}</code> ).</p>
<p>camper schrieb:</p>
<blockquote>
<p>élève eleven schrieb:</p>
<blockquote>
<p>Daher hier meine O(n)-Version:</p>
<pre><code class="language-cpp">...
</code></pre>
</blockquote>
<p>Clever. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
</blockquote>
<p>Ist weniger optimiert wie deines, dafür etwas weniger anfällig auf Off-by-one-Fehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441721</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441721</guid><dc:creator><![CDATA[élève eleven]]></dc:creator><pubDate>Sat, 07 Feb 2015 19:49:10 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 20:05:52 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>:</p>
<p>wie waers mit folgenden algo, der muesste auch in O (n) laufen.</p>
<pre><code>vector&lt;int&gt; getMax(vector&lt;int&gt; &amp;vec, int k) {
	vector&lt;int&gt; out;
	deque&lt;int&gt; d; // stores the indices, sorted from max value to min value within the window k

	for (int i = 0; i &lt; vec.size(); i++) {
		if (i &lt; k) {
			while (!d.empty() &amp;&amp; vec[i] &gt;= vec[d.back()]) {
				d.pop_back();
			}

			d.push_back(i);
		} 
		else {
			out.push_back(vec[d.front()]);

			while (!d.empty() &amp;&amp; vec[i] &gt;= vec[d.back()]) {
				d.pop_back();
			}

			while (!d.empty() &amp;&amp; d.front() &lt;= (i - k)) {
				d.pop_front();
			}

			d.push_back(i);
		}
	}

	out.push_back(vec[d.front()]);

	return out;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2441725</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441725</guid><dc:creator><![CDATA[alen]]></dc:creator><pubDate>Sat, 07 Feb 2015 20:05:52 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 21:41:58 GMT]]></title><description><![CDATA[<p>Neuer Versuch. Wobei das irgendwie falsch aussieht.</p>
<p>Edit: ist es auch..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441730</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441730</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 07 Feb 2015 21:41:58 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 21:51:29 GMT]]></title><description><![CDATA[<p>Worum geht's hier eigentlich? <code>nth_element</code> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441731</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441731</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 07 Feb 2015 21:51:29 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 22:22:08 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>: warum falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441733</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441733</guid><dc:creator><![CDATA[alen]]></dc:creator><pubDate>Sat, 07 Feb 2015 22:22:08 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 22:35:05 GMT]]></title><description><![CDATA[<p>Ich glaube mein Algorithmus ist nicht zu retten.</p>
<p>alen schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>:</p>
<p>wie waers mit folgenden algo, der muesste auch in O (n) laufen.</p>
<pre><code>...
</code></pre>
</blockquote>
<p>Das kommt dem nahe, was man wahrscheinlich tun würde, wenn man die Aufgabe mechanisch selbst zu lösen hätte.<br />
Ist wahrscheinlich auch die effizienteste Methode (für d könnte man einen Ringpuffer mit max k Elementen verwenden).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441734</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441734</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 07 Feb 2015 22:35:05 GMT</pubDate></item><item><title><![CDATA[Reply to find max - algorithmus optimieren on Sat, 07 Feb 2015 22:35:55 GMT]]></title><description><![CDATA[<p>alen schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>: warum falsch?</p>
</blockquote>
<p>Das bezog sich auf den Code, den ich ursprünglich in dem Post gezeigt hatte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441735</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441735</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 07 Feb 2015 22:35:55 GMT</pubDate></item></channel></rss>