<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Array on Shiglass</title>
    <link>https://hirotoshimizu.github.io/tags/array/</link>
    <description>Recent content in Array on Shiglass</description>
    <generator>Hugo -- gohugo.io</generator>
    <lastBuildDate>Wed, 20 Oct 2021 11:27:43 +0900</lastBuildDate><atom:link href="https://hirotoshimizu.github.io/tags/array/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Happy Number</title>
      <link>https://hirotoshimizu.github.io/posts/happy-number/</link>
      <pubDate>Wed, 20 Oct 2021 11:27:43 +0900</pubDate>
      
      <guid>https://hirotoshimizu.github.io/posts/happy-number/</guid>
      <description>パターン1 7 → 49 → 97 → 130 → 10 → 1
パターン2 2 → 4 → 16 → 37 → 30 → 9 → 81 → 65 → 61 → 37
Algorithm
  number n の次の数が何かを調べる
  cycle に入ったかを調べる。
  の調べ方 19 → 1^2 + 9^2 = 82
  変数 output に 0 を代入し、 while で n がある間
n = 19 の場合、
1の位を取得 19 % 10 = 9</description>
    </item>
    
    <item>
      <title>Replace Elements With Greatest Element on Right Side</title>
      <link>https://hirotoshimizu.github.io/posts/replace-elements-with-greatest-element-on-right-side/</link>
      <pubDate>Mon, 18 Oct 2021 20:06:14 +0900</pubDate>
      
      <guid>https://hirotoshimizu.github.io/posts/replace-elements-with-greatest-element-on-right-side/</guid>
      <description>https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/
一番最後が -1 になるので、 変数 right_max に -1 を入れる。
後ろからループしていき、 現在の値 と right_max の大きい値を new_max に入れる。 現在の値に right_max を代入し、 right_max の値に nex_max を代入しループを続ける。
 | 17 | 18 | 5 | 4 | 6 | 1 | -1 18 6 6 6 1 -1
class Solution: def replaceElements(self, arr: List[int]) -&amp;gt; List[int]: right_max = -1 for i in range(len(arr) -1, -1, -1): new_max = max(right_max, arr[i]) arr[i] = right_max right_max = new_max </description>
    </item>
    
    <item>
      <title>Merge Sorted Array</title>
      <link>https://hirotoshimizu.github.io/posts/merge-sorted-array/</link>
      <pubDate>Mon, 18 Oct 2021 14:52:25 +0900</pubDate>
      
      <guid>https://hirotoshimizu.github.io/posts/merge-sorted-array/</guid>
      <description>https://leetcode.com/problems/merge-sorted-array/
この問題は pointer を 3つ使う。 1つ目は、nums1 の最後に pointer を設定。 2つ目は、nums1 の m に pointer を設定。 3つ目は、nums2 の 最後に pointer を設定。
 | 1 | 2 | 3 | 0 | 0 | 0 |  ↑ ↑   | 2 | 5 | 6 |  ↑  nums1[m - 1] と nums2[n - 1] の値を比べて、 nums2[n - 1] の値が大きいので、 nums1[last] = nums2[n - 1] を設定する。 そして nums2 の n の pointer を -1 してずらす。 また nums1 の last の pointer も -1 する。</description>
    </item>
    
    <item>
      <title>Two Sum Ii Input Array Is Sorted</title>
      <link>https://hirotoshimizu.github.io/posts/two-sum-ii-input-array-is-sorted/</link>
      <pubDate>Mon, 18 Oct 2021 13:31:53 +0900</pubDate>
      
      <guid>https://hirotoshimizu.github.io/posts/two-sum-ii-input-array-is-sorted/</guid>
      <description>https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
この問題は pointer を2つ使い target になる数を探していく。
pointer を left と right に置き、 left と right の合計が target の値と同じかを計算する。
合計が大きい場合は right の pointer を左にずらし、 合計が小さい場合は left の pointer を右にずらしていく。
合計がtarget の値になるまでループして index の位置を返す。
target = 9
 | 1 | 3 | 4 | 5 | 7 | 11 | 　↑　↑
