<div dir="ltr"><div class="gmail_default"><font face="trebuchet ms, sans-serif">Hi Jaden Geller, <span style="color:rgb(0,0,0)">Saagar </span>,</font></div><div class="gmail_default"><font face="trebuchet ms, sans-serif"><br></font></div><div class="gmail_default"><font face="trebuchet ms, sans-serif">   Thanks for the reply and advice. Array2D is really faster than the default two dimension. But I think as a general language, Swift should provide default efficient ways for multiple dimension array.</font></div><div class="gmail_default"><font face="trebuchet ms, sans-serif"><br></font></div><div class="gmail_default"><font face="trebuchet ms, sans-serif">  I found this problem when I write an algorithm. It is a DP problem and the description is here     <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/">https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/</a></font></div><div class="gmail_default"><font face="trebuchet ms, sans-serif"><br></font></div><div class="gmail_default"><font face="trebuchet ms, sans-serif">My code is as following, a little long. It define Array3D<span style="color:rgb(68,68,68)">, including two  strategies to implement  it. One is with default Array way: another is, like </span>Jaden said, using one dimension array. The second one is twice faster comparing to the first one. I think swift should optimize swift array. Implementing the very basic multiple dimension array is not developer friendly.</font></div><div class="gmail_default"><font face="comic sans ms, sans-serif"><br></font></div>







