Pocket Gems专题

阅读更多

 

// Maximum Product Subarray 
public int maxProduct(int[] nums) {
    int result = nums[0];
    int maxHere = nums[0];
    int minHere = nums[0];
    for(int i=1; i rooms) {
	boolean[][] board = new boolean[m][n];
	int area = 0;
	for(Room room : rooms) {
		for(int i=room.y-room.h+1; i<=room.y; i++) {
			for(int j=room.x; j<=room.x+room.w-1; j++) {
				board[i][j] = true;
			}
		}
		area += room.h * room.w;
	}
	
	Queue queue = new LinkedList<>();
	int start = rooms.get(0).y * n + rooms.get(0).x;
	queue.offer(start);
	int sum = 0;
	
	while(!queue.isEmpty()) {
		sum++;
		int point = queue.poll();
		int y = point / n;
		int x = point % n;
		board[y][x] = false;
		for(int i=0; i= 0 && row < m && col >=0 && col < n && board[row][col]) {
				queue.offer(row*n+col);
			}
		}
	}
	
	return sum == area;
}

// sliding windows max
public static int[] slidingMax(int[] A, int w) {
	int n = A.length;
	w = Math.min(n, w);
	int k = n - w + 1;
	int[] max = new int[k];
	Deque deq = new ArrayDeque<>();
	for(int i=0; i= A[i] for slidingMin
			deq.removeLast();
		}
		deq.addLast(i);
		if(i < w-1) continue;
		while(!deq.isEmpty() && i-w>=deq.getFirst()) {
			deq.removeFirst();
		}
		max[i-w+1] = A[deq.getFirst()];
	}
	return max;
}

// given an array, return the starting and ending index of all subarrays that sums to 0;
public void getZeroSumIndex(int[] A) {
	int n = A.length;
	int[] sum = new int[n+1];
	Map> map = new HashMap<>();
	Set result = new HashSet<>();
	for(int i=0; i list = map.get(sum[i+1]);
		if(list == null) {
			list = new ArrayList<>();
			map.put(sum[i+1], list);
		} else {
			for(int index : list) {
				result.add((index+1)*31 + i);
			}
		}
		list.add(i);
	}
	
	for(int num: result) {
		System.out.println(num/31 + ", " + num%31);
	}
}

// word break 1
// 重要的是要写出时间复杂度 递归(2^n)? 和worst case(如aaac, 字典是("a", "aa", "aaa"))
// Time: O(n^2)
public boolean wordBreak(String s, Set wordDict) {
    int n = s.length();
    boolean[] f = new boolean[n+1];
    f[0] = true;
    for(int i=1; i<=n; i++) {
        for(int j=0; j dict){
	//Base case
	if(dict.contains(s)) return true;
	for(int i = 0; i < s.length(); i++){
		String sstr = s.substring(0, i);
		if(dict.contains(sstr) && wordBreak(s.substring(i), dict))
			return true;
	}
	return false;
}

// word break 1的另外一种写法 O(M*N), 
// Time: O(string length * dict size)
public boolean wordBreak(String s, Set dict) {
        boolean[] t = new boolean[s.length()+1];
        t[0] = true; //set first to be true, why?
        //Because we need initial state
        for(int i=0; i s.length()) continue;
                if(t[end]) continue;
                if(s.substring(i, end).equals(a)){
                    t[end] = true;
                }
            }
        }
        return t[s.length()];
    }
}

// word break II recursive method, Time is O(n^2) 
public List wordBreak(String s, Set dict) {
    List list = new ArrayList<>();
    boolean[] f = new boolean[s.length()+1];
    Arrays.fill(f, true);
    breakWord(list, dict, f, s, 0, "");
    return list;
}

public void breakWord(List list, Set dict, boolean[] f, String s, int start, String t) {
    if(start == s.length()) {
        list.add(t.substring(1));
        return;
    }
    for(int i=start+1; i<=s.length(); i++) {
        String word = s.substring(start, i);
        if(dict.contains(word) && f[i]) {
            int size = list.size();
            breakWord(list, dict, f, s, i, t+" "+word);
            if(list.size() == size) f[i] = false;
        }
    }
}

