From aabea9f9995a057490d8116b39c43275f8922625 Mon Sep 17 00:00:00 2001 From: Liangjia Fu Date: Sat, 24 Mar 2018 16:29:57 -0700 Subject: [PATCH] u --- Array/google/MaxAvgSubarrayII.java | 58 +++++++++++++++++++ String/google/EncodeDecodeStrings.java | 45 ++++++++++++++ bfs/uber/WordLadder.java | 52 +++++++++++++++++ .../microsoft/BinaryTreeInorderTraversal.java | 21 +++++++ bst-bt/zenefits/VerifyPreorderSeqInBST.java | 3 + 5 files changed, 179 insertions(+) create mode 100644 Array/google/MaxAvgSubarrayII.java create mode 100644 String/google/EncodeDecodeStrings.java create mode 100644 bfs/uber/WordLadder.java create mode 100644 bst-bt/microsoft/BinaryTreeInorderTraversal.java create mode 100644 bst-bt/zenefits/VerifyPreorderSeqInBST.java diff --git a/Array/google/MaxAvgSubarrayII.java b/Array/google/MaxAvgSubarrayII.java new file mode 100644 index 0000000..d3136fc --- /dev/null +++ b/Array/google/MaxAvgSubarrayII.java @@ -0,0 +1,58 @@ +public class MaxAvgSubarrayII { + public static void main(String[] args) { + + } + + private boolean check(double est, int[] nums, int k) { + double sum = 0.0; + for(int i=0; i= 0, meaning we find such a subarray whose sum >= 0.0 + if(sum >= 0.0) return true; + + double mi_pre_sum = 0; + double pre_k_sum = 0; + + for(int i=k; i= mi_pre_sum) return true; + } + + return false; + } + + public double findMaxAverage(int[] nums, int k) { + if(nums == null || nums.length == 0) return 0; + + int n = nums.length; + double mx = Double.MIN_VALUE, mi = Double.MAX_VALUE; + for(int x: nums) { + mx = Math.max(mx, (double)x); + mi = Math.min(mi, (double)x); + } + + if(mx == mi) return mx; + + double err = Double.MAX_VALUE; + double pre_est = mx; + double est = 0.0; + + while(err > 0.00001) { + est = (mx + mi) * 0.5; + if(check(est, nums, k)) { + mi = est; + } else { + mx = est; + } + + err = Math.abs(est - pre_est); + pre_est = est; + } + + return mx; + } +} diff --git a/String/google/EncodeDecodeStrings.java b/String/google/EncodeDecodeStrings.java new file mode 100644 index 0000000..b1c7e40 --- /dev/null +++ b/String/google/EncodeDecodeStrings.java @@ -0,0 +1,45 @@ +import java.util.*; + +public class EncodeDecodeStrings { + + public static void main(String[] args) { + EncodeDecodeStrings ed = new EncodeDecodeStrings(); + String encoded = ed.encode(new ArrayList(Arrays.asList("0"))); + System.out.println(encoded); + List decoded = ed.decode(encoded); + System.out.println(decoded); + } + + // Encodes a list of strings to a single string. + public String encode(List strs) { + StringBuilder sb = new StringBuilder(); + + for(String s: strs) { + int len = s.length(); + sb.append(Integer.toString(len)); + sb.append('#'); + sb.append(s); + } + + return sb.toString(); + } + + // Decodes a single string to a list of strings. + public List decode(String s) { + int i = 0; + List parsed = new ArrayList<> (); + if(s.equals("")) return parsed; + + while(i < s.length()) { + int markIdx = s.indexOf('#', i); + String lenStr = s.substring(i, markIdx); + int len = Integer.parseInt(lenStr); + + String word = s.substring(markIdx + 1, markIdx + len + 1); + parsed.add(word); + i = markIdx + len + 1; + } + + return parsed; + } +} diff --git a/bfs/uber/WordLadder.java b/bfs/uber/WordLadder.java new file mode 100644 index 0000000..1bbc404 --- /dev/null +++ b/bfs/uber/WordLadder.java @@ -0,0 +1,52 @@ +public class WordLadder { + + public static void main(String[] args) { + + } + + public void bfs(String beginWord, String endWord, Set wordList) { + Queue Q = new LinkedList<> (); + Set used = new HashSet<> (); + Map > paths = new HashMap<> (); + Q.offer(beginWord); + + boolean found = false; + + while(Q.isEmpty()) { + + used.addAll(Q); + int size = Q.size(); + + for(int k=0; k ()); + paths.get(newStr).add(word); + } + cw[i] = bkup; + } + } + } + + } + + if(found) break; //if endWord found, terminate the level bfs + } // end of while + + if(found) { + LinkedList load = new LinkedList (); + load.addLast(endWord); + dfs(endWord, beginWord, load, paths); + } + } +} diff --git a/bst-bt/microsoft/BinaryTreeInorderTraversal.java b/bst-bt/microsoft/BinaryTreeInorderTraversal.java new file mode 100644 index 0000000..6d988bf --- /dev/null +++ b/bst-bt/microsoft/BinaryTreeInorderTraversal.java @@ -0,0 +1,21 @@ +public class BinaryTreeInorderTraversal { + public static void main(String[] args) { + + } + + public List inorderTraversal(TreeNode root) { + List io = new ArrayList<> (); + if(root == null) return io; + + TreeNode curr = root; + while(curr != null && st.empty()) { + while(curr != null) { + st.push(curr); + curr = curr.left; + } + curr = st.pop(); + io.add(curr); + curr = curr.right; + } + } +} diff --git a/bst-bt/zenefits/VerifyPreorderSeqInBST.java b/bst-bt/zenefits/VerifyPreorderSeqInBST.java new file mode 100644 index 0000000..f928aff --- /dev/null +++ b/bst-bt/zenefits/VerifyPreorderSeqInBST.java @@ -0,0 +1,3 @@ +public class VerifyPreorderSeqInBST { + +}