Skip to content

Instantly share code, notes, and snippets.

@bilbo3000
bilbo3000 / segment_tree_build.java
Last active September 30, 2015 03:57
Segment Tree Build
Given [3,2,1,4]. The segment tree will be, where max is the max value within that segment.
Recursively build the segment tree has O(n) time complexity.
[0, 3] (max = 4)
/ \
[0, 1] (max = 3) [2, 3] (max = 4)
/ \ / \
[0, 0](max = 3) [1, 1](max = 2)[2, 2](max = 1) [3, 3] (max = 4)
public SegmentTreeNode build(int[] A) {
return helper(A, 0, A.length - 1);
Given [3,2,1,4]. The segment tree will be:
[0, 3] (max = 4)
/ \
[0, 1] (max = 3) [2, 3] (max = 4)
/ \ / \
[0, 0](max = 3) [1, 1](max = 2)[2, 2](max = 1) [3, 3] (max = 4)
Query the max fall into the given range in O(logn):
For array [0, 2, 3], the corresponding value Segment Tree is:
[0, 3, count=3]
/ \
[0,1,count=1] [2,3,count=2]
/ \ / \
[0,0,count=1] [1,1,count=0] [2,2,count=1], [3,3,count=1]
where count means the number of values fall into that range.
public void modify(SegmentTreeNode root, int index, int value) {
if (root == null) {
return;
}
// Current node if leaf node
if (root.start == index && root.end == index) {
root.max = value;
return;
}
@bilbo3000
bilbo3000 / Trie.java
Created October 18, 2015 04:20
Implementation of a Trie structure that supports insert, search, and prefix checking.
class TrieNode {
protected TrieNode[] children;
protected boolean isEnd = false;
// Initialize your data structure here.
public TrieNode() {
children = new TrieNode[26];
}
}
@bilbo3000
bilbo3000 / TopologicalSort.java
Created November 1, 2015 14:59
Topological Sorting.
/**
* Definition for Directed graph.
* class DirectedGraphNode {
* int label;
* ArrayList<DirectedGraphNode> neighbors;
* DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); }
* };
*
* BFS solution.
*/
@bilbo3000
bilbo3000 / pom.xml
Created May 23, 2016 07:06
RESTful
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>my-rest-service</artifactId>
<version>1.0</version>
<parent>
package hello;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);