This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrieNode { | |
protected TrieNode[] children; | |
protected boolean isEnd = false; | |
// Initialize your data structure here. | |
public TrieNode() { | |
children = new TrieNode[26]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for Directed graph. | |
* class DirectedGraphNode { | |
* int label; | |
* ArrayList<DirectedGraphNode> neighbors; | |
* DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); } | |
* }; | |
* | |
* BFS solution. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
OlderNewer