Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions contains-duplicate/JoyJaewon.py
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

num_set ์ฒ˜๋Ÿผ ์ž๋ฃŒํ˜•์„ ๋ณ€์ˆ˜ ๋ช…์œผ๋กœ ์“ฐ๋Š” ๊ฒƒ ๋ณด๋‹ค๋Š” ์˜๋ฏธ์žˆ๋Š” ๋‹ค๋ฅธ ์ด๋ฆ„์€ ์–ด๋–จ๊นŒ์š”?

์ €๋Š” appeared๋ผ๊ณ  ์ผ์–ด์š” ใ…Žใ…Ž

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์žฌ๋ฏธ์žˆ๋Š” ํ”ผ๋“œ๋ฐฑ์ด๋„ค์š”. ๋ณ€์ˆ˜ ์ด๋ฆ„ ์ง“๊ธฐ๊ฐ€ ํ•ญ์ƒ ์–ด๋ ต์ฃ . ์ €๋„ ๊ฐœ์ธ์ ์œผ๋กœ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง์„ ์งค ๋•Œ๋Š” ๋ณ€์ˆ˜ ์ด๋ฆ„์— ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹จ์–ด๋ฅผ ๋„ฃ๋Š” ๊ฒƒ์„ ํ”ผํ•˜๋Š” ํŽธ์ธ๋ฐ์š”, ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์—์„œ๋Š” ์˜คํžˆ๋ ค ๋ณ€์ˆ˜์— ๊ทธ๋Ÿฐ ๋‹จ์–ด๋ฅผ ๋„ฃ๋Š” ๊ฒƒ์ด ๋ฉด์ ‘๊ด€๊ณผ ์˜์‚ฌ ์†Œํ†ตํ•˜๊ธฐ๊ฐ€ ํŽธํ•  ๋•Œ๊ฐ€ ์žˆ๋”๋ผ๊ณ ์š”.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ‘

32 changes: 32 additions & 0 deletions two-sum/JoyJaewon.py
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)
43 changes: 43 additions & 0 deletions valid-anagram/JoyJaewon.py
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฐธ๊ณ : ์š”๋ ‡๊ฒŒ ํ•œ ์ค„๋กœ ์งœ์‹ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

Suggested change
if char in count:
count[char] += 1
else:
count[char] = 1
count[char] = count.get(char, 0) + 1


for char in t:
if char in count:
count[char] -= 1
if count[char] < 0:
return False
else:
return False

return all(x == 0 for x in count.values())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ชจ๋“  ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ด์–ด์•ผ true์ธ ๋‚ด์žฅ ํ•จ์ˆ˜์ธ๊ฐ€์š”? ์‹ ๊ธฐํ•˜๋„ค์š” ใ…Žใ…Ž

Copy link
Member

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/


#TC: O(N), SC: O(N)

#Normal case
print(isAnagram("anagram", "nagaram") == True)
print(isAnagram("rat", "car") == False)

#Edge case
print(isAnagram("", "") == True)