Skip to content

Instantly share code, notes, and snippets.

View yitonghe00's full-sized avatar

YitongHe yitonghe00

View GitHub Profile
@yitonghe00
yitonghe00 / 560. Subarray Sum Equals K (#1 Array + Brutal force).java
Created February 11, 2020 01:14
560. Subarray Sum Equals K (https://leetcode.com/problems/subarray-sum-equals-k/): Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
// Array + Brutal force solution
// Time: O(n ^ 2), 212ms
// Space: O(1), 40.6mb
class Solution {
public int subarraySum(int[] nums, int k) {
int ans = 0;
for(int i = 0; i < nums.length; i++) {
int sum = 0;
for(int j = 0; i + j < nums.length; j++) {
@yitonghe00
yitonghe00 / 896. Monotonic Array (#1 Array).java
Created February 10, 2020 08:33
896. Monotonic Array (https://leetcode.com/problems/monotonic-array/): An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j]. Return true if and only if the given array A is monotonic.
// Array solution: if A[i] > A[i + 1], A can't be increasing; if A[i] < A[i + 1], A can't be decreasing
// Time: O(n) for 2 pass, 1ms
// Space: O(1), 44.2mb
class Solution {
public boolean isMonotonic(int[] A) {
return increasing(A) || decreasing(A);
}
private boolean increasing(int[] A) {
for(int i = 0; i < A.length - 1; i++) {
@yitonghe00
yitonghe00 / 824. Goat Latin (#1 String).java
Created February 10, 2020 08:13
824. Goat Latin (https://leetcode.com/problems/goat-latin/): A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)
// String solution
// Time: O(L + N ^ 2) where L = S.length() and N = words.length, 2ms
// Space: O(L + N ^ 2), 38.3mb
class Solution {
public String toGoatLatin(String S) {
Set<Character> vowel = new HashSet<>();
for(char c : "aeiouAEIOU".toCharArray()) vowel.add(c);
StringBuilder ans = new StringBuilder();
String ending = "a";
@yitonghe00
yitonghe00 / 239. Sliding Window Maximum (#1 Array).java
Last active February 10, 2020 07:46
239. Sliding Window Maximum (https://leetcode.com/problems/sliding-window-maximum/): Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.
// Array solution: Sliding window and look back (from last) + remove out of window nums (from first)
// Time: O(n) for every element is added and removed once, 10ms
// Space: O(k), 49.3mb
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
// Corner case
if(nums == null || k <= 0) return new int[0];
// Deque of indices of potential max in window (first max, second max, ...)
Deque<Integer> deque = new ArrayDeque<>();
@yitonghe00
yitonghe00 / 265. Paint House II (#1 Array + DP).java
Created February 10, 2020 03:54
265. Paint House II (https://leetcode.com/problems/paint-house-ii/): There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The cost of painting each house with a cert…
// Array + DP solution: keep the min & second min of last row
// Time: O(nk), 2ms
// Space: O(1) by overwriting the input, 40.9mb
class Solution {
public int minCostII(int[][] costs) {
// Corner case
if(costs == null || costs.length == 0) return 0;
// Find the min and second min in the first row
int min = Integer.MAX_VALUE, secondMin = Integer.MAX_VALUE, minIndex = -1;
@yitonghe00
yitonghe00 / 238. Product of Array Except Self (#1 Array + DP).java
Created February 9, 2020 23:00
238. Product of Array Except Self (https://leetcode.com/problems/product-of-array-except-self/): Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
// Array + DP solutoin: prefix and postfix product array, idea like 42 trap water #2
// Time: O(n), 1ms
// Space: O(n), 47.7mb
class Solution {
public int[] productExceptSelf(int[] nums) {
// Build array of prefix and postfix product
int len = nums.length;
int[] prefix = new int[len];
int[] postfix = new int[len];
@yitonghe00
yitonghe00 / 989. Add to Array-Form of Integer (#1 Math + Array).java
Created February 8, 2020 00:55
989. Add to Array-Form of Integer (https://leetcode.com/problems/add-to-array-form-of-integer/): For a non-negative integer X, the array-form of X is an array of its digits in left to right order. For example, if X = 1231, then the array form is [1,2,3,1]. Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.
// Math + Array solution
// Time: O(max(n, logK)), 3ms
// Space: O(1), 43.1mb
class Solution {
public List<Integer> addToArrayForm(int[] A, int K) {
List<Integer> ans = new LinkedList<>();
for(int i = A.length - 1; i >= 0 || K > 0; i--) {
int sum = K;
if(i >= 0) sum += A[i];
// Math + Array solution: multiply the digits and put the result in specific indices
// Time: O(l1 + l2), 3ms
// Space: O(l1 + l2), 38.6mb
class Solution {
public String multiply(String num1, String num2) {
// Multiply the string into an array
int l1 = num1.length(), l2 = num2.length();
int[] ans = new int[l1 + l2];
for(int i1 = l1 - 1; i1 >= 0; i1--) {
@yitonghe00
yitonghe00 / 66. Plus One (#1 Array).java
Created February 4, 2020 00:09
66. Plus One (https://leetcode.com/problems/plus-one/): Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. You may assume the integer does not contain any leadin…
// Array solution: modify the original array or create a new array for carry digit
// Time: O(n), 0ms
// Space: O(n), 37.5mb
class Solution {
public int[] plusOne(int[] digits) {
// Add one on original array
for(int i = digits.length - 1; i >= 0; i--) {
if(digits[i] < 9) {
digits[i]++;
return digits;
@yitonghe00
yitonghe00 / 953. Verifying an Alien Dictionary (#1 String + Hash table).java
Created January 19, 2020 00:44
953. Verifying an Alien Dictionary (https://leetcode.com/problems/verifying-an-alien-dictionary/): In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the alien language, and the orde…
// String + Hash table solution: handling of two string of different length
// Time: O(n * l), 0ms
// Space: O(1), 43.5mb
class Solution {
int[] table; // table[alien] = order
public boolean isAlienSorted(String[] words, String order) {
// Create the mapping table
table = new int[26];
for(int i = 0; i < order.length(); i++) {