## LeetCode – Design Snake Game (Java)

Design a Snake game that is played on a device with screen size = width x height. Play the game online if you are not familiar with the game. The snake is initially positioned at the top left corner (0,0) with length = 1 unit. You are given a list of food’s positions in row-column … 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

## LeetCode – Lexicographical Numbers (Java)

Given an integer n, return 1 – n in lexicographical order. For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. Java Solution – DFS public List<Integer> lexicalOrder(int n) { int c=0; int t=n; while(t>0){ c++; t=t/10; }   ArrayList<Integer> … Read more

## LeetCode – Rearrange String k Distance Apart (Java)

Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other. All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string “”. Example: str = “aabbcc”, k = 3 Result: … Read more

## LeetCode – Design Twitter (Java)

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods: postTweet(userId, tweetId): Compose a new tweet. getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user’s news feed. … Read more

## LeetCode – Count Numbers with Unique Digits (Java)

Given a non-negative integer n, count all numbers with unique digits, x, where x is from 0 to 10^n-1. Java Solution public int countNumbersWithUniqueDigits(int n) { int[] arr = new int[n+1]; arr[0]=1; // x can be 0   for(int i=1; i<=n; i++){ arr[i]=9; for(int j=9; j>=9-i+2; j–){ arr[i] *= j; } }   int result … Read more

## LeetCode – Line Reflection (Java)

Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflects the given points. Example 1: Given points = [[1,1],[-1,1]], return true. Example 2: Given points = [[1,1],[-1,-1]], return false. Follow up: Could you do better than O(n2)? Java Solution For this problem, we first find the … Read more

## LeetCode – Nested List Weight Sum II (Java)

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list — whose elements may also be integers or other lists. Different from the previous question where weight is increasing from root to leaf, now the weight is … Read more

## LeetCode – Plus One Linked List (Java)

Given a non-negative number represented as a singly linked list of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. Example: Input: 1->2->3 Output: 1->2->4 Java Solution public ListNode plusOne(ListNode head) { ListNode h2 = reverse(head);   ListNode p=h2;   while(p!=null){ … Read more

## LeetCode – Design Phone Directory (Java)

Design a Phone Directory which supports the following operations: get: Provide a number which is not assigned to anyone. check: Check if a number is available or not. release: Recycle or release a number. Java Solution 1 public class PhoneDirectory { int max; HashSet<Integer> set; LinkedList<Integer> queue;   /** Initialize your data structure here @param … Read more

## LeetCode – Super Pow (Java)

Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array. Example1: a = 2 b = [3] Result: 8 Java Solution public int superPow(int a, int[] b) { int result=1;   for(int i=0; i<b.length; i++){ result … Read more

## LeetCode – Shuffle an Array (Java)

Shuffle a set of numbers without duplicates. Java Solution How we make sure each the probability of each element get shuffled is very similar to the streaming random problem. The algorithm is straightforward to understand, but the question is why it works. To have a working shuffle algorithm, every element in the array results in … Read more

## LeetCode – Mini Parser (Java)

Given a nested list of integers represented as a string, implement a parser to deserialize it. Each element is either an integer, or a list — whose elements may also be integers or other lists. Note: You may assume that the string is well-formed: String is non-empty. String does not contain white spaces. String contains … Read more

## LeetCode – Max Sum of Rectangle No Larger Than K (Java)

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. Example: Given matrix = [ [1, 0, 1], [0, -2, 3] ] k = 2 The answer is 2. Because the sum of rectangle [[0, 1], … Read more

## LeetCode – Largest Divisible Subset (Java)

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. If there are multiple solutions, return any subset is fine. Example 1: nums: [1,2,3] Result: [1,2] (of course, [1,3] will also … Read more