<div dir="ltr"><div>When compiling that from the command line, I get the following (after about 6 seconds):<br></div><div><br></div><div><div>test.swift:7:18: error: cannot assign through subscript: 'dp' is a 'let' constant</div><div> dp[0][0] = 0</div><div> ~~ ^</div><div>/.../</div></div><div><br></div><div>After fixing that (changing let dp to var dp), it will compile successfully, still taking a very long time though. This usually means that some expression(s) in the code happen to be a bit hard on the type checker (in its current state).</div><div>I tried the latest dev snapshot and it is a bit faster, perhaps 3 s instead of 6.</div><div><br></div><div>Anyway, here is a logically equivalent rewrite which will compile faster:</div><div><br></div><div><div>class Solution {</div><div> func rob(nums: [Int]) -> Int {</div><div> guard nums.count > 0 else { return 0 }</div><div> var dp = Array.init(repeating: Array.init(repeating: 0, count: nums.count),</div><div> count: 2)</div><div> dp[0][0] = 0</div><div> dp[0][1] = nums[0]</div><div> for i in 1 ..< nums.count {</div><div> <span style="white-space:pre-wrap"> </span>let dp_iMinus1_0 = dp[i - 1][0] </div><div> <span style="white-space:pre-wrap"> </span>let dp_iMinus1_1 = dp[i - 1][1] </div><div> dp[i][0] = max(dp_iMinus1_0, dp_iMinus1_1)</div><div> dp[i][1] = dp_iMinus1_0 + nums[i]</div><div> }</div><div> return 0</div><div> }</div><div>}</div></div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 5, 2017 at 2:32 PM, Hbucius Smith via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:simsun,serif;font-size:small">Hi Swift-Users,</div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"> when I compiled the code, Xcode cannot stop, I do not know why. It is very strange. Can anyone help ? Here is the code. I am using Xcode 8.1</div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small">
<p class="m_9138524867132845885gmail-p1"><span class="m_9138524867132845885gmail-s2">class</span><span class="m_9138524867132845885gmail-s1"> Solution {</span><br><span class="m_9138524867132845885gmail-s1"></span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">func</span><span class="m_9138524867132845885gmail-s1"> rob(nums: [</span><span class="m_9138524867132845885gmail-s3">Int</span><span class="m_9138524867132845885gmail-s1">]) -> </span><span class="m_9138524867132845885gmail-s3">Int</span><span class="m_9138524867132845885gmail-s1"> {</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">guard</span><span class="m_9138524867132845885gmail-s1"> nums.count > </span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">else</span><span class="m_9138524867132845885gmail-s1"> { </span><span class="m_9138524867132845885gmail-s2">return</span><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1"> }</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">let</span><span class="m_9138524867132845885gmail-s1"> dp = Array.</span><span class="m_9138524867132845885gmail-s2">init</span><span class="m_9138524867132845885gmail-s1">(repeating: Array.</span><span class="m_9138524867132845885gmail-s2">init</span><span class="m_9138524867132845885gmail-s1">(repeating: </span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">, count: nums.count),</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> count: </span><span class="m_9138524867132845885gmail-s4">2</span><span class="m_9138524867132845885gmail-s1">)</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> dp[</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">] = </span><span class="m_9138524867132845885gmail-s4">0</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> dp[</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">] = nums[</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">]</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">for</span><span class="m_9138524867132845885gmail-s1"> i </span><span class="m_9138524867132845885gmail-s2">in</span><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1"> ..< nums.count {</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> dp[i][</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">] = max(dp[i - </span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">], dp[i - </span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">])</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> dp[i][</span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">] = dp[i - </span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">] + nums[i]</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> }</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">return</span><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s4">0</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> }</span></p>
<p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1">}</span></p></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br></div><br clear="all"><div><div class="m_9138524867132845885gmail_signature"><i><font face="georgia, serif">best wishes for you </font></i></div></div>
</div>
<br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>