tag:blogger.com,1999:blog-42288598414530489382024-03-18T18:39:16.973+08:00Coding Interview QuestionsCoderCareer: Discussing Coding Interview Questions from Google, Amazon, Facebook, Microsoft, etcHarry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.comBlogger60125tag:blogger.com,1999:blog-4228859841453048938.post-50493059579207970792016-04-04T01:01:00.000+08:002016-09-25T00:29:51.161+08:00No. 59 - Duplications in Arrays<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span style="font-weight: bold;"><u>Questions</u>:</span> All numbers in an array with length <span style="font-style: italic;">n</span>+1 are in range from 1 to <span style="font-style: italic;">n</span>, so there is at least one duplication in the
array. How to find any a duplication? Please don't modify the input array.</div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<u><b>Analysis</b>:</u> The simple solution is to utilize
hash tables. When scanning the array, array elements are inserted into the hash
table one by one. In this way, it's easy to find a duplication with the hash
table, which costs O(<span style="font-style: italic;">n</span>) space.</div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
Let's try not to
employ extra space. Why there are duplications in the array? If there are no
duplications, the count of numbers ranging from 1 to <span style="font-style: italic;">n</span> is <span style="font-style: italic;">n</span>. Since the array
contains more than <span style="font-style: italic;">n</span> numbers, there
should be duplications. It looks like it's important to count numbers in
ranges.</div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
Let's divide numbers
from 1 to <span style="font-style: italic;">n</span> into two ranges, split with
the number in the middle (denoted as <span style="font-style: italic;">m</span>),
and then count the numbers of the two subranges. If the count of numbers from 1
to <span style="font-style: italic;">m</span> is greater than <span style="font-style: italic;">m</span>, the duplication is in the range from 1 to <span style="font-style: italic;">m</span>. Otherwise, there should be at least one
duplication in the range from <span style="font-style: italic;">m</span>+1 to <span style="font-style: italic;">n</span>. And then we continue the recursive process
until we find the duplication.</div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
The Java code is
listed below:</div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin: 0in;">
<span style="color: #0b5394;"><span style="color: #00b0f0;">static</span> <span style="color: #00b0f0;">int</span></span>
countRange(<span style="color: #0b5394;">int</span>[] numbers, <span style="color: #0b5394;">int</span> start, <span style="color: #0b5394;">int</span>
end)</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin: 0in;">
{</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .375in; margin: 0in;">
<span style="color: #00b0f0;"> </span><span style="color: #0b5394;">int</span> count = 0;</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .375in; margin: 0in;">
<span style="color: #00b0f0;"><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394;">for</span></span>(<span style="color: #0b5394;">int</span>
i = 0; i < numbers.length; i++)</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .75in; margin: 0in;">
<span style="color: #00b0f0;"><span style="font-size: 13.3333px;"> </span><span style="font-size: 13.3333px;"> </span></span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394; font-size: 10pt;">if</span><span style="font-size: 10pt;">(numbers[i] >= start &&
numbers[i] <= end)</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: 1.125in; margin: 0in;">
<span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="font-size: 10pt;">++count;</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .375in; margin: 0in;">
<span style="color: #00b0f0;"><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394;">return</span></span> count;</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin: 0in;">
}</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin: 0in;">
<br /></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin: 0in;">
<span style="color: #0b5394;"><span style="color: #00b0f0;">static</span> <span style="color: #00b0f0;">int</span></span>
getDuplication(<span style="color: #0b5394;">int</span>[] numbers)</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin: 0in;">
{</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .375in; margin: 0in;">
<span style="color: #00b0f0;"><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394;">int</span></span><span style="color: #00b0f0;"> </span>start = 1;</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .375in; margin: 0in;">
<span style="color: #00b0f0;"><span style="font-size: 13.3333px;"> </span><span style="font-size: 13.3333px;"> </span></span><span style="color: #0b5394; font-size: 10pt;">int</span><span style="font-size: 10pt;"> end = numbers.length;</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .375in; margin: 0in;">
<span style="color: #00b0f0;"><span style="font-size: 13.3333px;"> </span><span style="font-size: 13.3333px;"> </span></span><span style="color: #0b5394; font-size: 10pt;">while</span><span style="font-size: 10pt;">(end >= start)</span> </div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .375in; margin: 0in;">
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .75in; margin: 0in;">
<span style="color: #00b0f0;"><span style="font-size: 13.3333px;"> </span><span style="font-size: 13.3333px;"> </span></span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394; font-size: 10pt;">int</span><span style="font-size: 10pt;"> middle = ((end - start) >>
1) + start;</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .75in; margin: 0in;">
<span style="color: #00b0f0;"><span style="font-size: 13.3333px;"> </span><span style="font-size: 13.3333px;"> </span></span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394; font-size: 10pt;">int</span><span style="font-size: 10pt;"> count = countRange(numbers,
start, middle);</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .75in; margin: 0in;">
<span style="color: #00b0f0;"><span style="font-size: 13.3333px;"> </span><span style="font-size: 13.3333px;"> </span></span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394; font-size: 10pt;">if</span><span style="font-size: 10pt;">(end == start) {</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: 1.125in; margin: 0in;">
<span style="color: #00b0f0;"><span style="font-size: 13.3333px;"> </span><span style="font-size: 13.3333px;"> </span></span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394; font-size: 10pt;">if</span><span style="font-size: 10pt;">(count > 1)</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: 1.5in; margin: 0in;">
<span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="font-size: 10pt;">return start;</span></div>
<div style="font-family: 'courier new'; font-size: 10pt; margin: 0in;">
<span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394; font-size: 10pt;">else</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: 1.5in; margin: 0in;">
<span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="font-size: 10pt;">break;</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .75in; margin: 0in;">
<span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span>}</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .75in; margin: 0in;">
<br /></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .75in; margin: 0in;">
<span style="color: #00b0f0;"><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394;">if</span></span>(count > (middle - start + 1))</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: 1.125in; margin: 0in;">
<span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="font-size: 10pt;">end = middle;</span></div>
<div style="font-family: 'courier new'; font-size: 10pt; margin: 0in;">
<span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394;">else</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: 1.125in; margin: 0in;">
<span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="font-size: 10pt;">start = middle + 1;</span></div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .375in; margin: 0in;">
<span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span>}</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin-left: .375in; margin: 0in;">
<span style="color: #00b0f0;"><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #00b0f0; font-size: 13.3333px;"> </span><span style="color: #0b5394;">return</span></span><span style="color: #00b0f0;"> </span>-1;</div>
<div style="font-family: "Courier New"; font-size: 10.0pt; margin: 0in;">
}</div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
The code with unit
tests is shared at <a href="http://ideone.com/lhV22m">http://ideone.com/lhV22m</a>.</div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span lang="en-US" style="color: #222222;">More coding interview questions are discussed
in my book< Coding Interviews: Questions, Analysis & Solutions>. You
may find the details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span lang="zh-CN">Amazon.com</span></a><span lang="en-US" style="color: #222222;">, or </span><a href="http://www.apress.com/9781430247616"><span lang="zh-CN">Apress</span></a><span lang="en-US" style="color: #222222;">. </span></div>
<br />
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span lang="en-US" style="color: #222222;">The author Harry He owns all the rights of
this post. If you are going to use part of or the whole of this article in your
blog or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/"><span lang="zh-CN">http://codercareer.blogspot.com/</span></a><span lang="en-US" style="color: #222222;">. If you are going to use it in your books,
please contact the author via zhedahht@gmail.com . Thanks.</span></div>
</div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com236tag:blogger.com,1999:blog-4228859841453048938.post-62316120716448022512015-09-23T13:26:00.001+08:002015-09-25T14:30:42.957+08:00No. 58 - Search in Adjacent Numbers<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
<span style="font-weight: bold; text-decoration: underline;">Question</span>: Given an
array where two neighboring elements are adjacent (in absolute difference 1),
can you write an algorithm to search an element in the array and return its
position? If the element appears multiple times, please return the first
occurrence.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
For example, if
given the array {4, 5, 6, 5, 6, 7, 8, 9, 10, 9} and an element 9, the element
appears twice in the array, and the first occurrence is at position 7.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
<span style="font-weight: bold; text-decoration: underline;">Analysis</span>: The most
simple and straightforward solution is to traverse the array and compare
elements one by one. This strategy works for every array, and it does not
utilize the property of the array where two neighboring elements are in
absolute difference 1.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
Let's try to search
the first 9 from the array {4, 5, 6, 5, 6, 7, 8, 9, 10, 9}. Firstly we are at
position 0 where the element 4 is. The difference between 9 and 4 is 5, so we
move to the position 5. Why? Because the absolute difference between two neighboring
elements is 1. If the numbers in the array is increasingly sorted, the element
at position 5 is 9. If some elements decrease, 9 should sit on the right of
position 5. Therefore, 5 is the leftmost possible position of the element 9.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
It's 7 at position
5. The difference between 7 and 9 is 2, so we move to right by distance 2 to
the position 7, where the first occurrence of 9 has been found.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
We can summarize the
solution: We begin from the first element of the element, and compare it with
the given number. If the absolute difference is <span style="font-style: italic;">n</span>,
move to the right by distance <span style="font-style: italic;">n</span>. Then we
compare the current visited element. Repeat until the given element is found,
or the position is beyond the length of the array when the given element is not
available.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
The solution can be
implemented in C/C++ as below:</div>
<br />
<div style="font-family: Consolas; font-size: 9pt; line-height: 15pt; margin-bottom: 8pt; margin-top: 0pt;">
<span style="color: #3366ff;">int</span><span style="color: #333333;">
findFirst(</span><span style="color: #3366ff;">int</span><span style="color: #333333;">*
nums, </span><span style="color: #3366ff;">int</span><span style="color: #00b0f0;">
</span><span style="color: #333333;">length, </span><span style="color: #3366ff;">int</span><span style="color: #00b0f0;"> </span><span style="color: #333333;">target)<br />
{<br />
<span style="mso-spacerun: yes;"> </span></span><span style="color: #3366ff;">if</span><span style="color: #333333;"> (nums == nullptr || length <= 0)<br />
<span style="mso-spacerun: yes;"> </span>return -1;<br />
<br />
<span style="mso-spacerun: yes;"> </span></span><span style="color: #3366ff;">int</span><span style="color: #00b0f0;"> </span><span style="color: #333333;">index = 0;<br />
<span style="mso-spacerun: yes;"> </span></span><span style="color: #3366ff;">while</span><span style="color: #333333;"> (index < length && nums[index] !=
target)<br />
<span style="mso-spacerun: yes;"> </span>{<br />
<span style="mso-spacerun: yes;"> </span></span><span style="color: #3366ff;">int</span><span style="color: #333333;"> delta = target - nums[index];<br />
<span style="mso-spacerun: yes;"> </span>index += abs(delta);<br />
<span style="mso-spacerun: yes;"> </span>}<br />
<br />
<span style="mso-spacerun: yes;"> </span></span><span style="color: #3366ff;">if</span><span style="color: #00b0f0;"> </span><span style="color: #333333;">(index <
length)<br />
<span style="mso-spacerun: yes;"> </span>return
index;<br />
<br />
<span style="mso-spacerun: yes;"> </span></span><span style="color: #3366ff;">return</span><span style="color: #333333;"> -1;<br />
}</span></div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
<span lang="en-US">The
source code with unit test cases is shared at </span><a href="http://ideone.com/zuwjui"><span lang="zh-CN">http://ideone.com/zuwjui</span></a><span lang="en-US">.</span></div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
<span lang="en-US" style="color: #222222;">More coding interview questions are discussed
in my book< Coding Interviews: Questions, Analysis & Solutions>. You
may find the details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span lang="zh-CN">Amazon.com</span></a><span lang="en-US" style="color: #222222;">, or </span><a href="http://www.apress.com/9781430247616"><span lang="zh-CN">Apress</span></a><span lang="en-US" style="color: #222222;">. </span></div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
<span lang="en-US" style="color: #222222;">The author Harry He owns all the rights of
this post. If you are going to use part of or the whole of this article in your
blog or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/"><span lang="zh-CN">http://codercareer.blogspot.com/</span></a><span lang="en-US" style="color: #222222;">. If you are going to use it in your books,
please contact the author via zhedahht@gmail.com . Thanks.</span></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com35tag:blogger.com,1999:blog-4228859841453048938.post-84130678789895684742014-10-10T22:45:00.000+08:002016-09-26T12:44:41.444+08:00No. 57 - Integer Identical to Index<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
<span style="font-weight: bold; text-decoration: underline;">Problem:</span> Integers in
an array are unique and increasingly sorted. Please write a function/method to
find an integer from the array who equals to its index. For example, in the array
{-3, -1, 1, 3, 5}, the number 3 equals its index 3.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
<span style="font-weight: bold; text-decoration: underline;">Analysis:</span> If we scan
all integers in the array from beginning to end, we may check whether every
element equals its index. Obviously, this solution costs O(<span style="font-style: italic;">n</span>) time.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
Since
numbers are sorted in the array, let's try to utilize the binary search
algorithm to optimize. Supposing we reach the <span style="font-style: italic;">i</span><span style="font-style: italic; vertical-align: super;">th</span> element in the array
at some step. If the value of element is also <span style="font-style: italic;">i</span>,
it is a target integer and let's return it. </div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
What
would happen when the value <span style="font-style: italic;">m</span> is greater
than the index <span style="font-style: italic;">i</span>? For any <span style="font-style: italic;">k</span> (<span style="font-style: italic;">k</span>>0),
the value of element with index <span style="font-style: italic;">i</span>+<span style="font-style: italic;">k</span> should be greater than or equal to <span style="font-style: italic;">m</span>+<span style="font-style: italic;">k</span>,
because integers are unique and increasingly sorted in the array. Additionally
because <span style="font-style: italic;">m</span>><span style="font-style: italic;">i</span>, <span style="font-style: italic;">m</span>+<span style="font-style: italic;">k</span>><span style="font-style: italic;">i</span>+<span style="font-style: italic;">k</span>. Therefore, every element on the right side
of index <span style="font-style: italic;">i</span> should be greater than its
index in such a case.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
Similarly,
when the value of element with index <span style="font-style: italic;">i</span>
is less than <span style="font-style: italic;">i</span>, every integer on the left
side should be less than its index. Please prove it if you are interested.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
Therefore,
we could reduce the search scope to half for the next step, and it is a typical
process for binary search. The solution can be implemented with the following
Java code:</div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: blue;">public</span><span style="background: white; color: black;"> </span><span style="background: white; color: blue;">static</span><span style="background: white; color: black;"> </span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;">
getNumberSameAsIndex(</span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;">[] numbers) {</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;">(numbers == </span><span style="background: white; color: blue;">null</span><span style="background: white; color: black;"> || numbers.length == 0) {</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">return</span><span style="background: white; color: black;"> -1;</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span></span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> left = 0;</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> right = numbers.length - 1;</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">while</span><span style="background: white; color: black;">(left <= right) {</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> middle = left + ((right - left) >>> 1);</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;">(numbers[middle] == middle) {</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">return</span><span style="background: white; color: black;"> middle;</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span></span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;">(numbers[middle] > middle) {</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>right = middle - 1;</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">else</span><span style="background: white; color: black;"> {</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>left
= middle + 1;</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span></span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">return</span><span style="background: white; color: black;"> -1;</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;">}</span></div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
The
source code with unit test cases is shared at <a href="http://ideone.com/ZSd9kG">http://ideone.com/ZSd9kG</a>.</div>
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
More coding
interview questions are discussed in my book< Coding Interviews: Questions,
Analysis & Solutions>. You may find the details of this book on <a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614">Amazon.com</a>,
or <a href="http://www.apress.com/9781430247616">Apress</a>. </div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
The author Harry He owns all the rights of
this post. If you are going to use part of or the whole of this article in your
blog or webpages, please add a reference to <a href="http://codercareer.blogspot.com/">http://codercareer.blogspot.com/</a>.
If you are going to use it in your books, please contact the author via
zhedahht@gmail.com . Thanks.</div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com37tag:blogger.com,1999:blog-4228859841453048938.post-11778039060944189792014-10-04T13:17:00.000+08:002014-10-05T12:06:31.072+08:00No. 56 - Maximal Value of Gifts<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;"><b><u>Question</u></b>:
A board has <i>n</i>*<i>m</i> cells, and there is a gift with some value (value is greater than
0) in every cell. You can get gifts starting from the top-left cell, and move
right or down in each step, and finally reach the cell at the bottom-right
cell. What’s the maximal value of gifts you can get from the board?</span><span style="font-family: Calibri;"><o:p></o:p></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYDl5jsvxN749koI-JmVvho-qgHl_usmrOhuQvmQFxL8_NVdZC6gXaRpm73wpW5Z4jnTIs6eLGpHM_sjHua6PUpQjNNLS8DFggMvZ_7IAx3vYLSGnCKZYDbF3mjKLTK7iCCcMktahkZoA/s1600/matrix_of_gifts_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYDl5jsvxN749koI-JmVvho-qgHl_usmrOhuQvmQFxL8_NVdZC6gXaRpm73wpW5Z4jnTIs6eLGpHM_sjHua6PUpQjNNLS8DFggMvZ_7IAx3vYLSGnCKZYDbF3mjKLTK7iCCcMktahkZoA/s1600/matrix_of_gifts_2.png" /></a></div>
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="mso-no-proof: yes;"><!--[if gte vml 1]><v:shapetype
id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t"
path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
<v:stroke joinstyle="miter"/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
<v:f eqn="sum @0 1 0"/>
<v:f eqn="sum 0 0 @1"/>
<v:f eqn="prod @2 1 2"/>
<v:f eqn="prod @3 21600 pixelWidth"/>
<v:f eqn="prod @3 21600 pixelHeight"/>
<v:f eqn="sum @0 0 1"/>
<v:f eqn="prod @6 1 2"/>
<v:f eqn="prod @7 21600 pixelWidth"/>
<v:f eqn="sum @8 21600 0"/>
<v:f eqn="prod @7 21600 pixelHeight"/>
<v:f eqn="sum @10 21600 0"/>
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
<o:lock v:ext="edit" aspectratio="t"/>
</v:shapetype><v:shape id="Picture_x0020_1" o:spid="_x0000_i1025" type="#_x0000_t75"
style='width:105.75pt;height:108pt;visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\harryh\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png"
o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]--></span><br /></div>
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;">For example, the maximal value of gift from the board above
is 53, and the path is highlighted in red.<o:p></o:p></span></div>
<span style="font-family: inherit;"></span>
<br />
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Analysis</u></b>:
It is a typical problem about dynamic programming. Firstly let’s analyze it
with recursion. A function <i style="mso-bidi-font-style: normal;">f</i>(<i style="mso-bidi-font-style: normal;">i</i>, <i style="mso-bidi-font-style: normal;">j</i>)
is defined for the maximal value of gifts when reaching the cell (<i style="mso-bidi-font-style: normal;">i</i>, <i style="mso-bidi-font-style: normal;">j</i>).
There are two possible cells before the cell (<i style="mso-bidi-font-style: normal;">i</i>, <i style="mso-bidi-font-style: normal;">j</i>) is reached: One is
(<i style="mso-bidi-font-style: normal;">i</i> - 1, <i style="mso-bidi-font-style: normal;">j</i>), and the other is the cell (<i style="mso-bidi-font-style: normal;">i</i>,
<i style="mso-bidi-font-style: normal;">j</i>-1). Therefore, <i style="mso-bidi-font-style: normal;">f</i>(<i style="mso-bidi-font-style: normal;">i</i>, <i style="mso-bidi-font-style: normal;">j</i>)=<i style="mso-bidi-font-style: normal;"> max</i>(<i style="mso-bidi-font-style: normal;">f</i>(<i style="mso-bidi-font-style: normal;">i</i>-1,
<i style="mso-bidi-font-style: normal;">j</i>), <i style="mso-bidi-font-style: normal;">f</i>(<i style="mso-bidi-font-style: normal;">i</i>, <i style="mso-bidi-font-style: normal;">j</i>-1)) + gift[<i style="mso-bidi-font-style: normal;">i</i>, <i style="mso-bidi-font-style: normal;">j</i>].<o:p></o:p></span></div>
<span style="font-family: inherit;"></span>
<br />
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;">Even though it’s a recursive equation, it’s not a good idea
to write code in recursion, because there might be many over-lapping
sub-problems. A better solution is to solve is with iteration. A 2-D matrix is
utilized, and the value in each cell (<i style="mso-bidi-font-style: normal;">i</i>,
<i style="mso-bidi-font-style: normal;">j</i>) is the maximal value of gift when
reaching the cell (<i style="mso-bidi-font-style: normal;">i</i>, <i style="mso-bidi-font-style: normal;">j</i>) on the board. <o:p></o:p></span></div>
<span style="font-family: inherit;"></span>
<br />
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;">The iterative solution can be implemented in the following
Java code:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> getMaxValue(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">[][] values) {<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> rows = values.length;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> cols = values[0].length;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">[][] maxValues = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">[rows][cols];<o:p></o:p></span><br />
<br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> i = 0; i < rows; ++i) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> j = 0; j < cols; ++j) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> left = 0;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> up = 0;<o:p></o:p></span><br />
<br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(i > 0) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>up = maxValues[i - 1][j];<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(j > 0) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>left = maxValues[i][j - 1];<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>maxValues[i][j] = </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Math</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.max(left, up) +
values[i][j];<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> maxValues[rows - 1][cols - 1];<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<div class="MsoNormal">
<b><i>Optimization</i></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The maximal value of gifts when reaching the cell (<i>i</i>, <i>j</i>)
depends on the cells (<i>i</i>-1, <i>j</i>) and (<i>i</i>, <i>j</i>-1) only, so it is
not necessary to save the value of the cells in the rows <i>i</i>-2 and above. Therefore, we can replace the 2-D matrix with an
array, as the following code shows:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> getMaxValue(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[][] values) </span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">{</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> rows = values.length;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> cols = values[0].length;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[] maxValues = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[cols];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> i = 0; i < rows; ++i) </span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">{</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> j = 0; j < cols; ++j) </span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">{</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> left = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> up = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (i > 0) </span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">{</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> up = maxValues[j];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (j > 0) </span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">{</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> left = maxValues[j - 1];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> maxValues[j] = </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Math</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">.max(left, up) +
values[i][j];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> maxValues[cols - 1];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<br />
<span style="font-family: inherit;">The source code with unit test cases is shared at <a href="http://ideone.com/2vLRMk"><span style="color: #0563c1;">http://ideone.com/2vLRMk</span></a>.<o:p></o:p></span></div>
<span style="font-family: inherit;">More coding interview questions are discussed in my book< Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on <a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">Amazon.com</span></a>, or <a href="http://www.apress.com/9781430247616" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">Apress</span></a>. </span><br />
<span style="background-color: white; color: #222222; font-family: inherit; line-height: 13.5pt;"><br /></span>
<span style="font-family: inherit;">The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/" style="background-color: white; color: #888888; font-family: inherit; line-height: 13.5pt; text-decoration: none;"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a><span style="background-color: white; color: #222222; font-family: inherit; line-height: 13.5pt;">. If you are going to use it in your books, please contact the author via zhedahht@gmail.com . Thanks.</span></div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com29tag:blogger.com,1999:blog-4228859841453048938.post-35238316973304796182014-09-13T09:48:00.000+08:002014-09-15T09:39:11.582+08:00No. 55 - Translating Numbers to Strings<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
<span style="font-weight: bold; text-decoration: underline;">Question:</span> Given a
number, please translate it to a string, following the rules: 1 is translated
to 'a', 2 to 'b', …, 12 to 'l', …, 26 to 'z'. For example, the number 12258 can
be translated to "abbeh", "aveh", "abyh",
"lbeh" and "lyh", so there are 5 different ways to
translate 12258. How to write a function/method to count the different ways to
translate a number?</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
<span style="font-weight: bold; text-decoration: underline;">Analysis:</span> Let's take
the number 12258 as an example to analyze the steps to translate from the
beginning character to the ending one. There are two possible first characters
in the translated string. One way is to split the number 12258 into 1 and 2258
two parts, and 1 is translated into 'a'. The other way is to split the number
12258 into 12 and 258 two parts, and 12 is translated into 'l'. </div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
When the first one
or two digits are translated into the first character, we can continue to
translate the remaining digits. Obviously, we could write a recursive
function/method to translate.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
Let's define a
function <span style="font-style: italic;">f</span>(<span style="font-style: italic;">i</span>)
as the count of different ways to translate a number starting from the <span style="font-style: italic;">i</span><span style="font-style: italic; vertical-align: super;">th</span> digit, <span style="font-style: italic;">f</span>(<span style="font-style: italic;">i</span>)=<span style="font-style: italic;">g</span>(<span style="font-style: italic;">i</span>)*<span style="font-style: italic;">f</span>(<span style="font-style: italic;">i</span>+1)+<span style="font-style: italic;">h</span>(<span style="font-style: italic;">i</span>, <span style="font-style: italic;">i</span>+1)*<span style="font-style: italic;">f</span>(<span style="font-style: italic;">i</span>+2).
The function <span style="font-style: italic;">g</span>(i) gets 1 when the <span style="font-style: italic;">i</span><span style="font-style: italic; vertical-align: super;">th</span> digit is in the range 1 to 9 which can be converted to a
character, otherwise it gets 0. The function <span style="font-style: italic;">h</span>(<span style="font-style: italic;">i</span>, <span style="font-style: italic;">i</span>+1)
gets 1 the <span style="font-style: italic;">i</span><span style="font-style: italic; vertical-align: super;">th</span> and (<span style="font-style: italic;">i</span>+1)<span style="font-style: italic; vertical-align: super;">th</span> digits are in the
range 10 to 26 which can also be converted to a character. A single digit 0
can't be converted to a character, and two digits starting with a 0, such as 01
and 02, can't be converted either.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
Even though the
problem is analyzed with recursion, recursion is not the best approach because
of overlapping sub-problems. For example,<span style="mso-spacerun: yes;">
</span>The problem to translate 12258 is split into two sub-problems: one is to
translate 1 and 2258, and the other is to translate 12 and 258. In the next
step during recursion, the problem to translate 2258 can also split into two
sub-problems: one is to translate 2 and 258, and the other is to translate 22
and 58. Notice the sub-problem to translate 258 reoccurs.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
<span lang="en-US">Recursion
solves problem in the top-down order. We could solve this problem in the
bottom-up order, in order to </span><span lang="zh-CN">eliminate</span><span lang="en-US"> overlap sub-problems. That's to say, we start to translate the
number from the ending digits, and then move from right to left during
translation.</span></div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
The following is the
C# code to solve this problem:</div>
<br />
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: blue;">public</span><span style="background: white; color: black;"> </span><span style="background: white; color: blue;">static</span><span style="background: white; color: black;"> </span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;">
GetTranslationCount(</span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> number)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;">{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;"> (number <= 0)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">return</span><span style="background: white; color: black;"> 0;</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"> </span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">string</span><span style="background: white; color: black;"> numberInString = number.ToString();</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">return</span><span style="background: white; color: black;"> GetTranslationCount(numberInString);</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;">}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"> </span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: blue;">private</span><span style="background: white; color: black;"> </span><span style="background: white; color: blue;">static</span><span style="background: white; color: black;"> </span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;">
GetTranslationCount(</span><span style="background: white; color: blue;">string</span><span style="background: white; color: black;"> number)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;">{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> length = number.Length;</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;">[] counts = </span><span style="background: white; color: blue;">new</span><span style="background: white; color: black;"> </span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;">[length];</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"> </span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">for</span><span style="background: white; color: black;"> (</span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> i = length - 1; i >= 0; --i)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> count = 0;</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;"> (number[i] >= </span><span style="background: white; color: #a31515;">'1'</span><span style="background: white; color: black;"> && number[i] <= </span><span style="background: white; color: #a31515;">'9'</span><span style="background: white; color: black;">)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;"> (i < length - 1)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>count += counts[i + 1];</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">else</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>count += 1;</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"> </span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;"> (i < length - 1)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> digit1 = number[i] - </span><span style="background: white; color: #a31515;">'0'</span><span style="background: white; color: black;">;</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> digit2 = number[i + 1] - </span><span style="background: white; color: #a31515;">'0'</span><span style="background: white; color: black;">;</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">int</span><span style="background: white; color: black;"> converted = digit1 * 10 + digit2;</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;"> (converted >= 10 && converted
<= 26)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;"> (i <
length - 2)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>{</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>count += counts[i + 2];</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>}</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">else</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>{</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>count += 1;</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"> </span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>counts[i] = count;</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"> </span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">return</span><span style="background: white; color: black;"> counts[0];</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;">}</span></div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
In order to simply
the code implementation, we first convert the number into a string, and then
translate.</div>
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
The code with unit tests is shared at <a href="http://ideone.com/7wihgj">http://ideone.com/7wihgj</a>.</div>
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin: 0in;">
<span style="font-family: inherit;">More coding interview questions are discussed in my book< Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on <a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">Amazon.com</span></a>, or <a href="http://www.apress.com/9781430247616" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">Apress</span></a>.</span></div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 13.5pt; margin: 0in 0in 8pt;">
<span style="font-family: inherit;">The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to <a href="http://codercareer.blogspot.com/" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a>. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.</span></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com21tag:blogger.com,1999:blog-4228859841453048938.post-19228224867365323712014-08-09T23:11:00.000+08:002014-08-16T10:54:52.740+08:00No. 54 - Merge Ranges<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
<span style="font-weight: bold; text-decoration: underline;">Questions:</span> Given an
array of ranges, please merge the overlapping ones. For example, four ranges
[5, 13], [27, 39], [8, 19], [31, 37] (in blue in Figure1) are merged into
two ranges, which are [5, 19] and [27, 39] (in green in Figure 1). </div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjY2G_i0EAh_sYTE2iqyYKXL_esWUcNRcqR6_ptFXeH2Tqs_U6Sa9qkNW6MRX0ZX-8RusY2gfEGulTg7Tes5rKPfpoWEYTaJj1U7qxhVkq_UYcpToi_7mR_IN8c3GT0xilY0Int8geCTk/s1600/MergeRanges.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjY2G_i0EAh_sYTE2iqyYKXL_esWUcNRcqR6_ptFXeH2Tqs_U6Sa9qkNW6MRX0ZX-8RusY2gfEGulTg7Tes5rKPfpoWEYTaJj1U7qxhVkq_UYcpToi_7mR_IN8c3GT0xilY0Int8geCTk/s1600/MergeRanges.png" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: Merge four ranges [5, 13], [27, 39], [8, 19] and [31, 37] (in blue), and get [5, 19], and [27, 39] (in green)</td></tr>
</tbody></table>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
<span style="font-weight: bold; text-decoration: underline;">Analysis:</span> Before we
analyze how to merge an array of ranges, let’s discuss how to merge two ranges.
When two ranges don’t overlap each other, they can’t merge. When two ranges
overlap, the less starting value of two ranges becomes the starting value of
the merged range, and the greater ending value of two ranges becomes the ending
value of the merged range. </div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
Therefore,
two ranges [5, 13] and [8, 19] are merged into a new range [5, 19], and two
ranges [27, 39] and [31, 37] are merged into a new range [27, 39]. The two
merged ranges can’t be merged further because they don’t overlap.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
The
next question is: How to check whether two ranges overlap each other? When
two ranges overlap, there is at least on node in a range is contained in the
other range. For instance, the starting value 8 of the range [8, 19] is
contained in the range [5, 13], therefore, the two ranges [5, 13] and [8, 19]
overlap. No nodes in the range [8, 19] are contained in the range [31, 37], so
the two ranges don’t overlap.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
The
following code shows how to merge two ranges, as well as to check whether two
ranges overlap:</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">bool</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Contains(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> value)<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">{<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> (value >= </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Start && value <= </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.End)<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">true</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p> </o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">false</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p> </o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">bool</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Overlaps(</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Range</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> other)<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">{<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> (other == </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">null</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">)<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">false</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p> </o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Contains(other.Start) || </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Contains(other.End) </span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> || other<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Contains(<span style="color: blue;">this</span>.Start)</span> || other<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Contains(<span style="color: blue;">this</span>.End)</span>)<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">true</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p> </o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">false</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p> </o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Range</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Merge(</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Range</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> other)<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">{<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> (!</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Overlaps(other))<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">throw</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ApplicationException</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Can't merge two
ranges."</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">);<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p> </o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> newStart = (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Start < other.Start) ? </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Start : other.Start;<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> newEnd = (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.End > other.End) ? </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.End : other.End;<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p> </o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Range</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(newStart, newEnd);<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
<span style="font-family: Times New Roman; font-size: small;">
</span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
Now
let’s move on to merge an array of ranges.<span style="mso-spacerun: yes;">
</span>The first step is to sort the ranges based on their start values. When
the ranges [5, 13], [27, 39], [8, 19], and [31, 37] are sorted, they are in the
order of [5, 13], [8, 19], [27, 39], and [31, 37]. </div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
The
next steps are to merge the sorted ranges. The merged ranges are inserted into
a data container. At each step, a range is retrieved from the sorted array, and
merged with the existing ranges in the container.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
At
first the data container is empty, and the first range [5, 13] is inserted into
the container directly, without merging. Now there is only one range [5, 13] in
the container.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
Then
the next range [8, 19] is retrieved. Since it overlaps the range[5, 13], and
they become [5, 19] when they merged. There is still only one range, which is [5,
19] in the container.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
The
next range [27, 39] is retrieved, which does not overlap the range [5, 19] in
the container, so it is inserted into the range directly without merging. There
are two ranges [5, 19] and [27, 39] in the container.</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
The
last range in the sorted array is [31, 37]. It overlaps the last range [27, 39] in the container.
Therefore, the last range [27, 39] is deleted from the container, and then the
merged range is inserted into the container. At this step, the merged range is
also [27, 39].</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
Ranges
in the container are also sorted based on their starting values, and they don't
overlap each other. Notice that it's only necessary to check whether the new
range overlap the last range in the container. Why not the other ranges in the
container? Let's analyze what would happen when a new range in the sorted array
overlap two ranges in the container, with Figure 2:</div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHhUHJbdXXrnD8kDjSgi2O_p2tVUJL-SzIDWUeepIt4532GWhvzaCxvkEyUiWfumYBXGWnlorAyiyENx0tqlIA0x_g9gwYOs9jn0TfBev_Knt_WBaQJFEI1BngGBA9CCMB4hqROCMSYJU/s1600/MergeRanges2.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHhUHJbdXXrnD8kDjSgi2O_p2tVUJL-SzIDWUeepIt4532GWhvzaCxvkEyUiWfumYBXGWnlorAyiyENx0tqlIA0x_g9gwYOs9jn0TfBev_Knt_WBaQJFEI1BngGBA9CCMB4hqROCMSYJU/s1600/MergeRanges2.png" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: It causes problems when a new range overlaps two ranges in the merged container</td></tr>
</tbody></table>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
</div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
In
Figure 2, the container has two ranges A and B, and a new range C is retrieved
from the sorted array, which overlaps the ranges A and B. Since C overlaps A,
the starting value of C should be less than the ending value of A. On the other
hand, C is retrieved from the sorted array later than B, the starting value of
C is greater than starting value of B. Additionally, B is behind A and they
don't overlap, so the starting value of B is greater than the ending value A.
Therefore, the starting value of C is greater than the ending value of A. It
contradicts.</div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
Since
it's only necessary to merge new ranges from the sorted array with the last
range in the container. We implement the container as a stack, and the last
range is on the top of the stack. </div>
<br />
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
The
following is the C# code to merge a sort an array of ranges:</div>
<br />
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: blue;">public</span><span style="background: white; color: black;"> </span><span style="background: white; color: blue;">static</span><span style="background: white; color: black;"> </span><span style="background: white; color: #2b91af;">Range</span><span style="background: white; color: black;">[]
Merge(</span><span style="background: white; color: #2b91af;">Range</span><span style="background: white; color: black;">[] ranges)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;">{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: #2b91af;">Stack</span><span style="background: white; color: black;"><</span><span style="background: white; color: #2b91af;">Range</span><span style="background: white; color: black;">> results = </span><span style="background: white; color: blue;">new</span><span style="background: white; color: black;"> </span><span style="background: white; color: #2b91af;">Stack</span><span style="background: white; color: black;"><</span><span style="background: white; color: #2b91af;">Range</span><span style="background: white; color: black;">>();</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;"> (ranges != </span><span style="background: white; color: blue;">null</span><span style="background: white; color: black;">)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: #2b91af;">Array</span><span style="background: white; color: black;">.Sort(ranges, CompareRangeByStart);</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"> </span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">foreach</span><span style="background: white; color: black;"> (</span><span style="background: white; color: blue;">var</span><span style="background: white; color: black;"> range </span><span style="background: white; color: blue;">in</span><span style="background: white; color: black;"> ranges)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;"> (results.Count == 0)</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>results.Push(range);</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;">
</span></span><span style="background: white; color: blue;">else</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">var</span><span style="background: white; color: black;"> top =
results.Peek();</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">if</span><span style="background: white; color: black;">
(top.Overlaps(range))</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>{</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">var</span><span style="background: white; color: black;"> union = top.Merge(range);</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>results.Pop();</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>results.Push(union);</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>}</span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">else</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>{</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>results.Push(range);</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;">
</span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;"> </span></div>
<div style="font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white; color: black;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue;">return</span><span style="background: white; color: black;"> results.Reverse().ToArray();</span></div>
<div style="color: black; font-family: Consolas; font-size: 9.5pt; margin: 0in;">
<span style="background: white;">}</span></div>
<div style="font-family: Calibri; font-size: 11pt; margin-bottom: 8pt; margin-top: 0pt;">
The
code with unit tests are shared at <a href="http://ideone.com/kg4TwM">http://ideone.com/kg4TwM</a>.</div>
<br />
<div style="font-family: SimSun; line-height: 13pt; margin: 0in;">
<span lang="en-US" style="color: #222222; font-family: inherit;">More coding interview questions are discussed
in my book< Coding Interviews: Questions, Analysis & Solutions>. You
may find the details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span lang="zh-CN" style="font-family: inherit;">Amazon.com</span></a><span lang="en-US" style="color: #222222; font-family: inherit;">,
or </span><a href="http://www.apress.com/9781430247616"><span lang="zh-CN" style="font-family: inherit;">Apress</span></a><span lang="en-US" style="color: #222222; font-family: inherit;">.</span></div>
<span style="font-family: inherit;"></span><br />
<div style="font-family: SimSun; line-height: 13pt; margin-bottom: 8pt; margin-top: 0pt;">
<span lang="en-US" style="color: #222222; font-family: inherit;">The author Harry He
owns all the rights of this post. If you are going to use part of or the whole
of this ariticle in your blog or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/"><span lang="zh-CN" style="font-family: inherit;">http://codercareer.blogspot.com/</span></a><span lang="en-US" style="color: #222222; font-family: inherit;">. If you are going to use it in your books,
please contact him via zhedahht@gmail.com . Thanks.</span></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com34tag:blogger.com,1999:blog-4228859841453048938.post-34766825752104140952014-03-23T08:48:00.001+08:002014-03-23T08:49:13.145+08:00No. 53 - Longest Arithmetic Sequence<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<b><u>Question 1:</u></b>
Given an array, please get the length of the longest arithmetic sequence. The
element order in the arithmetic sequence should be same as the element order in
the array. For example, in the array {1, 6, 3, 5, 9, 7}, the longest arithmetic
sequence is 1, 3, 5, and 7, whose elements have same order as they are in the
array, and the length is 4.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><u>Analysis:</u></b> Every
pair of two adjacent numbers in an arithmetic sequence has exactly same
difference. For example, 1, 3, 5, and 7 is an arithmetic sequence, and the
pairs (1, 3), (3, 5), and (5, 7) have the same difference 2. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
There are <i>n</i>(<i>n</i>-1)/2 pairs out of an array with <i>n</i> elements. These pairs can be categorized
into a set of groups, of which each group of pairs have the same difference.
For example, the pairs of numbers in the array {1, 6, 3, 5, 9, 7} can be
categorized into groups:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Difference -1: (6, 5)<br />
Difference 2: (1, 3), (3, 5), (5, 7)<br />
Difference 3: (6, 9)<br />
…<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Therefore, a hash table can be defined for the groups. The
key in the hash table is the difference of pairs, and the value in the hash
table is a list of pairs with same difference. The code to build the hash table
can be implemented in C# as the following:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">internal</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">class</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Pair</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> First { </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">get</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">; </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">set</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> Second { </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">get</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">; </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">set</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Dictionary</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">List</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Pair</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">>> BuildHashTable(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[] numbers)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">var</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> hashTable = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Dictionary</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">List</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Pair</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">>>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> i = 0; i < numbers.Length; ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> j = i + 1; j < numbers.Length; ++j)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Pair</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> pair = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Pair</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> First = i,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> Second = j<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> diff = numbers[j] - numbers[i];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(hashTable.Keys.Contains(diff))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> hashTable[diff].Add(pair);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">else</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">List</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Pair</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">> newValue = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">List</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Pair</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> newValue.Add(pair);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> hashTable[diff] = newValue;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> hashTable;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 107%;">}</span> <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In the code above,
the values of the hash table is pairs of indexes, rather than elements
themselves of the array. The pairs are sorted according to their first
elements.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The next step is to get the length of pairs with each
difference. A list of pairs with difference <i>k</i>
is got given a key <i>k</i> in the hash
table. If an element A[<i>i</i>] is <i>m<sup>th</sup></i> element is an arithmetic
sequence with a common difference <i>k</i>,
and there is a pair (A[<i>i</i>], A[<i>j</i>]) (<i>j</i>><i>i</i>) in the list of pairs, the element A[<i>j</i>] should be the <i>m</i>+<i>1<sup>th</sup></i>elemement
in the arithmetic sequence. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Therefore, the code to get the max length of all arithmetic
sequences can be implemented as:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> Analyze(</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Dictionary</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">List</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Pair</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">>> hashTable, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> lengthOfNumbers)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxLength = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">foreach</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">var</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> key </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">in</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> hashTable.Keys)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[] lengths = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[lengthOfNumbers];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> i = 0; i < lengthOfNumbers; ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> lengths[i] = 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">foreach</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Pair</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> pair </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">in</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> hashTable[key])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> lengths[pair.Second] =
lengths[pair.First] + 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">foreach</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">var</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> length </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">in</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> lengths)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(length > maxLength)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxLength = length;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxLength;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> GetMaxLengthOfArithmeticSequence(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[] numbers)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">var</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> hashTable = BuildHashTable(numbers);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> Analyze(hashTable, numbers.Length);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 107%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The source code with unit test cases are shared at: <a href="http://ideone.com/jxRDkd">http://ideone.com/jxRDkd</a>.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
As mentioned above, there are O(<i>n</i><sup>2</sup>) pairs in an array with <i>n</i> elements. Therefore, the time and space efficiencies of this
solution is O(<i>n</i><sup>2</sup>) given an
array with <i>n</i> elements.<o:p></o:p></div>
<div class="MsoNormal">
<b><u><br /></u></b></div>
<div class="MsoNormal">
<b><u>Question 2:</u></b>
Given an array, please get the length of the longest arithmetic sequence. The
element order in the arithmetic sequence is not necessarily same as the element
order in the array. For example, in the array {1, 6, 3, 5, 9, 7}, the longest
arithmetic sequence is 1, 3, 5, 7, and 9, and the length is 5.<o:p></o:p></div>
<div class="MsoNormal">
<b><u><br /></u></b></div>
<div class="MsoNormal">
<b><u>Analysis:</u></b> Different
from the previous problem, there are no limitations on the order of arithmetic
sequence. Consequently, we can sort the array before we try to get the maximal
length of arithmetic sequences. The code is almost same as before, except for
the revision that there is an additional line of code to sort the array, as
listed below: <o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> GetMaxLengthOfArithmeticSequence(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[] numbers)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Array</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">.Sort(numbers);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">var</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> hashTable = BuildHashTable(numbers);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> Analyze(hashTable, numbers.Length);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 107%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The source code with unit test cases are shared at: <a href="http://ideone.com/lEqNm3">http://ideone.com/lEqNm3</a>.<o:p></o:p></div>
<div class="MsoNormal">
<b><u><br /></u></b></div>
<div class="MsoNormal">
<b><u>Question 3:</u></b>
Given an array, please get the length of the longest consecutive sequence. A
consecutive sequence is an arithmetic sequence with common difference 1. The
element order in the consecutive sequence is not necessarily same as the
element order in the array. The solution should not cost more than O(<i>n</i>) time and space if the length of the
input array is <i>n</i>. For example, in the
array {1, 6, 3, 5, 9, 7}, the longest consecutive sequence is 5, 6, and 7 whose
length is 3.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><u>Analysis: </u></b>The solution to solve the above problems cost O(<i>n</i><sup>2</sup>) time and space. Therefore,
we need a new solution to solve this problem.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
A consecutive can’t have duplicated elements. A hash set, of
which every element is unique, can be built from the input array. When a number
is located in the set, we try to locate its consecutive neighbors. For
instance, when the number 6 is found in the set, we try to find the number 5
and 7 in the set, and then we get a consecutive sequence 5, 6, and 7.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This solution can be implemented in C# code as listed below:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> GetMaxLengthConsecutiveSequence(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[] numbers)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">HashSet</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">> set = BuildHashSet(numbers);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> AnalyzeHashSet(set);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">HashSet</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">> BuildHashSet(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[] numbers)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">var</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> set = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">HashSet</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">foreach</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> number </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">in</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> numbers)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> set.Add(number);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> set;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> AnalyzeHashSet(</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">HashSet</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">> set)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxCount = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">while</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(set.Count > 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> number = set.First();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> count = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> toDelete = number;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">while</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(set.Remove(toDelete))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> count++;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> toDelete++;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> toDelete = number - 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">while</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(set.Remove(toDelete))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> count++;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> toDelete--;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(count > maxCount)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxCount = count;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxCount;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 107%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Every number in the input array is added into and removed
from the array only once, so the time and space efficiency is O(<i>n</i>) if there are <i>n</i> numbers in the array.<o:p></o:p></div>
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The source code with unit tests is shared at <a href="http://ideone.com/0oRqLq">http://ideone.com/0oRqLq</a>.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 13.5pt; margin: 0in 0in 0pt;">
<span style="font-family: inherit;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 13.5pt; margin: 0in 0in 0pt;">
<span style="font-family: inherit;">More coding interview questions are discussed in my book< Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on <a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">Amazon.com</span></a>, or <a href="http://www.apress.com/9781430247616" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">Apress</span></a>.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; color: #222222; font-family: inherit; font-size: 13px; line-height: 18.479999542236328px;"></span><br style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18.479999542236328px;" /></div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 13.5pt; margin: 0in 0in 8pt;">
<span style="font-family: inherit;">The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to <a href="http://codercareer.blogspot.com/" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a>. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.</span></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com14tag:blogger.com,1999:blog-4228859841453048938.post-32305695753602367072013-12-17T11:38:00.003+08:002013-12-17T11:40:13.613+08:00No. 52 - Maximal Product when Cutting Rope<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<b><u>Problem:</u></b> Given
a rope with length <i>n</i>, how to cut the
rope into <i>m</i> parts with length <i>n</i>[0], <i>n</i>[1], ..., <i>n</i>[<i>m</i>-1], in order to get the maximal
product of <i>n</i>[0]*<i>n</i>[1]* ... *<i>n</i>[<i>m</i>-1]? We have to cut once at least.
Additionally, the length of the whole length of the rope, as well as the length
of each part, are in integer value.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
For example, if the length of the rope is 8, the maximal
product of the part lengths is 18. In order to get the maximal product, the
rope is cut into three parts with lengths 2, 3, and 3 respectively.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><u>Analysis:</u></b>
There are two solutions to solve this problem. One is the traditional dynamic
programming solution with O(<i>n</i><sup>2</sup>)
time and O(<i>n</i>) space, and the other is
a quite creative and efficient solution with O(1) time and O(1) space.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><i>Solution 1: Dynamic programming<o:p></o:p></i></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Firstly let’s define a function <i>f</i>(<i>n</i>) for the maximal
length product after cutting a rope with length <i>n </i>into parts. We have <i>n</i>-1
choice for the first cut on the rope, with the length of the first part 1, 2, …
<i>n</i>-1 respectively. Therefore, <i>f</i>(<i>n</i>)=max(<i>f</i>(<i>i</i>)*f(<i>n</i>-<i>i</i>),
where 0<<i>i</i><<i>n</i>). <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
If the equation is resolved recursively in top-down order,
there are lots of overlapping sub-problems and it’s a waste of recalculation. It’s much more efficient to calculate in
bottom-up order. That is to say, we firstly get <i>f</i>(2), and then <i>f</i>(3), then
<i>f</i>(4), <i>f</i>(5). We continue till we get <i>f</i>(<i>n</i>).<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The following Java code solves the problem in bottom-up order:
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxProductAfterCutting_solution1(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> length) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(length < 2) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(length == 2) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(length == 3) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> 2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[] products = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[length + 1];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> products[0] = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> products[1] = 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> products[2] = 2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> products[3] = 3;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> i = 4; i <= length; ++i) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> max = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> j = 1; j <= i / 2; ++j) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> product = products[j] * products[i - j];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(max < product) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> max = product;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> products[i] = max;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> products[length];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
An array <span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 107%;">products
</span>with length <i>n</i>+1 is created, in
order to store the maximal product of for ropes with length 0, 1, 2, …, <i>n</i>.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><i>Solution 2: Tricky cutting strategy<o:p></o:p></i></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
There is a strategy to cut the rope to get maximal product:
We cut the parts with length either 3 or 2. Additionally, we try to keeping cut
parts with length 3 as many as possible. Therefore, we could solve the problem
with the following Java code:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxProductAfterCutting_solution2(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> length) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(length < 2) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(length == 2) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(length == 3) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> 2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> timesOf3 = length / 3;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">((length - timesOf3 * 3) % 2 == 1) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> timesOf3 -= 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> timesOf2 = (length - timesOf3 * 3) / 2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">)(Math.pow(3, timesOf3)) * (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">)(Math.pow(2, timesOf2));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This solution sounds a bit tricky, and it does not make
sense if we can’t prove it mathematically. Let’s try to demonstrate its correctness.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
When <i>n</i>≥5, we could
prove that 2(<i>n</i>-2)><i>n</i> and 3(<i>n</i>-3)><i>n</i>.<i> </i>Therefore, we continue to cut rope into
parts with length 3 or 2 when the length is greater than 5. Additionally, 3(<i>n</i>-3) ≥ 2(<i>n</i>-2) when <i>n</i>≥5, so we cut
parts with length 3 as many as possible.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The prerequisite of the proof above is <i>n</i>≥5. How about <i>n</i> is 4?
There are only two approaches to cut when the length of the rope is 4: Cut into
two parts with lengths 1 and 3, or with lengths 2 and 2. In our strategy, the
rope will be cut into two parts with length 2 and 2. The other approach is
discarded because a part with length 1 is not allowed. Notice that 4=2*2, and
2*2>1*3. That’s to say, it’s no harm to cut a rope with length 4 into two
parts with same length 2. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Therefore, our strategy to cut ropes is correct.<o:p></o:p></div>
<br />
<div class="MsoNormal">
<div style="text-align: left;">
<span style="font-family: inherit;"><span style="background-color: white; color: #222222; font-size: 13px;">Code with unit tests is shared at </span><span style="color: #222222; font-size: x-small;"><a href="http://ideone.com/wGvr86">http://ideone.com/wGvr86</a>.</span></span></div>
</div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 13.5pt; margin: 0in 0in 0pt;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">More coding interview questions are discussed in my book< Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on <a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">Amazon.com</span></a>, or <a href="http://www.apress.com/9781430247616" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">Apress</span></a>.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; color: #222222; font-family: inherit; font-size: 13px; line-height: 18.479999542236328px;"></span><br style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18.479999542236328px;" /></div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 13.5pt; margin: 0in 0in 8pt;">
<span style="font-family: inherit;">The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to <a href="http://codercareer.blogspot.com/" style="color: #888888; text-decoration: none;"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a>. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.</span></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com19tag:blogger.com,1999:blog-4228859841453048938.post-87608496791942303722013-12-05T14:17:00.002+08:002013-12-05T14:29:28.965+08:00No. 51 - Next Number with Same Set of Digits<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Problem: </u></b>Reorder
the digits of a number, in order to get the next number which is the least one
that is greater than the input number. For example, the number 34724126 is the
next number of 34722641 when reordering digits.</span><br />
<span style="font-family: inherit;"></span> </div>
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Analysis:</u></b> When
a digit in a number is swapped with a greater digit on its right side, the
number becomes greater. For example, if the digit 3 in the number 34722641 is swapped
with digit 7, the result is 74322641 which is greater than the original number.
The remaining issue how to get least number which is greater than the original
one.</span><br />
<span style="font-family: inherit;"></span> </div>
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;">Since we are going to get the least number after reordering
digits, let’s find digits to be swapped on the right side. The first three
digits on the right side of 34722641 are 641 which are decreasingly sorted. The
two digits among them are swapped, the whole number will become less.</span><br />
<span style="font-family: inherit;"></span> </div>
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;">Let’s move on to the left digits. The next digit on the
right side is 2, which is less than 6 and 4 on its right side. If the digit 2
is swapped with 6 or 4, the whole number will be become greater. Since we are
going to keep the swapped number as less as possible, the digit 2 is swapped
with 4, which is less one between 4 and 6. The number becomes 34724621.</span><br />
<span style="font-family: inherit;"></span> </div>
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;">Now 34724621 is greater than the original number 34722641,
but it’s not the least one which is greater than 34722641. The three digits on
the right side, 6, 2 and 1, should be increasingly sorted, in order to form the
least number 34724126 among numbers which are greater than of 34722641.</span><br />
<span style="font-family: inherit;"></span> </div>
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;">The solution can be implemented with the following JAVA
code:</span><br />
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> String getLeastGreaterNumber(String number) {<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>List<Character> decreasingChars = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> ArrayList();<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> firstDecreasing = getDecreasingChars(number,
decreasingChars);<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(isGreatestNumber(firstDecreasing)) {<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">""</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>String prefix = </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">""</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(firstDecreasing > 1) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>prefix = number.substring(0,
firstDecreasing - 1);<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>StringBuilder resultBuilder = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> StringBuilder(prefix);<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">char</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> target = number.charAt(firstDecreasing - 1);<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">char</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> leastGreater = swapLeastGreater(decreasingChars, target);<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>resultBuilder.append(leastGreater);<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>Collections.sort(decreasingChars);<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>appendList(resultBuilder, decreasingChars);<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"></span></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> resultBuilder.toString();<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 6pt 0in 8pt;">
<span style="font-family: inherit;">When all digits
are already increasingly sorted in the input number, the number itself is the
greatest number with given digits. We should discuss with our interviewers what
to return for this case during interviewers. Here we just return an empty
string for this case.</span><br />
</div>
<div class="MsoNormal" style="line-height: normal; margin: 6pt 0in 8pt;">
<span style="font-family: inherit;">When <span style="background: white; color: black; font-family: "Courier New", Courier, monospace; font-size: 9.5pt; mso-highlight: white;">firstDecreasing</span> is 0, it means all digits are
increasingly sorted, and the input number is the greatest number with given
digits, as listed in the following method <span style="background: white; color: black; font-family: "Courier New", Courier, monospace; font-size: 9.5pt; mso-highlight: white;">isGreatestNumber</span>.</span><br />
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Boolean isGreatestNumber(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> firstDecreasing) {<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> firstDecreasing == 0;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 6pt 0in 8pt;">
<span style="font-family: inherit;">The following method
<span style="background: white; color: black; font-family: "Courier New", Courier, monospace; mso-highlight: white;">getDecreasingChars</span> gets the longest sequence of
decreasing digits on the right side of a number:</span><br />
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> getDecreasingChars(String number, List<Character>
decreasing) {<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> firstDecreasing = number.length() - 1;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(; firstDecreasing > 0; --firstDecreasing) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">char</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> curChar = number.charAt(firstDecreasing);<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">char</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> preChar = number.charAt(firstDecreasing - 1);<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>decreasing.add(curChar);<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"></span></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(curChar > preChar) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">break</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> firstDecreasing;<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 6pt 0in 8pt;">
<span style="font-family: inherit;">The following method
<span style="background: white; color: black; font-family: "Courier New", Courier, monospace; font-size: 9.5pt; mso-highlight: white;">swapLeastGreater</span> swaps the digit before the
decreasing digits on the right side (<span style="background: white; color: black; font-family: "Courier New", Courier, monospace; font-size: 9.5pt; mso-highlight: white;">target</span>) and
the least digit which is greater than <span style="background: white; color: black; font-family: "Courier New", Courier, monospace; font-size: 9.5pt; mso-highlight: white;">target</span>:</span><br />
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">char</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> swapLeastGreater(List<Character> chars, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">char</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> target) {<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>Iterator it=chars.iterator();<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">char</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> finding = </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'9'</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">while</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(it.hasNext()) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">char</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> value = ((Character)it.next()).charValue();<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(value > target && value < finding) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>finding = value;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"></span></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>chars.remove(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Character(finding));<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>chars.add(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Character(target));<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> finding;<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 6pt 0in 8pt;">
<span style="font-family: inherit;">The following method
<span style="background: white; color: black; font-size: 9.5pt; mso-highlight: white;">appendList</span> appends characters from a list into a
string builder:</span><br />
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> appendList(StringBuilder str, List<Character>
chars) {<o:p></o:p></span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>Iterator it=chars.iterator();<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">while</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(it.hasNext()) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">char</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> value = ((Character)it.next()).charValue();<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>str.append(value);<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="background: white; color: #222222; mso-bidi-font-family: Arial;">Code with unit tests is shared at </span><span style="background: white; color: #2288bb; mso-bidi-font-family: Arial;"><a href="http://ideone.com/czs13W"><span style="color: #2288bb;">http://ideone.com/czs13W</span></a></span></span><span style="background: white; font-family: "inherit","serif"; mso-bidi-font-family: Arial;"><span style="font-family: inherit;">.</span> </span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Extended Problem 1</u></b>:
Given a set of digits, please output all numbers permutated by the digits in
increasing order. For example, if the input are five digits 1, 2, 3, 4, 5, the
output are numbers from 12345, 12354, ..., to 54321 in increasing order. </span><br />
</div>
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Extended Problem
2: </u></b>Given a number <i style="mso-bidi-font-style: normal;">n</i>, please
out put all numbers with <i style="mso-bidi-font-style: normal;">n</i> bits of 1
in increasing order. For example, if the input is 3, the output are numbers 7,
11, 13, …</span><br />
<span style="font-family: inherit;"></span> </div>
<div class="MsoNormal" style="background: white; line-height: 13.5pt; margin: 0in 0in 0pt;">
<span style="font-family: inherit;"><span style="color: #222222; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">More coding interview questions are discussed in my book<
Coding Interviews: Questions, Analysis & Solutions>. You may find the
details of this book on </span><span style="color: #222222; mso-fareast-font-family: "Times New Roman";"><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span style="color: #2288bb;">Amazon.com</span></a></span><span style="color: #222222; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">, or </span><span style="color: #222222; mso-fareast-font-family: "Times New Roman";"><a href="http://www.apress.com/9781430247616"><span style="color: #2288bb;">Apress</span></a></span><span style="color: #222222; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #222222; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></span></div>
<span style="font-family: inherit;"></span><br />
<div class="MsoNormal" style="background: white; line-height: 13.5pt; margin: 0in 0in 8pt;">
<span style="font-family: inherit;"><span style="color: #222222; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">The author Harry He
owns all the rights of this post. If you are going to use part of or the whole
of this ariticle in your blog or webpages, please add a reference to </span><span style="color: #222222; mso-fareast-font-family: "Times New Roman";"><a href="http://codercareer.blogspot.com/"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a></span><span style="color: #222222; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">. If you are going
to use it in your books, please contact him via zhedahht@gmail.com . Thanks.</span></span><span style="color: #222222; font-family: "Arial","sans-serif"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com4tag:blogger.com,1999:blog-4228859841453048938.post-30656585629191181782013-12-03T11:16:00.000+08:002013-12-03T13:57:59.920+08:00No. 50 - Numbers Appearing Once<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="margin: 0in 0in 8pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Problem:</u></b> In
an array, all numbers appear three times except one which only appears only
once. Please find the unique number.</span><br />
<b style="font-family: inherit;"><u><br /></u></b>
<b style="font-family: inherit;"><u>Analysis:</u></b><span style="font-family: inherit;"> It
is simpler if we modify the problem a little bit: Please find a unique number
from an array where other numbers appear twice. We could solve this simplified
problem with the XOR bitwise operation. If all numbers in the array are XORed,
the result is the number appearing only once, since pairs of numbers get 0 when
they are XORed.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">The strategy with XOR does not work since all numbers except
one appear three times, since the XOR result of a triple of numbers is the
number itself.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Even though we could not solve the problem with XOR, we may
still stick on the bitwise operations. A number appears three times, each bit
(either 0 or 1) also appears three times. If every bit of numbers appearing
three time is added, the sum of every bit should be multiple of 3.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Supposing every bit of numbers (including the unique number)
in the input array is added. If the sum of a bit is multiple of 3, the
corresponding bit in the unique number is 0. Otherwise, it is 1.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">The solution can be implemented in Java as the code listed
below:</span><br />
<span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="background-color: white; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">static</span><span style="background-color: white; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-color: white; color: black; font-family: Consolas; font-size: 9.5pt;"> FindNumberAppearingOnce(</span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-color: white; color: black; font-family: Consolas; font-size: 9.5pt;">[] numbers) {</span></div>
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">[] bitSum = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">[32];<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> i = 0; i < 32; ++i) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>bitSum[i] = 0;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> i = 0; i < numbers.length; ++i) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> bitMask = 1;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> j = 31; j >= 0; --j) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> bit = numbers[i] & bitMask;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(bit != 0) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>bitSum[j] += 1;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>bitMask = bitMask << 1;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> result = 0;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> i = 0; i < 32; ++i) {<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>result = result << 1;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>result += bitSum[i] % 3;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> result;<o:p></o:p></span><br />
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}<o:p></o:p></span><br />
<span style="background-color: white; color: #222222; font-family: inherit; font-size: 10pt; line-height: 13.5pt;"><br /></span>
<span style="background-color: white; color: #222222; font-family: inherit; font-size: 10pt; line-height: 13.5pt;">The time efficiency of this solution is O(<i>n</i>), and space efficiency is O(1) because an array with 32 elements is created. It's more efficient than two straightforward solutions: (1) It's easy to find the unique number from a sorted array, but it costs O(<i>n</i>log<i>n</i>) time to sort an array with <i>n</i> elements. (2) We may utilize a hash table to store the number of occurrences of each element in the array, but the cost for the hash table is O(<i>n</i>).</span><br />
<span style="background-color: white; color: #222222; font-family: inherit; font-size: 10pt; line-height: 13.5pt;"><br /></span>
<span style="background-color: white; color: #222222; font-family: inherit; font-size: 10pt; line-height: 13.5pt;">Code with unit tests
is shared at </span><span style="background-color: white; color: #2288bb; font-family: inherit; font-size: 10pt; line-height: 13.5pt;"><a href="http://ideone.com/tTk3RX">http://ideone.com/tTk3RX</a></span><span style="background-color: white; color: #222222; font-family: inherit; font-size: 10pt; line-height: 13.5pt;">.</span><br />
<span style="font-family: inherit;"></span><br />
<div class="MsoNormal" style="background: white; line-height: 13.5pt; margin: 0in 0in 0pt;">
<span style="color: #222222; font-size: 10pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"><span style="font-family: inherit;">More coding interview questions are discussed in my book<
Coding Interviews: Questions, Analysis & Solutions>. You may find the
details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span style="color: #2288bb; font-family: inherit;">Amazon.com</span></a><span style="font-family: inherit;">,
or </span><a href="http://www.apress.com/9781430247616"><span style="color: #2288bb; font-family: inherit;">Apress</span></a><span style="font-family: inherit;">.</span></span><span style="color: #222222; font-size: 10pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<span style="font-family: inherit;"></span><br />
<div class="MsoNormal" style="background: white; line-height: 13.5pt; margin: 0in 0in 8pt;">
<span style="color: #222222; font-size: 10pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"><span style="font-family: inherit;">The author Harry He
owns all the rights of this post. If you are going to use part of or the whole
of this ariticle in your blog or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/"><span style="color: #2288bb; font-family: inherit;">http://codercareer.blogspot.com/</span></a><span style="font-family: inherit;">.
If you are going to use it in your books, please contact him via
zhedahht@gmail.com . Thanks.</span></span></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com7tag:blogger.com,1999:blog-4228859841453048938.post-15255955791700983682013-11-29T17:40:00.001+08:002013-12-03T11:35:19.279+08:00No. 49 - Longest Substring without Duplication<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<b><u>Problem:</u></b>
Given a string, please get the length of the longest substring which does not
have duplicated characters. Supposing all characters in the string are in the
range from ‘a’ to ‘z’.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><u>Analysis:</u></b>
It’s not difficult to get all substrings of a string, and to check whether a
substring has duplicated characters. The only concern about this brute-force
strategy is performance. A string with <i>n</i>
characters has O(<i>n<sup>2</sup></i>)
substrings, and it costs O(<i>n</i>) time to
check whether a substring has duplication. Therefore, the overall cost is O(<i>n<sup>3</sup></i>).<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
We may improve the efficiency with dynamic programming. Let’s
denote the length of longest substring ending with the <i>i<sup>th</sup></i> character by <i>L</i>(<i>i</i>). <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
We scan the string one character after another. When the <i>i<sup>th</sup></i> character is scanned, <i>L</i>(<i>i</i>-1)
is already know. If the <i>i<sup>th</sup></i>
character has not appeared before, <i>L</i>(<i>i</i>) should be <i>L</i>(<i>i</i>-1)+1. It’s more
complex when the <i>i<sup>th</sup></i>
character is duplicated. Firstly we get the distance between the <i>i<sup>th</sup></i> character and its
previous occurrence. If the distance is greater than <i>L</i>(<i>i</i>-1), the character is
not in longest substring without duplication ending with the (<i>i</i>-1)<i><sup>th</sup></i>
character, so <i>L</i>(<i>i</i>) should also be <i>L</i>(<i>i</i>-1)+1. If the distance is less than <i>L</i>(<i>i</i>-1),
<i>L</i>(<i>i</i>)
is the distance, and it means between the two occurrence of the <i>i<sup>th</sup></i> character there are no
other duplicated characters. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This solution can be implemented in Java as the following
code:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> longestSubstringWithoutDuplication(String str) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> curLength = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxLength = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> position[] = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[26];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> i = 0; i < 26; ++i) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> position[i] = -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> i = 0; i < str.length(); ++i) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> prevIndex = position[str.charAt(i) - </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'a'</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(prevIndex < 0 || i - prevIndex > curLength) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> ++curLength;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">else</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(curLength > maxLength) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxLength = curLength;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> curLength = i - prevIndex;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> position[str.charAt(i) - </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'a'</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">] = i;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(curLength > maxLength) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxLength = curLength;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> maxLength;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<i>L</i>(<i>i</i>) is implemented as <span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 107%;">curLength</span> in the code above. An
integer array is used to store the positions of each character. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: 13.5pt; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: #222222; font-family: "Arial","sans-serif"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">Code with unit tests
is shared at </span><a href="http://ideone.com/CmY3xN">http://ideone.com/CmY3xN</a><span style="color: #222222; font-family: "Arial","sans-serif"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">.<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; line-height: 13.5pt; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background: white; line-height: 13.5pt; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: inherit;"><span style="color: #222222; font-size: 10.0pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">More coding interview questions are discussed in my book
<Coding Interviews: Questions, Analysis & Solutions>. You may find
the details of this book on </span><span style="color: #222222; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";"><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span style="color: #2288bb; text-decoration: none; text-underline: none;">Amazon.com</span></a></span><span style="color: #222222; font-size: 10.0pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">, or </span><span style="color: #222222; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";"><a href="http://www.apress.com/9781430247616"><span style="color: #2288bb; text-decoration: none; text-underline: none;">Apress</span></a></span><span style="color: #222222; font-size: 10.0pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #222222; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></span></div>
<span style="font-family: inherit;"><br /></span>
<br />
<div class="MsoNormal" style="background: white; line-height: 13.5pt; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: inherit;"><span style="background-position: initial initial; background-repeat: initial initial; color: #222222; font-size: 10pt;">The
author Harry He owns all the rights of this post. If you are going to use part
of or the whole of this ariticle in your blog or webpages, please add a
reference to </span><span style="background-position: initial initial; background-repeat: initial initial; color: #222222; font-size: 10pt;"><a href="http://codercareer.blogspot.com/"><span style="color: #2288bb; text-decoration: none; text-underline: none;">http://codercareer.blogspot.com/</span></a></span><span style="background-position: initial initial; background-repeat: initial initial; color: #222222; font-size: 10pt;">.
If you are going to use it in your books, please contact him via
zhedahht@gmail.com . Thanks.</span></span><span style="color: #222222; font-family: "Arial","sans-serif"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com14tag:blogger.com,1999:blog-4228859841453048938.post-54799939697149216372013-11-28T17:19:00.001+08:002013-11-28T17:46:28.692+08:00No. 48 - Least Number after Deleting Digits<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<b><u>Problem: </u></b>Please
get the least number after deleting <i>k</i>
digits from the input number. For example, if the input number is 24635, the
least number is 23 after deleting 3 digits.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
<b><u>Analysis: </u></b>Let’s
delete a digit from the number at each step. What’s the first digit to be
deleted from the number 24635, in order to get the least number with the
remaining digits? We may list all the remaining numbers after deleting a digit,
in the following table:<o:p></o:p><br />
<br /></div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTable15Grid4Accent1" style="border-collapse: collapse; border: none; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="background: #5B9BD5; border-right: none; border: solid #5B9BD5 1.0pt; mso-background-themecolor: accent1; mso-border-bottom-alt: solid #5B9BD5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-left-alt: solid #5B9BD5 .5pt; mso-border-left-themecolor: accent1; mso-border-themecolor: accent1; mso-border-top-alt: solid #5B9BD5 .5pt; mso-border-top-themecolor: accent1; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: white; mso-themecolor: background1;">Deleted
Digit<o:p></o:p></span></b></div>
</td>
<td style="background: #5B9BD5; border-left: none; border: solid #5B9BD5 1.0pt; mso-background-themecolor: accent1; mso-border-bottom-alt: solid #5B9BD5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-right-alt: solid #5B9BD5 .5pt; mso-border-right-themecolor: accent1; mso-border-themecolor: accent1; mso-border-top-alt: solid #5B9BD5 .5pt; mso-border-top-themecolor: accent1; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: white; mso-themecolor: background1;">Remaining
Number<o:p></o:p></span></b></div>
</td>
</tr>
<tr>
<td style="background: #DEEAF6; border-top: none; border: solid #9CC2E5 1.0pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
2<o:p></o:p></div>
</td>
<td style="background: #DEEAF6; border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
4635<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid #9CC2E5 1.0pt; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
4<o:p></o:p></div>
</td>
<td style="border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
2635<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="background: #DEEAF6; border-top: none; border: solid #9CC2E5 1.0pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
6<o:p></o:p></div>
</td>
<td style="background: #DEEAF6; border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
2435<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid #9CC2E5 1.0pt; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
3<o:p></o:p></div>
</td>
<td style="border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
2465<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="background: #DEEAF6; border-top: none; border: solid #9CC2E5 1.0pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
5<o:p></o:p></div>
</td>
<td style="background: #DEEAF6; border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
2463<o:p></o:p></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal">
<br />
The number 2435 is the least one in all remaining numbers,
by deleting the digit 6. Notice that the digit 6 is the first digit in the
number 24635 which is greater than the next digit.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
Let’s delete another digit from the number 2435, the
remaining least number after the first step. We may summarize the remaining
numbers after delete every digit from it in the following table:<o:p></o:p><br />
<br /></div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTable15Grid4Accent1" style="border-collapse: collapse; border: none; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="background: #5B9BD5; border-right: none; border: solid #5B9BD5 1.0pt; mso-background-themecolor: accent1; mso-border-bottom-alt: solid #5B9BD5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-left-alt: solid #5B9BD5 .5pt; mso-border-left-themecolor: accent1; mso-border-themecolor: accent1; mso-border-top-alt: solid #5B9BD5 .5pt; mso-border-top-themecolor: accent1; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: white; mso-themecolor: background1;">Deleted
Digit<o:p></o:p></span></b></div>
</td>
<td style="background: #5B9BD5; border-left: none; border: solid #5B9BD5 1.0pt; mso-background-themecolor: accent1; mso-border-bottom-alt: solid #5B9BD5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-right-alt: solid #5B9BD5 .5pt; mso-border-right-themecolor: accent1; mso-border-themecolor: accent1; mso-border-top-alt: solid #5B9BD5 .5pt; mso-border-top-themecolor: accent1; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: white; mso-themecolor: background1;">Remaining
Number<o:p></o:p></span></b></div>
</td>
</tr>
<tr>
<td style="background: #DEEAF6; border-top: none; border: solid #9CC2E5 1.0pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
2<o:p></o:p></div>
</td>
<td style="background: #DEEAF6; border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
435<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid #9CC2E5 1.0pt; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
4<o:p></o:p></div>
</td>
<td style="border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
235<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="background: #DEEAF6; border-top: none; border: solid #9CC2E5 1.0pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
3<o:p></o:p></div>
</td>
<td style="background: #DEEAF6; border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
245<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid #9CC2E5 1.0pt; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
5<o:p></o:p></div>
</td>
<td style="border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
243<o:p></o:p></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal">
<br />
The number 235 is the least one in all remaining numbers, by
deleting the digit 4. Notice that the digit 4 is the first digit in the number
2435 which is greater than the next digit.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
The remaining three digits in the number 235 are
increasingly sorted. What is the next digit to be deleted to get the least
remaining number? Again, we may list the remaining numbers after deleting each
digit in a table:<o:p></o:p><br />
<br /></div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTable15Grid4Accent1" style="border-collapse: collapse; border: none; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="background: #5B9BD5; border-right: none; border: solid #5B9BD5 1.0pt; mso-background-themecolor: accent1; mso-border-bottom-alt: solid #5B9BD5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-left-alt: solid #5B9BD5 .5pt; mso-border-left-themecolor: accent1; mso-border-themecolor: accent1; mso-border-top-alt: solid #5B9BD5 .5pt; mso-border-top-themecolor: accent1; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: white; mso-themecolor: background1;">Deleted
Digit<o:p></o:p></span></b></div>
</td>
<td style="background: #5B9BD5; border-left: none; border: solid #5B9BD5 1.0pt; mso-background-themecolor: accent1; mso-border-bottom-alt: solid #5B9BD5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-right-alt: solid #5B9BD5 .5pt; mso-border-right-themecolor: accent1; mso-border-themecolor: accent1; mso-border-top-alt: solid #5B9BD5 .5pt; mso-border-top-themecolor: accent1; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: white; mso-themecolor: background1;">Remaining
Number<o:p></o:p></span></b></div>
</td>
</tr>
<tr>
<td style="background: #DEEAF6; border-top: none; border: solid #9CC2E5 1.0pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
2<o:p></o:p></div>
</td>
<td style="background: #DEEAF6; border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
35<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid #9CC2E5 1.0pt; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
3<o:p></o:p></div>
</td>
<td style="border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
25<o:p></o:p></div>
</td>
</tr>
<tr>
<td style="background: #DEEAF6; border-top: none; border: solid #9CC2E5 1.0pt; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 2.25in;" valign="top" width="216"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
5<o:p></o:p></div>
</td>
<td style="background: #DEEAF6; border-bottom: solid #9CC2E5 1.0pt; border-left: none; border-right: solid #9CC2E5 1.0pt; border-top: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #9CC2E5 .5pt; mso-border-bottom-themecolor: accent1; mso-border-bottom-themetint: 153; mso-border-left-alt: solid #9CC2E5 .5pt; mso-border-left-themecolor: accent1; mso-border-left-themetint: 153; mso-border-right-themecolor: accent1; mso-border-right-themetint: 153; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9CC2E5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153; padding: 0in 5.4pt 0in 5.4pt; width: 193.5pt;" valign="top" width="258"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
23<o:p></o:p></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal">
<br />
The number 23 is the least one in all remaining numbers, by
deleting the last digit 5.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
If we are going to deleting more digits from a number whose
digits are increasingly sorted to get the least number, the last digit is
deleted at each step.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
Now we get the rules to delete digits to get the least
remaining number: If there are digits who are greater than the next one,
delete the first digit. If all digits in the number are increasingly sorted,
delete the last digit gets deleted. The process repeats until the required <i>k</i> digits are deleted.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
The code can be implemented in Java as the following:<o:p></o:p><br />
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> String getLeastNumberDeletingDigits_1(String number, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> k) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> String leastNumber = number;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">while</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(k > 0 && leastNumber.length() > 0) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> firstDecreasingDigit = getFirstDecreasing(leastNumber);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(firstDecreasingDigit >= 0) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> leastNumber =
removeDigit(leastNumber, firstDecreasingDigit);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">else</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> leastNumber =
removeDigit(leastNumber, leastNumber.length() - 1);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> --k;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> leastNumber;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> getFirstDecreasing(String number) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> i = 0; i < number.length() - 1; ++i) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> curDigit = number.charAt(i) - </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'0'</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> nextDigit = number.charAt(i + 1) - </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'0'</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(curDigit > nextDigit) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> i;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> String removeDigit(String number, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> digitIndex) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> String result = </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">""</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(digitIndex > 0) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result = number.substring(0,
digitIndex);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(digitIndex < number.length() - 1) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result += number.substring(digitIndex +
1);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result;</span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 107%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 107%;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">
<div class="MsoNormal">
<b><i>Optimization: Save the start index for the next round of search for
the first decreasing digit<o:p></o:p></i></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In the method <span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt; line-height: 107%;">getFirstDecreasing</span>
above to get the first digit which is greater than the next one, we always start
from the first digit. Is it necessary to start over in every round of search?<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The answer is no. If the <i>i<sup>th</sup></i>
digit is the first digit which is greater than the next one, all digits before
the <i>i<sup>th</sup></i> digit are
increasingly sorted. The (<i>i-1)<sup>th</sup></i>
digit might be less than the (<i>i+1)<sup>th</sup></i>
digit, the next digit of the (<i>i-1)<sup>th</sup></i>
digit after the <i>i<sup>th</sup></i> digit
is deleted. Therefore, it is safe to start from the (<i>i-1)<sup>th</sup></i> digit in the next round of search.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
With this optimization strategy, the efficiency gets
improved from O(<i>n</i>*<i>k</i>) to O(<i>n</i>), if the length of the input number has <i>n</i> digits and <i>k</i> digits are
deleted.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The optimized solution can be implemented as:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">class</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 9.5pt;">DecreasingResult</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> firstDecreasing;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> nextStart;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">static</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> String getLeastNumberDeletingDigits_2(String number, </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> k) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> String leastNumber = number;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> start = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">while</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">(k > 0 && leastNumber.length() > 0) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">
DecreasingResult result = getNextDecreasing(leastNumber, start);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">if</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">(result.firstDecreasing >= 0) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> leastNumber =
removeDigit(leastNumber, result.firstDecreasing);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">else</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> leastNumber = removeDigit(leastNumber,
leastNumber.length() - 1);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> start = result.nextStart;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> --k;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">return</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> leastNumber;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">static</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> DecreasingResult getNextDecreasing(String number, </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> start) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> firstDecreasing = -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> nextStart;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">for</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">(</span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> i = start; i < number.length() - 1; ++i) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> curDigit = number.charAt(i) - </span><span style="background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 9.5pt;">'0'</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> nextDigit = number.charAt(i + 1) - </span><span style="background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 9.5pt;">'0'</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">if</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">(curDigit > nextDigit) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> firstDecreasing = i;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">break</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">if</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;">(firstDecreasing == 0) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> nextStart = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">else</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">if</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> (firstDecreasing > 0) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> nextStart = firstDecreasing - 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">else</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> nextStart = number.length();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> DecreasingResult result = </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> DecreasingResult();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> result.firstDecreasing = firstDecreasing;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> result.nextStart = nextStart;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 9.5pt;">return</span><span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt;"> result;</span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt; line-height: 107%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 9.5pt; line-height: 107%;"><br /></span></div>
Code with unit tests is shared at <a href="http://ideone.com/0Mdfcf" style="background-color: transparent;">http://ideone.com/0Mdfcf</a>.</div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">
<span style="font-family: inherit;">More coding interview questions are discussed in my book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888; text-decoration: none;"><span style="color: #2288bb; font-family: inherit;">Amazon.com</span></a><span style="font-family: inherit;">, or </span><a href="http://www.apress.com/9781430247616" style="color: #888888; text-decoration: none;"><span style="color: #2288bb; font-family: inherit;">Apress</span></a><span style="font-family: inherit;">.</span></div>
<div class="MsoNormal">
<span style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;"><br /><span style="font-family: inherit;">The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/" style="color: #888888; text-decoration: none;"><span style="color: #2288bb; font-family: inherit;">http://codercareer.blogspot.com/</span></a><span style="font-family: inherit;">. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.</span></span></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com20tag:blogger.com,1999:blog-4228859841453048938.post-18440990510584116532013-03-31T17:01:00.001+08:002013-04-19T08:27:00.918+08:00No. 47 - Search in a Rotation of an Array<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Question:</u></b> When
some elements at the beginning of an array are moved to the end, it gets a
rotation of the original array. Please implement a function to search a number
in a rotation of an increasingly sorted array. Assume there are no duplicated
numbers in the array.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">For example, array {3, 4, 5, 1, 2} is a rotation of array {1,
2, 3, 4, 5}. If the target number to be searched is 4, the index of the number
4 in the rotation 1 should be returned. If the target number to be searched is
6, -1 should be returned because the number does not exist in the rotated
array.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Analysis:</u></b> Binary
search is suitable for sorted arrays. Let us try to utilize it on a rotation of
a sorted array. Notice that a rotation of a sorted array can be partitioned
into two sorted sub-arrays, and numbers in the first sub-array are greater than
numbers in the second one. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Two pointers P1 and P2 are utilized. P1 references to the first
element in the array, and P2 references to the last element. According to the
rotation rule, the first element should be greater than the last one.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">The algorithm always compares the number in middle with numbers
pointed by P1 and P2 during binary search. If the middle number is in the first
increasingly sorted sub-array, it is greater than the number pointed by P1. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">If the value of target number to be search is between the
number pointed by P1 and the middle number, we then search the target number in
the first half sub-array. In such a case the first half sub-array is in the
first increasing sub-array, we could utilize the binary search algorithm. For
example, if we search the number 4 in a rotation {3, 4, 5, 1, 2}, we could
search the target number 4 in the sub-array {3, 4, 5} because 4 is between the
first number 3 and the middle number 5.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">If the value of target number is not between the number
pointed by P1 and the middle number, we search the target in the second half
sub-array. Notice that the second half sub-array also contains two increasing
sub-array and itself is also a rotation, so we could search recursively with
the same strategy. For example, if we search the number 1 in a rotation {3, 4,
5, 1, 2}, we could search the target number 1 in the sub-array {5, 1, 2} recursively.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">The analysis above is for two cases when the middle number
is in the first increasing sub-array. Please analyze the other two cases when
the middle number is in the second increasing sub-array yourself, when the
middle number is less than the number pointed by P2. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">The code implementing this algorithm is listed below, in C/C++:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">int</span><span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">
searchInRotation(<span style="color: blue;">int</span> numbers[], <span style="color: blue;">int</span> length, <span style="color: blue;">int</span> k)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(numbers == NULL || length <= 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span>
-1;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span>
searchInRotation(numbers, k, 0, length - 1); <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">int</span><span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">
searchInRotation(<span style="color: blue;">int</span> numbers[], <span style="color: blue;">int</span> k, <span style="color: blue;">int</span> start, <span style="color: blue;">int</span> end)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(start
> end)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span>
-1;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> middle
= start + (end - start) / 2;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(numbers[middle]
== k)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span>
middle;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// the middle
number is in the first increasing sub-array<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(numbers[middle]
>= numbers[start])<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(k
>= numbers[start] && k < numbers[middle])<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span>
binarySearch(numbers, k, start, middle - 1);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span>
searchInRotation(numbers, k, middle + 1, end);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// the middle
number is in the second increasing sub-array<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">else</span> <span style="color: blue;">if</span>(numbers[middle] <= numbers[end])<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(k
> numbers[middle] && k <= numbers[end])<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span>
binarySearch(numbers, k, middle + 1, end);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span>
searchInRotation(numbers, k, start, middle - 1);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// It should
never reach here if the input is valid<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>assert(<span style="color: blue;">false</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Since the function binarySearch is for the classic binary
search algorithm, it is not listed here. You might implement your own binary
search code if you are interested.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">In each round of search, half of the array is excluded for
the next round, so the time complexity is O(log<i style="mso-bidi-font-style: normal;">n</i>).<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">You may wonder why we assume there are no duplications in the
input array. We determine whether the middle number is in the first or second sub-array
by comparing the middle number and the numbers pointed by P1 or P2. When the
middle number, the number pointed by P1 and P2 are identical, we don’t know
whether the middle number is in the first or second increasing sub-array.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Let’s look at some examples. Two arrays {1, 0, 1, 1, 1} and {1,
1, 1, 0, 1} are both rotations of an increasingly sorted array {0, 1, 1, 1, 1},
which are visualized in Figure 1.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt; page-break-after: avoid;">
<span style="mso-no-proof: yes;"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f"><span style="font-family: inherit;">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></span></v:shapetype><span style="font-family: Calibri;"></span></span></div>
<span style="font-family: Calibri;"></span><br />
<span style="font-family: Calibri;"></span><br />
<span style="font-family: Calibri;"></span><br />
<div class="separator" style="clear: both; text-align: center;">
<span style="font-family: Calibri;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWK_S2Yjr_ohX63PvplETLX6-CWJE2NVygG0K8wnbS2OhsFOX6CnEKD2dOysZSi-_f8mVn0diUKLlxgIrLDcEWKd22C3QTN4JLZaOQlhnZF5R2xW_KozU7yXVEAaOhLZqnl81sZYXLlqM/s1600/No+47.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="77" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWK_S2Yjr_ohX63PvplETLX6-CWJE2NVygG0K8wnbS2OhsFOX6CnEKD2dOysZSi-_f8mVn0diUKLlxgIrLDcEWKd22C3QTN4JLZaOQlhnZF5R2xW_KozU7yXVEAaOhLZqnl81sZYXLlqM/s320/No+47.PNG" width="320" /></a></span></div>
<span style="font-family: Calibri;">
</span><o:p></o:p>
<div class="MsoCaption" style="margin: 0in 0in 10pt;">
<b><span style="font-size: x-small;"><span style="color: #4f81bd;"><span style="font-family: Calibri;">Figure <span style="mso-no-proof: yes;">1</span>: Two rotations of an increasingly sorted array {0,
1, 1, 1, 1}: {1, 0, 1, 1, 1} and {1, 1, 1, 0, 1}. Elements with gray background
are in the second increasing sub-array.<o:p></o:p></span></span></span></b></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">In Figure 1, the elements pointed by P1 and P2, as well as
the middle element are all 1. The middle element with index 2 is in the second
sub-array in Figure 1 (a), while the middle element is in the first sub-array
in Figure 1 (b).<o:p></o:p></span></div>
<span style="font-family: inherit; font-size: 11pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: 宋体; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;">Since we can’t determine whether the middle
number in the first or second increasing sub-array, we have to search
sequentially for such cases, and our code listed above should be revised.</span><br />
<span style="font-family: inherit;"></span><br />
<div class="MsoNormal" style="background-color: white; color: #222222; line-height: 18px;">
<span style="font-family: inherit;">More coding interview questions are discussed in my
book <Coding Interviews: Questions, Analysis & Solutions>. You may
find the details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888;"><span style="color: #2288bb; font-family: inherit;">Amazon.com</span></a><span style="font-family: inherit;">, or
</span><a href="http://www.apress.com/9781430247616" style="color: #888888;"><span style="color: #2288bb; font-family: inherit;">Apress</span></a><span style="font-family: inherit;">.</span></div>
<span style="background-color: white; color: #222222; line-height: 18px;"><br /><span style="font-family: inherit;">The author Harry He owns all the rights of this
post. If you are going to use part of or the whole of this ariticle in your blog
or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/" style="color: #888888;"><span style="color: #2288bb; font-family: inherit;">http://codercareer.blogspot.com/</span></a><span style="font-family: inherit;">. If you are
going to use it in your books, please contact him via zhedahht@gmail.com .
Thanks.</span></span></div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com12tag:blogger.com,1999:blog-4228859841453048938.post-31279946964042781092013-03-10T21:55:00.000+08:002013-03-10T23:06:14.522+08:00No. 46 - Nodes with Sum in a Binary Search Tree<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Problem:</u></b>
Given a binary search tree, please check whether there are two nodes in it
whose sum equals a given value.<o:p></o:p></span><br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3J-jwAjgDMhz2Uuc_9L1VHT6czFpMmE6cV3er1-vCedodANuD_wlZrQWo-4kOOAD7RBrbdPngEOpi3poNRpJp-1GguuJgCQVCqTUzIV46GcQRTHpbZYZkI_b_1kdp1XpA8fc8N4lcAFk/s1600/No45.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3J-jwAjgDMhz2Uuc_9L1VHT6czFpMmE6cV3er1-vCedodANuD_wlZrQWo-4kOOAD7RBrbdPngEOpi3poNRpJp-1GguuJgCQVCqTUzIV46GcQRTHpbZYZkI_b_1kdp1XpA8fc8N4lcAFk/s1600/No45.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><span style="font-family: inherit;"><span style="font-family: "Calibri","sans-serif"; font-size: 11pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;">Figure
</span><span style="font-family: "Calibri","sans-serif"; font-size: 11pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;"><span style="mso-no-proof: yes;">1</span></span><span style="font-family: "Calibri","sans-serif"; font-size: 11pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;">: A sample binary search tree</span></span></td></tr>
</tbody></table>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">For example, if the given sum is 66, there are two nodes in
Figure 1 with value 25 and 41 whose sum is 66. While the given sum is 58, there
are not two nodes whose sum is same as the given value.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Analysis:</u></b>
In </span><a href="http://codercareer.blogspot.com/2011/10/no-09-numbers-with-given-sum.html"><span style="color: blue; font-family: inherit;">a
previous blog</span></a><span style="font-family: inherit;">, we discussed how to check whether there are two numbers in a
sorted array whose sum equals a given value. In order to solve this problem, we
initialize a number <i style="mso-bidi-font-style: normal;">num1</i> as the
smallest number in the array, and initialize <i style="mso-bidi-font-style: normal;">num2</i> as the greatest one. If the sum of <i style="mso-bidi-font-style: normal;">num1</i> and <i style="mso-bidi-font-style: normal;">num2</i> is same as
the given value, two required numbers have been found. If the sum is greater
than the given value, we move <i style="mso-bidi-font-style: normal;">num2</i> to
the previous number in the array (with less value). Similarly, we move <i style="mso-bidi-font-style: normal;">num1</i> to the next number in the array
(with greater value) if the sum is less than the given value.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Inspired by the solution, we may find two nodes with a given
sum in a binary search tree with similar strategy. Firstly we initialize a
pointer <i style="mso-bidi-font-style: normal;">P1</i> to the smallest node in
the tree, and another pointer <i style="mso-bidi-font-style: normal;">P2</i> to
the greatest node. When the sum of two values in the nodes pointed by <i style="mso-bidi-font-style: normal;">P1</i> and <i style="mso-bidi-font-style: normal;">P2</i> is same as the given value, two required nodes have been found.
If their sum is greater than the given value, we move <i style="mso-bidi-font-style: normal;">P2</i> to the previous node (containing less value). Moreover, we move <i style="mso-bidi-font-style: normal;">P1</i> to the next node (containing greater
value) if their sum is less than the given value.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">It is not difficult to get the least and greatest value of a
binary search tree. If we move along the links to left children, and the leaf
node we arrive at finally contains the least value. For instance, if we move
along the links to left children in Figure 1, the nodes on the path contain
value 32, 24, 21 and 14, and 14 is the least value in the binary search tree.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Similarly, if we move along links to right children, and the
leaf node we arrive at finally contains the greatest value.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">The key to solve this problem is how to get the next nodes
(with greater values) and the previous nodes (with less values) in a binary
search tree. Let’s utilize stacks.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">In order to get next nodes, we scan the tree along the links
to leaf children, and push the scanned nodes into a stack. That’s to say, there
are four nodes in the stack (nodes 32, 24, 21 and 14), and node 14 is on the
top. <o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">When the top of the stack is popped, node 21 on the top of
stack is the next node of 14. And when the node 21 is popped, the node 24 on
the top is the next node of 21.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">The steps to get the next node of node 24 are more complex,
because the node 24 has a right child. We pop the node 24, and push its right
child, node 28, into the stack. And then, we scan the nodes from the right
child along the links to left children. Therefore, the node 31 will also be
pushed into the stack. At this time, there are three nodes in the stack (nodes
32, 28 and 25), and the top on the stack (node 25) is the next node of node 24.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Let’s summarize the steps to get next nodes. If the node on the
top does not have a right child, the node is popped off, and the next node is
on the top again. If the node on the top has a right child, after the node is
popped off and then the right child is pushed, and all nodes along the links to
left children are pushed into the stack. The last pushed node on the top is the
next node. We continue the steps to pop and push until the stack is empty.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">If you are interested, please try to analyze the steps to
the next nodes after the node 25 by yourself.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">The steps to get previous nodes are quite similar. At first
we push the root node, and nodes along the links to right children. The node
with least is on the top of stack. In order to get the previous nodes with less
values, the top is popped off. If the top does not have a left child, the
previous node is the new top node in the stack. If the top has the left
child, we push its left child, as well as nodes along links to right children.
The last pushed node is the previous node of the node popped.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Our solution can be implemented with the following C++ code:<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">bool</span><span style="font-family: Consolas; font-size: 9.5pt;"> hasTwoNodes(BinaryTreeNode* pRoot, <span style="color: blue;">int</span> sum)<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>stack<BinaryTreeNode*> nextNodes, prevNodes;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>buildNextNodes(pRoot, nextNodes);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>buildPrevNodes(pRoot, prevNodes);<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>BinaryTreeNode* pNext = getNext(nextNodes);<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>BinaryTreeNode* pPrev = getPrev(prevNodes);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">while</span>(pNext != NULL && pPrev != NULL
&& pNext != pPrev)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> currentSum = pNext->m_nValue +
pPrev->m_nValue;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(currentSum == sum)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">true</span>;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(currentSum < sum)<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>pNext = getNext(nextNodes);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">else</span><o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>pPrev = getPrev(prevNodes);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> buildNextNodes(BinaryTreeNode* pRoot,
stack<BinaryTreeNode*>& nodes)<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>BinaryTreeNode* pNode = pRoot;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">while</span>(pNode != NULL)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>nodes.push(pNode);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>pNode
= pNode->m_pLeft;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> buildPrevNodes(BinaryTreeNode* pRoot,
stack<BinaryTreeNode*>& nodes)<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>BinaryTreeNode* pNode = pRoot;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">while</span>(pNode != NULL)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>nodes.push(pNode);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>pNode
= pNode->m_pRight;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">BinaryTreeNode* getNext(stack<BinaryTreeNode*>& nodes)<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>BinaryTreeNode* pNext = NULL;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(!nodes.empty())<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>pNext
= nodes.top();<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>nodes.pop();<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>BinaryTreeNode* pRight = pNext->m_pRight;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">while</span>(pRight != NULL)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>nodes.push(pRight);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>pRight = pRight->m_pLeft;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> pNext;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">BinaryTreeNode* getPrev(stack<BinaryTreeNode*>& nodes)<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>BinaryTreeNode* pPrev = NULL;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(!nodes.empty())<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>pPrev
= nodes.top();<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>nodes.pop();<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>BinaryTreeNode* pLeft = pPrev->m_pLeft;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">while</span>(pLeft != NULL)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>nodes.push(pLeft);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>pLeft = pLeft->m_pRight;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> pPrev;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">This solution costs O(<i style="mso-bidi-font-style: normal;">n</i>)
time. The space complexity is O(height of tree), which is O(log<i style="mso-bidi-font-style: normal;">n</i>) on average, and O(<i style="mso-bidi-font-style: normal;">n</i>) in the worst cases.<o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;"><div class="MsoNormal" style="background-color: white; color: #222222; line-height: 18px;">
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">More coding interview questions are discussed in my
book <Coding Interviews: Questions, Analysis & Solutions>. You may
find the details of this book on </span></span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888;"><span style="color: #2288bb;"><span style="font-family: inherit;"><span style="font-family: inherit;">Amazon.com</span></span></span></a><span style="font-family: inherit;"><span style="font-family: inherit;">, or </span></span><a href="http://www.apress.com/9781430247616" style="color: #888888;"><span style="color: #2288bb;"><span style="font-family: inherit;"><span style="font-family: inherit;">Apress</span></span></span></a><span style="font-family: inherit;"><span style="font-family: inherit;">.</span></span></span></div>
<span style="background-color: white; color: #222222; line-height: 18px;"><span style="font-family: inherit;"><br /><span style="font-family: inherit;"><span style="font-family: inherit;">The author
Harry He owns all the rights of this post. If you are going to use part of or
the whole of this ariticle in your blog or webpages, please add a reference to
</span></span><a href="http://codercareer.blogspot.com/" style="color: #888888;"><span style="color: #2288bb;"><span style="font-family: inherit;"><span style="font-family: inherit;">http://codercareer.blogspot.com/</span></span></span></a><span style="font-family: inherit;"><span style="font-family: inherit;">. If you are going to use it in your books, please
contact him via zhedahht@gmail.com . Thanks.</span></span></span></span></span><br /></div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com26tag:blogger.com,1999:blog-4228859841453048938.post-41583607268335952332013-03-02T22:29:00.003+08:002013-03-02T22:30:53.209+08:00No. 45 - Closest Node in a Binary Search Tree<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Problem:</u></b>
Given a binary search tree and a value <i style="mso-bidi-font-style: normal;">k</i>,
please find a node in the binary search tree whose value is closest to <i style="mso-bidi-font-style: normal;">k</i>. </span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">For instance, in the binary search tree in Figure 1, the
node closest to 29 is the node with value 28.<o:p></o:p></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeEvYIomkqDCmFxxQrD9QEXYnMkfXCAIpghf05i5xi2CNS9OMrBg5_AMsTvMwdMSW6vvp8LAMqfOWxza7AMFMTPp7Y1ZpyZ5ZaxnPzQztHHqaTiMRHdTBMPl2xW7KDLKqSep-_1_Zr_Zg/s1600/No45.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeEvYIomkqDCmFxxQrD9QEXYnMkfXCAIpghf05i5xi2CNS9OMrBg5_AMsTvMwdMSW6vvp8LAMqfOWxza7AMFMTPp7Y1ZpyZ5ZaxnPzQztHHqaTiMRHdTBMPl2xW7KDLKqSep-_1_Zr_Zg/s1600/No45.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: A sample binary search tree, in which the closest node to 29 is the node with value 28</td></tr>
</tbody></table>
<span style="font-family: inherit;">
</span><br />
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;"><div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Analysis:</u></b>
Let’s analyze this problem step by step, taking the binary search in Figure 1
and 29 as an example. </span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">We start from the root node with value 32, and the distance
to 29 is 3. Since the value 32 is greater than 29, and all values in the right
sub-tree are greater than 32, distances to 29 in the right sub-tree should be
greater than 3. We move to the left sub-tree.</span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">The next node to be visited contains value 24, and the
distance to 29 is 5. Since 5 is greater than the previous closest distance 3,
the closest node up to now remains the node with value 32. Additionally, the
current value 24 is less than 29, and all values in the left sub-tree are less
than 24, so distances to 29 in the left sub-tree will be greater than 5. We
move on to visit the right sub-tree.</span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">The next node to be visited contains value 28, and the
distance to 29 is 1. Since 1 is less than the previous closest distance 3, the
closest node is updated to the node with value 28. Additionally, the value 28 is less
than 29, and all values in the left sub-tree areless than 28, so distances to 29
in the left sub-tree will be greater than 1. Let’s continue to visit the right
sub-tree.</span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Finally we reach a left node with value 31. The distance to
29 is 2 and it is greater than the previous closest distance 1, so the closest node to 29 is still the node with
value 28.</span></div>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">According to the step-by-step analysis above, we could
implement the following code:<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">BinaryTreeNode* getClosestNode(BinaryTreeNode*
pRoot, <span style="color: blue;">int</span> value)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>BinaryTreeNode*
pClosest = NULL;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> minDistance = 0x7FFFFFFF;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>BinaryTreeNode* pNode = pRoot;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">while</span>(pNode != NULL)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> distance = abs(pNode->m_nValue - value);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(distance < minDistance)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>minDistance = distance;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>pClosest = pNode;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(distance == 0)<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">break</span>;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(pNode->m_nValue > value)<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>pNode = pNode->m_pLeft;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">else</span> <span style="color: blue;">if</span>(pNode->m_nValue
< value)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>pNode = pNode->m_pRight;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> pClosest;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">}</span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"></span> </span><br />
<div class="MsoNormal" style="background-color: white; color: #222222; line-height: 18px;">
<span style="font-family: inherit;"><span style="font-family: inherit;">More coding interview
questions are discussed in my book <Coding Interviews: Questions, Analysis
& Solutions>. You may find the details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888;"><span style="color: #2288bb;"><span style="font-family: inherit;">Amazon.com</span></span></a><span style="font-family: inherit;">, or </span><a href="http://www.apress.com/9781430247616" style="color: #888888;"><span style="color: #2288bb;"><span style="font-family: inherit;">Apress</span></span></a><span style="font-family: inherit;">.</span></span></div>
<span style="background-color: white; color: #222222; line-height: 18px;"><span style="font-family: inherit;"><br /><span style="font-family: inherit;">The author Harry He owns all the rights of this
post. If you are going to use part of or the whole of this ariticle in your blog
or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/" style="color: #888888;"><span style="color: #2288bb;"><span style="font-family: inherit;">http://codercareer.blogspot.com/</span></span></a><span style="font-family: inherit;">. If you are
going to use it in your books, please contact him via zhedahht@gmail.com .
Thanks.</span></span></span></div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com17tag:blogger.com,1999:blog-4228859841453048938.post-41291346691433641812013-02-23T09:43:00.000+08:002013-03-05T16:30:50.533+08:00No. 44 - Dynamic Programming on Stolen Values<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
<b><u>Problem:</u></b> There
are <i>n</i> houses built in a line, each of
which contains some value in it. A thief is going to steal the maximal value in
these houses, but he cannot steal in two adjacent houses because the owner of a
stolen house will tell his two neighbors on the left and right side. What is
the maximal stolen value?<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
For example, if there are four houses with values {6, 1, 2,
7}, the maximal stolen value is 13 when the first and fourth houses are stolen.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><u>Analysis:</u></b> A function <i>f</i>(<i>i</i>) is defined to denote the maximal
stolen value from the first house to the <i>i<sup>th</sup></i>
house, and the value contained in the <i>i<sup>th</sup></i> house is denoted as <i><span style="line-height: 115%;"><span style="font-family: inherit;">v<sub>i</sub></span></span></i>. When the thief reaches the <i>i<sup>th</sup></i>
house, he has two choices: to steal or not. Therefore, <i>f</i>(<i>i</i>) can be defined with
the following equation:</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsxm7leZhpGwQQkJw87Z-jEv4d0PL74-ltvqELYapRBb0xBkzduzhMf-rzogtj4D5n3yOr37dLQbM7sd1E59y3hVQ0a9PI_i8NJWXIMRybwzURJyZ1RbDRz5PmXPjdaQxeqKNNrEXANeI/s1600/No44.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsxm7leZhpGwQQkJw87Z-jEv4d0PL74-ltvqELYapRBb0xBkzduzhMf-rzogtj4D5n3yOr37dLQbM7sd1E59y3hVQ0a9PI_i8NJWXIMRybwzURJyZ1RbDRz5PmXPjdaQxeqKNNrEXANeI/s1600/No44.PNG" /></a></div>
<div class="MsoNormal">
It would be much more efficient to calculate in bottom-up order
than to calculate recursively. It looks like a 1D array with size <i>n</i> is needed, but actually it is only
necessary to cache two values for <i>f</i>(<i>i</i>-1) and <em>f</em>(<i>i</i>-2) to calculate <em>f</em>(<i>i</i>).</div>
<div class="MsoNormal">
<o:p></o:p><br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This algorithm can be implemented with the following C++
code:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> maxStolenValue(<span style="color: blue;">const</span>
vector<<span style="color: blue;">int</span>>& values)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> length = values.size();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(length == 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> value1 = values[0];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(length == 1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> value1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> value2 = max<<span style="color: blue;">int</span>>(values[0],
values[1]);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(length == 2)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> value2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> value;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 2; i <
length; ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> value
= max<<span style="color: blue;">int</span>>(value2, value1 + values[i]);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
value1 = value2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
value2 = value;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> value;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
</div>
<div class="MsoNormal" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">
<span style="font-family: inherit;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; color: #222222; line-height: 18px;">
<span style="font-family: inherit;">More coding interview questions are discussed in my book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on <a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888; text-decoration: initial;"><span style="color: #2288bb;">Amazon.com</span></a>, or <a href="http://www.apress.com/9781430247616" style="color: #888888; text-decoration: initial;"><span style="color: #2288bb;">Apress</span></a>.</span></div>
<span style="background-color: white; color: #222222; line-height: 18px;"><span style="font-family: inherit;"><br />The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to <a href="http://codercareer.blogspot.com/" style="color: #888888; text-decoration: initial;"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a>. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.</span></span></div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com9tag:blogger.com,1999:blog-4228859841453048938.post-7933160117195239512013-02-22T17:51:00.001+08:002013-12-02T09:48:03.415+08:00No. 43 - Minimal Number of Palindromes on a String<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="border-bottom: solid #4F81BD 1.0pt; border: none; mso-border-bottom-themecolor: accent1; mso-element: para-border-div; padding: 0in 0in 4.0pt 0in;">
<b><u>Problem:</u></b> A
string can be partitioned into some substrings, such that each substring is a
palindrome. For example, there are a few strategies to split the string “abbab”
into palindrome substrings, such as: “abba”|”b”, “a”|”b”|”bab” and “a”|”bb”|”a”|”b”.</div>
<div style="border-bottom: solid #4F81BD 1.0pt; border: none; mso-border-bottom-themecolor: accent1; mso-element: para-border-div; padding: 0in 0in 4.0pt 0in;">
<br /></div>
<div class="MsoNormal">
Given a string <i>str</i>,
please get the minimal numbers of splits to partition it into palindromes. The
minimal number of splits to partition the string “abbab” into a set of
palindromes is 1.<o:p></o:p></div>
<div class="MsoNormal">
<b><u><br /></u></b></div>
<div class="MsoNormal">
<b><u>Analysis:</u></b> This
is a typical problem which can be solved by dynamic programming. We have two
strategies to analyze and solve this problem<o:p></o:p></div>
<div class="MsoNormal">
<b><i><br /></i></b></div>
<div class="MsoNormal">
<b><i>Solution 1: Split at any space between two characters<o:p></o:p></i></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Given a substring of <i>str</i>,
starting from the index <i>i</i> and ending
at the index <i>j </i>(denoted as <i>str</i>[<i>i</i>:<i>j</i>]), we define a function <i>f</i>(<i>i</i>,
<i>j</i>) to denote the minimal number of
splits to partition the substring <i>str</i>[<i>i</i>:<i>j</i>]
into a set of palindromes. If the substring is a palindrome itself, we don’t
have to split so <i>f</i>(<i>i</i>, <i>j</i>)
is 0. If the substring is not a palindrome, the substring is split between two
characters <i>k</i> and <i>k</i>+1. <i>f</i>(<i>i</i>, <i>j</i>)=<i> f</i>(<i>i</i>,
<i>k</i>)+<i> f</i>(<i>k</i>+1, <i>j</i>)+1 under such conditions. Therefore, <i>f</i>(<i>i</i>,
<i>j</i>) can be defined with the following
equation:<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhD4PmyRmBdKF-zFEnQKToWLTeAdV-xUe9btLTqbQqmJ8lvTIICBCtasMXIMsMLyadLeduHm4Yj6rSBd2LR3kXKbSE0H5X4ciKToppOX0JRVrnlLNaJL8ccmpX_fG31ds-GYT9XSznDFNg/s1600/Figure+1.PNG" /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The value of <i>f</i>(0, <i>n</i>-1) is the value of the minimal number
of splits to partition <i>str</i> into palindromes,
if <i>n</i> is the length of <i>str</i>.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
If the equation is calculated recursively, its complexity
grows exponentially with the length <i>n</i>.
A better choice is to calculate in bottom-up order with a 2D matrix with size <i>n</i>×<i>n</i>.
The following C++ code implements this solution:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> minSplit_1(<span style="color: blue;">const</span>
string& str)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> length = str.size();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span>* split = <span style="color: blue;">new</span> <span style="color: blue;">int</span>[length * length];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 0; i <
length; ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
split[i * length + i] = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 1; i <
length; ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(<span style="color: blue;">int</span> j = length -
i; j > 0; --j)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> row = length - i - j;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> col = row + i;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(isPalindrome(str, row, col))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
split[row * length + col] = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">int</span> min = 0x7FFFFFFF;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">for</span>(<span style="color: blue;">int</span>
k = row; k < col; ++k)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">int</span> temp1 = split[row * length + k];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">int</span> temp2 = split[(k + 1) * length +
col];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span>(min
> temp1 + temp2 + 1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> min = temp1 + temp2 +
1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
split[row * length + col] = min;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> minSplit = split[length - 1];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">delete</span>[] split;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> minSplit;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal">
<b><i>Solution 2: Split only before a palindrome<o:p></o:p></i></b></div>
<div class="MsoNormal">
<b><i><br /></i></b></div>
<div class="MsoNormal">
We split the string <i>str</i>
with another strategy. Given a substring ending at the index <i>i</i>, <i>str</i>[0,
i], we do not have to split if the substring is a palindrome itself. Otherwise
it is split between two characters at index <i>j</i>
and <i>j</i>+1 only if the substring <i>str</i>[<i>j</i>+1,<i>i</i>] is a palindrome. Therefore, an
equation <i>f</i>(<i>i</i>) can be defined as the following:<o:p></o:p><br />
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3wke7MvqVaN3-NIaPFdm4SR2qAYPG8Cqa7lfq5bVU1kW73qQPMzqtAwsZwXF9V6OgpVdrEAGEljPD8E5kU3dffLnGjNIAsp-pc7XFc24qzqWUC3i0bz8uzmj0J3J-K6Tkwdc_e5oglLY/s1600/Figure+2.PNG" /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The value of <i>f</i>(<i>n</i>-1) is the value of the minimal number
of splits to partition <i>str</i> into palindromes,
if <i>n</i> is the length of <i>str</i>.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
We could utilize a 1D array to solve this equation in
bottom-up order, as listed in the following code:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> minSplit_2(<span style="color: blue;">const</span>
string& str)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> length = str.size();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span>* split = <span style="color: blue;">new</span> <span style="color: blue;">int</span>[length];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 0; i <
length; ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
split[i] = i;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 1; i <
length; ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(isPalindrome(str, 0, i))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
split[i] = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">continue</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(<span style="color: blue;">int</span> j = 0; j <
i; ++j)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(isPalindrome(str, j + 1, i) && split[i]
> split[j] + 1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
split[i] = split[j] + 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> minSplit = split[length - 1];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">delete</span>[] split;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> minSplit;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal">
<b><i>Optimization to verify palindromes:<o:p></o:p></i></b></div>
<div class="MsoNormal">
<b><i><br /></i></b></div>
<div class="MsoNormal">
Usually it costs O(<i>n</i>)
time to check whether a string with length <i>n</i>
is a palindrome, and the typical implementation looks like the following code:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">bool</span><span style="font-family: Consolas; font-size: 9.5pt;"> isPalindrome(<span style="color: blue;">const</span>
string& str, <span style="color: blue;">int</span> begin, <span style="color: blue;">int</span> end)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = begin; i
< end - (i - begin); ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(str[i] != str[end - (i - begin)])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal">
Both solutions above cost O(<i>n</i><sup>3</sup>) time. The first solution contains three nesting
for-loops. The function isPalindrome is inside two nesting for-loops.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
If we could reduce the cost of isPalindrome to O(1), the time
complexity of the second solution would be O(<i>n</i><sup>2</sup>).<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Notice that the substring <i>str</i>[<i>i</i>,<i>j</i>] is a palindrome only if the
characters at index <i>i</i> and <i>j</i>, and <i>str</i>[<i>i</i>+1,<i>j</i>-1] is also a palindrome. We could
build a 2D table accordingly to store whether every substring of <i>str</i> is a palindrome or not during the preprocessing.
With such a table, the function isPalindrome can verify the substring <i>str</i>[<i>i</i>,<i>j</i>] in O(1) time.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="background-color: white; color: #222222; line-height: 18px;">
<span style="font-family: inherit;">More coding interview questions are discussed in my book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on <a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="color: #888888; text-decoration: initial;"><span style="color: #2288bb;">Amazon.com</span></a>, or <a href="http://www.apress.com/9781430247616" style="color: #888888; text-decoration: initial;"><span style="color: #2288bb;">Apress</span></a>.</span></div>
<span style="background-color: white; color: #222222; line-height: 18px;"><span style="font-family: inherit;"><br />The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to <a href="http://codercareer.blogspot.com/" style="color: #888888; text-decoration: initial;"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a>. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.</span></span></div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com10tag:blogger.com,1999:blog-4228859841453048938.post-59686279144266253992013-02-21T13:53:00.000+08:002014-08-14T13:29:29.451+08:00No. 42 - Three Increasing Elements in an Array<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
<b><u>Problem:</u></b>
Given an array of integers A, please find three indexes <i>i</i>, <i>j</i>, <i>k</i>, such that <i>i</i><<i>j</i><<i>k</i> and A[<i>i</i>]<A[<i>j</i>]<A[<i>k</i>].<o:p></o:p></div>
<div class="MsoNormal">
<b><u><br /></u></b></div>
<div class="MsoNormal">
<b><u>Analysis: </u></b>Firstly
let’s analyze this problem with the brute-force solution: All elements in the
array are scanned one by one. When an element is scanned, we take is as A[<i>j</i>], and check whether there is a smaller
element on its left side (A[<i>i</i>]), and
a greater element on its right side (A[<i>k</i>]).
<o:p></o:p></div>
<div class="MsoNormal">
Since it takes O(<i>n</i>)
time to scan numbers on two sides of each A[<i>j</i>],
the overall time of this solution is O(<i>n</i><sup>2</sup>)
on an array with <i>n</i> elements.<o:p></o:p></div>
<div class="MsoNormal">
<b><i><br /></i></b></div>
<div class="MsoNormal">
<b><i>Solution 1: O(n) time efficiency with O(n) space consumption<o:p></o:p></i></b></div>
<div class="MsoNormal">
As our analysis above, for each element A[<i>j</i>], we need to know whether there is a
smaller element on its left side. If there is not a smaller element on the left
side, A[<i>j</i>] itself is the smallest of
all elements from the leftmost element to A[<i>j</i>].
If there is a smaller element on the left side, the smallest of all elements
from the leftmost element to A[<i>j</i>] is
on the left side of A[<i>j</i>].<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
Therefore, we could construct an auxiliary array B. The
element B[<i>j</i>] stores the index of the
smallest element of elements from the leftmost element to A[<i>j</i>]. The array B can be constructed based
on elements in A from left to right.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
Similarly, we could also construct another auxiliary array C.
The element C[<i>j</i>] stores the index of
the greatest element of elements from the rightmost element to A[<i>j</i>]. The array C can be constructed based
on elements in A from right to left.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
When an element A[<i>j</i>]
is scanned, the index <i>j</i> is compared
with B[<i>j</i>] and C[<i>j</i>]. if B[<i>j</i>]<<i>j</i> (there is a smaller element on the
left side) and <i>j</i><C[<i>j</i>] (there is a greater element on the right side), three
increasing elements have been found.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
This solution can be implemented with the following C/C++
code:<o:p></o:p><br />
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> increasingIndex_1(<span style="color: blue;">int</span>*
nums, <span style="color: blue;">int</span> length, <span style="color: blue;">int</span>*
i, <span style="color: blue;">int</span>* j, <span style="color: blue;">int</span>*
k)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(nums == NULL || length <= 0 || i == NULL || j
== NULL || k == NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span>* minIndex = <span style="color: blue;">new</span> <span style="color: blue;">int</span>[length];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> index = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
minIndex[0] = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> t;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(t = 1; t < length; ++t)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(nums[t] < nums[index])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
index = t;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
minIndex[t] = index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span>* maxIndex = <span style="color: blue;">new</span> <span style="color: blue;">int</span>[length];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> index =
length - 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(t = length - 1; t >= 0; --t)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(nums[t] > nums[index])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
index = t;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
maxIndex[t] = index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(t = 1; t < length - 1; ++t)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(minIndex[t] < t && maxIndex[t] > t)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">break</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(t < length - 1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> *i =
minIndex[t];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> *j =
t;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> *k =
maxIndex[t];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> *i =
*j = *k = -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">delete</span>[] minIndex;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">delete</span>[] maxIndex;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<b><i><br /></i></b></div>
<div class="MsoNormal">
<b><i>Solution 2: O(n) time efficiency with O(1) space consumption<o:p></o:p></i></b></div>
<div class="MsoNormal">
In order to find an even more efficient solution, let’s scan
all elements in an array and try to find three increasing elements. The array {3,
2, 5, 1, 4, 7, 9, 6, 8} is taken as an example to simulate the process.<o:p></o:p></div>
<div class="MsoNormal">
<br />
At first, all elements A[<i>i</i>],
A[<i>j</i>], and A[<i>k</i>] have not been found, so three indexes <i>i</i>, <i>j</i>, and <i>k</i> are initialized as -1.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
The number 3 is the first number to be scanned, and A[<i>i</i>] is updated as 3.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
The next number to be scanned is the number 2. Notice that 2
is less than 3, and it is a better candidate of A[<i>i</i>]. The less A[<i>i</i>] is, the
larger range A[<i>j</i>] and A[<i>k</i>] will have. Therefore, A[<i>i</i>] is updated as 2.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
Let’s continue to scan the next number 5. Since 5 is greater
the current A[<i>i</i>], it is a candidate
of A[<i>j</i>]. A[<i>j</i>] is updated as 5.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
The number 1 is scanned in the next round. Because 1 is less
than the current A[<i>j</i>], it cannot be
A[<i>k</i>]. Additionally, the number 1 is a
candidate to be A[<i>i</i>] or A[<i>j</i>] in the future since it is less than
the current A[<i>i</i>] and A[<i>j</i>]. However, neither A[<i>i</i>] nor A[<i>j</i>] can be updated as 1 at this time. If A[<i>i</i>] is updated as 1, A[<i>i</i>]<A[<i>j</i>] but <i>i</i>><i>j</i>. If A[<i>j</i>] is updated as 1, A[<i>i</i>]>A[<i>j</i>]. Therefore, we store the number 1 into another variable named <i>t</i>.<o:p></o:p></div>
<div class="MsoNormal">
<br />
We move on to scan the next number 4. Notice that the number
4 is less than A[<i>j</i>] again. At this
time we have two numbers 1 (the stored variable <i>t</i>) and 4, so we have a new pair of numbers to be A[<i>i</i>] and A[<i>j</i>]. A[<i>i</i>] and A[<i>j</i>] are updated as 1 and 4 respectively.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
The next number, 7, is greater than the current A[<i>j</i>], so A[<i>k</i>] is updated as 7. The process terminates.<o:p></o:p></div>
<div class="MsoNormal">
The following table summarizes the step-by-step analysis
above:<o:p></o:p><br />
<br /></div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableLightShadingAccent1" style="border-collapse: collapse; border: currentColor; mso-border-bottom-alt: solid #4F81BD 1.0pt; mso-border-bottom-themecolor: accent1; mso-border-top-alt: solid #4F81BD 1.0pt; mso-border-top-themecolor: accent1; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border-color: rgb(79, 129, 189) currentColor; border-style: solid none; border-width: 1pt medium; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0in 5.4pt; width: 41.4pt;" valign="top" width="55"><div class="MsoNormal" style="margin-bottom: 0pt;">
<b><span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;"> Step<o:p></o:p></span></b></div>
</td>
<td style="border-color: rgb(79, 129, 189) currentColor; border-style: solid none; border-width: 1pt medium; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0in 5.4pt; width: 94.5pt;" valign="top" width="126"><div class="MsoNormal" style="margin-bottom: 0pt;">
<b><span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">Scanned Number<o:p></o:p></span></b></div>
</td>
<td style="border-color: rgb(79, 129, 189) currentColor; border-style: solid none; border-width: 1pt medium; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0in 5.4pt; width: 40.5pt;" valign="top" width="54"><div class="MsoNormal" style="margin-bottom: 0pt;">
<b><span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">A[<i>i</i>]<o:p></o:p></span></b></div>
</td>
<td style="border-color: rgb(79, 129, 189) currentColor; border-style: solid none; border-width: 1pt medium; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<b><span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">A[<i>j</i>]<o:p></o:p></span></b></div>
</td>
<td style="border-color: rgb(79, 129, 189) currentColor; border-style: solid none; border-width: 1pt medium; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<b><span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">A[<i>k</i>]<o:p></o:p></span></b></div>
</td>
<td style="border-color: rgb(79, 129, 189) currentColor; border-style: solid none; border-width: 1pt medium; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0in 5.4pt; width: 31.5pt;" valign="top" width="42"><div class="MsoNormal" style="margin-bottom: 0pt;">
<b><i><span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">t<o:p></o:p></span></i></b></div>
</td>
<td style="border-color: rgb(79, 129, 189) currentColor; border-style: solid none; border-width: 1pt medium; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><div class="MsoNormal" style="margin-bottom: 0pt;">
<b><span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">Operation<o:p></o:p></span></b></div>
</td>
</tr>
<tr>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 41.4pt;" valign="top" width="55"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-bidi-font-weight: bold; mso-themecolor: accent1; mso-themeshade: 191;">1<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 94.5pt;" valign="top" width="126"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">3<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 40.5pt;" valign="top" width="54"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">3<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 31.5pt;" valign="top" width="42"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">Update A[<i>i</i>]<o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border: currentColor; padding: 0in 5.4pt; width: 41.4pt;" valign="top" width="55"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-bidi-font-weight: bold; mso-themecolor: accent1; mso-themeshade: 191;">2<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 94.5pt;" valign="top" width="126"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">2<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 40.5pt;" valign="top" width="54"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">2<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 31.5pt;" valign="top" width="42"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">Update A[<i>i</i>]<o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 41.4pt;" valign="top" width="55"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-bidi-font-weight: bold; mso-themecolor: accent1; mso-themeshade: 191;">3<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 94.5pt;" valign="top" width="126"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">5<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 40.5pt;" valign="top" width="54"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">2<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">5<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 31.5pt;" valign="top" width="42"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">Update A[<i>j</i>]<o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border: currentColor; padding: 0in 5.4pt; width: 41.4pt;" valign="top" width="55"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-bidi-font-weight: bold; mso-themecolor: accent1; mso-themeshade: 191;">4<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 94.5pt;" valign="top" width="126"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">1<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 40.5pt;" valign="top" width="54"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">2<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">5<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 31.5pt;" valign="top" width="42"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">1<o:p></o:p></span></div>
</td>
<td style="border: currentColor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">Update <i>t</i><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 41.4pt;" valign="top" width="55"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-bidi-font-weight: bold; mso-themecolor: accent1; mso-themeshade: 191;">5<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 94.5pt;" valign="top" width="126"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">4<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 40.5pt;" valign="top" width="54"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">1<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">4<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 31.5pt;" valign="top" width="42"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="background: rgb(211, 223, 238); border: currentColor; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><div class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.5in;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">Update A[<i>i</i>], A[<i>j</i>] and <i>t</i><o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border: currentColor; mso-border-bottom-themecolor: accent1; padding: 0in 5.4pt; width: 41.4pt;" valign="top" width="55"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-bidi-font-weight: bold; mso-themecolor: accent1; mso-themeshade: 191;">6<o:p></o:p></span></div>
</td>
<td style="border: currentColor; mso-border-bottom-themecolor: accent1; padding: 0in 5.4pt; width: 94.5pt;" valign="top" width="126"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">7<o:p></o:p></span></div>
</td>
<td style="border: currentColor; mso-border-bottom-themecolor: accent1; padding: 0in 5.4pt; width: 40.5pt;" valign="top" width="54"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">1<o:p></o:p></span></div>
</td>
<td style="border: currentColor; mso-border-bottom-themecolor: accent1; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">4<o:p></o:p></span></div>
</td>
<td style="border: currentColor; mso-border-bottom-themecolor: accent1; padding: 0in 5.4pt; width: 0.5in;" valign="top" width="48"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">7<o:p></o:p></span></div>
</td>
<td style="border: currentColor; mso-border-bottom-themecolor: accent1; padding: 0in 5.4pt; width: 31.5pt;" valign="top" width="42"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">-1<o:p></o:p></span></div>
</td>
<td style="border: currentColor; mso-border-bottom-themecolor: accent1; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: #365f91; mso-themecolor: accent1; mso-themeshade: 191;">Update A[<i>k</i>]. Terminate.<o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal">
<br />
The strategy of this solution is to store as less value to
A[<i>i</i>] and A[<i>j</i>] as possible, in order to enlarge the value range for steps in the future.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
With our step-by-step analysis, we could implement this
solution with the following C++ code:<o:p></o:p><br />
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> increasingIndex_2(<span style="color: blue;">int</span>*
nums, <span style="color: blue;">int</span> length, <span style="color: blue;">int</span>*
i, <span style="color: blue;">int</span>* j, <span style="color: blue;">int</span>*
k)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(nums == NULL || length <= 0 || i == NULL || j
== NULL || k == NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> *i = *j =
*k = -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> backup = -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(index = 0; index < length; ++index)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(*i == -1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
*i = index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span>(*j == -1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(nums[index] > nums[*i])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
*j = index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
*i = index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span>(nums[index]
> nums[*j])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
*k = index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">break</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span>(nums[index]
< nums[*j])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(backup != -1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span>(nums[index] > nums[backup])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
*i = backup;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
*j = index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
backup = -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">else</span> <span style="color: blue;">if</span>
(nums[index] < nums[backup])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
backup = index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span>(nums[index]
< nums[*j])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
backup = index;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(index == length)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> *i =
*j = *k = -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
<div class="MsoNormal">
<span style="font-family: inherit;"><span style="background-color: white; color: #222222; line-height: 18px;">More coding interview questions are discussed in my book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="background-color: white; color: #888888; line-height: 18px;"><span style="color: #2288bb;">Amazon.com</span></a><span style="background-color: white; color: #222222; line-height: 18px;">, or </span><a href="http://www.apress.com/9781430247616" style="background-color: white; color: #888888; line-height: 18px;"><span style="color: #2288bb;">Apress</span></a><span style="background-color: white; color: #222222; line-height: 18px;">.</span></span></div>
<span style="font-family: inherit;"><span style="background-color: white; color: #222222; line-height: 18px;"><br />The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/" style="background-color: white; color: #888888; line-height: 18px;"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a><span style="background-color: white; color: #222222; line-height: 18px;">. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.</span></span></div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com12tag:blogger.com,1999:blog-4228859841453048938.post-37770075586810184502013-02-20T11:04:00.003+08:002013-02-20T11:08:29.109+08:00No. 41 - Group of 1s in a Matrix<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: inherit;"><span style="font-family: inherit; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;"><strong><u>Problem:</u></strong></span><span style="font-family: "Calibri","sans-serif"; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;"> Given a matrix with 1s and 0s, please find the
number of groups of 1s. A group is defined by horizontally or vertically
adjacent 1s. For example, there are four groups of 1s in Figure 1 which are
drawn with different colors.</span></span><br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQOvlbD7gVyjmN6Gcro65wzwxcWlXvU4jTHRzs59DMZszsIRz7vRSEk2dfPgo-r9Srl8ESsap0d5M9wfECHVIV9ZbRhWDfs-m0vT4G1C7xh83VRQqhhlyqsjGt3XmDktluBgmkmG8O1G4/s1600/Figure1.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQOvlbD7gVyjmN6Gcro65wzwxcWlXvU4jTHRzs59DMZszsIRz7vRSEk2dfPgo-r9Srl8ESsap0d5M9wfECHVIV9ZbRhWDfs-m0vT4G1C7xh83VRQqhhlyqsjGt3XmDktluBgmkmG8O1G4/s1600/Figure1.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: A matrix with four groups of 1s. Different groups are drawn with different colors.</td></tr>
</tbody></table>
<span style="font-family: "Calibri","sans-serif"; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;"><span style="font-family: Times New Roman;">
</span></span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Analysis:</u></b> All
numbers in the matrix are scanned one by one. When a 1 is met, a group of 1s has
been found, and then all 1s in the group are flipped to 0s. <o:p></o:p></span></div>
<span style="font-family: inherit;">
</span><span style="font-family: "Calibri","sans-serif"; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;"><span style="font-family: inherit;">For example, the first entry in the matrix of
Figure 1 contains 1, so the first group of 1s is found when we access to the
first entry. After all 1s in the group have been flipped to 0s, the matrix
becomes the one in Figure 2.</span></span><br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWjwS_RDgojlWfCVRO-yoKRXGK_XESt2E398tcYSsHYyxsEI1fdGH5SWcE97L1jlNKGUoQIc9ND7VFPbV5L6Wa_Ucajsc54QB8M2NFgcoliaxhnO5ihize7Jch2XShmRINwmizXish9Q4/s1600/Figure2.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWjwS_RDgojlWfCVRO-yoKRXGK_XESt2E398tcYSsHYyxsEI1fdGH5SWcE97L1jlNKGUoQIc9ND7VFPbV5L6Wa_Ucajsc54QB8M2NFgcoliaxhnO5ihize7Jch2XShmRINwmizXish9Q4/s1600/Figure2.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: The new matrix after all 1s in the first group (in the red boundary) have been flipped</td></tr>
</tbody></table>
<span style="font-family: inherit;"><span style="font-family: "Calibri","sans-serif"; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;"></span></span>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Therefore, the overall function to count the groups of 1s in
a matrix can be implemented in the following C++ code:<o:p></o:p></span></div>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> groupOf1(<span style="color: blue;">int</span>* nums, <span style="color: blue;">int</span> rows, <span style="color: blue;">int</span> cols)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(nums == NULL || rows <= 0 || cols <= 0)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> 0;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> group = 0;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 0; i <
rows * cols; ++i)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(nums[i] == 1)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>group++;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>flipAdjacent1(nums, rows, cols, i);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> group;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Let’s move on to flip 1s inside a group. Actually, it is an
application of the </span><a href="http://en.wikipedia.org/wiki/Flood_fill"><span style="color: blue; font-family: inherit;">seed
filling</span></a><span style="font-family: inherit;"> algorithm. When we are going to flip a group starting from an
entry, we take the entry as a seed and push it into a stack. At each step, we
get an entry from the top of the stack, flip it, and then push its neighboring
entries into the stack. The process continues until the stack is empty. The
following code implements this algorithm:<o:p></o:p></span></div>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> flipAdjacent1(<span style="color: blue;">int</span>* nums,
<span style="color: blue;">int</span> rows, <span style="color: blue;">int</span>
cols, <span style="color: blue;">int</span> index)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>stack<<span style="color: blue;">int</span>> group;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>group.push(index);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">while</span>(!group.empty())<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> topIndex = group.top();<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>group.pop();<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>nums[topIndex] = 0;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> row = topIndex / cols;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> col = topIndex % cols;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// up</span><o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(row > 0 && nums[(row - 1) * cols + col]
== 1)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>group.push((row - 1) * cols + col);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// right</span><o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(col < cols - 1 && nums[row * cols + col
+ 1] == 1)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>group.push(row * cols + col + 1);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// down</span><o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(row < rows - 1 && nums[(row + 1) * cols
+ col] == 1)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>group.push((row + 1) * cols + col);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">// left</span><o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(col > 0 && nums[row * cols + col - 1]
== 1)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>group.push(row * cols + col - 1);<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
<span style="font-family: inherit;"><span style="background-color: white; color: #222222; line-height: 18px;"><span style="font-family: inherit;">More coding
interview questions are discussed in my book <Coding Interviews: Questions,
Analysis & Solutions>. You may find the details of this book on </span></span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="background-color: white; color: #888888; line-height: 18px;"><span style="color: #2288bb;"><span style="font-family: inherit;">Amazon.com</span></span></a><span style="background-color: white; color: #222222; line-height: 18px;"><span style="font-family: inherit;">,
or </span></span><a href="http://www.apress.com/9781430247616" style="background-color: white; color: #888888; line-height: 18px;"><span style="color: #2288bb;"><span style="font-family: inherit;">Apress</span></span></a><span style="background-color: white; color: #222222; line-height: 18px;"><span style="font-family: inherit;">.<br /><br />The
author Harry He owns all the rights of this post. If you are going to use part
of or the whole of this ariticle in your blog or webpages, please add a
reference to </span></span><a href="http://codercareer.blogspot.com/" style="background-color: white; color: #888888; line-height: 18px;"><span style="color: #2288bb;"><span style="font-family: inherit;">http://codercareer.blogspot.com/</span></span></a><span style="background-color: white; color: #222222; line-height: 18px;"><span style="font-family: inherit;">. If you are
going to use it in your books, please contact him via zhedahht@gmail.com .
Thanks.</span></span></span></div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com15tag:blogger.com,1999:blog-4228859841453048938.post-36465996690549308492013-02-18T10:54:00.000+08:002013-02-18T10:55:48.374+08:00No. 40 - Add on Lists<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: inherit;"><b><u><span style="line-height: 115%;">Problem:</span></u></b><span style="line-height: 115%;"> Nodes in a list represent a number. For
example, the nodes in Figure 1 (a) and (b) represent numbers 123 and 4567
respectively. Please implement a function/method to add numbers in two lists,
and store the sum into a new list.</span></span><br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-bJQlB853JZfmCQlqT84q3IZvAYKFcB9SgZ3M9vnlbSylXbN6AqgHUw3qlwzp6JRW_eu5D8kN55JgiIQHNh5FmADTNWJn8nU5Uvm8b1Z_BY4PECQ66DrkgDoca63UTu5KPKWjPQZPxgc/s1600/Figure+1.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-bJQlB853JZfmCQlqT84q3IZvAYKFcB9SgZ3M9vnlbSylXbN6AqgHUw3qlwzp6JRW_eu5D8kN55JgiIQHNh5FmADTNWJn8nU5Uvm8b1Z_BY4PECQ66DrkgDoca63UTu5KPKWjPQZPxgc/s1600/Figure+1.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><div class="MsoCaption">
Figure <!--[if supportFields]><span style='mso-element:
field-begin'></span><span style='mso-spacerun:yes'> </span>SEQ Figure \* ARABIC
<span style='mso-element:field-separator'></span><![endif]-->1<!--[if supportFields]><span style='mso-element:
field-end'></span><![endif]-->: Two lists representing numbers. (a) A list for
123; (b) A list for 4567.<o:p></o:p></div>
</td></tr>
</tbody></table>
<span style="font-family: inherit;"><b><u><span style="line-height: 115%;">Analysis:</span></u></b><span style="line-height: 115%;"> Usually numbers are added beginning from the
least significant digits (The digit 3 in the number 123, and the digit 7 in the number 4567). As shown in Figure 1, the least significant digits
are at the tail of lists, and they can be accessed after the whole lists are
scanned. Therefore, lists should be reversed at first, in order get the least
significant digits before other digits. The two reversed lists of lists in
Figure 1 are shown in Figure 2.</span></span><br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6KUKv-NJXrjqIQQzv93PUczflPUSoj6vY205xGr2aG5kqFTF7panxWVH9mhdm0ecdf57TaJDooO9VV7nKp7ebj7VoESTGm1wANnfohJ8Ycv0sgKDHlmuk-NDrhs9UoiVvCsn_jZ9Gi8A/s1600/Figure+2.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6KUKv-NJXrjqIQQzv93PUczflPUSoj6vY205xGr2aG5kqFTF7panxWVH9mhdm0ecdf57TaJDooO9VV7nKp7ebj7VoESTGm1wANnfohJ8Ycv0sgKDHlmuk-NDrhs9UoiVvCsn_jZ9Gi8A/s1600/Figure+2.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: Two reversed lists of the lists in Figure 1.</td></tr>
</tbody></table>
<br />
<div class="MsoNormal">
After two lists are reversed, we can add nodes along the
links between nodes, and then reversed the result list after all nodes are
added. Therefore, the overall structure to add numbers in two lists can be
implemented with the following code in C/C++:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">ListNode* Add(ListNode* pHead1, ListNode* pHead2)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(pHead1 == NULL || pHead2 == NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> NULL;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> pHead1 =
Reverse(pHead1);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> pHead2 =
Reverse(pHead2);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> ListNode*
pResult = AddReversed(pHead1, pHead2);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> Reverse(pResult);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Now let’s implement the function <span style="font-family: Courier New, Courier, monospace;">AddReversed</span>, to add nodes
in two reversed lists. Digits are gotten in nodes along links between nodes.
When we get two digits in two lists, we add them and create a new node to store
the sum, and append the new node into the list for result. There are two issues
worthy of attention: (1) The length of two lists might be different; (2) The
sum of two digits may be greater than 10, so we have to take care of the carry
when adding two digits. The function <span style="font-family: Courier New, Courier, monospace;">AddReversed</span> can be implemented with the
following C/C++ code:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">ListNode* AddReversed(ListNode* pHead1, ListNode*
pHead2)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> carry = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> ListNode*
pPrev = NULL;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> ListNode*
pHead = NULL;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">while</span>(pHead1 != NULL || pHead2 != NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
ListNode* pNode = AddNode(pHead1, pHead2, &carry);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
AppendNode(&pHead, &pPrev, pNode);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(pHead1 != NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
pHead1 = pHead1->m_pNext;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(pHead2 != NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
pHead2 = pHead2->m_pNext;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(carry > 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
ListNode* pNode = CreateListNode(carry);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
AppendNode(&pHead, &pPrev, pNode);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> pHead;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The function <span style="font-family: Courier New, Courier, monospace;">AddNode</span> adds digits in two nodes. The third parameter
of this function takes the carry for addition calculation, as listed below:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">ListNode* AddNode(ListNode* pNode1, ListNode*
pNode2, <span style="color: blue;">int</span>* carry)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> num1 =
0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(pNode1 != NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> num1
= pNode1->m_nValue;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> num2 = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(pNode2 != NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> num2
= pNode2->m_nValue;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> sum = num1 + num2 + *carry;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> *carry =
(sum >= 10) ? 1 : 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> value = (sum >= 10) ? (sum - 10) : sum;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> CreateListNode(value);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The function <span style="font-family: Courier New, Courier, monospace;">AppendNode</span> is used append a node into the tail
of lists. In order to avoid scanning the whole list to get the previous tail every
time, the previous tails is stored in the parameter/variable pPrev, as listed in the
following code:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> AppendNode(ListNode** pHead, ListNode** pPrev, ListNode*
pNode)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(*pHead == NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
*pHead = pNode;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(*pPrev == NULL)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
*pPrev = pNode;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
(*pPrev)->m_pNext = pNode;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
*pPrev = pNode;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The function <span style="font-family: Courier New, Courier, monospace;">CreateListNode</span> is to create a list node
according to a value, which is omitted here because it’s quite straightforward. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The steps to reverse a list are discussed in <a href="http://codercareer.blogspot.com/2011/10/no-18-reverse-linked-list.html" target="_blank">my previous blog</a>.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: inherit;"><span style="background-color: white; color: #222222; line-height: 18px;">More coding interview questions are discussed in my book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="background-color: white; color: #888888; line-height: 18px; text-decoration: initial;"><span style="color: #2288bb;">Amazon.com</span></a><span style="background-color: white; color: #222222; line-height: 18px;">, or </span><a href="http://www.apress.com/9781430247616" style="background-color: white; color: #888888; line-height: 18px; text-decoration: initial;"><span style="color: #2288bb;">Apress</span></a><span style="background-color: white; color: #222222; line-height: 18px;">.<br /><br />The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/" style="background-color: white; color: #888888; line-height: 18px; text-decoration: initial;"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a><span style="background-color: white; color: #222222; line-height: 18px;">. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks. </span></span></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com12tag:blogger.com,1999:blog-4228859841453048938.post-13507494699339925912013-02-06T14:09:00.000+08:002013-02-18T11:21:25.683+08:00No. 39 - Stacks Sharing an Array<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Problem 1</u></b>:
How can you implement two stacks in a single array, where no stack overflows
until no space left in the entire array space?<o:p></o:p></span><br />
<span style="font-family: inherit;">
</span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Analysis</u></b>:
An array has two ends, so each of the two stacks may grow from an end in the
array. Figure 1 below shows the initial status of the array and two stacks (assuming
the capacity of the array is 10).<o:p></o:p></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJQ1vEGm1i6GuvkgkUvPDfo40bUPGTEqCWtc_rzPkFUu2QSUuoMkHXLyF1Jhzd8p-i9QhWELvoJRcO_e_G51sDu2br0KsmP4Oapeixlh_z8cy4kiOsE4LO_Pd4toaom-4urfDqeI91HD0/s1600/Figure+01.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJQ1vEGm1i6GuvkgkUvPDfo40bUPGTEqCWtc_rzPkFUu2QSUuoMkHXLyF1Jhzd8p-i9QhWELvoJRcO_e_G51sDu2br0KsmP4Oapeixlh_z8cy4kiOsE4LO_Pd4toaom-4urfDqeI91HD0/s1600/Figure+01.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: Initial status of the array and two stacks</td></tr>
</tbody></table>
<span style="font-family: inherit;"><span style="font-family: inherit;">Since two stacks are empty at first, so indexes of top items
are initialized as -1 and 10 at first.</span></span><br />
<span style="font-family: inherit;">When items are pushed into first stack, it grows from left
to right. Similarly, the second stack grows from right to left when items are
items are pushed into it. For example, Figure 2 shows the status when three
items </span><i style="font-family: inherit;">a</i><span style="font-family: inherit;">, </span><i style="font-family: inherit;">b</i><span style="font-family: inherit;"> and </span><i style="font-family: inherit;">c</i><span style="font-family: inherit;"> are pushed into
the first stack, and </span><i style="font-family: inherit;">d</i><span style="font-family: inherit;">, </span><i style="font-family: inherit;">e</i><span style="font-family: inherit;"> are pushed into the second stack.</span><span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">
</span>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL9nMMYk2LQSe-RhZ6StQvJrQxlDUZq-2xnNC7QN-EdBaolJJMFpEFHAlhFuDvGL2GZnl7-wXDOCZIRwIiS12JoDmOou45X5eQ8IlUTYPFjNILH96JGQESzaYtmP62gmFXjzeu9v4Qypk/s1600/Figure+02.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL9nMMYk2LQSe-RhZ6StQvJrQxlDUZq-2xnNC7QN-EdBaolJJMFpEFHAlhFuDvGL2GZnl7-wXDOCZIRwIiS12JoDmOou45X5eQ8IlUTYPFjNILH96JGQESzaYtmP62gmFXjzeu9v4Qypk/s1600/Figure+02.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: The status after three items are pushed into the first stack and two items are pushed into the second stack </td></tr>
</tbody></table>
</div>
<span style="font-family: inherit;">
</span>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">No more items can be pushed into stacks when two top items are adjacent to each other, because all space in the array has been occupied.</span></div>
<span style="font-family: inherit;">
</span>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;">Our solution can be implemented with the following C++ class:</span><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">template</span><span style="font-family: Consolas; font-size: 9.5pt;"> <<span style="color: blue;">typename</span> T, <span style="color: blue;">int</span> capacity> <span style="color: blue;">class</span>
TwoStacks<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>TwoStacks()<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>topFirst = -1;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>topSecond = capacity;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>T top(<span style="color: blue;">int</span> stackIndex)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>validateIndex(stackIndex);<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(empty(stackIndex))<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">throw</span> <span style="color: blue;">new</span> exception(<span style="color: #a31515;">"The stack is empty."</span>);<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(stackIndex == 0)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> items[topFirst];<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> items[topSecond];<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">void</span> push(<span style="color: blue;">int</span>
stackIndex, T item)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>validateIndex(stackIndex);<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(full())<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">throw</span> <span style="color: blue;">new</span> exception(<span style="color: #a31515;">"All space has been occupied."</span>);<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(stackIndex == 0)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>items[++topFirst] = item;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">else</span><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>items[--topSecond] = item;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">void</span> pop(<span style="color: blue;">int</span>
stackIndex)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>validateIndex(stackIndex);<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(empty(stackIndex))<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">throw</span> <span style="color: blue;">new</span> exception(<span style="color: #a31515;">"The stack is empty."</span>);<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(stackIndex
== 0)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>--topFirst;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">else</span><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>++topSecond;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">bool</span> empty(<span style="color: blue;">int</span>
stackIndex)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(stackIndex == 0 && topFirst == -1)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">true</span>;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(stackIndex == 1 && topSecond == capacity)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">true</span>;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">false</span>;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">bool</span> full()<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> (topFirst >= topSecond - 1);<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">void</span> validateIndex(<span style="color: blue;">int</span>
stackIndex)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(stackIndex < 0 || stackIndex > 1)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">throw</span> <span style="color: blue;">new</span> exception(<span style="color: #a31515;">"Invalid Stack Index."</span>);<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>T items[capacity];<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> topFirst;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> topSecond;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: inherit;"><span style="font-family: Consolas; font-size: 9.5pt;">};<o:p></o:p></span></span></div>
</div>
<span style="font-family: inherit;">
</span>
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Problem 2</u></b>:
How can you implement <i style="mso-bidi-font-style: normal;">n</i> (<i style="mso-bidi-font-style: normal;">n</i> > 2) stacks in a single array,
where no stack overflows until no space left in the entire array space?<o:p></o:p></span></span></div>
<span style="font-family: inherit;"><span style="font-family: inherit;">
</span></span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><span style="font-family: inherit;"><b style="mso-bidi-font-weight: normal;"><u>Analysis</u></b>: An
array only has two ends, so we need new strategies to implement more than two
stacks in a single array.<o:p></o:p></span></span></div>
<span style="font-family: inherit;"><span style="font-family: inherit;">
</span></span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><span style="font-family: inherit;">We may implement the array like a list, where each item in
the array has a link to another item. When an item has not been occupied by any
stacks and it is available, it is linked to the next available item. When an
item is pushed into a stack, it is linked to the previous item in the stack.
That’s to say, there are <i style="mso-bidi-font-style: normal;">n</i> + 1 list
in total in the system if there are <i style="mso-bidi-font-style: normal;">n</i>
stacks sharing an array, a list for the available space left in the array, and
a list for each stack.<o:p></o:p></span></span></div>
<span style="font-family: inherit;"><span style="font-family: inherit;">
</span></span><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<span style="font-family: inherit;"><span style="font-family: inherit;">Let’s take three stacks sharing an array with capacity 10 as
an example. Figure 3 shows the initial status for the array and stacks. Each
item in the array has two blocks, one for item data and the other for the index
of another block.<o:p></o:p></span></span></div>
<span style="font-family: inherit;"><span style="font-family: inherit;">
</span></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: inherit; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2XH3oIGoPUEfxMy9Etu1WFjVBYOkFAPzZ4-eXwbbTrV52kZs5SPVVUuwtWjMfDpHk_FtJFWBeaMLZSzATTIL3ZYhavOJ35fpPqy_k9shL21rQ-V_gGmVsFIs-vi26wYyijbuo4qzqtHU/s1600/Figure+03.PNG" style="margin-left: auto; margin-right: auto;" /></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 3: Initial status of three stacks sharing an array</td></tr>
</tbody></table>
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<div style="font-family: inherit;">
<br /></div>
<div style="font-family: inherit;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit;">
As shown in Figure 3, each item is linked to the next item
(the link block of the <i>i<sup>th</sup></i>
item is <i>i</i>+1). The head of list for
available items in the array points to the item at position 0, which is the Ava
pointer in the figure. Since three stacks are empty, their top (Top1, Top2 and
Top3 in the figure) are initialized as -1.<o:p></o:p></div>
<div class="MsoNormal" style="font-family: inherit;">
<br /></div>
<div style="font-family: inherit;">
<span style="line-height: 115%;"><span style="font-family: inherit;">Let’s try to push an item <i>a</i> into the first stack. Currently the first available item is at
the position 0, so we set its data block as <i>a</i>.
The link block of the item is 1, which means the next available item is at the
position 1, so we update the Ava pointer to the position 1. Additionally, the
link block of the item at the position 1 should be updated to -1, the previous
top index of the first stack. Lastly, we update to top index of the first stack
to 0. The status of the array and stacks are shown in Figure 4.</span></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: inherit; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNAoUM8_Ey18GdowwJsjtTMdVpSZpezwofigDsnJNWqgwmR2131jUVcNXiacWmoFGCkvPMVtFN9jvaVUAYDNdcHI04yi1Y0CykTRrl1REjF4WrYcs8hcBKKmtU0Sga-epUVnJd27srfyo/s1600/Figure+04.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNAoUM8_Ey18GdowwJsjtTMdVpSZpezwofigDsnJNWqgwmR2131jUVcNXiacWmoFGCkvPMVtFN9jvaVUAYDNdcHI04yi1Y0CykTRrl1REjF4WrYcs8hcBKKmtU0Sga-epUVnJd27srfyo/s1600/Figure+04.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 4: The status after pushing a into the first stack</td></tr>
</tbody></table>
<div style="font-family: inherit;">
<span style="line-height: 115%;"><span style="font-family: inherit;">Let’s
push two more items <i>b</i> and <i>c</i> into the first stack. The operations
are similar as before, and the status is shown in Figure 5.</span></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: inherit; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXId2NsGda6OEG4ZKEvcRTaYHZhgGv8ZpiPNX-v55OSUjAS6gifeOzaYmEpYKtirU8vP7KmiOITd4xjeit4hTBYehrvIGjvpo0cRwPgVDlV5pnAkgmrIBFGdtYHJVJannmGxZVxNEee0w/s1600/Figure+05.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXId2NsGda6OEG4ZKEvcRTaYHZhgGv8ZpiPNX-v55OSUjAS6gifeOzaYmEpYKtirU8vP7KmiOITd4xjeit4hTBYehrvIGjvpo0cRwPgVDlV5pnAkgmrIBFGdtYHJVJannmGxZVxNEee0w/s1600/Figure+05.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 5: The status after adding two more items, b and c, into the first stack</td></tr>
</tbody></table>
<div style="font-family: inherit;">
<span style="line-height: 115%;"><span style="font-family: inherit;">In
the next step we are going to push another <i>d</i>
into the second stack. Most operations are similar as before. The link block in
the item at the position 3 is updated to -1, since the second stack is empty
before and its top index is -1 previously. And then the top index of the second
stack is updated to 3. The status after adding <i>d</i> into the second stack is shown in Figure 6.</span></span></div>
<div style="font-family: inherit;">
<span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;"><br /></span>
</div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: inherit; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3z7sglmkIcixPt2UqAmlgz9DZsK4Q9QV-zBoJhIKubpO8AzbxMHCvRZCMoYR4M04D4Wh4CxNUgK8rXLCxQefzZHA43snDXfDt4kpQhHO1ruRsnn0ylz4rNR1eV5WCP8TqdAc6CEI0TkA/s1600/Figure+06.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3z7sglmkIcixPt2UqAmlgz9DZsK4Q9QV-zBoJhIKubpO8AzbxMHCvRZCMoYR4M04D4Wh4CxNUgK8rXLCxQefzZHA43snDXfDt4kpQhHO1ruRsnn0ylz4rNR1eV5WCP8TqdAc6CEI0TkA/s1600/Figure+06.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 6: The status after pushing d into the second stack</td></tr>
</tbody></table>
<div style="font-family: inherit;">
<span style="font-family: inherit;">If we continue to push three more item </span><i style="font-family: inherit;">e</i><span style="font-family: inherit;">, </span><i style="font-family: inherit;">f</i><span style="font-family: inherit;">, and </span><i style="font-family: inherit;">g</i><span style="font-family: inherit;"> into the second stack, the status is
shown in Figure 7.</span></div>
<div class="MsoNormal" style="font-family: inherit;">
<o:p></o:p></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: inherit; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvSwU3ssPt89iOtmEJhT1YjP5twVwRKQR90FiH5ke2GrY1OTA09-i_Ecg6KZ9agSzo0FnkDTdeOZnkUpBzqX7PAHnDz49fYCvYdswBjUjCH3jRoxGULIOCdAu8GWb0lJXKV6e24-36ElA/s1600/Figure+07.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvSwU3ssPt89iOtmEJhT1YjP5twVwRKQR90FiH5ke2GrY1OTA09-i_Ecg6KZ9agSzo0FnkDTdeOZnkUpBzqX7PAHnDz49fYCvYdswBjUjCH3jRoxGULIOCdAu8GWb0lJXKV6e24-36ElA/s1600/Figure+07.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 7: The status after pushing three more items, e, f, and g, into the second stack</td></tr>
</tbody></table>
<div class="MsoNormal" style="font-family: inherit;">
<span style="line-height: 115%;"><span style="font-family: inherit;">At
this time we are going to pop an item from the first stack. Since the top index
of the first stack is 2, the item to be popped off is at the position 2. The
link value in that item is 1, which means the previous top in the first stack
is at the position 1 in the array, so we update the top index of the first
stack as 1. Now the item at the position 2 becomes available, it should be
linked to the list for available items. We move the Ava pointer to 2, and then
update the link value of the item at the position 2 as 7, which is the previous
head position of the list for available items. The status is shown in Figure 8.</span></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: inherit; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFBQbj8LPmdMYiTo-hYq4IyZDvCrH20IWEh1Qf_AeZP5RHi2M8R0r537yiGZCE7j2Z2IH4eP6GOY_1pZ8llQafi4__veoPs-posg0Nhf31L6TDODYdc8Xzs-8VWCiavWVbOJbgL1O4ItQ/s1600/Figure+08.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFBQbj8LPmdMYiTo-hYq4IyZDvCrH20IWEh1Qf_AeZP5RHi2M8R0r537yiGZCE7j2Z2IH4eP6GOY_1pZ8llQafi4__veoPs-posg0Nhf31L6TDODYdc8Xzs-8VWCiavWVbOJbgL1O4ItQ/s1600/Figure+08.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 8: The status after popping an item from the first stack</td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: inherit; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><div style="text-align: left;">
<span style="line-height: 115%;"><span style="font-family: inherit;">If
we pop an item off the second stack, the item at the position 6 will be linked
to the list for available items too. Figure 9 depicts the status after the
related operations.</span></span></div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4JNPrkgGUfZ_9MtVITEzQYbRo24CLZpw5ROIdf89gLBkMctDLtkpCUnOlvtrIEuN00rQ2nnfcegMkQRGVXiVr7AOlByjP3bHHhwxFkgL1fzfvaAdmZ1W0ZRm64mQLI25JuMZ1MAg7oEI/s1600/Figure+09.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4JNPrkgGUfZ_9MtVITEzQYbRo24CLZpw5ROIdf89gLBkMctDLtkpCUnOlvtrIEuN00rQ2nnfcegMkQRGVXiVr7AOlByjP3bHHhwxFkgL1fzfvaAdmZ1W0ZRm64mQLI25JuMZ1MAg7oEI/s1600/Figure+09.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 9: The status after popping an item off the second stack</td></tr>
</tbody></table>
<div style="font-family: inherit;">
<span style="font-family: inherit;">If we push </span><i style="font-family: inherit;">h</i><span style="font-family: inherit;"> into
the third stack, it will be placed into the item at the position 6 because Ava
points to that item. The next available item is at the position 2 (the link
value in the item at the position 6). Therefore, the head of the list for
available items points to location 2, as shown in Figure 10.</span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: inherit; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB0fTrYWrzo2hVXE-svIoGkgYIM_ciIHOCiPMcDxanq62OLJ3gWTTwClWzIjx2bqdu7UTNpzQet69pkSFgxUgg_hGeMdxy-XxnGgxPIcRgnzrQms8HZZMIMy-Iju5lBIVaX2Sxu3tUwGQ/s1600/Figure+10.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB0fTrYWrzo2hVXE-svIoGkgYIM_ciIHOCiPMcDxanq62OLJ3gWTTwClWzIjx2bqdu7UTNpzQet69pkSFgxUgg_hGeMdxy-XxnGgxPIcRgnzrQms8HZZMIMy-Iju5lBIVaX2Sxu3tUwGQ/s1600/Figure+10.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 10: The status after pushing h into the third stack</td></tr>
</tbody></table>
<div style="font-family: inherit;">
<span style="font-family: inherit;">Let’s continue to push four more items into the third stack,
</span><i style="font-family: inherit;">i</i><span style="font-family: inherit;">, </span><i style="font-family: inherit;">j</i><span style="font-family: inherit;">, </span><i style="font-family: inherit;">k</i><span style="font-family: inherit;">, and </span><i style="font-family: inherit;">l</i><span style="font-family: inherit;">. The status after these four items are
pushed is shown in Figure 11. At this time, there are two items in the first
stack (</span><i style="font-family: inherit;">a</i><span style="font-family: inherit;"> and </span><i style="font-family: inherit;">b</i><span style="font-family: inherit;">), three items in the second stack (</span><i style="font-family: inherit;">d</i><span style="font-family: inherit;">, </span><i style="font-family: inherit;">e</i><span style="font-family: inherit;"> and </span><i style="font-family: inherit;">f</i><span style="font-family: inherit;">), and five items in the third stack (</span><i style="font-family: inherit;">h</i><span style="font-family: inherit;">, </span><i style="font-family: inherit;">i</i><span style="font-family: inherit;">,
</span><i style="font-family: inherit;">j</i><span style="font-family: inherit;">, </span><i style="font-family: inherit;">k</i><span style="font-family: inherit;">, and </span><i style="font-family: inherit;">l</i><span style="font-family: inherit;">). Please note
that items inside a stack are not necessarily adjacent. </span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: inherit; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF16AT9WISXStZvU54yA-qgCM48-eiK2T0mmMqzESl2zrbUu4A7rSD5lKxjW4jjVRpEV7tMz80aVy8DxQKopqqcMN_voepxdClvPEhxSRcA41drwAfgIWlXWX0RHpgY9JYmQ8Ru-bPSes/s1600/Figure+11.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF16AT9WISXStZvU54yA-qgCM48-eiK2T0mmMqzESl2zrbUu4A7rSD5lKxjW4jjVRpEV7tMz80aVy8DxQKopqqcMN_voepxdClvPEhxSRcA41drwAfgIWlXWX0RHpgY9JYmQ8Ru-bPSes/s1600/Figure+11.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 11: The status after pushing i, j, k, l into the third stack</td></tr>
</tbody></table>
<div style="font-family: inherit;">
<span style="font-family: inherit; line-height: 115%;">After </span><i style="font-family: inherit; line-height: 115%;">l</i><span style="font-family: inherit; line-height: 115%;"> is pushed
into the item at the position 9, the Ava pointer is update to -1 (the previous link
value in the item at the position 9), which means all items in the array have
been occupied by stacks. We can’t push more items until some items are popped
off.</span><span style="font-size: 11pt; line-height: 115%;"></span></div>
<div class="MsoNormal" style="font-family: inherit;">
<span style="font-family: inherit; line-height: 115%;">The source code in C++ to implement stacks
sharing an array is listed below:</span></div>
<div style="font-family: inherit;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">template</span><span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> <<span style="color: blue;">typename</span> T, <span style="color: blue;">int</span> capacity, <span style="color: blue;">int</span>
count> <span style="color: blue;">class</span> Stacks</span></div>
<div class="MsoNormal" style="font-family: inherit; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;">{</span></div>
<div style="font-family: inherit;">
<span style="font-size: 11pt; line-height: 115%;">
</span>
</div>
<div class="MsoNormal" style="font-family: inherit; line-height: normal; margin-bottom: 0in;">
<span style="font-size: 11pt; line-height: 115%;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></span></div>
<div style="font-family: inherit;">
<span style="font-size: 11pt; line-height: 115%;">
</span>
</div>
<div class="MsoNormal" style="font-family: inherit; line-height: normal; margin-bottom: 0in;">
<span style="font-size: 11pt; line-height: 115%;"><span style="font-family: Consolas; font-size: 9.5pt;"> Stacks()<o:p></o:p></span></span></div>
<div style="font-family: inherit;">
<span style="font-size: 11pt; line-height: 115%;">
</span>
</div>
<div class="MsoNormal" style="font-family: inherit; line-height: normal; margin-bottom: 0in;">
<span style="font-size: 11pt; line-height: 115%;"><span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></span></div>
<div style="font-family: inherit;">
<span style="font-size: 11pt; line-height: 115%;">
</span></div>
<div class="MsoNormal" style="font-family: inherit; line-height: normal; margin-bottom: 0in;">
<span style="font-size: 11pt; line-height: 115%;"><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> i;<o:p></o:p></span></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(i = 0; i < capacity - 1; ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
items[i].link = i + 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
items[i].link = -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
emptyHead = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span>(i = 0; i < count; ++i)<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
stackHead[i] = -1;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> T top(<span style="color: blue;">int</span> stackIndex)<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> validateIndex(stackIndex);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(empty(stackIndex))<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> exception(<span style="color: #a31515;">"The stack is empty."</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> items[stackHead[stackIndex]].data;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> push(<span style="color: blue;">int</span>
stackIndex, <span style="color: blue;">const</span> T& item)<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
validateIndex(stackIndex);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(full())<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> exception(<span style="color: #a31515;">"All space has been occupied."</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Item<T>& block = items[emptyHead];<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> nextEmpty = block.link;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
block.data = item;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
block.link = stackHead[stackIndex];<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
stackHead[stackIndex] = emptyHead;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
emptyHead = nextEmpty;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> pop(<span style="color: blue;">int</span>
stackIndex)<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
validateIndex(stackIndex);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(empty(stackIndex))<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> exception(<span style="color: #a31515;">"The stack is empty."</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Item<T>& block =
items[stackHead[stackIndex]];<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> nextItem = block.link;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0in;">
<div style="font-family: inherit; font-size: 11pt; line-height: normal;">
<span style="font-family: Consolas; font-size: 9.5pt;">
block.link = emptyHead;</span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"> emptyHead = stackHead[stackIndex];</span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"> stackHead[stackIndex] = nextItem;</span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">bool</span> empty(<span style="color: blue;">int</span>
stackIndex)<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> (stackHead[stackIndex] < 0);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> validateIndex(<span style="color: blue;">int</span>
stackIndex)<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span>(stackIndex < 0 || stackIndex >= count)<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> exception(<span style="color: #a31515;">"Invalid index of stack."</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">bool</span> full()<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> (emptyHead < 0);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">template</span> <<span style="color: blue;">typename</span>
T> <span style="color: blue;">struct</span> Item<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> T
data;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> link;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> };<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Item<T> items[capacity];<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> emptyHead;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> stackHead[count];<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: inherit; font-size: 11pt; line-height: normal; margin-bottom: 0in;">
<span style="font-family: Consolas; font-size: 9.5pt;">};<o:p></o:p></span></div>
<div style="font-family: inherit;">
</div>
<div style="font-family: inherit;">
<span style="font-size: 11pt; line-height: 115%;"><span style="font-size: 11pt; line-height: 115%;"><span style="font-family: inherit;"><br /></span></span></span>
<span style="font-family: inherit; font-size: 11pt; line-height: 115%;"><span style="background-color: white; color: #222222; font-size: 13px; line-height: 18px;">More coding interview questions are discussed in my book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614" style="background-color: white; color: #888888; font-size: 13px; line-height: 18px; text-decoration: initial;"><span style="color: #2288bb;">Amazon.com</span></a><span style="background-color: white; color: #222222; font-size: 13px; line-height: 18px;">, or </span><a href="http://www.apress.com/9781430247616" style="background-color: white; color: #888888; font-size: 13px; line-height: 18px; text-decoration: initial;"><span style="color: #2288bb;">Apress</span></a><span style="background-color: white; color: #222222; font-size: 13px; line-height: 18px;">.<br /><br />The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to </span><a href="http://codercareer.blogspot.com/" style="background-color: white; color: #888888; font-size: 13px; line-height: 18px; text-decoration: initial;"><span style="color: #2288bb;">http://codercareer.blogspot.com/</span></a><span style="background-color: white; color: #222222; font-size: 13px; line-height: 18px;">. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks. </span></span></div>
</div>
<div class="MsoNormal" style="font-family: inherit; margin: 0in 0in 10pt;">
</div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com9tag:blogger.com,1999:blog-4228859841453048938.post-11240979899184761742013-02-05T12:43:00.000+08:002016-10-19T12:42:31.819+08:00No. 38 - Digits in a Sequence<div dir="ltr" style="text-align: left;" trbidi="on">
<strong><u>Problem: </u></strong>Numbers are serialized increasingly into a sequence in the format of 0123456789101112131415..., which each digit occupies a position in the sequence. For instance, the digit in the position 5 is 5, in the position 13 is 1, in the position 19 is 4, and so on.<br />
<br />
Please write a function/method to get the digit on any given position.<br />
<br />
<strong><u>Analysis:</u></strong> Let's take a specific position as an example to analyze this problem. For example, what is the digit at the position 1001?<br />
<br />
The first 10 digits are for 10 numbers with only one digit (0, 1, 2, ..., 9). Since the position 1001 is beyond of the range of the first 10 digits, we continue to look for the digit at the position at 991 (991 = 1001 - 10) in the following sequence.<br />
<br />
The next 180 digits are for 90 numbers with two digits, from 10 to 99. Since 991 is greater than 180, the digit at position 991 is beyond the numbers with two digits. Let's continue to get the 811 (811 = 991-180) in the following sequence.<br />
<br />
The next 2700 digits are for 900 numbers with three digits, from 100 to 999. Since 811 is less than 2700, the position 811 should be inside a number with three digits.<br />
<br />
Every number has three digits, so the position 811 should be the second digit of the 270-th nubmer starting from 100 (811 = 270 * 3 + 1). Therefore, the digit at the position 811 is the second digit in the number 370, which is digit 7.<br />
<br />
The overall solution can be implemented with the following code:<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> digitAtIndex(<span style="color: blue;">int</span> index)<o:p></o:p></span></div>
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(index < 0)<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> -1;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> digits = 1;<o:p></o:p></span></div>
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">while</span>(<span style="color: blue;">true</span>)<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> numbers = countOfIntegers(digits);<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(index < numbers * digits)<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> digitAtIndex(index, digits);<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>index
-= digits * numbers;<o:p></o:p></span></div>
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>digits++;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> -1;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
We can get the count of integers with <em>n</em> digits via the following function:<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> countOfIntegers(<span style="color: blue;">int</span>
digits)<o:p></o:p></span></div>
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(digits == 1)<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> 10;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> count = 1;<o:p></o:p></span></div>
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 1; i <
digits; ++i)<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>count
*= 10;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> 9 * count;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
After we know the digit inside an integer with <em>m</em> digits, we could get the digit with the following function:<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> digitAtIndex(<span style="color: blue;">int</span> index, <span style="color: blue;">int</span> digits)<o:p></o:p></span></div>
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> number = beginNumber(digits) + index / digits;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> indexFromRight = digits - index % digits;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 1; i <
indexFromRight; ++i)<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>number /= 10;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> number % 10;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
In the function above, we need to know the first number with <em>m</em> digits. The first number with two digits is 10, and the first number with three digits is 100. These numbers can be calculated with the function below:<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> beginNumber(<span style="color: blue;">int</span> digits)<o:p></o:p></span></div>
<span style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(digits == 1)<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> 0;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> begin = 1;<o:p></o:p></span></div>
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 1; i <
digits; ++i)<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>begin
*= 10;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> begin;<o:p></o:p></span><br />
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
The source code with unit tests is available at <a href="http://ideone.com/yogYbu">http://ideone.com/yogYbu</a>.<br />
<br />
<span style="font-family: "calibri";"><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">More coding
interview questions are discussed in my book <Coding Interviews: Questions,
Analysis & Solutions>. You may find the details of this book on
</span></span></span></span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span style="color: #2288bb; font-family: "arial" , "helvetica" , sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">Amazon.com</span></span></span></span></a><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">, or
</span></span></span></span><a href="http://www.apress.com/9781430247616"><span style="color: #2288bb; font-family: "arial" , "helvetica" , sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">Apress</span></span></span></span></a><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">.<br /><br />The
author Harry He owns all the rights of this post. If you are going to use part
of or the whole of this ariticle in your blog or webpages, please add a
reference to </span></span></span></span><a href="http://codercareer.blogspot.com/"><span style="color: #2288bb; font-family: "arial" , "helvetica" , sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">http://codercareer.blogspot.com/</span></span></span></span></a><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">. If you are
going to use it in your books, please contact him via zhedahht@gmail.com .
Thanks.</span></span></span></span></span> </div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com5tag:blogger.com,1999:blog-4228859841453048938.post-82671588713766542542013-02-04T14:31:00.000+08:002014-07-09T11:19:08.372+08:00No. 37 - Missing Number in an Array<div dir="ltr" style="text-align: left;" trbidi="on">
<strong><u>Problem 1</u></strong>: An array <em>n</em> - 1 unique numbers in the range from 0 to <em>n</em> - 1. There is only one number in the range from 0 to <em>n</em> - 1 missing. Please write a function to find the missing number. <br />
<br />
<strong><u>Analysis:</u></strong> If we scan all numbers in the array, we could get the sum of all numbers. The sum is denoted as <em>sum1</em>. <br />
<br />
Additionally, we could also get the sum of all numbers in the range from 0 to <em>n</em> - 1, which is <em>n</em>*(<em>n</em>-1)/2. The sum is denoted as <em>sum2</em>. <br />
<br />
The only number missing in the array is the difference between <em>sum2</em> and <em>sum1</em>. <br />
<br />
This solution can be implemented with the following code:<br />
<br />
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> getOnceNumber_unsorted(<span style="color: blue;">int</span>*
numbers, <span style="color: blue;">int</span> length)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(numbers == NULL || length <= 0)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> -1;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> sum1 = 0;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> i = 0; i <
length; ++i)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>sum1
+= numbers[i];<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> sum2 = length * (length + 1) / 2;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> sum2 - sum1;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
Since we have to scan all numbers in the array, it costs O(<em>n</em>) time if the size of array is <em>n</em>.<br />
<br />
<strong><u>Problem 2</u></strong>: An <strong><em>sorted</em></strong> array <em>n</em> - 1 unique numbers in the range from 0 to <em>n</em> - 1. There is only one number in the range from 0 to <em>n</em> - 1 missing. Please write a function to find the missing number. <br />
<br />
<strong><u>Analysis:</u></strong> Of couse, we could use the solution above to solve this problem, which costs O(<em>n</em>) time. This solution does not utilize the properties of sorted arrays. <br />
<br />
Since numbers from 0 to <em>n</em> - 1 are sorted in an array, the first numbers should be same as their indexes. That's to say, the number 0 is located at the cell with index 0, the number 1 is located at the cell with index 1, and so on. If the missing number is denoted as <em>m</em>. Numbers less then <em>m</em> are located at cells with indexes same as values.<br />
<br />
The number <em>m </em>+ 1 is located at a cell with index <em>m</em>, The number <em>m</em> + 2 is located at a cell with index <em>m</em> + 1, and so on. We can see that, the missing number <em>m</em> is the first cell whose value is not identical to its value.<br />
<br />
Therefore, it is required to search in an array to find the first cell whose value is not identical to its value. Since the array is sorted, we could find it in O(lg<em>n</em>) time based on the binary search algorithm as implemented below:<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> getOnceNumber_sorted(<span style="color: blue;">int</span>*
numbers, <span style="color: blue;">int</span> length)<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(numbers == NULL || length <= 0)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> -1;<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> left = 0;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> right = length - 1;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">while</span>(left <= right)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> middle = (right + left) >> 1;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(numbers[middle] != middle)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(middle == 0 || numbers[middle - 1] == middle - 1)<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span><span style="color: blue;">return</span> middle;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>right = middle - 1;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">else</span><o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>left = middle + 1;<o:p></o:p></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}</span><br />
<span style="font-family: Consolas;"> </span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span><span style="color: black;">(</span><span style="color: black;">left</span><span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: black;">length</span><span style="color: silver;"> </span><span style="color: black;">)</span><span style="color: silver;"> <span style="color: #6aa84f;">// corrected by <span class="gD" email="kakooyang@gmail.com" name="Kyunghee Kim">Kyunghee Kim</span></span></span></span><br />
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="color: silver;"> <span style="color: blue;">return</span></span><span style="color: silver;"> </span><span style="color: black;">length</span><span style="color: black;">;</span><o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> -1;<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;">
<o:p><span style="font-family: Calibri;"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;">More coding interview questions are discussed in my
book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on </span></span></span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span style="color: #2288bb; font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;">Amazon.com</span></span></span></a><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;">, or </span></span></span><a href="http://www.apress.com/9781430247616"><span style="color: #2288bb; font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;">Apress</span></span></span></a><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;">.<br /><br />The author Harry He owns all the rights of
this post. If you are going to use part of or the whole of this ariticle in your
blog or webpages, please add a reference to </span></span></span><a href="http://codercareer.blogspot.com/"><span style="color: #2288bb; font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;">http://codercareer.blogspot.com/</span></span></span></a><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;"><span style="font-family: inherit;">. If you are going to use it in your books, please
contact him via zhedahht@gmail.com . Thanks.</span> </span></span></span></o:p></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com15tag:blogger.com,1999:blog-4228859841453048938.post-13481062280388407292013-02-03T20:40:00.001+08:002013-02-03T20:40:55.960+08:00No. 36 - Permutation <div dir="ltr" style="text-align: left;" trbidi="on">
<strong><u>Questions:</u></strong> Please print all permutations of a given string. For example, print “abc”, “acb”, “bac”, “bca”, “cab”, and “cba” when given the input string “abc”.<br />
<br />
<strong><u>Analysis:</u></strong> For many candidates, it is not a simple problem to get all permutations of a set of characters. In order to solve such a problem, candidates might try to divide it into simple subproblems. An input string is partitioned into two parts. The first part only contains the first characters, and the second part contains others. As shown in Figure 1, a string is divided into two parts with different background colors.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4uwSzsfYHLgeT3X0MsGLQ05MGZsH715zDnjU0oUFuuyLS6CwRyyicMYlKI25LohmCt_TvhVpxV0BNKZLQ1ODZiZxTBsA5hPHvc7r5BpU62BeWfueexay0mXtHa6cH9kmCi4SBIqH4pbI/s1600/Permutation.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4uwSzsfYHLgeT3X0MsGLQ05MGZsH715zDnjU0oUFuuyLS6CwRyyicMYlKI25LohmCt_TvhVpxV0BNKZLQ1ODZiZxTBsA5hPHvc7r5BpU62BeWfueexay0mXtHa6cH9kmCi4SBIqH4pbI/s1600/Permutation.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: The process to get permutations of a string. (a) A string is divided into two parts of which the<br />
first part only contains the first character, and the second part contains others (with gray background). (b)<br />
All characters in the second part are swapped with the first character one by one.</td></tr>
</tbody></table>
<br />
This solution gets permutations of a given string with two steps. The first step is to swap the first<br />
character with the following characters one by one. The second step is to get permutations of the string excluding the first character. Take the sample string “abc” as an example. It gets permutations of “bc” when the first character is ‘a’. It then swaps the first character with ‘b’, gets permutations of “ac”, and finally gets permutation of “ba” after swapping the first character with ‘c’.<br />
<br />
The process to get permutations of a string excluding the first character is similar to the process to<br />
get permutations of a whole string. Therefore, it can be solved recursively, as shown below:<br />
<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">void</span><span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">
Permutation(<span style="color: blue;">char</span>* pStr) <o:p></o:p></span></div>
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">{<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(pStr ==
NULL)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span>;<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>PermutationCore(pStr, pStr);<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">}<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">void</span><span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">
PermutationCore(<span style="color: blue;">char</span>* pStr, <span style="color: blue;">char</span>* pBegin) <o:p></o:p></span></div>
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;">{<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">char</span> *pCh =
NULL;<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">char</span> temp;<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(*pBegin
== <span style="color: #a31515;">'\0'</span>) <o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>printf(<span style="color: #a31515;">"%s\n"</span>,
pStr);<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">else</span> <o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">for</span>(pCh
= pBegin; *pCh != <span style="color: #a31515;">'\0'</span>; ++ pCh) <o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>temp = *pCh;<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>*pCh = *pBegin;<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>*pBegin = temp;<o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>PermutationCore(pStr, pBegin + 1);<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"></span></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>temp = *pCh;<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>*pCh = *pBegin;<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>*pBegin = temp;<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 115%; mso-no-proof: yes;">}</span><o:p></o:p><br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;">The discussion about
this problem is included in my book <Coding Interviews: Questions, Analysis
& Solutions>, with some revisions. You may find the details of this book
on </span></span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span style="color: #2288bb; font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;">Amazon.com</span></span></a><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;">, or </span></span><a href="http://www.apress.com/9781430247616"><span style="color: #2288bb; font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;">Apress</span></span></a><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;">.<br /><br />The author Harry He
owns all the rights of this post. If you are going to use part of or the whole
of this ariticle in your blog or webpages, please add a reference to </span></span><a href="http://codercareer.blogspot.com/"><span style="color: #2288bb; font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;">http://codercareer.blogspot.com/</span></span></a><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-family: inherit;">. If you are going to use it
in your books, please contact him via zhedahht@gmail.com . Thanks. </span></span></div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com5tag:blogger.com,1999:blog-4228859841453048938.post-84117200564213731672013-01-28T16:44:00.001+08:002013-01-28T17:00:41.056+08:00No. 35 - Depth of Binary Trees<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
<strong><u>Question 1</u></strong>: <span style="font-family: Arial, Helvetica, sans-serif;">How do you get the depth of a binary tree? Nodes from the root to a leaf form a path. Depth of a binary tree is the maximum length of all paths. For example, the depth of the binary tree in Figure 1 is 4, with the longest path through nodes 1, 2, 5, and 7.</span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDqn2Av-hrckRNyLr6A9hhCHMxQUaM3vBIqxUJ9npIUwv_jNvzIxRSHGz894TB99QIQSMWOLIiv4vkYzE5E8xOWsqtEYkCxWhGRrwURYgROTgw6movW2e5i3r4LBObKCHGYBnSu0yzask/s1600/treeDepth.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDqn2Av-hrckRNyLr6A9hhCHMxQUaM3vBIqxUJ9npIUwv_jNvzIxRSHGz894TB99QIQSMWOLIiv4vkYzE5E8xOWsqtEYkCxWhGRrwURYgROTgw6movW2e5i3r4LBObKCHGYBnSu0yzask/s1600/treeDepth.PNG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: A binary Tree with depth 4</td></tr>
</tbody></table>
<div style="text-align: left;">
<strong><u>Analysis: </u></strong><span style="font-family: Arial, Helvetica, sans-serif;">We have discussed how to store nodes of a path in a stack while traversing a binary tree in the blog "</span><a href="http://codercareer.blogspot.com/2011/09/no-04-paths-with-specified-sum-in.html" target="_blank"><span style="font-family: Arial, Helvetica, sans-serif;">Paths with Specified Sum in Binary Tree </span></a><span style="font-family: Arial, Helvetica, sans-serif;">". The depth of a binary tree is the length of the longest path. This solution works, but it is not the most concise one.</span></div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;"></span></div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;">The depth of a binary tree can be gotten in another way. If a binary tree has only one node, its depth is 1. If the root node of a binary tree has only a left subtree, its depth is the depth of the left subtree plus 1. Similarly, its depth is the depth of the right subtree plus 1 if the root node has only a right subtree. What is the depth if the root node has both left subtree and right subtree? It is the greater value of the depth of the left and right subtrees plus 1.</span></div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;"> </span></div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;">For example, the root node of the binary tree in Figure 1 has both left and right subtrees. The depth of the left subtree rooted at node 2 is 3, and the depth of the right subtree rooted at node 3 is 2, so the depth of the whole binary tree is 4; 1 plus the greater value of 3 and 2.</span></div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;"> </span></div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;">It is easy to implement this solution recursively, with little modification on the post-order traversal</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">algorithm, as shown below:</span></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> TreeDepth(BinaryTreeNode* pRoot) <o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(pRoot == NULL)<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> 0;<o:p></o:p></span></div>
<div style="text-align: left;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> nLeft = TreeDepth(pRoot->m_pLeft);<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> nRight = TreeDepth(pRoot->m_pRight);<o:p></o:p></span></div>
<div style="text-align: left;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> (nLeft > nRight) ? (nLeft + 1) : (nRight +
1);<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;"><strong><u>Question 2:</u></strong> How do you verify whether a binary tree is balanced? If the depth difference between a left subtree and right subtree of any node in a binary tree is not greater than 1, it is balanced. For instance, the binary tree in Figure 1 is balanced.</span></div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;"> </span></div>
<div style="text-align: left;">
<strong><u><span style="font-family: Arial, Helvetica, sans-serif;">Analysis:</span></u></strong></div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;"> </span></div>
<div style="text-align: left;">
<strong><em><span style="font-family: Arial, Helvetica, sans-serif;">Solution 1: Visiting Nodes for Multiple Times</span></em></strong></div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;"></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">According to the definition of balanced binary trees, this problem can be solved by getting the depth difference between the left and right subtrees of every node. When a node is visited, the function depth is invoked to get the depth of its left and right subtrees. If the depth different is 1 at most for all nodes in a binary tree, it is balanced. This solution can be implemented based on the TreeDepth discussed in the preceding problem, as shown below:</span></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">bool</span><span style="font-family: Consolas; font-size: 9.5pt;"> IsBalanced_Solution1(BinaryTreeNode* pRoot) <o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(pRoot == NULL)<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div style="text-align: left;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> left = TreeDepth(pRoot->m_pLeft);<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> right = TreeDepth(pRoot->m_pRight);<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> diff = left - right;<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(diff > 1 || diff < -1)<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<div style="text-align: left;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> IsBalanced_Solution1(pRoot->m_pLeft)<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>&& IsBalanced_Solution1(pRoot->m_pRight);<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast;">}</span> </div>
<span style="font-family: Consolas; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast;"></span><br />
<span style="font-family: Consolas; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast;"></span><br />
<span style="font-family: Consolas; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: SimSun; mso-fareast-language: ZH-CN; mso-fareast-theme-font: minor-fareast;"><div align="LEFT">
<span style="font-family: Arial, Helvetica, sans-serif;">This solution looks concise, but it is inefficient because it visits some nodes for multiple times. Take the binary tree in Figure 1 as an example. When the function TreeDepth takes the node 2 as a parameter, it visits nodes 4, 5, and 7. When it verifies whether the binary tree rooted at node 2 is balanced, it visits nodes 4, 5, and 7 again. Obviously, we could improve performance if nodes are visited only once.</span></div>
<div align="LEFT">
</div>
<div align="LEFT">
<strong><em><span style="font-family: Arial, Helvetica, sans-serif;">Solution 2: Visiting Every Node Only Once</span></em></strong></div>
<div align="LEFT">
<span style="font-family: Times New Roman;"></span> </div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;">If a binary tree is scanned with the post-order algorithm, its left and right subtrees are traversed before the root node. If we record the depth of the currently visited node (the depth of a node is the maximum length of paths from the node to its leaf nodes), we can verify whether the subtree rooted at the currently visited node is balanced. If any subtree is unbalanced, the whole tree is unbalanced.</span></div>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span><div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;">
</span></div>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span><div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;">This new solution can be implemented as shown in below:</span></div>
<div style="text-align: left;">
<span style="font-family: Times New Roman;"></span> </div>
<div style="text-align: left;">
<span style="font-family: Times New Roman;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">bool</span><span style="font-family: Consolas; font-size: 9.5pt;"> IsBalanced_Solution2(BinaryTreeNode* pRoot) <o:p></o:p></span></span></div>
<span style="font-family: Times New Roman;"><div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> depth = 0;<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> IsBalanced(pRoot, &depth);<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div style="text-align: left;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">bool</span><span style="font-family: Consolas; font-size: 9.5pt;"> IsBalanced(BinaryTreeNode* pRoot, <span style="color: blue;">int</span>* pDepth) <o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(pRoot == NULL) <o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>*pDepth = 0;<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div style="text-align: left;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> left, right;<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(IsBalanced(pRoot->m_pLeft, &left) </span><span style="font-family: Consolas; font-size: 9.5pt;">&& IsBalanced(pRoot->m_pRight, &right)) <o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">int</span> diff = left - right;<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">if</span>(diff <= 1 && diff >= -1) <o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>*pDepth = 1 + (left > right ? left : right);<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div style="text-align: left;">
</div>
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">return</span> <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<div style="text-align: left;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
</div>
</span><div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;">After verifying left and right subtrees of a node, the solution verifies the subtree rooted at the current visited node and passes the depth to verify its parent node. When the recursive process returns to the root node finally, the whole binary tree is verified. </span></div>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span><div style="text-align: left;">
<span style="font-family: Times New Roman;"></span> </div>
<div style="text-align: left;">
<span style="font-family: Arial, Helvetica, sans-serif;">The discussion about this problem is included in my book <Coding Interviews:
Questions, Analysis & Solutions>, with some revisions. You may find the
details of this book on </span><a href="http://www.amazon.com/Coding-Interviews-Questions-Analysis-Solutions/dp/1430247614"><span style="color: #2288bb; font-family: Arial, Helvetica, sans-serif;">Amazon.com</span></a><span style="font-family: Arial, Helvetica, sans-serif;">,
or </span><a href="http://www.apress.com/9781430247616"><span style="color: #2288bb; font-family: Arial, Helvetica, sans-serif;">Apress</span></a><span style="font-family: Arial, Helvetica, sans-serif;">.<br /><br />The author
Harry He owns all the rights of this post. If you are going to use part of or
the whole of this ariticle in your blog or webpages, please add a reference to
</span><a href="http://codercareer.blogspot.com/"><span style="color: #2288bb; font-family: Arial, Helvetica, sans-serif;">http://codercareer.blogspot.com/</span></a><span style="font-family: Arial, Helvetica, sans-serif;">.
If you are going to use it in your books, please contact him via
zhedahht@gmail.com . Thanks. <span style="font-family: "Courier New"; font-size: 9pt; line-height: 115%;"> </span></span></div>
</span><div style="text-align: left;">
<br /></div>
</div>
Harry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.com11