<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>옳은 길로 바르게</title>
    <link>https://sgwin.tistory.com/</link>
    <description>sgwin 님의 블로그 입니다.</description>
    <language>ko</language>
    <pubDate>Sun, 7 Jun 2026 07:55:27 +0900</pubDate>
    <generator>TISTORY</generator>
    <ttl>100</ttl>
    <managingEditor>snp0301</managingEditor>
    <item>
      <title>Dijkstra: 최단거리, 근데 이제 음수가 아닌 가중치를 곁들인.</title>
      <link>https://sgwin.tistory.com/6</link>
      <description>&lt;h3&gt;2025년 11월 5일&lt;/h3&gt;
&lt;p&gt;Dijkstra는 가중치를 고려해 어떤 노드에서 다른 노드의 최단거리를 구할 때 사용할 수 있다.&lt;br&gt;음수 가중치나 싸이클이 있을 때는 사용할 수 없다.&lt;/p&gt;
&lt;p&gt;BFS에서 queue에 enqueue할 때, 같은 거리를 가진 케이스들이 연속적으로 enqueue 됨을 보장해야 하는 것처럼&lt;br&gt;Dijkstra에서는 priority_queue(heap)을 사용해 지금 가진 정보가 최신 정보임을 보장해야 한다.&lt;/p&gt;
&lt;p&gt;즉, pq를 믿어야 동작한다. pq는 &lt;code&gt;greater&amp;lt;&amp;gt;&lt;/code&gt;와 같은 비교 기준을 가지고 노드를 배치하기 때문에&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;기준을 잘 정하고&lt;/li&gt;
&lt;li&gt;기준에 맞게 heapPush 해야한다.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;계속 dist 배열을 빼먹고 있다는 것은 Dijkstra를 이해하지 못했기 때문에 생기는 일이다.&lt;/p&gt;</description>
      <category>Algorithm &amp;amp; PS/Algorithm</category>
      <author>snp0301</author>
      <guid isPermaLink="true">https://sgwin.tistory.com/6</guid>
      <comments>https://sgwin.tistory.com/6#entry6comment</comments>
      <pubDate>Wed, 5 Nov 2025 16:44:30 +0900</pubDate>
    </item>
    <item>
      <title>mergeSort</title>
      <link>https://sgwin.tistory.com/5</link>
      <description>&lt;h3&gt;Time Complexity&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;O(n log n)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Code&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
using namespace std;

void merge(vector&amp;lt;int&amp;gt;&amp;amp; vec, int left, int mid, int right){
    int i,j,k;
    int fIdx = mid - left + 1;
    int sIdx = right - mid;

    vector&amp;lt;int&amp;gt; leftVc(fIdx), rightVc(sIdx);

    for(i=0; i&amp;lt;fIdx; ++i) leftVc[i] = vec[left+i];
    for(j=0; j&amp;lt;sIdx; ++j) rightVc[j] = vec[mid+1+j];

    i=0;
    j=0;
    k=left;

    while (i &amp;lt; fIdx &amp;amp;&amp;amp; j &amp;lt;sIdx){
        if (leftVc[i] &amp;lt;= rightVc[j]){
            vec[k] = leftVc[i];
            ++i;
        }
        else{
            vec[k] = rightVc[j];
            ++j;
        }
        ++k;
    }

    while (i &amp;lt; fIdx){
        vec[k] = leftVc[i];
        ++i;
        ++k;
    }

    while (j &amp;lt; sIdx){
        vec[k] = rightVc[j];
        ++j;
        ++k;
    }
}

void mergeSort(vector&amp;lt;int&amp;gt;&amp;amp; vec, int left, int right){
    if (left &amp;lt;right){ // base case: size가 1인 경우 left == right가 true이므로 재귀 종료.
        int mid = left + (right - left) / 2;

        mergeSort(vec,left,mid);
        mergeSort(vec,mid+1,right);

        merge(vec, left, mid, right);
    }

}&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Programming Language/cpp</category>
      <author>snp0301</author>
      <guid isPermaLink="true">https://sgwin.tistory.com/5</guid>
      <comments>https://sgwin.tistory.com/5#entry5comment</comments>
      <pubDate>Mon, 13 Oct 2025 19:07:37 +0900</pubDate>
    </item>
    <item>
      <title>binarySearch</title>
      <link>https://sgwin.tistory.com/4</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;Use binarySearch to find an element in &lt;b&gt;sorted or monotonic space&lt;/b&gt;.&lt;/p&gt;
&lt;pre class=&quot;arduino&quot;&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;

using namespace std;

