Skip to content

Instantly share code, notes, and snippets.

@johntran
Last active November 14, 2018 02:20
Show Gist options
  • Save johntran/83031929ebd13f1b98ba6b4bcc5446af to your computer and use it in GitHub Desktop.
Save johntran/83031929ebd13f1b98ba6b4bcc5446af to your computer and use it in GitHub Desktop.

https://gist.github.com/johntran - > 2018nov13.md

Santa Monica Algorithms 2018 11 06

algorithms

Problem Set

Detect Cycle in a Directed Graph

Given a directed graph, check if there is a cycle in it.

Build a Graph class that has a property called, "isCyclic".

  • There can be multiple cycles
  • We don’t need to print all cycles. Just return a boolean true/false if there is/is-no at least one cycle.
// JavaScript

class Graph {
  // fill me
  isCycle() {
    // fill me
  }
}

// This is just sample code. You can represent your graph however way you want.
const cyclicGraph = new Graph({ 0: [1, 2], 1: [2], 2: [0, 3], 3: [3] });
cyclicGraph.isCyclic(); // Returns true

const acyclicGraph = new Graph({ 0: [1, 2], 1: [2], 2: [] });
acyclicGraph.isCyclic();

Merge Overlapping Intervals

Given a set of time intervals in any order, merge all overlapping intervals into one and output the result which should have only mutually exclusive intervals. Let the intervals be represented as pairs of integers for simplicity. For example, let the given set of intervals be {{1,3}, {2,4}, {5,7}, {6,8} }. The intervals {1,3} and {2,4} overlap with each other, so they should be merged and become {1, 4}. Similarly {5, 7} and {6, 8} should be merged and become {5, 8}

Example 1:

Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:

Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considerred overlapping.

Robbery

There are n houses build in a line, each of which contains some value in it. A thief is going to steal the maximal value of these houses, but he can’t steal in two adjacent houses because owner of the stolen houses will tell his two neighbour left and right side. What is the maximum stolen value.

Input  : hval[] = {6, 7, 1, 3, 8, 2, 4}
Output : 19
Thief will steal 6, 1, 8 and 4 from house.

Input  : hval[] = {5, 3, 4, 11, 2}
Output : 16
Thief will steal 5 and 11

Optimal Strategy for Game

Consider a row of n coins of values v1 . . . vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin.

Determine the maximum possible amount of money we can definitely win if we move first.

Note: The opponent is as clever as the user.

 Examples
 1. 5, 3, 7, 10 : The user collects maximum value as 15(10 + 5)
 2. 8, 15, 3, 7 : The user collects maximum value as 22(7 + 15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment