This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |