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
// HashTable solution: 2 HashSet, one for nums1 and one for answer | |
// Time: O(n1 + n2), 2ms | |
// Space: O(n1), 37.4mb | |
class Solution { | |
public int[] intersection(int[] nums1, int[] nums2) { | |
Set<Integer> set1 = new HashSet<>(); | |
Set<Integer> inter = new HashSet<>(); | |
for(int i = 0; i < nums1.length; i++) { | |
set1.add(nums1[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
// HashMap Solution | |
// Time: O(n), 2ms | |
// Space: O(n), 36.5mb | |
class Solution { | |
public int[] intersect(int[] nums1, int[] nums2) { | |
// HashMap to save the nums in nums1 | |
Map<Integer, Integer> map = new HashMap<>(); | |
for(int n : nums1) { | |
if(map.containsKey(n)) { | |
map.put(n, map.get(n) + 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
// Two pointer solution: min/max substring template | |
// Time: O(s + t) where m is the length of s and t is the length of t, 3ms | |
// Space: O(1) since we have int[128] instead of int[t], 37.8mb | |
class Solution { | |
public String minWindow(String s, String t) { | |
// Init the char table | |
int[] table = new int[128]; | |
int count = 0; // count is the flag that whether we have all the char in t in the substring | |
for(int i = 0; i < t.length(); i++) { | |
table[t.charAt(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
class Solution { | |
public String minWindow(String s, String t) { | |
int[] table = new int[128]; //Hash table | |
int begin = 0, end = 0, len = Integer.MAX_VALUE, minBegin = 0; | |
int count = 0; //Check whether the substring is valid | |
for(char c : t.toCharArray()) { | |
//Initialize the hash table here | |
} | |
while(end < s.length()) { | |
if(table[s.charAt(end++)]-- /*various condition*/){//Move end forward to make it valid(min) / invalid(max) |
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
// Two pointer solution: min/max substring template | |
// Time: O(n), 4ms | |
// Space: O(1), 38mb | |
class Solution { | |
public int findMaxConsecutiveOnes(int[] nums) { | |
int begin = 0, end = 0, maxLen = 0, zeros = 0; | |
while(end < nums.length) { | |
// Expand the window to the right | |
if(nums[end++] == 0) { | |
zeros++; |
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 + Two pointer solution | |
// Time: O(n), 10ms | |
// Space: O(1), 54.3mb | |
class Solution { | |
public int numSubarrayProductLessThanK(int[] nums, int k) { | |
// Subarray is [slow, fast). product is the product of subarray | |
int slow = 0, fast = 0, ans = 0, product = 1; | |
while(fast < nums.length) { | |
while(fast < nums.length && (product *= nums[fast]) < k) { | |
ans += (fast - slow + 1); // Since [slow, fast] is valid, [slow + 1, fast], [slow + 2, fast], ... are all valid |
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
// Two pointer: 3 pointers -> loop + begin/end pointers | |
// Time: O(n ^ 2), 6ms | |
// Space: O(1), 36.5mb | |
class Solution { | |
public int threeSumClosest(int[] nums, int target) { | |
Arrays.sort(nums); | |
int ans = nums[0] + nums[1] + nums[2]; | |
for(int i = 0; i < nums.length - 2; i++) { | |
int l = i + 1, r = nums.length - 1; | |
while(l < r) { |
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
// Two pointer: 3 pointer -> loop + begin/end pointers | |
// Time: O(n ^ 2), 3ms | |
// Space: O(1), 36.3mb | |
class Solution { | |
public int threeSumSmaller(int[] nums, int target) { | |
int ans = 0; | |
Arrays.sort(nums); | |
for(int i = 0; i < nums.length - 2; i++) { | |
int l = i + 1, r = nums.length -1; | |
while(l < r) { |
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
// Two pointer: 3 pointers -> loop + begin/end pointers | |
// Time: O(n ^ 2), 4ms | |
// Space: O(1), 39.3mb | |
class Solution { | |
public int triangleNumber(int[] nums) { | |
// Sort the array first to use two pointer technic | |
Arrays.sort(nums); | |
int ans = 0; | |
for(int i = nums.length - 1; i >= 2; 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
// Two pointer solution: min/max substring template | |
// Time: O(n), 1ms | |
// Space: O(1), 35.9mb | |
class Solution { | |
public int lengthOfLongestSubstringTwoDistinct(String s) { | |
// Init table that keeps track of char we have and the count | |
int[] table = new int[128]; | |
int count = 0; | |
// Two pointer |