Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
fu11211129 committed Mar 24, 2018
1 parent 32e39fb commit aabea9f
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Array/google/MaxAvgSubarrayII.java
Original file line number Diff line number Diff line change
@@ -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<k; ++i) sum += 1.0 * nums[i] - est;

// if sum of first leading k elements >= 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<nums.length; ++i) {
sum += 1.0 * nums[i] - est;
pre_k_sum += 1.0 * nums[i-k] - est;
mi_pre_sum = Math.min(mi_pre_sum, pre_k_sum);

// get the max sum.
if(sum >= 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;
}
}
45 changes: 45 additions & 0 deletions String/google/EncodeDecodeStrings.java
Original file line number Diff line number Diff line change
@@ -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<String> decoded = ed.decode(encoded);
System.out.println(decoded);
}

// Encodes a list of strings to a single string.
public String encode(List<String> 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<String> decode(String s) {
int i = 0;
List<String> 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;
}
}
52 changes: 52 additions & 0 deletions bfs/uber/WordLadder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
public class WordLadder {

public static void main(String[] args) {

}

public void bfs(String beginWord, String endWord, Set<String> wordList) {
Queue<String> Q = new LinkedList<> ();
Set<String> used = new HashSet<> ();
Map<String, Set<String> > paths = new HashMap<> ();
Q.offer(beginWord);

boolean found = false;

while(Q.isEmpty()) {

used.addAll(Q);
int size = Q.size();

for(int k=0; k<size; ++k) {
String word = Q.poll();
char[] cw = word.toCharArray();

for(int i=0; i<cw.length; ++i) {
for(char c='a', c<='z'; ++c) {
if(cw[i] != c) {
char bkup = cw[i];
cw[i] = c;
String newStr = new String(cw);
if(!used.contains(newStr) && wordList.contains(newStr)) {
if(newStr.equals(endWord)) found = true;
Q.offer(newStr);
paths.putIfAbsent(newStr, new HashSet<String> ());
paths.get(newStr).add(word);
}
cw[i] = bkup;
}
}
}

}

if(found) break; //if endWord found, terminate the level bfs
} // end of while

if(found) {
LinkedList<String> load = new LinkedList<String> ();
load.addLast(endWord);
dfs(endWord, beginWord, load, paths);
}
}
}
21 changes: 21 additions & 0 deletions bst-bt/microsoft/BinaryTreeInorderTraversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class BinaryTreeInorderTraversal {
public static void main(String[] args) {

}

public List<TreeNode> inorderTraversal(TreeNode root) {
List<TreeNode> 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;
}
}
}
3 changes: 3 additions & 0 deletions bst-bt/zenefits/VerifyPreorderSeqInBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class VerifyPreorderSeqInBST {

}

0 comments on commit aabea9f

Please sign in to comment.