Clover Blog

Love Piano, Love Roller Coaster

LeetCode Rolling Hash template

Rolling Hash templates in Java and relevant questions

Rolling Hash time = O(n) space = O(1) public static void main(String[] args) { String s = "abcdefg"; String t = "bcd"; int mod = 1_000_000_007; int p = 26; long target =...

LeetCode Stock questions template

Stock templates in Java and relevant questions

Best Time to Buy and Sell Stock Best Time to Buy and Sell Stock II Best Time to Buy and Sell Stock III time = O(n) space = O(1) class Solution { public int maxProfit(int[] prices) { ...

LeetCode quick sort/select template

quick sort / select in Java and relevant questions

quick sort Java template class Solution { public int[] sortArray(int[] nums) { if (nums == null || nums.length <= 1) { return nums; } quickSort(nums, 0, ...

LeetCode Priority Queue schedule template

Use heap to schedule tasks

Java Template Use priority Queue to schedule tasks Use two priority queues to present servers in using and available Each time new task comes, move using to available, and then assign server f...

LeetCode manache template

Using manache for palindrome in Java and relevant questions

Java Template Manache algo to find longest palindrome original for odd number palindrome we can build odd/even number string to odd number by adding ‘#’ between each char e.g aba -> a#b#a, ...

LeetCode Monotonic stack template

Monotonic stack in Java and relevant questions

Java Template Use Stack or Deque in ascending or descending order time = O(n) space = O(n) Leetcode 1944. Number of Visible People in a Queue class Solution { public int[] canSeePersons...

LeetCode bit mask template

Bit mask in Java and relevant questions

Java Template Use int bit operation to do memorization, for combination recursion. time = O(n!) space = O(n) Leetcode 1947. Maximum Compatibility Score Sum class Solution { public int m...

VPC connection

VPC direct connect, VPC peering, VPC private link (endpoint)

Basic concepts AWS Direct Connect AWS Direct Connect is a cloud service solution that makes it easy to establish a dedicated network connection from your premises to AWS. AWS Vpc Peering ...

LeetCode binary search template

classic binary search, left/right boundary

Binary search Template Classic Binary Search public static int binarySearch(int[] array, int target) { int left = 0, right = array.length - 1; while (left <= right) { int mid = left + (rig...

LeetCode dp template -- substring

dp and substring type in Java and relevant questions

Palindrome dp Template Longest Palindromic Substring time = O(n^2) space = O(n^2) class Solution { public String longestPalindrome(String s) { if (s == null || s.length() == 0) ...