<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: drinkingWater</title>
    <description>The latest articles on DEV Community by drinkingWater (@drinkingwater64).</description>
    <link>https://dev.to/drinkingwater64</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F942953%2F7dd0dbb9-ab5f-4d37-8a32-2b8736a864a7.jpeg</url>
      <title>DEV Community: drinkingWater</title>
      <link>https://dev.to/drinkingwater64</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/drinkingwater64"/>
    <language>en</language>
    <item>
      <title>Singleton in Unreal Engine</title>
      <dc:creator>drinkingWater</dc:creator>
      <pubDate>Thu, 03 Nov 2022 13:13:43 +0000</pubDate>
      <link>https://dev.to/drinkingwater64/singleton-in-unreal-engine-2831</link>
      <guid>https://dev.to/drinkingwater64/singleton-in-unreal-engine-2831</guid>
      <description>&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Singleton_pattern"&gt;Singleton pattern&lt;/a&gt; is a very important pattern for storing states in a game and keep them persistent between levels. Also, to share data between at some point. &lt;br&gt;
*&lt;em&gt;Problem: *&lt;/em&gt; In a game I am currently working on, there are some &lt;em&gt;actors&lt;/em&gt; called trash. After a certain amount delay if these items aren't pick up, they destroy and increase pollution factor in the world. Now the pollution factor has to be accessed by every trash to send the event if they are destroyed. Also, if scene changes, the pollution factor won't change. &lt;/p&gt;

&lt;p&gt;**Solution: **Make a singleton class where all the variables which have to be persistent between levels or scenes.&lt;/p&gt;

&lt;p&gt;**Challange: **In unity I used a lot of DontdestroyOnLoad to make a lot of persistent object which is recommended. I could keep multiple singleton classes for different mechanism or work. In unreal however you can work with only one singleton class which is called "Game Instance" here. &lt;/p&gt;

&lt;p&gt;**My Approach: **Firstly created a Game Instance from blueprint menu. It's like any other blueprint.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0SbIOvrP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewgb2iakgib75u3gapu8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0SbIOvrP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewgb2iakgib75u3gapu8.png" alt="Image description" width="727" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the project settings I changed the default instance with the new one I created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TjIXAgcS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byqtf78w4f6np5zrzvjx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TjIXAgcS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byqtf78w4f6np5zrzvjx.png" alt="Image description" width="880" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the Game Instance blueprint I created a custom event which will count pollution factor within. For that I also added a variable to store the pollution factor. And also, why not the life span of the trashes. That way I can change the life span for every trash at once. Also, how much trash to spawn at each time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DOfMPCQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e9f58pcbruakw3ubqwkl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DOfMPCQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e9f58pcbruakw3ubqwkl.png" alt="Image description" width="880" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the trash I call the event and update the trash count. For that we &lt;em&gt;Get the game instance&lt;/em&gt; and cast it to the newly made game instance. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pl65C0Ce--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ok6v7860suk3luk3xov2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pl65C0Ce--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ok6v7860suk3luk3xov2.png" alt="Image description" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how I am implementing singleton for now. This does solve all the singleton related problem for me. But the bad habit of having multiple singleton classes in Unity does irritate me xD.&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>ue4</category>
      <category>unrealengine</category>
      <category>design</category>
    </item>
    <item>
      <title>Back-To-CP: day 10</title>
      <dc:creator>drinkingWater</dc:creator>
      <pubDate>Fri, 21 Oct 2022 18:07:39 +0000</pubDate>
      <link>https://dev.to/drinkingwater64/back-to-cp-day-10-2p11</link>
      <guid>https://dev.to/drinkingwater64/back-to-cp-day-10-2p11</guid>
      <description>&lt;p&gt;21-10-2022&lt;/p&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/longest-substring-without-repeating-characters"&gt;Longest Substring Without Repeating Characters&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem is quite simple if applied brute force, but the time complexity will be O(n^3). So, the good approach is sliding window. Where we keep two pointers at the beginning and a map to store the frequency of the characters in the array. Right pointer keeps moving forward if doesn't encounter the same character twice. If encountered, left pointer will start from the Right pointer position now. This is achieved by in the frequency map, instead of the actual frequency, the position of right pointer is stored. This allows left to jump directly to right + 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n = s.length();
        int ans = 0;
        map&amp;lt;char, int&amp;gt; hmap;
        int left = 0, right = 0;
        while(right&amp;lt;n){
            if(hmap[s[right]] &amp;gt; 0){
                left = max(hmap[s[right]], left);
            }
            ans = max(ans, right-left + 1);
            hmap[s[right]] = right + 1;
            right++;
        }
        return ans;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Back-To-CP: day 3</title>
      <dc:creator>drinkingWater</dc:creator>
      <pubDate>Sat, 15 Oct 2022 17:58:25 +0000</pubDate>
      <link>https://dev.to/drinkingwater64/back-to-cp-day-3-2jii</link>
      <guid>https://dev.to/drinkingwater64/back-to-cp-day-3-2jii</guid>
      <description>&lt;p&gt;15-10-2022&lt;/p&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/"&gt;Remove Nth Node From End of List&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another problem that can be solved using two pointers technique.&lt;br&gt;
