Unique Paths This is the follow-up for the last tutorial. I will show you how to cope with Dynamic Programming problems.
Advantages Addresses problems with exponantial time complexity to Polynomial (sometimes even linear)
$O(c^n)$ -> $O(n^c)$ -> $O(n)$
When to Use When we don’t want to recalculate things. We want to rely on existing solutions.
Requirments Properties Optimal Substructure Use $X_1$ to solve $X_2$ to solve $X_3$ etc. to solve the entire problem.
Question You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.
You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.
Example 1:
Input: cost = [10,15,20]
Output: 15
Explanation: You will start at index 1.
Photo by Javier Quesada on Unsplash
Random Node From the book - Cracking the Coding Interview (4.11). Build a binary tree class along with the insert, delete, find and the getRandomNode method which returns a random node in the tree. (All nodes shall have the same possibility to be returned.)
Brute Force The intuition brute force solution would be to traverse all nodes and collect them into an array/list then randomly choose one of them out of Uniform Distribution.
isnanx commented on 30 Apr 2019 Hi, as I write both Chinese and English in my posts, I found it that Chinese characters were not included when counting the words, I doubt if there is something wrong of my usage or it’s truly not supported by now, any reference? Thanks. Chinese characters may not supported by variable Wordcount · Issue #5913 · gohugoio/hugo (github.com) Solution Found I
In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that stores a collection of disjoint (non-overlapping) sets. Equivalently, it stores a partition of a set into disjoint subsets. It provides operations for adding new sets, merging sets (replacing them by their union), and finding a representative member of a set. The last operation makes it possible to find out efficiently if any two elements are in the same or different sets.
Photo by Kevin Ku on Unsplash
Is BFS/DFS a Greedy Algorithm and Why? Though, the simple answer could be YES. To better understand this I would suggest reading on greedy vs heuristics algorithm.
Greedy algorithms supply an exact solution! Heuristic algorithms use probability and statistics in order to avoid running through all the possibilities and provide an “estimated best solution” (which means that if a better solution exists, it will be only slightly better).