// longest palindrome substring
public String longestPalindrome(String s) {
    String res = "";
    for(int i=0; i res.length()) {
            res = str;
        }
        
        str = palindromeAtCenter(s, i, i+1);
        if(str.length() > res.length()) {
            res = str;
        }
    }
    return res;
}

private String palindromeAtCenter(String s, int c1, int c2) {
    while(c1>=0 && c2 stack = new Stack<>();
	for(int i=0; i stack) {
	visited[v] = true;
	for(int u : g.adj[v]) {
		if(!visited[u]) {
			toposort(g, u, visited, stack);
		}
	}
	stack.push(v);
}

// room isConnected
// 第四轮的题还问到了时间复杂度
// 对于访问到的每个点判断是否满足小于k需要O(k),
// 所有访问到的点形成的图形最长半径为n的话则有O(n2)个点,所以总共是O(n2*k)。
public class MonkeyProblem {
	static class Point {
		int x, y;
		Point(int x, int y) {
			this.x = x;
			this.y = y;
		}
		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (!(o instanceof Point)) return false;
			Point pair = (Point) o;
			return x == pair.x && y == pair.y;
		}
		@Override
		public int hashCode() {
			return 31 * x + y;
		}
	}
	
	public static int digitSum(int n) {
		if(n < 0) n = -n;
		int sum = 0;
		while(n != 0) {
			sum += n % 10;
			n /= 10;
		}
		return sum;
	}

	private static int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
	public static int countSteps(int k) {
		Set set = new HashSet<>();
		Queue queue = new LinkedList<>();
		queue.offer(new Point(0, 0));
		while(!queue.isEmpty()) {
			Point p = queue.poll();
			if(set.contains(p) || (digitSum(p.x) + digitSum(p.y)) > k) continue;
			set.add(p);
			for(int i=0; i<4; i++) {
				queue.offer(new Point(p.x+dir[i][0], p.y+dir[i][1]));
			}
		}
		return set.size();
	}

	public static void main(String[] args) {
		System.out.println(countSteps(19));
	}
}

// serialize binary tree
public String serialize(TreeNode root){  
    StringBuilder sb = new StringBuilder();  
    serialize(root, sb);  
    return sb.toString();  
}  
   
private void serialize(TreeNode x, StringBuilder sb){  
    if (x == null) {  
        sb.append("# ");  
    } else {  
        sb.append(x.val + " ");  
        serialzie(x.left, sb);  
        serialzie(x.right, sb);  
    }  
}  
   
public TreeNode deserialize(String s){  
    if (s == null || s.length() == 0) return null;  
    StringTokenizer st = new StringTokenizer(s, " ");  
    return deserialize(st);  
}  
   
private TreeNode deserialize(StringTokenizer st){  
    if (!st.hasMoreTokens())  
        return null;  
    String val = st.nextToken();  
    if (val.equals("#"))  
        return null;  
    TreeNode root = new TreeNode(Integer.parseInt(val));  
    root.left = deserialize(st);  
    root.right = deserialize(st);  
    return root;  
}  

// a?b:c  tenary tree
public TreeNode convertToTree (char[] values) {
    TreeNode root = new TreeNode(values[0]);
    TreeNode n = root;
    Stack stack =  new Stack();
    for (int i = 1; i < values.length; i += 2) {
        if (values[i] == '?') {
            n.left = new TreeNode (values[i + 1]);
            stack.push(n);
            n = n.left;

        }
        else if (values[i] == ':') {
            n = stack.pop();
            while (n.right != null) {
                n = stack.pop();
            }             
            n.right = new TreeNode (values[i + 1]);
            stack.push(n);
            n = n.right;
        }
    }
    return root;
}	

// mutable string
public char charAt(int index) {
    if ((index < 0) || (index >= count)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index + offset];
}

public String substring(int beginIndex, int endIndex) {
	if (beginIndex < 0) {
	    throw new StringIndexOutOfBoundsException(beginIndex);
	}
	if (endIndex > count) {
	    throw new StringIndexOutOfBoundsException(endIndex);
	}
	if (beginIndex > endIndex) {
	    throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
	}
	return ((beginIndex == 0) && (endIndex == count)) ? this :
	    new String(offset + beginIndex, endIndex - beginIndex, value);
}

 

 

你可能感兴趣的:(Pocket Gems专题)