<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>remove Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/remove/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/remove/</link>
	<description></description>
	<lastBuildDate>Tue, 29 Mar 2022 03:05:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>remove Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/remove/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2216. Minimum Deletions to Make Array Beautiful</title>
		<link>https://zxi.mytechroad.com/blog/two-pointers/leetcode-2216-minimum-deletions-to-make-array-beautiful/</link>
					<comments>https://zxi.mytechroad.com/blog/two-pointers/leetcode-2216-minimum-deletions-to-make-array-beautiful/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 29 Mar 2022 03:02:37 +0000</pubDate>
				<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9587</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;integer array&#160;nums. The array&#160;nums&#160;is&#160;beautiful&#160;if: nums.length&#160;is even. nums[i] != nums[i + 1]&#160;for all&#160;i % 2 == 0. Note that an empty array is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-2216-minimum-deletions-to-make-array-beautiful/">花花酱 LeetCode 2216. Minimum Deletions to Make Array Beautiful</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>. The array&nbsp;<code>nums</code>&nbsp;is&nbsp;<strong>beautiful</strong>&nbsp;if:</p>



<ul><li><code>nums.length</code>&nbsp;is even.</li><li><code>nums[i] != nums[i + 1]</code>&nbsp;for all&nbsp;<code>i % 2 == 0</code>.</li></ul>



<p>Note that an empty array is considered beautiful.</p>



