<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Elliot Tian's Playground]]></title><description><![CDATA[Thoughts, stories and ideas.]]></description><link>https://tian321.com/</link><image><url>https://tian321.com/favicon.png</url><title>Elliot Tian&apos;s Playground</title><link>https://tian321.com/</link></image><generator>Ghost 5.87</generator><lastBuildDate>Sun, 03 Aug 2025 08:24:41 GMT</lastBuildDate><atom:link href="https://tian321.com/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Leetcode Crash Note: Array]]></title><description><![CDATA[<p>Here we first talk about one dimensional array: which is itself, array.<br>
Then we are going to introduce the two dimensional array: Metrics.</p>
<h4 id="array">Array:</h4>
<p>As always we go with tricks in arrays first!</p>
<h4 id="array-tricks">Array Tricks!</h4>
<ol>
<li>initialization</li>
</ol>
<pre><code class="language-python">array = [1, 3, 4]
# use * to initialize
l = [0] * len(array)
# use list generator</code></pre>]]></description><link>https://tian321.com/leetcode-topic-crash-array/</link><guid isPermaLink="false">6694675e91fcbf0001b754ae</guid><dc:creator><![CDATA[Elliot Tian]]></dc:creator><pubDate>Mon, 15 Jul 2024 00:05:50 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1546900703-cf06143d1239?crop=entropy&amp;cs=tinysrgb&amp;fit=max&amp;fm=jpg&amp;ixid=M3wxMTc3M3wwfDF8c2VhcmNofDEzfHxjb2RlfGVufDB8fHx8MTcyMTAwMTkyMHww&amp;ixlib=rb-4.0.3&amp;q=80&amp;w=2000" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1546900703-cf06143d1239?crop=entropy&amp;cs=tinysrgb&amp;fit=max&amp;fm=jpg&amp;ixid=M3wxMTc3M3wwfDF8c2VhcmNofDEzfHxjb2RlfGVufDB8fHx8MTcyMTAwMTkyMHww&amp;ixlib=rb-4.0.3&amp;q=80&amp;w=2000" alt="Leetcode Crash Note: Array"><p>Here we first talk about one dimensional array: which is itself, array.<br>
Then we are going to introduce the two dimensional array: Metrics.</p>
<h4 id="array">Array:</h4>
<p>As always we go with tricks in arrays first!</p>
<h4 id="array-tricks">Array Tricks!</h4>
<ol>
<li>initialization</li>
</ol>
<pre><code class="language-python">array = [1, 3, 4]
# use * to initialize
l = [0] * len(array)
# use list generator
l = [0 for _ in len(array)]
</code></pre>
<ol start="2">
<li>From behind</li>
</ol>
<pre><code class="language-python">array = [1, 2, 3]
lastOne = array[-1]
lastTwo = array[-2:]
lastThree = array[-3:]
</code></pre>
<ol start="3">
<li>Copy array</li>
</ol>
<pre><code class="language-python">array = [1, 2, 3]
# WRONG!!!!
c = array

# shadow copy: can only copy array, cannot copy nested array!!
# CORRECT COPY
c = array[:]
# or
c = array.copy()

# deep copy: can copy whatever you want
import copy
c = copy.deepcopy(array)
</code></pre>
<ol start="4">
<li>Enumerate</li>
</ol>
<pre><code class="language-python">array = [1, 2, 3]

for index, item in enumerate(array):
    print(&quot;index:&quot;, index)
    print(&quot;item:&quot;, item)
# Result:
# index: 0
# item 1
# index: 1
# item 2
# index: 2
# item 3
</code></pre>
<ol start="5">
<li>zip</li>
</ol>
<pre><code class="language-python">x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)
# zipped = [(1, 4), (2, 5), (3, 6)
</code></pre>
<ol start="6">
<li>sort</li>
</ol>
<pre><code class="language-python">l1 = [(1,2), (0,1), (3,10)]
l2 = l1[:]

l2.sort()
# l2 = [(0, 1), (1, 2), (3, 10)]
l2 = sorted(l1)
# L2 = [(0, 1), (1, 2), (3, 10)]

l2.sort(reverse=True)
# l2 = [(3, 10), (1, 2), (0, 1)]
l2 = sorted(l1, reverse=True)
# l2 = [(3, 10), (1, 2), (0, 1)]

