Skip to content

Commit

Permalink
1: Solve in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
donjuardo committed Jul 10, 2024
1 parent dd13bf8 commit c26615c
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 51 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ favors time.
## 🧩 Solved

- [x] 1. Two Sum
- [x] 217. Contains Duplicate
- [x] 242. Valid Anagram

Expand Down
15 changes: 15 additions & 0 deletions python3/1/a.py
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]
22 changes: 22 additions & 0 deletions python3/1/b.py
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
14 changes: 14 additions & 0 deletions python3/1/c.py
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
13 changes: 5 additions & 8 deletions python3/217/217-contains-duplicate-a.py → python3/217/a.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# Programming language: Python 3
# Algorithm/technique: nested iteration
# Time complexity: O(n^2)
# Auxiliary space: O(1)
# Optimal: no
# Notes:
# Algorithm/technique: nested iteration
# Time complexity: O(n^2)
# Auxiliary space: O(1)
# Optimal: no

class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
n = len(nums)

if n < 2:
return False

# enumerate does not allow the last item to be skipped without slicing,
# which would require O(n) auxiliary space.
# which would require linear auxiliary space.
for i in range(n - 1):
for j in range(i + 1, n):
if nums[i] == nums[j]:
Expand Down
10 changes: 4 additions & 6 deletions python3/217/217-contains-duplicate-b.py → python3/217/b.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Programming language: Python 3
# Algorithm/technique: sorting
# Time complexity: O(n lb n)
# Auxiliary space: O(1)
# Optimal: no
# Notes:
# Algorithm/technique: sorting
# Time complexity: O(n lb n)
# Auxiliary space: O(1)
# Optimal: no

class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
Expand Down
10 changes: 4 additions & 6 deletions python3/217/217-contains-duplicate-c.py → python3/217/c.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Programming language: Python 3
# Algorithm/technique: set
# Time complexity: O(n)
# Auxiliary space: O(n)
# Optimal: yes
# Notes:
# Algorithm/technique: hash table
# Time complexity: O(n)
# Auxiliary space: O(n)
# Optimal: yes

class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
Expand Down
10 changes: 4 additions & 6 deletions python3/217/217-contains-duplicate-d.py → python3/217/d.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Programming language: Python 3
# Algorithm/technique: set
# Time complexity: O(n)
# Auxiliary space: O(n)
# Optimal: yes
# Notes:
# Algorithm/technique: hash table
# Time complexity: O(n)
# Auxiliary space: O(n)
# Optimal: yes

class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
Expand Down
13 changes: 0 additions & 13 deletions python3/242/242-valid-anagram-a.py

This file was deleted.

11 changes: 11 additions & 0 deletions python3/242/a.py
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()
10 changes: 4 additions & 6 deletions python3/242/242-valid-anagram-b.py → python3/242/b.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Programming language: Python 3
# Algorithm/technique: hash table
# Time complexity: O(n)
# Auxiliary space: O(n)
# Optimal: yes
# Notes:
# Algorithm/technique: hash table
# Time complexity: O(n)
# Auxiliary space: O(n)
# Optimal: yes

class Solution:
def isAnagram(self, s: str, t: str) -> bool:
Expand Down
10 changes: 4 additions & 6 deletions python3/242/242-valid-anagram-c.py → python3/242/c.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Programming language: Python 3
# Algorithm/technique: hash table
# Time complexity: O(n)
# Auxiliary space: O(n)
# Optimal: yes
# Notes:
# Algorithm/technique: hash table
# Time complexity: O(n)
# Auxiliary space: O(n)
# Optimal: yes

from collections import Counter

Expand Down

0 comments on commit c26615c

Please sign in to comment.