<p>You can delete any number of elements from&nbsp;<code>nums</code>. When you delete an element, all the elements to the right of the deleted element will be&nbsp;<strong>shifted one unit to the left</strong>&nbsp;to fill the gap created and all the elements to the left of the deleted element will remain&nbsp;<strong>unchanged</strong>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;number of elements to delete from&nbsp;</em><code>nums</code><em>&nbsp;to make it&nbsp;</em><em>beautiful.</em></p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1,2,3,5]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can delete either <code>nums[0]</code> or <code>nums[1]</code> to make <code>nums</code> = [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make <code>nums</code> beautiful.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1,2,2,3,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can delete <code>nums[0]</code> and <code>nums[5]</code> to make nums = [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li></ul>



<h2><strong>Solution: Greedy + Two Pointers</strong></h2>



<p>If two consecutive numbers are the same, we must remove one. We don&#8217;t need to actually remove elements from array, just need to track how many elements have been removed so far.</p>



<p>i is the position in the original array, ans is the number of elements been removed. i &#8211; ans is the position in the updated array.</p>



<p>ans += nums[i &#8211; ans] == nums[i &#8211; ans + 1]</p>



<p>Remove the last element (just increase answer by 1) if the length of the new array is odd.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minDeletion(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    int ans = 0;
    for (int i = 0; i - ans + 1 &lt; n; i += 2)
      ans += nums[i - ans] == nums[i - ans + 1];
    ans += (n - ans) &amp; 1;
    return ans;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-2216-minimum-deletions-to-make-array-beautiful/">花花酱 LeetCode 2216. Minimum Deletions to Make Array Beautiful</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/two-pointers/leetcode-2216-minimum-deletions-to-make-array-beautiful/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 27. Remove Element</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-27-remove-element/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-27-remove-element/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 02 Oct 2018 19:32:20 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[in place]]></category>
		<category><![CDATA[remove]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4135</guid>

					<description><![CDATA[<p>Problem Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-27-remove-element/">花花酱 LeetCode 27. Remove Element</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given an array <em>nums</em> and a value <em>val</em>, remove all instances of that value <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noopener"><strong>in-place</strong></a> and return the new length.</p>
<p>Do not allocate extra space for another array, you must do this by <strong>modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noopener">in-place</a></strong> with O(1) extra memory.</p>
<p>The order of elements can be changed. It doesn&#8217;t matter what you leave beyond the new length.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false">Given <em>nums</em> = <strong>[3,2,2,3]</strong>, <em>val</em> = <strong>3</strong>,

Your function should return length = <strong>2</strong>, with the first two elements of <em>nums</em> being <strong>2</strong>.

It doesn't matter what you leave beyond the returned length.
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false">Given <em>nums</em> = <strong>[0,1,2,2,3,0,4,2]</strong>, <em>val</em> = <strong>2</strong>,

Your function should return length = <strong><code>5</code></strong>, with the first five elements of <em><code>nums</code></em> containing <strong><code>0</code></strong>, <strong><code>1</code></strong>, <strong><code>3</code></strong>, <strong><code>0</code></strong>, and <strong>4</strong>. Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length.</pre>
<p><strong>Clarification:</strong></p>
<p>Confused why the returned value is an integer but your answer is an array?</p>
<p>Note that the input array is passed in by <strong>reference</strong>, which means modification to the input array will be known to the caller as well.</p>
<p>Internally you can think of this:</p>
<pre class="crayon:false">// <strong>nums</strong> is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);

// any modification to <strong>nums</strong> in your function would be known by the caller.
// using the length returned by your function, it prints the first <strong>len</strong> elements.
for (int i = 0; i &lt; len; i++) {
    print(nums[i]);
}</pre>
<h1><strong>Solution:</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int removeElement(vector&lt;int&gt;&amp; nums, int val) {
    int len = 0;
    for (int num : nums)
      if (num != val) nums[len++] = num;
    return len;
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">class Solution:
  def removeElement(self, nums, val):
    l = 0
    for num in nums:
      if num != val:
        nums[l], l = num, l + 1        
    return l</pre><p></div></div></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/">花花酱 LeetCode 26. Remove Duplicates from Sorted Array</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-27-remove-element/">花花酱 LeetCode 27. Remove Element</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-27-remove-element/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 26. Remove Duplicates from Sorted Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 02 Oct 2018 16:33:53 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[deduplication]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4130</guid>

					<description><![CDATA[<p>Problem Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/">花花酱 LeetCode 26. Remove Duplicates from Sorted Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given a sorted array <em>nums</em>, remove the duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noopener"><strong>in-place</strong></a> such that each element appear only <em>once</em> and return the new length.</p>
<p>Do not allocate extra space for another array, you must do this by <strong>modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noopener">in-place</a></strong> with O(1) extra memory.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false">Given <em>nums</em> = <strong>[1,1,2]</strong>,

Your function should return length = <strong><code>2</code></strong>, with the first two elements of <em><code>nums</code></em> being <strong><code>1</code></strong> and <strong><code>2</code></strong> respectively. It doesn't matter what you leave beyond the returned length.</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false">Given <em>nums</em> = <strong>[0,0,1,1,1,2,2,3,3,4]</strong>,

Your function should return length = <strong><code>5</code></strong>, with the first five elements of <em><code>nums</code></em> being modified to <strong><code>0</code></strong>, <strong><code>1</code></strong>, <strong><code>2</code></strong>, <strong><code>3</code></strong>, and <strong><code>4</code></strong> respectively. It doesn't matter what values are set beyond the returned length.</pre>
<p><strong>Clarification:</strong></p>
<p>Confused why the returned value is an integer but your answer is an array?</p>
<p>Note that the input array is passed in by <strong>reference</strong>, which means modification to the input array will be known to the caller as well.</p>
<p>Internally you can think of this:</p>
<pre class="crayon:false">// <strong>nums</strong> is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);

// any modification to <strong>nums</strong> in your function would be known by the caller.
// using the length returned by your function, it prints the first <strong>len</strong> elements.
for (int i = 0; i &lt; len; i++) {
    print(nums[i]);
}</pre>
<h1>Solution:</h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int removeDuplicates(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    if (n &lt;= 1) return n;        
    int ans = 0;
    int i = 0;
    while (i &lt; n) {
      nums[ans++] = nums[i];
      int j = i + 1;
      while (j &lt; n &amp;&amp; nums[j] == nums[j - 1]) ++j;
      i = j;
    }
    return ans;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/">花花酱 LeetCode 26. Remove Duplicates from Sorted Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 203. Remove Linked List Elements</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-203-remove-linked-list-elements/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-203-remove-linked-list-elements/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 15 Mar 2018 16:07:02 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[remove]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2092</guid>

					<description><![CDATA[<p>题目大意：移除单向链表中所有值等于val的节点。 Problem: https://leetcode.com/problems/remove-linked-list-elements/description/ Remove all elements from a linked list of integers that have value val. Example Given: 1 &#8211;&#62; 2 &#8211;&#62; 6 &#8211;&#62; 3 &#8211;&#62; 4&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-203-remove-linked-list-elements/">花花酱 LeetCode 203. Remove Linked List Elements</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>题目大意：移除单向链表中所有值等于val的节点。</p>
<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/remove-linked-list-elements/description/">https://leetcode.com/problems/remove-linked-list-elements/description/</a></p>
<p>Remove all elements from a linked list of integers that have value <b><i>val</i></b>.</p>
<p><b>Example</b><br />
<i><b>Given:</b></i> 1 &#8211;&gt; 2 &#8211;&gt; 6 &#8211;&gt; 3 &#8211;&gt; 4 &#8211;&gt; 5 &#8211;&gt; 6, <b><i>val</i></b> = 6<br />
<i><b>Return:</b></i> 1 &#8211;&gt; 2 &#8211;&gt; 3 &#8211;&gt; 4 &#8211;&gt; 5</p>
<p><strong>Idea:</strong></p>
<p>Use a dummy head</p>
<p><strong>Solution:</strong></p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 30 ms
class Solution {
public:
  ListNode* removeElements(ListNode* head, int val) {
    ListNode dummy{0};
    dummy.next = head;
    ListNode* curr = &amp;dummy;
    while (curr) {
      ListNode* next = curr-&gt;next;
      while (next &amp;&amp; next-&gt;val == val) next = next-&gt;next;
      curr-&gt;next = next;
      curr = next;
    }
    return dummy.next;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-203-remove-linked-list-elements/">花花酱 LeetCode 203. Remove Linked List Elements</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/list/leetcode-203-remove-linked-list-elements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
