This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } | |
| * } | |
| */ | |
| class Solution { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } | |
| * } | |
| */ | |
| class Solution { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RecentCounter { | |
| List<Integer> array; | |
| int left = 0; | |
| public RecentCounter() { | |
| this.array = new ArrayList<>(); | |
| } | |
| public int ping(int t) { | |
| array.add(t); |