Skip to content

Instantly share code, notes, and snippets.

View yitonghe00's full-sized avatar

YitongHe yitonghe00

View GitHub Profile
let arrays = [[1, 2, 3], [4, 5], [6]];
// Your code here.
const flat = (arrays) => {
const ret = [];
arrays.forEach((array) => {
array.forEach((num) => ret.push(num));
});
return ret;
};
@yitonghe00
yitonghe00 / Eloquent JavaScript 0404 Deep comparison.js
Last active April 12, 2020 01:53
Excises of Eloquent JavaScript 4th edition. Chapter 04 Problem 04. https://eloquentjavascript.net/04_data.html#i_IJBU+aXOIC
// Your code here.
const deepEqual = (a, b) => {
if (typeof a != 'object' || typeof b != 'object') return a === b;
if (a == null || b == null) return a === b;
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length != bKeys.length) return false;
for (let i = 0 ; i < aKeys.length; i++) {
@yitonghe00
yitonghe00 / Eloquent JavaScript 0403 A list.js
Created April 12, 2020 01:16
Excises of Eloquent JavaScript 4th edition. Chapter 04 Problem 03. https://eloquentjavascript.net/04_data.html#i_6xTmjj4Rf5
// Your code here.
const arrayToList = (arr) => {
return arrayToListR(arr, 0);
}
const arrayToListR = (arr, start) => {
if (start >= arr.length) return null;
const ans = {};
ans.value = arr[start];
// Your code here.
const reverseArray = (arr) => {
const ans = [];
for (let i = arr.length - 1; i >= 0; i--) {
ans.push(arr[i]);
}
return ans;
}
const reverseArrayInPlace = (arr) => {
// Your code here.
const range = (start, end, step = 1) => {
const ans = [];
if (step > 0 && start < end) {
for (let i = start; i <= end; i += step) {
ans.push(i);
}
} else {
if (step > 0) step = -1;
for (let i = start; i >= end; i += step) {
@yitonghe00
yitonghe00 / 7. Reverse Integer (#1 Math).java
Created February 11, 2020 09:26
7. Reverse Integer (https://leetcode.com/problems/reverse-integer/): Given a 32-bit signed integer, reverse digits of an integer.
// Math solution: Pull the digits and take care of the overflow
// Time: O(logx), 1ms
// Space: O(1), 36.6mb
class Solution {
public int reverse(int x) {
int ans = 0;
while(x != 0) {
int tail = x % 10;
int newAns = ans * 10 + tail;
@yitonghe00
yitonghe00 / 724. Find Pivot Index (#1 Array + DP).java
Created February 11, 2020 08:54
724. Find Pivot Index (https://leetcode.com/problems/find-pivot-index/): Given an array of integers nums, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index. If no such index exists, …
// Array + DP solution: store the prefix sum in array and scan from left
// Time: O(n), 1ms
// Space: O(n), 42.4mb
class Solution {
public int pivotIndex(int[] nums) {
// Count the prefix sum of nums: prefix[i] = nums[0] + nums[1] + .. + nums[i - 1]
int len = nums.length, sum = 0;
int[] prefix = new int[len];
for(int i = 0; i < len; i++) {
prefix[i] = sum;
@yitonghe00
yitonghe00 / 724. Find Pivot Index (#1 Array + Hash table).java
Last active September 8, 2020 12:08
724. Find Pivot Index (https://leetcode.com/problems/find-pivot-index/): Given an array of integers nums, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index. If no such index exists, …
// Array + Hash table solution: store the prefix sum in array and scan from left
// Time: O(n), 1ms
// Space: O(n), 42.4mb
class Solution {
public int pivotIndex(int[] nums) {
// Count the prefix sum of nums: prefix[i] = nums[0] + nums[1] + .. + nums[i - 1]
int len = nums.length, sum = 0;
int[] prefix = new int[len];
for(int i = 0; i < len; i++) {
prefix[i] = sum;
@yitonghe00
yitonghe00 / 974. Subarray Sums Divisible by K (#1 Array + Hash table).java
Last active February 11, 2020 08:55
974. Subarray Sums Divisible by K (https://leetcode.com/problems/subarray-sums-divisible-by-k/): Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
// Array + Hash table solution: store prefix sum mod in table as we counting; combination of 560 & 523
// Time: O(n), 19ms
// Space: O(k), 45.2mb
class Solution {
public int subarraysDivByK(int[] A, int K) {
int ans = 0;
// Init map of <sum % K, count>
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
@yitonghe00
yitonghe00 / 523. Continuous Subarray Sum (#1 Array + Brutal force).java
Created February 11, 2020 02:21
523. Continuous Subarray Sum (https://leetcode.com/problems/continuous-subarray-sum/): Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple of k, that is, sums up to n*k where n is also an integer.
// Array + Brutal force solution
// Time: O(n ^ 2), 16ms
// Space: O(1), 42.2mb
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
for(int i = 0; i < nums.length; i++) {
int sum = nums[i];
for(int j = 1; i + j < nums.length; j++) {
sum += nums[i + j];