-
-
Notifications
You must be signed in to change notification settings - Fork 244
[Joy] Week1 Solutions #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
''' | ||
Brainstrom | ||
- brute force: double for-loop (O^2) | ||
- efficient approach: use set O(N) | ||
|
||
Plan | ||
1. Initialize the set | ||
2. Iterate over the array | ||
2-1. For each element, check if it's in the set. If it is, return True | ||
2-2. If not, add the num to the set | ||
3. Return False since no duplicates are found | ||
''' | ||
|
||
def containsDuplicate(nums): | ||
num_set=set() | ||
for num in nums: | ||
if num in num_set: | ||
return True | ||
num_set.add(num) | ||
return False | ||
|
||
#TC: O(N), SC: O(N) | ||
|
||
#Normal case | ||
print(containsDuplicate([1, 2, 3, 1]) == True) | ||
print(containsDuplicate([1, 2, 3, 4]) == False) | ||
|
||
#Edge case | ||
print(containsDuplicate([1]) == False) | ||
print(containsDuplicate([]) == False) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
''' | ||
Brainstorm | ||
- Brute force: double for-loop / recursion - O(N^2) | ||
- Two Pointer: O(nlogn) - sorting | ||
- Efficient approach: use hashmap to go through the array once O(N) | ||
|
||
Plan | ||
1. Initialize the dictionary | ||
2. Iterate over the array | ||
2-1. For each number, calculate the complement | ||
2-2. check if the complement exists in the dictionary | ||
2-3. Return the indices if it exists | ||
2-4. If not, add the current number and its index to the dictionary | ||
''' | ||
|
||
def twoSum(nums, target): | ||
memo = {} | ||
for i in range(len(nums)): | ||
diff = target - nums[i] | ||
if diff in memo: | ||
return [memo[diff], i] | ||
memo[nums[i]] = i | ||
|
||
#TC:O(N), SC:O(N) | ||
|
||
#Normal case | ||
print(twoSum([2, 7, 11, 15], 9) == [0, 1]) | ||
print(twoSum([3, 2, 4], 6) == [1, 2]) | ||
|
||
#Edge case | ||
print(twoSum([1, 2], 3) == [0, 1]) | ||
print(twoSum([1, 5, 10], 20) == None) |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,43 @@ | ||||||||||||
''' | ||||||||||||
Brainstorm | ||||||||||||
- brute force: double for-loop O(N^2) | ||||||||||||
- efficient: use hash map O(N) | ||||||||||||
|
||||||||||||
plan | ||||||||||||
1. check the string lengths. Return false if different | ||||||||||||
2. Initialize the dictionary | ||||||||||||
3. Check the dictionary | ||||||||||||
3-1. If all counts are zero, strings are anagrams. Return True | ||||||||||||
3-2. If not, return False | ||||||||||||
''' | ||||||||||||
def isAnagram(s, t): | ||||||||||||
if len(s) != len(t): | ||||||||||||
return False | ||||||||||||
|
||||||||||||
count = {} | ||||||||||||
for char in s: | ||||||||||||
if char in count: | ||||||||||||
count[char] += 1 | ||||||||||||
else: | ||||||||||||
count[char] = 1 | ||||||||||||
|
if char in count: | |
count[char] += 1 | |
else: | |
count[char] = 1 | |
count[char] = count.get(char, 0) + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ชจ๋ ๊ฒฐ๊ณผ๊ฐ ์ฐธ์ด์ด์ผ true์ธ ๋ด์ฅ ํจ์์ธ๊ฐ์? ์ ๊ธฐํ๋ค์ ใ ใ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด ๋ธ๋ก๊ทธ ๊ธ์ ์ฝ์ด๋ณด์๋ฉด ํธ๊ธฐ์ฌ์ด ์ข ํ๋ฆฌ์ค ๊ฒ ๊ฐ์์: https://www.daleseo.com/python-all/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
num_set ์ฒ๋ผ ์๋ฃํ์ ๋ณ์ ๋ช ์ผ๋ก ์ฐ๋ ๊ฒ ๋ณด๋ค๋ ์๋ฏธ์๋ ๋ค๋ฅธ ์ด๋ฆ์ ์ด๋จ๊น์?
์ ๋
appeared
๋ผ๊ณ ์ผ์ด์ ใ ใ There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ๋ฏธ์๋ ํผ๋๋ฐฑ์ด๋ค์. ๋ณ์ ์ด๋ฆ ์ง๊ธฐ๊ฐ ํญ์ ์ด๋ ต์ฃ . ์ ๋ ๊ฐ์ธ์ ์ผ๋ก ๋น์ง๋์ค ๋ก์ง์ ์งค ๋๋ ๋ณ์ ์ด๋ฆ์ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋ํ๋ด๋ ๋จ์ด๋ฅผ ๋ฃ๋ ๊ฒ์ ํผํ๋ ํธ์ธ๋ฐ์, ์ฝ๋ฉ ํ ์คํธ์์๋ ์คํ๋ ค ๋ณ์์ ๊ทธ๋ฐ ๋จ์ด๋ฅผ ๋ฃ๋ ๊ฒ์ด ๋ฉด์ ๊ด๊ณผ ์์ฌ ์ํตํ๊ธฐ๊ฐ ํธํ ๋๊ฐ ์๋๋ผ๊ณ ์.