Snapsack problem refers to the situation you want to find a combination of items that can fill up a container. Each items should be attached with a value, and there should be a target value that means a container is filled up. For example, a container has 25 spaces and…

Unable to install Homebrew with this? ruby -e "\$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)" What’s happened -e:70: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777 ==> This script will install: /usr/local/bin/brew /usr/local/Library/... /usr/…

Question You have 9 marbles. 8 marbles weigh 1 ounce each, & one marble weighs 1.5 ounces. You are unable to determine which is the heavier marble by looking at them. You have a weighing scale that consists of 2 pans, but the scale is only good for 2…

Question Write an algorithm to remove duplicated node from a linked list. Solution There are many ways to do it. For the first one, as the simplest one, we could use 2 loops to compare each element with all other elements. However, this takes forever: O(n²). The second way…

Question Sum up 1 to n without using division, multiplication, for loop, while loop, if else, switch and condition statement (A?B:C). Solution 1 Actually, for loop can be simulated through making use of the static property. Since static value will be stored in the class, we can increase…

Question Get the greatest distance between two nodes in a binary tree. Assume links between nodes are bidirectional. Distance is defined as the amount of nodes connected along the path linked two nodes. Write an algorithm to calculate distance between two nodes. Take the figure at the right, the greatest…

Question Given 2 linked list head pointers, determine whether they intersect at some point. Solution First of all, linked list can have loop or not, and this gives 3 possible situations. For the 2 loops head pointer 1 and head pointer 2, move pointer 1 at normal speed, and pointer…

Question Define a stack structure with “min” function — a function to get the minimum value within the stack. The time complexity of min, push and pop functions must be O(1). Solution 結合鍊錶一起做。首先我做插入以下數字: 10, 7, 3, 3, 8, 5, 2, 6 0: 10 -> NULL (MIN=10, POS=…