<?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 longest increasing subsequence]]></title><description><![CDATA[<p>Hi,</p>
<p>ich moechte die laengste increasing subsequence in einem array finden. Eine Increasing Subsequence ist eine Sequenz in der die Zahlen strikt groesser werden.</p>
<p>mit dynamic programming liegt die laufzeitkomplexiteat des algos bei O(n^2)...kann man/wie kann man den algo noch schneller machen?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;unordered_map&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
using namespace std;

vector&lt;int&gt; find_long_inc_subseq(vector&lt;int&gt; &amp;seq) {
	unordered_map&lt;int, vector&lt;int&gt;&gt; ht;

	for(int i = 0; i &lt; seq.size(); i++) {
		vector&lt;int&gt; subseq;

		if(i &gt; 0) {
			int max_index = 0;
			int max_size = 0;
			int index_start = i - 1;
			bool smaller_sub_seq_found = false;

			for(int j = 0; j &lt; ht.size(); j++) {
				auto it = ht.find(index_start);

				if(it-&gt;second.back() &lt; seq[i] &amp;&amp; it-&gt;second.size() &gt; max_size) {
					smaller_sub_seq_found = true;
					max_size = it-&gt;second.size();
					max_index = index_start;
				}

				index_start--;
			}

			if(smaller_sub_seq_found) {
				auto it = ht.find(max_index);

				for(auto it2 = it-&gt;second.begin(); it2 != it-&gt;second.end(); it2++) {
					subseq.push_back(*it2);
				}
			}
		}

		subseq.push_back(seq[i]);
		ht.insert(make_pair(i, subseq));
	}

	return ht.find(seq.size()-1)-&gt;second;
}

int main() {
	// your code goes here

	vector&lt;int&gt; seq =  {10, 22, 9, 33, 21, 50, 41, 60, 80};
	vector&lt;int&gt; long_sub_seq = find_long_inc_subseq(seq);

	for_each(long_sub_seq.begin(), long_sub_seq.end(), [](int val) { cout &lt;&lt; val &lt;&lt; ' '; });
	cout &lt;&lt; '\n';

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/322150/find-longest-increasing-subsequence</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Jul 2026 08:07:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/322150.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 09 Dec 2013 05:20:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to find longest increasing subsequence on Mon, 09 Dec 2013 05:20:16 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich moechte die laengste increasing subsequence in einem array finden. Eine Increasing Subsequence ist eine Sequenz in der die Zahlen strikt groesser werden.</p>
<p>mit dynamic programming liegt die laufzeitkomplexiteat des algos bei O(n^2)...kann man/wie kann man den algo noch schneller machen?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;unordered_map&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
using namespace std;

vector&lt;int&gt; find_long_inc_subseq(vector&lt;int&gt; &amp;seq) {
	unordered_map&lt;int, vector&lt;int&gt;&gt; ht;

	for(int i = 0; i &lt; seq.size(); i++) {
		vector&lt;int&gt; subseq;

		if(i &gt; 0) {
			int max_index = 0;
			int max_size = 0;
			int index_start = i - 1;
			bool smaller_sub_seq_found = false;

			for(int j = 0; j &lt; ht.size(); j++) {
				auto it = ht.find(index_start);

				if(it-&gt;second.back() &lt; seq[i] &amp;&amp; it-&gt;second.size() &gt; max_size) {
					smaller_sub_seq_found = true;
					max_size = it-&gt;second.size();
					max_index = index_start;
				}

				index_start--;
			}

			if(smaller_sub_seq_found) {
				auto it = ht.find(max_index);

				for(auto it2 = it-&gt;second.begin(); it2 != it-&gt;second.end(); it2++) {
					subseq.push_back(*it2);
				}
			}
		}

		subseq.push_back(seq[i]);
		ht.insert(make_pair(i, subseq));
	}

	return ht.find(seq.size()-1)-&gt;second;
}

int main() {
	// your code goes here

	vector&lt;int&gt; seq =  {10, 22, 9, 33, 21, 50, 41, 60, 80};
	vector&lt;int&gt; long_sub_seq = find_long_inc_subseq(seq);

	for_each(long_sub_seq.begin(), long_sub_seq.end(), [](int val) { cout &lt;&lt; val &lt;&lt; ' '; });
	cout &lt;&lt; '\n';

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2370656</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2370656</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Mon, 09 Dec 2013 05:20:16 GMT</pubDate></item><item><title><![CDATA[Reply to find longest increasing subsequence on Mon, 09 Dec 2013 07:52:56 GMT]]></title><description><![CDATA[<p>das wäre in deinem beispiel 41-60-80 ?</p>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2370658</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2370658</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Mon, 09 Dec 2013 07:52:56 GMT</pubDate></item><item><title><![CDATA[Reply to find longest increasing subsequence on Mon, 09 Dec 2013 08:18:27 GMT]]></title><description><![CDATA[<p>Meep Meep schrieb:</p>
<blockquote>
<p>das wäre in deinem beispiel 41-60-80 ?</p>
</blockquote>
<p>Der Code spuckt folgendes Ergebnis aus:<br />
10 22 33 41 60 80</p>
<p>Das ist aber nicht eindeutig. Könnte genauso gut das hier liefern:<br />
10 22 33 50 60 80</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2370663</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2370663</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 09 Dec 2013 08:18:27 GMT</pubDate></item><item><title><![CDATA[Reply to find longest increasing subsequence on Mon, 09 Dec 2013 08:20:03 GMT]]></title><description><![CDATA[<p>Meep: Nee, die Elemente müssen nicht angrenzend in der Sequenz sein.<br />
OP: Warum googlest Du nicht einfach? Mannmannmann...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2370664</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2370664</guid><dc:creator><![CDATA[decimad]]></dc:creator><pubDate>Mon, 09 Dec 2013 08:20:03 GMT</pubDate></item><item><title><![CDATA[Reply to find longest increasing subsequence on Mon, 09 Dec 2013 18:59:41 GMT]]></title><description><![CDATA[<p>habe ein problem behoben:</p>
<pre><code>vector&lt;int&gt; find_long_inc_subseq(vector&lt;int&gt; &amp;seq) {
	unordered_map&lt;int, vector&lt;int&gt;&gt; ht;

	for(int i = 0; i &lt; seq.size(); i++) {
		vector&lt;int&gt; subseq;

		if(i &gt; 0) {
			int max_index = 0;
			int max_size = 0;
			int index_start = i - 1;
			bool smaller_sub_seq_found = false;

			for(int j = 0; j &lt; ht.size(); j++) {
				auto it = ht.find(index_start);

				if(it-&gt;second.back() &lt; seq[i] &amp;&amp; it-&gt;second.size() &gt; max_size) {
					smaller_sub_seq_found = true;
					max_size = it-&gt;second.size();
					max_index = index_start;
				}

				index_start--;
			}

			if(smaller_sub_seq_found) {
				auto it = ht.find(max_index);

				for(auto it2 = it-&gt;second.begin(); it2 != it-&gt;second.end(); it2++) {
					subseq.push_back(*it2);
				}
			}
		}

		subseq.push_back(seq[i]);
		ht.insert(make_pair(i, subseq));
	}

	return ht.find(seq.size()-1)-&gt;second;
}
</code></pre>
<p>mein algo gibt nur eine longest increasing subsequence aus...<br />
10 22 33 50 60 80...waere auch ein gueltiges ergebnis</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2370824</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2370824</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Mon, 09 Dec 2013 18:59:41 GMT</pubDate></item><item><title><![CDATA[Reply to find longest increasing subsequence on Mon, 09 Dec 2013 19:01:45 GMT]]></title><description><![CDATA[<p>sorry, habe vergessen den ganzen code zu kopieren:</p>
<pre><code>vector&lt;int&gt; find_long_inc_subseq(vector&lt;int&gt; &amp;seq) {
	unordered_map&lt;int, vector&lt;int&gt;&gt; ht;

	for(int i = 0; i &lt; seq.size(); i++) {
		vector&lt;int&gt; subseq;

		if(i &gt; 0) {
			int max_index = 0;
			int max_size = 0;
			int index_start = i - 1;
			bool smaller_sub_seq_found = false;

			for(int j = 0; j &lt; ht.size(); j++) {
				auto it = ht.find(index_start);

				if(it-&gt;second.back() &lt; seq[i] &amp;&amp; it-&gt;second.size() &gt; max_size) {
					smaller_sub_seq_found = true;
					max_size = it-&gt;second.size();
					max_index = index_start;
				}

				index_start--;
			}

			if(smaller_sub_seq_found) {
				auto it = ht.find(max_index);
				for(auto it2 = it-&gt;second.begin(); it2 != it-&gt;second.end(); it2++) {
					subseq.push_back(*it2);
				}
			}
		}

		subseq.push_back(seq[i]);
		ht.insert(make_pair(i, subseq));
	}

	int max_index = 0;
	int max_size = 0;
	for(int i = 0; i &lt; ht.size(); i++) {
		auto it = ht.find(i);

		if(it-&gt;second.size() &gt; max_size) {
			max_size = it-&gt;second.size();
			max_index = i;
		}
	}
	return ht.find(max_index)-&gt;second;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2370827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2370827</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Mon, 09 Dec 2013 19:01:45 GMT</pubDate></item></channel></rss>