LeetCode – Patching Array (Java)

Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required. Example 1: nums = [1, 3], n = 6 Return 1. … Read more

LeetCode – Number of Islands II (Java)

A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and … Read more

LeetCode – Find Median from Data Stream (Java)

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Analysis First of all, it seems that the best time complexity we can get for this problem is O(log(n)) of add() … Read more

LeetCode – Verify Preorder Serialization of a Binary Tree (Java)

One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as #. 9 / \ 3 2 / \ / \ 4 1 # 6 / \ / \ … Read more

LeetCode – Sparse Matrix Multiplication (Java)

Given two sparse matrices A and B, return the result of AB. You may assume that A’s column number is equal to B’s row number. 1. Naive Method We can implement Sum(A_ik * B_kj) -> C_ij as a naive solution. public int[][] multiply(int[][] A, int[][] B) { //validity check   int[][] C = new int[A.length][B[0].length]; … Read more

LeetCode – Binary Watch (Java)

Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent. Example: Input: n = 1 Return: [“1:00”, “2:00”, “4:00”, “8:00”, “0:01”, “0:02”, “0:04”, “0:08”, “0:16”, “0:32”] Accepted Java Solution public List<String> readBinaryWatch(int num) { List<String> result = new ArrayList<String>();   for(int i=0; … Read more

Find a Path in a Matrix

Given a 2d matrix, find a path from the top left corner to bottom right corner. Assume there exists at least one path, and you only need to find one valid path. You can move up, right, down, left at any position. For example, given [1, 0, 0, 0, 0] [1, 0, 1, 1, 1] … Read more

LeetCode – Decode String (Java)

Given an encoded string, return it’s decoded string. For example, given “3[a2[b]]”, return “abbabbabb”. https://leetcode.com/problems/decode-string/ Java Solution The key to solve this problem is convert the string to a structured data structure and recursively form the return string. class Solution { public String decodeString(String s) { Stack<Exp> stack = new Stack<>();   Exp e = … Read more

LeetCode – Longest Substring with At Least K Repeating Characters (Java)

Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times. Example 1: Input: s = “aaabb”, k = 3 Output: 3 The longest substring is “aaa”, as ‘a’ is repeated 3 times. Java Solution This problem … Read more

LeetCode – Longest Absolute File Path (Java)

https://leetcode.com/problems/longest-absolute-file-path/ Java Solution 1 class Node{ int level; int len; public Node(int lev, int len){ this.level = lev; this.len = len; } }   public class Solution { public int lengthLongestPath(String input) { if(input==null||input.length()==0) return 0;   int max=0;   String[] arr = input.split("\n");   Stack<Node> stack = new Stack<Node>();   for(int i=0; i<arr.length; i++){ … Read more