It can be easily solved if we run a loop and hold all the addresses in an array then just find out the middle point, remove the link between nodes.&lt;br&gt;
But it can be done efficiently using two pointers. Firstly, the fast node is advanced n times, so that the slow pointer will be at the n-1'th position when fast pointer reaches the end.&lt;/p&gt;

&lt;p&gt;This however gives an error if the length of the list is equal to n. In which case answer would be null pointer. So, an edge case for checking for list length equal 1 is necessary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *f = head;
        ListNode *s = head;
        for(int i = 0; i &amp;lt; n ; i++){
            f = f-&amp;gt;next;
        }
        if(f == nullptr) {
            return head-&amp;gt;next;
        }
        while(f-&amp;gt;next != nullptr &amp;amp;&amp;amp; f != nullptr) {
            s = s-&amp;gt;next;
            f = f-&amp;gt;next;
        }

        s-&amp;gt;next = s-&amp;gt;next-&amp;gt;next;
        return head;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Back-To-CP: day 2</title>
      <dc:creator>drinkingWater</dc:creator>
      <pubDate>Fri, 14 Oct 2022 16:47:13 +0000</pubDate>
      <link>https://dev.to/drinkingwater64/back-to-cp-day-2-hfa</link>
      <guid>https://dev.to/drinkingwater64/back-to-cp-day-2-hfa</guid>
      <description>&lt;p&gt;14-10-2022&lt;/p&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/description/"&gt;Delete the Middle Node of a Linked List&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem is pretty straightforward. With the given head of a singly linked list, we delete the middle node. The definition of is given as middle = n/2 where n is the length of the list. &lt;br&gt;