l2.sort(key=lambda x: x[1])
# l2 = [(0, 1), (1, 2), (3, 10)]
l2 = sorted(l1, key=lambda x: x[1])
# l2 = [(0, 1), (1, 2), (3, 10)]

l2.sort(key=lambda x: x[1], reverse=1)
# l2 = [(3, 10), (1, 2), (0, 1)]
l2 = sorted(l1, key=lambda x: x[1], reverse=1)
# l2 = [(3, 10), (1, 2), (0, 1)]
</code></pre>
<ol start="7">
<li>conversion</li>
</ol>
<pre><code class="language-python">array = [1, 1, 2, 3]
# convert to set
b = set(array)
# -&gt; b = {1, 2, 3}

# convert to string
# cannot directly convert
str_array = []
for item in array:
    str_array.append(str(item))
b = &apos;&apos;.join(str_array)
# b = &quot;1123&quot;
</code></pre>
<h4 id="leetcode-with-array">Leetcode with Array:</h4>
<h5 id="grind-75-selection">GRIND 75 selection:</h5>
<h5 id="121-best-time-to-buy-and-sell-stock">121. Best Time to Buy and Sell Stock</h5>
<p>Description:</p>
<p>Input: prices = [7,1,5,3,6,4]<br>
Output: 5<br>
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.<br>
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.</p>
<p>Solution:</p>
<pre><code class="language-python">def maxProfit(self, prices: List[int]) -&gt; int:
    min_price, max_profit = prices[0], float(&apos;-inf&apos;)
    
    for i in range(len(prices)):
        min_price = min(prices[i], min_price)
        max_profit = max(max_profit, prices[i] - min_price)


    return max_profit
</code></pre>
<h5 id="238-move-zeroes">238. Move Zeroes</h5>
<p>Description:</p>
<p>Solution 1 with exchange:</p>
<pre><code class="language-python">def moveZeroes(self, nums: List[int]) -&gt; None:
    pos = 0
    for i in range(len(nums)):
        ele = nums[i]
        if ele != 0:
            nums[pos], nums[i] = nums[i], nums[pos]
            pos += 1
</code></pre>
<h5 id="566reshape-the-matrix">566.Reshape the Matrix</h5>
<p>Description:<br>
Solution 1 with converting to one line then convert to others:</p>
<pre><code class="language-python">    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -&gt; List[List[int]]:
        rows = len(mat)
        cols = len(mat[0])     
        if r * c != rows * cols:
            return mat
        res = [mat[row][col] for row in range(rows) for col in range(cols)]

        ret = []
        for i in range(0, len(res), c):
            ret.append(res[i:i + c])
            
        return ret
</code></pre>
<p>Solution 2 with / and % (my god, 0ms):</p>
<pre><code class="language-java">public int[][] matrixReshape(int[][] nums, int r, int c) {
    int m = nums.length, n = nums[0].length;
    if (m * n != r * c) {
        return nums;
    }
    int[][] reshapedNums = new int[r][c];
    int index = 0;
    for (int i = 0; i &lt; r; i++) {
        for (int j = 0; j &lt; c; j++) {
            reshapedNums[i][j] = nums[index / n][index % n];
            index++;
        }
    }
    return reshapedNums;
}
</code></pre>
<h5 id="485-max-consecutive-1">485. Max consecutive 1</h5>
<pre><code class="language-python">def findMaxConsecutiveOnes(self, nums: List[int]) -&gt; int:
        maxn, cur = 0, 0
        for item in nums:
            cur = 0 if item == 0 else cur + 1
            maxn = max(maxn, cur)
        return maxn
</code></pre>
<h5 id="240-search-a-2d-matrix-ii">240. Search a 2D Matrix II</h5>
<pre><code class="language-Python">def searchMatrix(self, matrix: List[List[int]], target: int) -&gt; bool:
    rows = len(matrix)
    cols = len(matrix[0])
    row, col = rows - 1, 0
    while col &lt; cols and row &gt;= 0:
        if matrix[row][col] &gt; target:
            row -= 1
        elif matrix[row][col] &lt; target:
            col += 1
        else:
            return 1
    return 0
</code></pre>
]]></content:encoded></item></channel></rss>