Skip to content

Commit

Permalink
230
Browse files Browse the repository at this point in the history
  • Loading branch information
jackqiang123 authored and Lian Lu committed May 18, 2016
1 parent b4ad763 commit 81156ed
Show file tree
Hide file tree
Showing 25 changed files with 761 additions and 70 deletions.
30 changes: 0 additions & 30 deletions KdTree.java

This file was deleted.

File renamed without changes.
61 changes: 52 additions & 9 deletions lc207-210.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,74 @@
// There are a total of n courses you have to take, labeled from 0 to n - 1.
//
// Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
// Some courses may have prerequisites, for example to take course 0 you have to first take course 1,
// which is expressed as a pair: [0,1]
//
// Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
// Given the total number of courses and a list of prerequisite pairs, return the ordering of courses
// you should take to finish all courses.
//
// There may be multiple correct orders, you just need to return one of them.
// If it is impossible to finish all courses, return an empty array.
//
// For example:
//
// 2, [[1,0]]
// There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]
// There are a total of 2 courses to take. To take course 1 you should have finished course 0.
// So the correct course order is [0,1]
//
// 4, [[1,0],[2,0],[3,1],[3,2]]
// There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].
// There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2.
// Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3].
// Another correct ordering is[0,2,1,3].
//
// Note:
// The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
// The input prerequisites is a graph represented by a list of edges, not adjacency matrices.
// Read more about how a graph is represented.
//
// click to show more hints.
//
// Hints:
// This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
// Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
// This problem is equivalent to finding the topological order in a directed graph. If a cycle exists,
// no topological ordering exists and therefore it will be impossible to take all courses.
// Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic
// concepts of Topological Sort.
// Topological sort could also be done via BFS.
public class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {

public int[] findOrder(int numCourses, int[][] preq) {
int [] res = new int[numCourses];
int index = 0;
Queue<Integer> queue = new LinkedList<Integer>();
HashMap<Integer, List<Integer>> map = new HashMap();
int [] preqCount = new int[numCourses];
for (int i = 0; i < preq.length; i++)
{
if (map.get(preq[i][1]) != null)
map.get(preq[i][1]).add(preq[i][0]);
else {
List<Integer> nextHop = new ArrayList<Integer>();
nextHop.add(preq[i][0]);
map.put(preq[i][1], nextHop);
}
preqCount[preq[i][0]]++;
}
for (int i = 0; i < numCourses; i++){
if (preqCount[i] == 0)
queue.add(i);
}
while(!queue.isEmpty()){
int cur = queue.remove();
if (preqCount[cur] == 0){
res[index++] = cur;
}
List<Integer> nextClass = map.get(cur);
if (nextClass != null){
for (int next : nextClass){
preqCount[next]--;
if (preqCount[next] == 0)
queue.add(next);
}
}
}
if (index == numCourses) return res;
return new int[0];
}
}
39 changes: 38 additions & 1 deletion lc208.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

class TrieNode {
// Initialize your data structure here.
TrieNode[] next;
boolean isWord;
public TrieNode() {

next = new TrieNode[26];
isWord = false;
}
}

Expand All @@ -20,18 +23,52 @@ public Trie() {

// Inserts a word into the trie.
public void insert(String word) {
root = insert(root, word, 0);
}

private TrieNode insert(TrieNode root, String word, int start){
if (root == null) {
root = new TrieNode();
}
if (start == word.length()) {
root.isWord = true;
return root;
}
char c = word.charAt(start);
root.next[c-'a'] = insert(root.next[c-'a'], word, start + 1);
return root;
}

// Returns if the word is in the trie.
public boolean search(String word) {
return search(root, word, 0);
}

private boolean search(TrieNode root, String word, int start){
if (root == null) return false;
else if (word.length() == start){
return root.isWord;
}
else{
char c = word.charAt(start);
return search(root.next[c-'a'], word, start + 1);
}
}

// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
return startsWith(root, prefix, 0);
}

private boolean startsWith(TrieNode root, String prefix, int start){
if (root == null) return false;
else if (prefix.length() == start)
return true;
else {
char c = prefix.charAt(start);
return startsWith(root.next[c-'a'], prefix, start + 1);
}
}
}

Expand Down
18 changes: 17 additions & 1 deletion lc209.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@
// More practice:
// If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
public class Solution {
// nlogn method will be get the accmulation, and do binary search on that array for each iterator.
public int minSubArrayLen(int s, int[] nums) {

// linear solution will be two pointer
int i = 0; int j = 0; int len = nums.length;
if (len == 0) return 0;
int curSum = 0;
int best = Integer.MAX_VALUE;
while(j < len){
curSum += nums[j];
while(curSum >= s) {
best = Math.min(best, j - i + 1);
curSum -= nums[i];
i++;
}
j++;
}
if (best == Integer.MAX_VALUE) return 0;
return best;
}
}
76 changes: 69 additions & 7 deletions lc211.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,85 @@
//
// click to show hint.
//
// You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.
// You should be familiar with how a Trie works. If not, please work on this problem:
// Implement Trie (Prefix Tree) first.

public class WordDictionary {

Trie dict = new Trie();
// Adds a word into the data structure.
public void addWord(String word) {

dict.insert(word);
}

// Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
public boolean search(String word) {
return dict.search(word);
}
}
// Implement a trie with insert, search, and startsWith methods.
//
// Note:
// You may assume that all inputs are consist of lowercase letters a-z.
//

class TrieNode {
// Initialize your data structure here.
TrieNode[] next;
boolean isWord;
public TrieNode() {
next = new TrieNode[26];
isWord = false;
}
}

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
class Trie {
private TrieNode root;

public Trie() {
root = new TrieNode();
}

// Inserts a word into the trie.
public void insert(String word) {
root = insert(root, word, 0);
}

private TrieNode insert(TrieNode root, String word, int start){
if (root == null) {
root = new TrieNode();
}
if (start == word.length()) {
root.isWord = true;
return root;
}
char c = word.charAt(start);
root.next[c-'a'] = insert(root.next[c-'a'], word, start + 1);
return root;
}

// Returns if the word is in the trie.
public boolean search(String word) {
return search(root, word, 0);
}

private boolean search(TrieNode root, String word, int start){
if (root == null) return false;
else if (word.length() == start){
return root.isWord;
}
else{
char c = word.charAt(start);
if (c != '.')
return search(root.next[c-'a'], word, start + 1);
else {
for (int i = 0; i < 26; i++)
{
if (search(root.next[i], word, start + 1))
return true;
return false;
}
}
}
}
}
Loading

0 comments on commit 81156ed

Please sign in to comment.