<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Samantha Fortuna on Medium]]></title>
        <description><![CDATA[Stories by Samantha Fortuna on Medium]]></description>
        <link>https://medium.com/@saf1119?source=rss-591a39fc4b47------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ZOgC6jFdrb6fZj2f</url>
            <title>Stories by Samantha Fortuna on Medium</title>
            <link>https://medium.com/@saf1119?source=rss-591a39fc4b47------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 04:21:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saf1119/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using some stuff to solve other stuff: my dive into dynamic programming]]></title>
            <link>https://medium.com/@saf1119/using-some-stuff-to-solve-other-stuff-my-dive-into-dynamic-programming-7e7d79c11a62?source=rss-591a39fc4b47------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e7d79c11a62</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Samantha Fortuna]]></dc:creator>
            <pubDate>Tue, 13 Nov 2018 16:04:06 GMT</pubDate>
            <atom:updated>2018-11-13T20:42:32.574Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v3ikxyKvhmgFmwncD7AfsA.jpeg" /></figure><p>Over the last few weeks, I’ve been throwing myself into data structures and algorithms. More often than not, this involved staring at a tricky problem blankly, feeling like my brain was melting, taking a walk, forgetting about the problem, and then coming back two hours later with half of an idea sketched out (I’ve been told that I won’t be able to do this during an interview). Along the way, I’ve been learning about different problem-solving strategies and theories, which has seriously cut down on my staring blankly time. One such theory is called dynamic programming. Dynamic programming involves breaking a large, complicated problem into several smaller and overlapping subproblems. To get the most optimal solution, you save the result of these subproblems, which you’ll later use to get the answer to the final problem.</p><p>A simple, real-world problem example would be something like this: say that you are picking apples, and you want to know how many apples you’ve picked in any given moment. The very naive (and unlikely) approach would be to count the number of apples every single time you’d like to know how many apples you have. This would produce an O(n) time result and O(1) space result every time. The alternate, and more intuitive approach, would be to keep a count variable, and add to it every time you pick a new apple. Therefore, any time you’d like to know the count, it would be an O(1) query for both space and time. The intermediate, overlapping subproblems of previous counts are saved to solve the final count problem.</p><p>These same principles can be applied to more complex questions, allowing more optimal solutions for difficult algorithm question. An example of a problem where you can apply the same sort of concept would be one like this:</p><blockquote>Write a function that takes in an array of positive integers and returns an integer representing the maximum sum of non-adjacent elements in the array. If there is no sum possible (the array is empty), the function should return zero.</blockquote><p>How can we use dynamic programming to solve this problem? Well, first we need to consider how to break this problem down into different subproblems. One approach is to find the maximum sum at the subarray ending at each element, until you reach the full size of the array. For example, let’s say we’re applying this algorithm to the following array:</p><p>[60, 35, 27, 9, 43, 7]</p><p>We can examine for the following subarrays before analyzing the full array:</p><p>[60]</p><p>[60, 35]</p><p>[60, 35, 27]</p><p>[60, 35, 27, 9]</p><p>[60, 35, 27, 9, 43]</p><p>Specifically, we can first find the maximum sum of the subset ending at 60, then at 35, then 27, 9, 43, and 7. Once we store all of these, we can find the maximum of these numbers, which is the solution of [60, 35, 27, 9, 43, 7]. Each solution to the previous problem informs the solution to the next subarray.</p><p>So, let’s create a place to store the results of these intermediate calculations. Given that the problem specifies that these will be positive integers, we know the minimum sum will be zero. Let’s first create an array the length of the input array filled with zeroes.</p><p>Now, we need to come up with a solution for how to calculate intermediate subarrays. In the base case of array[0], we know that the maximum sum is just the element itself, array[0]. So the max sum of [60] is 60, and we can store 60 in the first element of the result array maxSums.</p><p>Next, let’s look at the case of [60, 35]. We know the elements of the sum have to be non-adjacent, so we can’t just sum up 60 and 35. For this case, let’s focus on the maximum subset sum ending at 35. Since there aren’t any elements before 60, we know this has to be just 35. Let’s save that in maxSums[1].</p><p>Now, let’s look at the non-trivial case of [60, 35, 27]. We now want to calculate the maximum sum ending at 27, and see which one is bigger. We’ll save that in maxSums[2]. So what is the formula for figuring this out?</p><p>We know that 27 must be added to non adjacent elements. Therefore, it must be added to any subset that ends at 60 or earlier (though, in this case, 60 is the first element of the array). Therefore, we can add 27 to the maximum sum of the subarray ending at 60, to get the maximum sum of any subset ending at 27. We have the maximum of the subarray ending at 60 stored in the maxSums array, at maxSums[0].</p><p>Now, let’s look at one final case to solidify the formula for this problem, that of [60, 35, 27, 9]. To find the maximum subset at 9, we can either add 9 to the subset ending at 60 (60+9 = 69) or the subset ending at 35 (35 + 9 = 44). We want the maximum sum, so we should take the maximum of each of these subset sums (35, 60) and add that to 9 to get the number we should place in maxSums[3].</p><p>The code for this problem, therefore, will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VK_3PWPDdemOZDby1BzFEA.png" /></figure><p>This is just one example of dynamic programming — it’s a lengthy topic with more and more complex examples. However, this problem demonstrates several techniques that can be applied to other dynamic programming problems, and with an understanding of these basic principles, I’m looking forward to exploring it even more.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e7d79c11a62" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Iterative Solutions and Trees]]></title>
            <link>https://medium.com/@saf1119/iterative-solutions-and-trees-88a91c3b0371?source=rss-591a39fc4b47------2</link>
            <guid isPermaLink="false">https://medium.com/p/88a91c3b0371</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Samantha Fortuna]]></dc:creator>
            <pubDate>Sat, 29 Sep 2018 20:46:42 GMT</pubDate>
            <atom:updated>2018-11-26T01:14:29.499Z</atom:updated>
            <content:encoded><![CDATA[<p>When I first learned about binary search trees, I was amazed by the different recursive tree traversal solutions — traversing the tree in a myriad of different ways took only a few lines of code. However, as I’ve been learning more about space complexity, I’ve learned the tradeoffs involved in filling up the call stack with recursive calls. Therefore, I was recently intrigued by the possibility of an iterative solution to traversing binary search trees. In this post, I’ll be exploring the solution to depth-first in-order traversal. This means that for a given subtree, I’d traverse and call a callback function the left-most node, the center node, and then the right node. For example, in the binary search tree</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/303/1*Kx0FV_BjYvhsQBmYJXWVfg.png" /><figcaption>(source: <a href="https://en.wikipedia.org/wiki/Binary_search_tree#/media/File:Binary_search_tree.svg">https://en.wikipedia.org/wiki/Binary_search_tree#/media/File:Binary_search_tree.svg</a>)</figcaption></figure><p>The output would be:</p><p>Callback(1), Callback(3), Callback(4), Callback(6), Callback(7), Callback(8), Callback(10), Callback(13), Callback(14)</p><p>In order to approach the iterative solution, we need to loop over all of the nodes, and take action on the node depending what case we’re considering. We’ll need to consider the following cases:</p><ol><li>The first node in the tree</li><li>An interim node (like the node containing 3) that still has more leftmost nodes to be called</li><li>The leftmost node (like 1)</li><li>An center node where the previous node called was its left node</li><li>A rightmost node</li></ol><p>To start coding this, we’ll want to create a function that takes in the topmost node of the tree, as well as a callback function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*bqxI-jSzXg2SI0saIV7amQ.png" /></figure><p>Here, we’re setting up a while loop that will operate as long as currentNode is defined. We’ll be updating currentNode as we traverse through the list, until we’ve reached the end of the tree.</p><p>The first case we’ll want to consider is if we’re either starting at the parent node or we’ve just come from a parent node to a child node. In that case, if there’s a node to the left, we’ll want to move current node to that node. Otherwise, we’ll want to call the callback function on the node and move the node to the right. If there’s no node to the right or the left, then we’ll want to call the callback function and then move the current node back up to the parent node.</p><p>This will look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AE9EGmrLN6zRtRxsqEMETw.png" /></figure><p>Next, we’ll want to consider the scenario where we just came from a leftmost node and moved back up to a parent node. In that case, we’ll want to call the callback on the current node and then check if there’s a right-hand node. If there is, we’ll move the currentNode there. Otherwise, we’ll move it up again to the parent node. This is represented in code with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JFy-stDz52QPiDMkpm6jSg.png" /></figure><p>Finally, if the previous node was the rightmost node, then we want to move the currentNode up to the currentNode’s parent, like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ha1Ksrz0iPj8IXZsaU-bUA.png" /></figure><p>Therefore, we’ve accounted for every scenario and can successfully iterate over every element of the tree.</p><p>Although the amount of code written is much more than for the simple recursive solution, we’re only using O(n) time and a constant O(1) space. Therefore, this allows for a more space-efficient solution than the recursive version.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88a91c3b0371" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>