1 + 11 = 12 &amp;gt; 9
 | 1 | 3 | 4 | 5 | 7 | 11 | 　↑　↑</description>
    </item>
    
    <item>
      <title>Maximum Subarray</title>
      <link>https://hirotoshimizu.github.io/posts/maximum-subarray/</link>
      <pubDate>Tue, 21 Sep 2021 13:23:28 +0900</pubDate>
      
      <guid>https://hirotoshimizu.github.io/posts/maximum-subarray/</guid>
      <description>https://leetcode.com/problems/maximum-subarray/
 | -2 | 1 | -3 | 4 | -1 | 2 | 1 | -5 | 4 | 最初の3つの合計(-2 + 1 -3)はマイナスになるので、 そのため subarray のスタートは 4 もしくは 4以降の数字になる。
合計が正の数になる場合は保持しておく。 変数に0 を代入して、 iterate しながら数字を追加していく。 合計がマイナスになる場合は保持する必要がないので、0 にする。
Time complexity: O(n) Mem complexity: O(1)
class Solution(object): def maxSubArray(self, nums): &amp;#34;&amp;#34;&amp;#34; :type nums: List[int] :rtype: int &amp;#34;&amp;#34;&amp;#34; max_subarray = nums[0] current_subarray = 0 for n in nums: if current_subarray &amp;lt; 0: current_subarray = 0 current_subarray += n max_subarray = max(max_subarray, current_subarray) return max_subarray </description>
    </item>
    
    <item>
      <title>Best Time to Buy and Sell Stock</title>
      <link>https://hirotoshimizu.github.io/posts/best-time-to-buy-and-sell-stock/</link>
      <pubDate>Tue, 21 Sep 2021 10:49:58 +0900</pubDate>
      
      <guid>https://hirotoshimizu.github.io/posts/best-time-to-buy-and-sell-stock/</guid>
      <description>https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
pointer を2つ使う。 left が right より低い時点で売る可能性あり。 max_profit に最大の profit を格納。
right の pointer は右に移動し続け
left の　pointer は prices[l] が prices[r] より大きい際に、 left の pointer を right の pointer の位置に移動する。
Time complexity: O(n)
Mem complexity: O(1)
class Solution(object): def maxProfit(self, prices): &amp;#34;&amp;#34;&amp;#34; :type prices: List[int] :rtype: int &amp;#34;&amp;#34;&amp;#34; l, r = 0, 1 max_profit = 0 while r &amp;lt; len(prices): if prices[l] &amp;lt; prices[r]: profit = prices[r] - prices[l] max_profit = max(max_profit, profit) else: l = r r += 1 return max_profit </description>
    </item>
    
    <item>
      <title>Two Sum</title>
      <link>https://hirotoshimizu.github.io/posts/two-sum/</link>
      <pubDate>Tue, 21 Sep 2021 10:05:05 +0900</pubDate>
      
      <guid>https://hirotoshimizu.github.io/posts/two-sum/</guid>
      <description>https://leetcode.com/problems/two-sum/
 | 2 | 7 | 11 | 15 | 　↑　↑ 全てのコンビネーションを確認しようと Brute Force で問題を解こうとすると ループの中でループをするのでo(n^2)となる。
 | 2 | 7 | 11 | 15 | そのため HashMapを使う必要がある。 ループして array の中を確認する際に、 HashMap に val と key を持たせる。 ループ時に target - val の値が HashMap にあれば、 合計の値を持つ index が return できる。
 | 2 | 7 | 11 | 15 |  ↑   | HashMap | | val : index | | 2 : 1 |  | HashMap | | val : index | | 2 : 1 | | 7 : 2 |← | 11 : 3 | | 15 : 4 | target は 9 なので val 7 を確認時に HashMap にすでに 2 が入っているので この時点で return できる。</description>
    </item>
    
  </channel>
</rss>
