-
-
Notifications
You must be signed in to change notification settings - Fork 195
[croucs] WEEK 1 #654
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
Merged
Merged
[croucs] WEEK 1 #654
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
29a569b
ADD : contains-duplicate/heypaprika.py
heypaprika fbe1ac8
ADD : #220 #237 #240 #264 by heypaprika
heypaprika 9eda183
EDIT : Big-O
heypaprika a21bc67
Merge branch 'DaleStudy:main' into main
heypaprika 499d34a
Update valid-palindrome/heypaprika.py
heypaprika fb3980d
EDIT : contains-duplicate Big-O prediction
heypaprika 188db98
Merge branch 'main' of https://github.com/heypaprika/leetcode-study
heypaprika File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Big-O 예상 : O(n) | ||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
num_dict = {} | ||
for num in nums: | ||
if num in num_dict: | ||
return True | ||
else: | ||
num_dict[num] = 1 | ||
return False | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Big-O 예상 : O(n) | ||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
a = [0] * len(nums) | ||
|
||
if len(nums) == 1: | ||
return nums[0] | ||
elif len(nums) == 2: | ||
return max(nums[0], nums[1]) | ||
|
||
a[0] = nums[0] | ||
a[1] = nums[1] | ||
a[2] = max(a[0] + nums[2], a[1]) | ||
|
||
for i in range(3, len(nums)): | ||
a[i] = max(a[i-3], a[i-2]) + nums[i] | ||
|
||
return max(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Big-O 예상 : O(nlog(n)) | ||
class Solution: | ||
def longestConsecutive(self, nums: List[int]) -> int: | ||
nums = sorted(list(set(nums))) | ||
if len(nums) == 0: | ||
return 0 | ||
elif len(nums) == 1: | ||
return 1 | ||
Comment on lines
+5
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (사소) nums 길이가 1이하면 sorting하기 전에도 결과를 반환할 수 있어서 가지치기 느낌으로 longestConsecutive 내 최상단에 두면 더 좋아보입니다. |
||
cur_long = 1 | ||
longest = 1 | ||
for i, num in enumerate(nums): | ||
if i == 0: | ||
continue | ||
else: | ||
if nums[i-1] + 1 == nums[i]: | ||
cur_long += 1 | ||
if longest < cur_long: | ||
longest = cur_long | ||
else: | ||
cur_long = 1 | ||
return longest | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Big-O 예상 : O(nlog(n)) | ||
import heapq | ||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
|
||
num_dict = {} | ||
for num in nums: | ||
num_dict[num] = num_dict.get(num, 0) + 1 | ||
heap = [] | ||
for k_, v in num_dict.items(): | ||
heapq.heappush(heap, [-v, k_]) | ||
ans = [] | ||
for i in range(k): | ||
ans.append(heapq.heappop(heap)[1]) | ||
return ans | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Big-O 예상 : O(n) | ||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
s = "".join(s.lower().split(" ")) | ||
new_s = "" | ||
for item in s: | ||
if (ord("a") <= ord(item) <= ord("z")) or (ord("0") <= ord(item) <= ord("9")): | ||
new_s += item | ||
output = True | ||
new_s_2 = new_s[::-1] | ||
return new_s_2 == new_s | ||
return output | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
입력받은 List는 정렬되지 않은 상태이기 때문에 중복된 요소간의 List내 위치가 많이 멀 경우 시간복잡도가 안좋게 잡힐 가능성이 커서 정렬한 상태에서 비교하는건 어떨까요?