Since it's a singly linked list, traversal to black is not possible. So, it's necessary to get to the previous node of middle node or middle - 1.&lt;br&gt;
Since only head is given, a traversal is needed to count the length. Then break the connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteMiddle(ListNode* head) {
        if(head-&amp;gt;next == nullptr){
            return nullptr;
        }
        int count = 0;
        ListNode* ptr = head;
        ListNode* ptr2 = head;

        while(ptr != nullptr){
            count++;
            ptr = ptr-&amp;gt;next;
        }
        for(int i = 0; i &amp;lt; (count / 2) - 1; i++){
            ptr2 = ptr2-&amp;gt;next;
        }

        ptr2-&amp;gt;next = ptr2-&amp;gt;next-&amp;gt;next;
        return head;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After accepted I looked for other efficient ways to solve this problem and learnt this new way called &lt;strong&gt;Slow pointer Fast pointer&lt;/strong&gt;&lt;br&gt;
This is a pretty straight forward technic to find middle of a list with unknown size. There is a slow pointer and fast pointer who starts from the beginning. The fast node increments itself twice more than the slow node. As a result, when the fast node already reaches the end, the slow pointer will be at the middle.&lt;/p&gt;

&lt;p&gt;With first attempt I ran to an error&lt;br&gt;
&lt;code&gt;runtime error: member access within null pointer of type 'ListNode' (solution.cpp)&lt;br&gt;
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior&lt;/code&gt;&lt;br&gt;
for&lt;br&gt;
&lt;code&gt;while(f != nullptr || f-&amp;gt;next != nullptr){&lt;br&gt;
            f = f-&amp;gt;next-&amp;gt;next;&lt;br&gt;
            s = s-&amp;gt;next;&lt;br&gt;
        }&lt;/code&gt;&lt;br&gt;
Turns out if I need to check both for pointer to be not null.&lt;/p&gt;

&lt;p&gt;On the second attempt I got the same error. Because I initialize fast pointer before and if it was a list with length one, it gives us the error for accessing fast-&amp;gt;next-&amp;gt;next which doesn't exist. &lt;br&gt;
So, an edge case was necessary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteMiddle(ListNode* head) {

        if(head-&amp;gt;next == nullptr){
            return 0;
        }

        ListNode *s = head;
        ListNode *f = head-&amp;gt;next-&amp;gt;next;

        while(f != nullptr &amp;amp;&amp;amp; f-&amp;gt;next != nullptr){
            f = f-&amp;gt;next-&amp;gt;next;
            s = s-&amp;gt;next;
        }

        s-&amp;gt;next = s-&amp;gt;next-&amp;gt;next;
        return head;
        return head;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://leetcode.com/problems/reverse-string"&gt;Reverse String&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My approach was simple. Have two pointers at beginning and end. Swap the two pointer and increment and decrement the pointers accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    void reverseString(vector&amp;lt;char&amp;gt;&amp;amp; s) {
        int l = 0, h = s.size() - 1;
        while(l &amp;lt; h){
            swap(s[l], s[h]);
            l++;
            h--;
        }
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Back-To-CP Day 1</title>
      <dc:creator>drinkingWater</dc:creator>
      <pubDate>Thu, 13 Oct 2022 16:38:21 +0000</pubDate>
      <link>https://dev.to/drinkingwater64/back-to-cp-day-1-klp</link>
      <guid>https://dev.to/drinkingwater64/back-to-cp-day-1-klp</guid>
      <description>&lt;p&gt;13-10-2022&lt;/p&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/"&gt;Two Sum II&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the array I used two pointers, one for the beginning of the and one for the end of the array. In a loop I checked if sum of those pointer value greater or less than the target value and decrease end pointer or increase start pointer by 1. Otherwise stop the loop.&lt;br&gt;
Returned a vector containing start pointer + 1 and end pointer + 1 as the index in result supposed to start from 1 and not 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    vector&amp;lt;int&amp;gt; twoSum(vector&amp;lt;int&amp;gt;&amp;amp; numbers, int target) {
         vector&amp;lt;int&amp;gt; res;
         int l = 0, h = numbers.size() - 1;
         for(int i = 0; i &amp;lt; numbers.size(); i++){
            if(numbers[l] + numbers[h] &amp;lt; target){
                l++;
            }else if(numbers[l] + numbers[h] &amp;gt; target){
                h--;
            }
            else{
                res.push_back(l+1);
                res.push_back(h+1);
                break;              
            }
        }
        return res;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://leetcode.com/problems/move-zeroes/"&gt;Move Zero&lt;/a&gt;&lt;br&gt;
The approach is quite easy and solved it with one go. Firstly, I declared a vector. Then checked if the array contains non-zero, append that that vector otherwise counts how many 0s are there. After the counting is done we just append the number of 0s that was connected. At the end I just replaced the given vector with the new vector. Quite Simple and fun.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    void moveZeroes(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
        vector&amp;lt;int&amp;gt; res;
        int cc = 0;
        for(int i = 0; i &amp;lt; nums.size(); i++){
            if(nums[i] != 0){
                res.push_back(nums[i]);
            }else{
                cc++;
            }
        }
        for(int i = nums.size() - cc; i &amp;lt; nums.size(); i++){
            res.push_back(0);
        }
        nums = res;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Starting Problem Solving Again</title>
      <dc:creator>drinkingWater</dc:creator>
      <pubDate>Thu, 13 Oct 2022 15:39:10 +0000</pubDate>
      <link>https://dev.to/drinkingwater64/starting-problem-solving-again-1bpi</link>
      <guid>https://dev.to/drinkingwater64/starting-problem-solving-again-1bpi</guid>
      <description>&lt;p&gt;I started doing problem solving at the beginning of my first year. Then the corona hit, the lockdown started. Lost all motivation to code and do anything productive. Spent my days playing games and watching animes or web series. &lt;br&gt;
Three years later I have nothing but regrets. Well, and some skills with Unity, digital arts, Illustrator, photoshop and ricing Linux.&lt;br&gt;
So, here I am again, after 3 years, starting competitive programming again, to better myself.&lt;br&gt;
Also starting this blog to record myself.  &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
