This file contains hidden or 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: | |
def threeSumClosest(self, nums: List[int], target: int) -> int: | |
""" | |
One outer loop + Two pointer | |
Time: O(n^2) | |
Space: O(1) | |
""" | |
nums.sort() | |
n = len(nums) | |
closest_distance = float('inf') |
This file contains hidden or 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: | |
def threeSumSmaller(self, nums: List[int], target: int) -> int: | |
""" | |
If we fix one dimension, the question essentially becomes a "two sum smaller" problem | |
The ideas are: | |
1) when sum < target, count += r - l, then we need to move left to the next bigger value | |
2) otherwise move r to the next smaller value | |
""" | |
nums.sort() | |
n = len(nums) |
This file contains hidden or 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: | |
def twoSumLessThanK(self, nums: List[int], k: int) -> int: | |
nums.sort() | |
res = -1 | |
l, r = 0, len(nums) - 1 | |
while l < r: | |
curr_sum = nums[l] + nums[r] | |
if curr_sum >= k: | |
r -= 1 | |
else: |
This file contains hidden or 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: | |
def triangleNumber(self, nums: List[int]) -> int: | |
""" | |
Idea: a variant of 3-sum where two pointers need to be in the same side | |
Time: O(n^2) | |
Space: O(1) | |
Requirement of a valid triangle: the smaller two numbers must greater than the biggest number in the triplet | |
""" | |
nums.sort() | |
n = len(nums) |
This file contains hidden or 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: | |
def addStrings(self, num1: str, num2: str) -> str: | |
res = [] | |
len1, len2 = len(num1), len(num2) | |
carry = 0 | |
i = 0 | |
while i < len1 or i < len2: | |
sum = carry | |
if i < len1: | |
sum += int(num1[len1-1-i]) |
This file contains hidden or 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: | |
def addBinary(self, a: str, b: str) -> str: | |
res = [] | |
len_a = len(a) | |
len_b = len(b) | |
i = 0 | |
carry = 0 | |
while i < len_a or i < len_b: | |
sum = carry | |
if i < len_a: |
This file contains hidden or 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 { | |
// A typical union-find problem | |
// Time: O(nK), where K = tree height | |
// Space: O(n) | |
public int earliestAcq(int[][] logs, int N) { | |
Arrays.sort(logs, (l, r) -> l[0] - r[0]); | |
UnionFind uf = new UnionFind(N); | |
for (int i = 0; i < logs.length; i++) { | |
uf.union(logs[i][1], logs[i][2]); | |
if (uf.componentCnt == 1) { |
This file contains hidden or 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 { | |
// A typical union-find problem | |
// Time: O(n^2 logn) | |
// Space: O(n) | |
public int findCircleNum(int[][] isConnected) { | |
int n = isConnected.length; | |
UnionFind uf = new UnionFind(n); | |
for (int i = 0; i < n - 1; i++) { | |
for (int j = i + 1; j < n; j++) { | |
if (isConnected[i][j] == 1) { |
This file contains hidden or 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 { | |
// Typical union find problem | |
// Time: O(n *26), where n is len(A) | |
// Space: O(26) | |
public String smallestEquivalentString(String A, String B, String S) { | |
UnionFind uf = new UnionFind(); | |
for (int i = 0; i < A.length(); i++) { | |
uf.union(A.charAt(i), B.charAt(i)); | |
} | |
StringBuilder res = new StringBuilder(); |
This file contains hidden or 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 WordDictionary { | |
static final int ALPHABET_SIZE = 26; | |
public static class TrieNode { | |
char val; | |
int count; | |
TrieNode[] children = new TrieNode[ALPHABET_SIZE]; | |
TrieNode(char val) { |