LeetCode – Longest Increasing Path in a Matrix (Java)

Given an integer matrix, get the length of the longest increasing path. https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Java Solution 1 – Naive DFS public int longestIncreasingPath(int[][] matrix) { int[] max = new int[1]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { dfs(matrix, i, j, max, 1); } … Read more

LeetCode – Moving Average from Data Stream (Java)

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. Java Solution This problem is solved by using a queue. class MovingAverage { double sum; int size; LinkedList<Integer> list;   /** Initialize your data structure here. */ public MovingAverage(int size) { this.list = new LinkedList<>(); … Read more

LeetCode – Nim Game (Java)

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones. Analysis Java … Read more

LeetCode – Binary Tree Vertical Order Traversal (Java)

Given a binary tree, return the vertical order traversal of its nodes’ values. (ie, from top to bottom, column by column). Java Solution 1 For each node, its left child’s degree is -1 and is right child’s degree is +1. We can do a level order traversal and save the degree information. public List<List<Integer>> verticalOrder(TreeNode … Read more

LeetCode – Swap Nodes in Pairs (Java)

Given a linked list, swap every two adjacent nodes and return its head. For example, given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. Java Solution 1 Use two template variable to track … Read more

LeetCode – Count Primes (Java)

Count the number of prime numbers less than a non-negative number, n Java Solution 1 This solution exceeds time limit. public int countPrimes(int n) { n = n-1;   ArrayList<Integer> primes = new ArrayList<Integer>();   if(n<=1) return 0; if(n==2) return 1; if(n==3) return 2;   primes.add(2); primes.add(3);   for(int i=4; i<=n; i++){ boolean isPrime = … Read more

LeetCode – Binary Tree Longest Consecutive Sequence (Java)

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse). Java Solution 1 – BFS … Read more

LeetCode – Simplify Path (Java)

Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” path = “/../”, => “/” path = “/home//foo/”, => “/home/foo” Java Solution public String simplifyPath(String path) { Stack<String> stack = new Stack<String>();   //stack.push(path.substring(0,1));   while(path.length()> 0 && path.charAt(path.length()-1) ==’/’){ path = path.substring(0, … Read more

LeetCode – Maximum Product of Word Lengths (Java)

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Java Solution public int maxProduct(String[] words) { if(words==null || words.length==0) return 0;   … Read more

LeetCode – Range Sum Query – Mutable (Java)

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to val. Java Solution 1 – Segment Tree class TreeNode{ int start; int end; int sum; TreeNode leftChild; TreeNode rightChild;   public … Read more