Skip to content

Instantly share code, notes, and snippets.

View wushbin's full-sized avatar

Shengbin Wu wushbin

  • San Francisco Bay Area
View GitHub Profile
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
class WordDictionary {
class TrieNode {
char c;
TrieNode[] children;
boolean isWord;
public TrieNode(char c) {
this.c = c;
this.children = new TrieNode[26];
this.isWord = false;
class Solution {
public int trap(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int left = 0;
int right = height.length - 1;
int leftMax = height[left];
int rightMax = height[right];
// from lee215's discussion in leetcode
class Solution {
public int shortestSubarray(int[] A, int K) {
// assumptions
int len = A.length;
int[] sum = new int[len + 1];
for (int i = 1; i <= len; i++) {
sum[i] = sum[i - 1] + A[i - 1];
}
class Solution {
public String removeDuplicateLetters(String s) {
if (s == null || s.length() == 0) {
return "";
}
int[] hash = new int[26];
for (char c : s.toCharArray()) {
hash[c - 'a'] += 1;
}
class Solution {
public List<Integer> findNumOfValidWords(String[] words, String[] puzzles) {
// valid, puzzle[0] in word, each letter in word is in puzzle
Map<Integer, Integer> map = new HashMap<>();
for (String w : words) {
int hash = 0;
for (int i = 0; i < w.length(); i++) {
hash |= (1 << (w.charAt(i) - 'a'));
}
map.put(hash, map.getOrDefault(hash, 0) + 1);
class Solution {
public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
if (Profits == null || Profits.length == 0 || Capital == null || Capital.length == 0 || k == 0) {
return 0;
}
// sort by capital
PriorityQueue<int[]> minCap = new PriorityQueue<>((a, b) -> (Integer.compare(a[1], b[1])));
int len = Profits.length;
for (int i = 0; i < len; i++) {
class Solution {
public int leastInterval(char[] tasks, int n) {
Map<Integer, Integer> map = new HashMap<>();
for (int t : tasks) {
map.put(t, map.getOrDefault(t, 0) + 1);
}
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((a, b)->b.getValue().compareTo(a.getValue()));
queue.addAll(map.entrySet());
int res = 0;
class RecentCounter {
List<Integer> array;
int left = 0;
public RecentCounter() {
this.array = new ArrayList<>();
}
public int ping(int t) {
array.add(t);