-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Jay-Mo-99] Week 5 #861
Open
Jay-Mo-99
wants to merge
4
commits into
DaleStudy:main
Choose a base branch
from
Jay-Mo-99:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Jay-Mo-99] Week 5 #861
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,27 @@ | ||
#해석 | ||
#prices list를 순회하면서 최솟값 min_val을 업데이트 한다 | ||
#현재 prices[n]과 min_val의 차를 업데이트 해준다. | ||
|
||
|
||
#Big O | ||
#N: prices 의 크기 | ||
|
||
#Time Complexity: O(N) | ||
#- for loop : prices의 원소 갯수만큼 순회하므로 O(N) | ||
|
||
|
||
#Space Complexity: O(1) | ||
#- min_val, answer : 변수는 상수이므로 O(1) | ||
class Solution(object): | ||
def maxProfit(self, prices): | ||
|
||
#Initialize variables | ||
min_val = prices[0] | ||
answer = 0 | ||
|
||
for n in range(len(prices)): | ||
min_val= min(min_val,prices[n]) #Update min value of the prices list | ||
answer = max(prices[n]-min_val,answer) #Update max value of prices[n] - min value | ||
|
||
return answer | ||
|
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,44 @@ | ||
#해석 | ||
#encode함수: 매개변수 strs 리스트를 join 메소드와 특정 매개변수를 사용해 하나의 string인 answer로 전환 | ||
#decode함수: 매개변수 string s를 split 메소드를 사용해 특정 매개변수를 기점으로 나누어 list로 전환하여 return한다. | ||
# 만약 strs가 비어있을때는 특정 string을 주입하여 decode 에서 해당 string을 인식하여 빈 배열([])를 return한다. | ||
|
||
|
||
#Big O | ||
#N: 리스트 strs의 길이 (element 갯수) | ||
#L: strs의 각 element 평균 길이 (문자열의 길이) | ||
#M: string s 의 길이 | ||
|
||
#Time Complexity: | ||
#-encode: O(N*L) | ||
#-- join(strs): 리스트에 있는 N개의 element와 각 문자열의 길이 L을 합산하여 문자열 생성 -> O(N * L) | ||
#-decode: O(M): | ||
#- split('구분자'): split 메서드는 구분자를 찾는 과정에서 string s를 순회하므로 -> O(M) | ||
|
||
|
||
|
||
#Space Complexity: | ||
#-encode: O(N*L) | ||
#-- answer: join 메서드로 생성되는 문자열은 strs 리스트의 모든 문자열을 합친 값이므로 -> O(N * L) | ||
#-decode: O(M) | ||
#-- answer:split 메서드로 생성되는 리스트는 string s의 길이에 비례하여 메모리를 차지 -> O(M) | ||
|
||
|
||
|
||
class Solution: | ||
|
||
|
||
def encode(self, strs: List[str]) -> str: | ||
answer = '!@#$%123456789'.join(strs) | ||
if len(strs) == 0: | ||
answer = "I am empty" | ||
return answer | ||
|
||
def decode(self, s: str) -> List[str]: | ||
answer = s.split('!@#$%123456789') | ||
if s == "I am empty": | ||
answer = [] | ||
return answer | ||
|
||
|
||
|
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,37 @@ | ||
#해석 | ||
#tempDict = defaultdict(list) 로 자동으로 빈 리스트를 생성하는 딕셔너리 | ||
#for loop 로 strs의 각 element를 순회 | ||
#key=tuple(sorted(s)) 각 element인 s를 정렬하여 tuple로 변환하여 key로 저장한다 -> key는 변경 불가능 해야하므로 리스트 대신 tuple이 적합 | ||
#tempDict[key].append(s) 로 key를 기준으로 element를 value값으로 tempDict에 저장한다. | ||
#tempDict의 value값만 return하여 같은 key를 가지는 value가 list로 묶인 이중 list를 return한다. | ||
|
||
#Big O | ||
#- N: strs 리스트의 element 갯수 | ||
#- K: 각 element의 길이 | ||
|
||
#Time Complexity: O(N∗K∗Log(K)) = O(N) * O(K*Log(K)) | ||
#- sorted(s) : sort,sorted알고리즘은 Timsort 알고리즘이므로 정렬 대상 길이(K)에 영향받음 -> O(K∗Log(K)) | ||
#- for loop: strs의 element갯수만큼 순회 -> O(N) | ||
|
||
|
||
|
||
#Space Complexity: O(N∗K) = O(N) * O(N) | ||
#- tempDict key : 각 키는 최대 K 크기의 tuple로 저장 -> O(K) | ||
#- tempDict value: strs에 각 고유한 element만 있다면 tempDict의 value의 최댓값은 N개 -> O(N) | ||
|
||
|
||
class Solution(object): | ||
def groupAnagrams(self, strs): | ||
""" | ||
:type strs: List[str] | ||
:rtype: List[List[str]] | ||
""" | ||
tempDict = defaultdict(list) | ||
|
||
for s in strs: | ||
key = tuple(sorted(s)) | ||
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. 정렬을 하지 않고 다르게 풀면 시간복잡도를 줄일 수 있을 것 같은데 어떤 방법이 있을까요? |
||
tempDict[key].append(s) | ||
|
||
return list(tempDict.values()) | ||
|
||
|
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.
만약 strs배열의 구성요소가 ['!@#$%123456789', '!@#$%', ''!@#$%123456789123123'] 이라면 encode가 조금은 꼬이지 않을까 합니다
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.
네, 구분자(!@#$%123456789) 가 strs의 요소로 나온다면 해당 풀이법은 유효하지 않습니다. 가장 안전한 방법은 strs의 각 요소의 길이를 측정하여 decode때 해당 길이를 기반으로 나눠주는 방법이라고 들었습니다.