-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
88 additions
and
51 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ favors time. | |
## 🧩 Solved | ||
|
||
- [x] 1. Two Sum | ||
- [x] 217. Contains Duplicate | ||
- [x] 242. Valid Anagram | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Algorithm/technique: nested iteration | ||
# Time complexity: O(n^2) | ||
# Auxiliary space: O(1) | ||
# Optimal: no | ||
|
||
class Solution: | ||
def twoSum(self, nums: list[int], target: int) -> list[int]: | ||
n = len(nums) | ||
|
||
# enumerate does not allow the last item to be skipped without slicing, | ||
# which would require linear auxiliary space. | ||
for i in range(n - 1): | ||
for j in range(i + 1, n): | ||
if nums[i] + nums[j] == target: | ||
return [i, j] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Algorithm/technique: sorting | ||
# Time complexity: O(n lb n) | ||
# Auxiliary space: O(n) | ||
# Optimal: no | ||
|
||
class Solution: | ||
def twoSum(self, nums: list[int], target: int) -> list[int]: | ||
l = 0 | ||
r = len(nums) - 1 | ||
snums = sorted(nums) | ||
|
||
while l < r: | ||
total = snums[l] + snums[r] | ||
if total == target: | ||
res = [nums.index(snums[l]), nums.index(snums[r])] | ||
if res[0] == res[1]: | ||
res[1] = nums.index(snums[r], res[0] + 1) | ||
return res | ||
elif total < target: | ||
l += 1 | ||
else: | ||
r -= 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Algorithm/technique: hash table | ||
# Time complexity: O(n) | ||
# Auxiliary space: O(n) | ||
# Optimal: yes | ||
|
||
class Solution: | ||
def twoSum(self, nums: list[int], target: int) -> list[int]: | ||
visited = {} | ||
|
||
for i, num in enumerate(nums): | ||
diff = target - num | ||
if diff in visited: | ||
return [visited[diff], i] | ||
visited[num] = 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
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Algorithm/technique: sorting | ||
# Time complexity: O(n lb n) | ||
# Auxiliary space: O(1) | ||
# Optimal: no | ||
|
||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
if len(s) != len(t): | ||
return False | ||
|
||
return s.sort() == t.sort() |
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
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