<p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s1">import</span><span class="gmail-s2"> Foundation</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s1">enum</span><span class="gmail-s2"> Strategy {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">case</span><span class="gmail-s2"> defaultImpletions</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">case</span><span class="gmail-s2"> useOneDemensionArray</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">}</font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s1">class</span><span class="gmail-s2"> Solution {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">static</span><span class="gmail-s2"> </span><span class="gmail-s1">func</span><span class="gmail-s2"> maxProfit(</span><span class="gmail-s1">_</span><span class="gmail-s2"> k: </span><span class="gmail-s3">Int</span><span class="gmail-s2">, </span><span class="gmail-s1">_</span><span class="gmail-s2"> prices: [</span><span class="gmail-s3">Int</span><span class="gmail-s2">], strategy:</span><span class="gmail-s4">Strategy</span><span class="gmail-s2">) -&gt; </span><span class="gmail-s3">Int</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">if</span><span class="gmail-s2"> (k &lt;= </span><span class="gmail-s5">0</span><span class="gmail-s2"> </span><span class="gmail-s3">||</span><span class="gmail-s2"> prices.</span><span class="gmail-s3">count</span><span class="gmail-s2"> &lt;= </span><span class="gmail-s5">1</span><span class="gmail-s2">) {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">            </span><span class="gmail-s1">return</span><span class="gmail-s2"> </span><span class="gmail-s5">0</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">        }</font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">if</span><span class="gmail-s2"> (k &gt;= prices.</span><span class="gmail-s3">count</span><span class="gmail-s2">) {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">            </span><span class="gmail-s1">return</span><span class="gmail-s2"> </span><span class="gmail-s4">quickSolve</span><span class="gmail-s2">(prices)</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">        }</font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">let</span><span class="gmail-s2"> profit = </span><span class="gmail-s4">Array3D</span><span class="gmail-s2">&lt;</span><span class="gmail-s3">Int</span><span class="gmail-s2">&gt;(x: </span><span class="gmail-s5">2</span><span class="gmail-s2">, y: k + </span><span class="gmail-s5">1</span><span class="gmail-s2">, z: </span><span class="gmail-s5">2</span><span class="gmail-s2">, value: </span><span class="gmail-s5">0</span><span class="gmail-s2">, strategy: strategy)</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">for</span><span class="gmail-s2"> j </span><span class="gmail-s1">in</span><span class="gmail-s2"> </span><span class="gmail-s5">0</span><span class="gmail-s2"> ... k { profit</span><span class="gmail-s4">[</span><span class="gmail-s5">0</span><span class="gmail-s2">, j, </span><span class="gmail-s5">1</span><span class="gmail-s4">]</span><span class="gmail-s2"> = -prices[</span><span class="gmail-s5">0</span><span class="gmail-s2">] }</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">for</span><span class="gmail-s2"> i </span><span class="gmail-s1">in</span><span class="gmail-s2"> </span><span class="gmail-s5">1</span><span class="gmail-s2"> ... prices.</span><span class="gmail-s3">count</span><span class="gmail-s2"> - </span><span class="gmail-s5">1</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">            </span><span class="gmail-s1">for</span><span class="gmail-s2"> j </span><span class="gmail-s1">in</span><span class="gmail-s2"> </span><span class="gmail-s5">0</span><span class="gmail-s2"> ... k {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">                profit</span><span class="gmail-s4">[</span><span class="gmail-s2">i &amp; </span><span class="gmail-s5">1</span><span class="gmail-s2">, j, </span><span class="gmail-s5">0</span><span class="gmail-s4">]</span><span class="gmail-s2"> = </span><span class="gmail-s3">max</span><span class="gmail-s2">(profit</span><span class="gmail-s4">[</span><span class="gmail-s2">(i - </span><span class="gmail-s5">1</span><span class="gmail-s2">) &amp; </span><span class="gmail-s5">1</span><span class="gmail-s2">, j, </span><span class="gmail-s5">0</span><span class="gmail-s4">]</span><span class="gmail-s2">,  j &gt; </span><span class="gmail-s5">0</span><span class="gmail-s2"> ? profit</span><span class="gmail-s4">[</span><span class="gmail-s2">(i - </span><span class="gmail-s5">1</span><span class="gmail-s2">) &amp; </span><span class="gmail-s5">1</span><span class="gmail-s2"> , j - </span><span class="gmail-s5">1</span><span class="gmail-s2">, </span><span class="gmail-s5">1</span><span class="gmail-s4">]</span><span class="gmail-s2"> + prices[i] : </span><span class="gmail-s5">0</span><span class="gmail-s2">)</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">                profit</span><span class="gmail-s4">[</span><span class="gmail-s2">i &amp; </span><span class="gmail-s5">1</span><span class="gmail-s2">, j, </span><span class="gmail-s5">1</span><span class="gmail-s4">]</span><span class="gmail-s2"> = </span><span class="gmail-s3">max</span><span class="gmail-s2">(profit</span><span class="gmail-s4">[</span><span class="gmail-s2">(i - </span><span class="gmail-s5">1</span><span class="gmail-s2">) &amp; </span><span class="gmail-s5">1</span><span class="gmail-s2">, j, </span><span class="gmail-s5">1</span><span class="gmail-s4">]</span><span class="gmail-s2">, profit</span><span class="gmail-s4">[</span><span class="gmail-s2">(i - </span><span class="gmail-s5">1</span><span class="gmail-s2">) &amp; </span><span class="gmail-s5">1</span><span class="gmail-s2">, j, </span><span class="gmail-s5">0</span><span class="gmail-s4">]</span><span class="gmail-s2"> - prices[i])</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">            }</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">        }</font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">return</span><span class="gmail-s2"> profit</span><span class="gmail-s4">[</span><span class="gmail-s2">(prices.</span><span class="gmail-s3">count</span><span class="gmail-s2"> - </span><span class="gmail-s5">1</span><span class="gmail-s2">) &amp; </span><span class="gmail-s5">1</span><span class="gmail-s2">, k, </span><span class="gmail-s5">0</span><span class="gmail-s4">]</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">    }</font></span></p><p class="gmail-p2"><span class="gmail-s2"><font face="georgia, serif">    </font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">static</span><span class="gmail-s2"> </span><span class="gmail-s1">func</span><span class="gmail-s2"> quickSolve(</span><span class="gmail-s1">_</span><span class="gmail-s2"> prices:[</span><span class="gmail-s3">Int</span><span class="gmail-s2">]) -&gt; </span><span class="gmail-s3">Int</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">var</span><span class="gmail-s2"> sum = </span><span class="gmail-s5">0</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">for</span><span class="gmail-s2"> i </span><span class="gmail-s1">in</span><span class="gmail-s2"> </span><span class="gmail-s5">1</span><span class="gmail-s2"> ... prices.</span><span class="gmail-s3">count</span><span class="gmail-s2"> - </span><span class="gmail-s5">1</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">            </span><span class="gmail-s1">if</span><span class="gmail-s2"> prices[i] - prices[i - </span><span class="gmail-s5">1</span><span class="gmail-s2">] &gt; </span><span class="gmail-s5">0</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">                sum += prices[i] - prices[i - </span><span class="gmail-s5">1</span><span class="gmail-s2">]</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">            }</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">        }</font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">return</span><span class="gmail-s2"> sum</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">    }</font></span></p><p class="gmail-p2"><span class="gmail-s2"><font face="georgia, serif">    </font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">static</span><span class="gmail-s2"> </span><span class="gmail-s1">func</span><span class="gmail-s2"> randomArrayWithCount(count: </span><span class="gmail-s3">Int</span><span class="gmail-s2">) -&gt; [</span><span class="gmail-s3">Int</span><span class="gmail-s2">] {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">var</span><span class="gmail-s2"> result = [</span><span class="gmail-s3">Int</span><span class="gmail-s2">]()</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">for</span><span class="gmail-s2"> </span><span class="gmail-s1">_</span><span class="gmail-s2"> </span><span class="gmail-s1">in</span><span class="gmail-s2"> </span><span class="gmail-s5">0</span><span class="gmail-s2"> ... count - </span><span class="gmail-s5">1</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">            result.</span><span class="gmail-s3">append</span><span class="gmail-s2">(</span><span class="gmail-s3">Int</span><span class="gmail-s2">(</span><span class="gmail-s3">arc4random</span><span class="gmail-s2">() % </span><span class="gmail-s3">UInt32</span><span class="gmail-s2">(</span><span class="gmail-s5">1000</span><span class="gmail-s2">)))</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">        }</font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">return</span><span class="gmail-s2"> result</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">    }</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">}</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif"><br></font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s1">class</span><span class="gmail-s2"> Array3D &lt;T&gt; {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">let</span><span class="gmail-s2"> x: </span><span class="gmail-s3">Int</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">let</span><span class="gmail-s2"> y: </span><span class="gmail-s3">Int</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">let</span><span class="gmail-s2"> z: </span><span class="gmail-s3">Int</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">var</span><span class="gmail-s2"> array: [</span><span class="gmail-s4">T</span><span class="gmail-s2">]!</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">var</span><span class="gmail-s2"> threeDemsionArray: [[[</span><span class="gmail-s4">T</span><span class="gmail-s2">]]]!</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">var</span><span class="gmail-s2"> strategy: </span><span class="gmail-s4">Strategy</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">init</span><span class="gmail-s2">(x:</span><span class="gmail-s3">Int</span><span class="gmail-s2">, y:</span><span class="gmail-s3">Int</span><span class="gmail-s2">, z:</span><span class="gmail-s3">Int</span><span class="gmail-s2">, value: </span><span class="gmail-s4">T</span><span class="gmail-s2">, strategy: </span><span class="gmail-s4">Strategy</span><span class="gmail-s2">) {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">self</span><span class="gmail-s2">.</span><span class="gmail-s4">x</span><span class="gmail-s2"> = x</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">self</span><span class="gmail-s2">.</span><span class="gmail-s4">y</span><span class="gmail-s2"> = y</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">self</span><span class="gmail-s2">.</span><span class="gmail-s4">z</span><span class="gmail-s2"> = z</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">self</span><span class="gmail-s2">.</span><span class="gmail-s4">strategy</span><span class="gmail-s2"> = strategy</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">if</span><span class="gmail-s2"> strategy == .</span><span class="gmail-s4">useOneDemensionArray</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">            </span><span class="gmail-s4">array</span><span class="gmail-s2"> = </span><span class="gmail-s3">Array</span><span class="gmail-s2">.</span><span class="gmail-s1">init</span><span class="gmail-s2">(repeating: value, count: x * y * z)</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        } </span><span class="gmail-s1">else</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">            </span><span class="gmail-s4">threeDemsionArray</span><span class="gmail-s2"> = </span><span class="gmail-s3">Array</span><span class="gmail-s2">(repeating: </span><span class="gmail-s3">Array</span><span class="gmail-s2">(repeating: </span><span class="gmail-s3">Array</span><span class="gmail-s2">(repeating: value, count: z), count: y), count: x)</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">        }</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">    }</font></span></p><p class="gmail-p2"><span class="gmail-s2"><font face="georgia, serif">    </font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">subscript</span><span class="gmail-s2">(i:</span><span class="gmail-s3">Int</span><span class="gmail-s2">, j:</span><span class="gmail-s3">Int</span><span class="gmail-s2">, k: </span><span class="gmail-s3">Int</span><span class="gmail-s2">) -&gt; </span><span class="gmail-s4">T</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">get</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p3"><font face="georgia, serif"><span class="gmail-s6">            </span><span class="gmail-s1">if</span><span class="gmail-s6"> </span><span class="gmail-s2">strategy</span><span class="gmail-s6"> == .</span><span class="gmail-s2">useOneDemensionArray</span><span class="gmail-s6"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">                </span><span class="gmail-s1">return</span><span class="gmail-s2"> </span><span class="gmail-s4">array</span><span class="gmail-s2">[</span><span class="gmail-s4">offset</span><span class="gmail-s2">(i: i, j: j, k: k)]</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">            } </span><span class="gmail-s1">else</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">                </span><span class="gmail-s1">return</span><span class="gmail-s2"> </span><span class="gmail-s4">threeDemsionArray</span><span class="gmail-s2">[i][j][k]</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">            }</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">        }</font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">set</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p3"><font face="georgia, serif"><span class="gmail-s6">            </span><span class="gmail-s1">if</span><span class="gmail-s6"> </span><span class="gmail-s2">strategy</span><span class="gmail-s6"> == .</span><span class="gmail-s2">useOneDemensionArray</span><span class="gmail-s6"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">                </span><span class="gmail-s4">array</span><span class="gmail-s2">[</span><span class="gmail-s4">offset</span><span class="gmail-s2">(i: i, j: j, k: k)] = newValue</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">            } </span><span class="gmail-s1">else</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">                </span><span class="gmail-s4">threeDemsionArray</span><span class="gmail-s2">[i][j][k] = newValue</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">            }</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">        }</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">    }</font></span></p><p class="gmail-p2"><span class="gmail-s2"><font face="georgia, serif">    </font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">func</span><span class="gmail-s2"> offset(i: </span><span class="gmail-s3">Int</span><span class="gmail-s2">, j: </span><span class="gmail-s3">Int</span><span class="gmail-s2">, k: </span><span class="gmail-s3">Int</span><span class="gmail-s2">) -&gt; </span><span class="gmail-s3">Int</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">var</span><span class="gmail-s2"> offset = i * </span><span class="gmail-s4">y</span><span class="gmail-s2"> * </span><span class="gmail-s4">z</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        offset += j * </span><span class="gmail-s4">z</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">        offset += k</font></span></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">        </span><span class="gmail-s1">return</span><span class="gmail-s2"> offset</span></font></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">    }</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">}</font></span></p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif"><br></font></span></p><p class="gmail-p2"><font face="georgia, serif"><span class="gmail-s2"></span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s1">var</span><span class="gmail-s2"> count = </span><span class="gmail-s5">10</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s1">for</span><span class="gmail-s2"> i </span><span class="gmail-s1">in</span><span class="gmail-s2"> </span><span class="gmail-s5">1</span><span class="gmail-s2"> ... </span><span class="gmail-s5">5</span><span class="gmail-s2"> {</span></font></p><p class="gmail-p3"><font face="georgia, serif"><span class="gmail-s6">    </span><span class="gmail-s1">let</span><span class="gmail-s6"> array = </span><span class="gmail-s2">Solution</span><span class="gmail-s6">.</span><span class="gmail-s2">randomArrayWithCount</span><span class="gmail-s6">(count: </span><span class="gmail-s2">count</span><span class="gmail-s6">)</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">var</span><span class="gmail-s2"> lastDate = </span><span class="gmail-s3">Date</span><span class="gmail-s2">()</span></font></p><p class="gmail-p3"><font face="georgia, serif"><span class="gmail-s6">    </span><span class="gmail-s1">_</span><span class="gmail-s6"> = </span><span class="gmail-s2">Solution</span><span class="gmail-s6">.</span><span class="gmail-s2">maxProfit</span><span class="gmail-s6">(</span><span class="gmail-s5">6</span><span class="gmail-s6">, array, strategy: .</span><span class="gmail-s2">useOneDemensionArray</span><span class="gmail-s6">)</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">let</span><span class="gmail-s2"> oneDemensionArrayRunTime = </span><span class="gmail-s3">Date</span><span class="gmail-s2">().</span><span class="gmail-s3">timeIntervalSince</span><span class="gmail-s2">(lastDate) * </span><span class="gmail-s5">1000</span></font></p><p class="gmail-p2"><font face="georgia, serif"><span class="gmail-s2">    lastDate = </span><span class="gmail-s3">Date</span><span class="gmail-s2">()</span><br></font></p><p class="gmail-p3"><font face="georgia, serif"><span class="gmail-s6">    </span><span class="gmail-s1">_</span><span class="gmail-s6"> = </span><span class="gmail-s2">Solution</span><span class="gmail-s6">.</span><span class="gmail-s2">maxProfit</span><span class="gmail-s6">(</span><span class="gmail-s5">6</span><span class="gmail-s6">, array, strategy: .</span><span class="gmail-s2">defaultImpletions</span><span class="gmail-s6">)</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s1">let</span><span class="gmail-s2"> defaultImpletionsRuntime = </span><span class="gmail-s3">Date</span><span class="gmail-s2">().</span><span class="gmail-s3">timeIntervalSince</span><span class="gmail-s2">(lastDate) * </span><span class="gmail-s5">1000</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s3">print</span><span class="gmail-s2">(</span><span class="gmail-s7">&quot;count: </span><span class="gmail-s2">\</span><span class="gmail-s7">(</span><span class="gmail-s4">count</span><span class="gmail-s7">) run time: oneDemension: </span><span class="gmail-s2">\</span><span class="gmail-s7">(</span><span class="gmail-s2">oneDemensionArrayRunTime</span><span class="gmail-s7">), defaultImpl:</span><span class="gmail-s2">\</span><span class="gmail-s7">(</span><span class="gmail-s2">defaultImpletionsRuntime</span><span class="gmail-s7">) ratio:</span><span class="gmail-s2">\</span><span class="gmail-s7">(</span><span class="gmail-s2">defaultImpletionsRuntime / oneDemensionArrayRunTime</span><span class="gmail-s7">)&quot;</span><span class="gmail-s2">)</span></font></p><p class="gmail-p1"><font face="georgia, serif"><span class="gmail-s2">    </span><span class="gmail-s4">count</span><span class="gmail-s2"> *= </span><span class="gmail-s5">10</span></font></p><p class="gmail-p1">













































































































