Skip to content

Instantly share code, notes, and snippets.

@artlovan
artlovan / App.java
Created April 14, 2017 00:13
Dijkstra Algorithm in Java
public class App {
public static void main(String[] args) {
Vertex v1 = new Vertex("A");
Vertex v2 = new Vertex("B");
Vertex v3 = new Vertex("C");
v1.addNeighbour(new Edge(1, v1, v2));
v1.addNeighbour(new Edge(10, v1, v2));
v2.addNeighbour(new Edge(1, v2, v3));
@artlovan
artlovan / Armstrong.java
Created April 8, 2017 16:19
Java 8 / Lambda approach to generate Armstrong number.
import java.util.List;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
class Armstrong {
/**
* /**
* Java 8 / Lambda approach to generate Armstrong number.
@artlovan
artlovan / Fibonacci.java
Created April 8, 2017 02:10
Java 8 / Lambda approach to generate fibonacci series.
import java.util.List;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class Fibonacci {
/**
* Java 8 / Lambda approach to generate fibonacci series.
* Fibonacci always start as classic (e.g. 0, 1, 1, 2, 3, 5)
* @param series Number of how many fibonacci number should be generated
@artlovan
artlovan / PrimeNumber.java
Created April 8, 2017 19:10
Java 8 / Lambda approach to generate Prime number.
import java.util.*;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class PrimeNumber {
/**
* Java 8 / Lambda approach to generate Prime number.
@artlovan
artlovan / CountNegativeIntegersInMatrix.java
Created June 6, 2017 21:40
CountNegativeIntegersInMatrix
import java.util.ArrayList;
import java.util.List;
public class CountNegativeIntegersInMatrix {
public static void main(String[] args) {
int[][] data = new int[][]{
{-3, -2, -1, 0, 1, 2, 4, 6, 7, 8},
{-3, -2, -1, 1},
{-2, 2, 3, 4},
{4, 5, 7, 8}
@artlovan
artlovan / TrafficLight.java
Created May 22, 2017 22:05
Simple TrafficLight Impl
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TrafficLight {
public static void main(String[] args) throws InterruptedException {
Road north = new Road(Direction.NORTH, Color.RED);
Road south = new Road(Direction.SOUTH, Color.RED);
Road east = new Road(Direction.EAST, Color.RED);
Road west = new Road(Direction.WEST, Color.RED);
@artlovan
artlovan / PathsWithSum.java
Created May 21, 2017 20:30
PathsWithSum (_4_12)
import java.util.ArrayList;
import java.util.List;
/**
* Paths with Sum: You are given a binary tree in which each node contains
* an integer value (which might be positive or negative).
* Design an algorithm to count the number of paths that sum to a given value.
* The path does not need to start or end at the root or a leaf,
* but it must go downwards (traveling only from parent nodes to child nodes).
* <p>
@artlovan
artlovan / CheckSubtree.java
Created May 21, 2017 18:36
CheckSubtree (_4_10)
import java.util.ArrayList;
import java.util.List;
/**
* Check Subtree: T1 and T2 are two very large binary trees, with T1 much bigger than T2.
* Create an algorithm to determine if T2 is a subtree of T1.
* A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of n is identical to T2.
* That is, if you cut off the tree at node n, the two trees would be identical.
* <p>
* Hints: #4, #77, #78, #37, #37
@artlovan
artlovan / Successor.java
Created May 20, 2017 22:52
Successor (_4_6)
/**
* Successor: Write an algorithm to find the "next" node (i.e., in-order successor)
* of a given node in a binary search tree. You may assume that each node has a link to its parent.
* Hints: #79, #91
*/
public class Successor {
public static void main(String[] args) {
Node n6 = new Node(25, null, null, null);
Node n5 = new Node(13, null, null, null);
@artlovan
artlovan / ValidateBST.java
Created May 20, 2017 22:51
ValidateBST (_4_5)
/**
* Validate BST: Implement a function to check if a binary tree is a binary search tree.
* Hints: #35, #57, #86, # 773, # 728
*/
public class ValidateBST {
public static void main(String[] args) {
Node n6 = new Node(25, null, null);
Node n5 = new Node(13, null, null);
Node n4 = new Node(8, null, null);