int binarySearch(vector&amp;lt;int&amp;gt; &amp;amp;arr, int x){
    int start = 0;
    int end = (int)arr.size() - 1;
    while (start &amp;lt;= end){
        int mid = start + (end - start) / 2;
        if (arr[mid] == x) return mid; // if found, return that index
        if (arr[mid] &amp;lt; x) start = mid + 1; // if x is greater than the middle one, ignore left half
        else end = mid - 1; // if x is smaller than the middle one, ignore right half
    }

    return -1; // if we couldn't find the element, return -1 or any failure sign code
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;Both average and worst case has &lt;b&gt;O(log N)&lt;/b&gt; time complexity.&lt;/p&gt;</description>
      <category>Algorithm &amp;amp; PS/Algorithm</category>
      <category>binarysearch</category>
      <category>binarySearch in C++</category>
      <category>binarySearch in Cpp</category>
      <category>C++ algorithm</category>
      <category>cpp algorithm</category>
      <author>snp0301</author>
      <guid isPermaLink="true">https://sgwin.tistory.com/4</guid>
      <comments>https://sgwin.tistory.com/4#entry4comment</comments>
      <pubDate>Sun, 12 Oct 2025 16:39:23 +0900</pubDate>
    </item>
    <item>
      <title>Is ++i better than i++?</title>
      <link>https://sgwin.tistory.com/3</link>
      <description>&lt;p&gt;Assuming that prefix and postfix operations perform the same computation,&lt;br&gt;&lt;strong&gt;&lt;code&gt;++i&lt;/code&gt;&lt;/strong&gt; is generally more efficient than &lt;code&gt;i++&lt;/code&gt; in terms of cost.&lt;/p&gt;
&lt;p&gt;Since &lt;strong&gt;&lt;code&gt;i++&lt;/code&gt;&lt;/strong&gt; requires storing the original value in a temporary register, &lt;strong&gt;&lt;code&gt;++i&lt;/code&gt;&lt;/strong&gt; is slightly cheaper.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;++i&lt;/code&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;    add rax, 1        ; i = i + 1&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;code&gt;i++&lt;/code&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;    mov rcx, rax      ; old = i   (temporary value)
    add rax, 1        ; i = i + 1 (original value)&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Programming Language/cpp</category>
      <category>C++</category>
      <category>CPP</category>
      <category>postfix</category>
      <category>prefix</category>
      <category>register cost</category>
      <author>snp0301</author>
      <guid isPermaLink="true">https://sgwin.tistory.com/3</guid>
      <comments>https://sgwin.tistory.com/3#entry3comment</comments>
      <pubDate>Sat, 11 Oct 2025 21:50:11 +0900</pubDate>
    </item>
    <item>
      <title>std::array vs std::vector</title>
      <link>https://sgwin.tistory.com/2</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;span style=&quot;background-color: #ffe6e6;&quot;&gt; 해당 포스트는 cppreference.com을 참조해 작성했습니다. &lt;/span&gt;&lt;/p&gt;
&lt;h1&gt;std::array&lt;/h1&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;a container that encapsulates &lt;b&gt;fixed size&lt;/b&gt; arrays.&lt;/li&gt;
&lt;li&gt;it doesn't decay to &lt;code&gt;T*&lt;/code&gt; automatically.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;cpp&quot;&gt;&lt;code&gt;template&amp;lt;
    class T,
    std::size_t N
&amp;gt; struct array;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;std::vector&lt;/h1&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;a sequence container that encapsualteds &lt;b&gt;dynamic size&lt;/b&gt; arrays.&lt;/li&gt;
&lt;li&gt;Except for &lt;code&gt;std::vector&amp;lt;bool&amp;gt;&lt;/code&gt; partial specialization, the element are stored &lt;b&gt;contiguously&lt;/b&gt;, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.&lt;/li&gt;
&lt;li&gt;Vectors usually &lt;b&gt;occupy more space than static arrays&lt;/b&gt;, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, &lt;b&gt;buy only when the additional memory is exhausted&lt;/b&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;monkey&quot;&gt;&lt;code&gt;template&amp;lt;
    class T,
    class Allocator = std::allocator&amp;lt;T&amp;gt;
&amp;gt; class vector;&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Programming Language/cpp</category>
      <author>snp0301</author>
      <guid isPermaLink="true">https://sgwin.tistory.com/2</guid>
      <comments>https://sgwin.tistory.com/2#entry2comment</comments>
      <pubDate>Fri, 10 Oct 2025 21:29:34 +0900</pubDate>
    </item>
  </channel>
</rss>