</p><p class="gmail-p1"><span class="gmail-s2"><font face="georgia, serif">}</font></span></p><div class="gmail_default">








<p class="gmail-p2"><font face="comic sans ms, sans-serif" color="#444444"><span class="gmail-s2"></span></font></p>






































<p class="gmail-p2"><font face="comic sans ms, sans-serif" color="#444444"><span class="gmail-s2"></span></font></p>










































<p class="gmail-p2"><font face="comic sans ms, sans-serif" color="#444444"><span class="gmail-s2"></span></font></p><p class="gmail-p2"><font face="comic sans ms, sans-serif" color="#444444"><span class="gmail-s2"></span></font></p>











</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><blockquote><i><font face="georgia, serif"><br></font></i></blockquote><i><font face="georgia, serif">best wishes for you </font></i></div></div>
<br><div class="gmail_quote">2017-04-19 17:55 GMT+08:00 Jaden Geller <span dir="ltr">&lt;<a href="mailto:jaden.geller@gmail.com" target="_blank">jaden.geller@gmail.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi Hbucius,<div><br></div><div>You can define your own `Array2D` type that uses a 1-dimensional array as backing (for fast performance) but exposes a high-level 2-dimensional array API.</div><div><br></div><div>Here’s a really primitive starting point I found on GitHub:</div><div><a href="https://github.com/raywenderlich/swift-algorithm-club/blob/master/Array2D/Array2D.swift" target="_blank">https://github.com/<wbr>raywenderlich/swift-algorithm-<wbr>club/blob/master/Array2D/<wbr>Array2D.swift</a></div><div><br></div><div>Cheers,</div><div>Jaden Geller</div><div><div class="h5"><div><br><div><blockquote type="cite"><div>On Apr 18, 2017, at 11:39 PM, Saagar Jha via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-1040982056930886070Apple-interchange-newline"><div><div style="word-wrap:break-word">It might be helpful if you showed a bit more of the code you’re working on, so that we can better see what you’re trying to do. Is there any operation in particular that is slow?<div><br></div><div>Also, CC’ing swift-users since I think it belongs there.</div><div><br><div>
<div style="letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word">Saagar Jha</div>

</div>
<br><div><blockquote type="cite"><div>On Apr 18, 2017, at 22:57, Hbucius Smith via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-1040982056930886070Apple-interchange-newline"><div><div dir="ltr"><div class="gmail_default" style="font-family:simsun,serif;font-size:small">Hello Swift community,</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><div class="gmail_default" style="font-family:simsun,serif;font-size:small">    When I used multidimensional array in swift, I found it is very low efficiency.</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">  </div><div class="gmail_default" style="font-family:simsun,serif;font-size:small">    I used in the following way : </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">   </div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><b>    var array = Array.init(repeating: Array.init(repeating: 0, count: 5), count: 5)</b></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><b>   </b></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><b>    array[0][0] = 0</b></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">   </div><div class="gmail_default" style="font-family:simsun,serif;font-size:small">  I have read some posts in stack overflow. It suggests using one dimension to fake multidimensional array. I think it is too ugly. DO we have better choice for this ?</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">    </div><div class="gmail_default" style="font-family:simsun,serif;font-size:small">    </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_-1040982056930886070gmail_signature" data-smartmail="gmail_signature"><i><font face="georgia, serif">best wishes for you </font></i></div></div>
</div>
______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div><br></div></div>______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div><br></div></div></div></div></blockquote></div><br></div>