File tree Expand file tree Collapse file tree 3 files changed +108
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings Expand file tree Collapse file tree 3 files changed +108
-0
lines changed Original file line number Diff line number Diff line change 1+ #해석
2+ #prices list를 순회하면서 최솟값 min_val을 업데이트 한다
3+ #현재 prices[n]과 min_val의 차를 업데이트 해준다.
4+
5+
6+ #Big O
7+ #N: prices 의 크기
8+
9+ #Time Complexity: O(N)
10+ #- for loop : prices의 원소 갯수만큼 순회하므로 O(N)
11+
12+
13+ #Space Complexity: O(1)
14+ #- min_val, answer : 변수는 상수이므로 O(1)
15+ class Solution (object ):
16+ def maxProfit (self , prices ):
17+
18+ #Initialize variables
19+ min_val = prices [0 ]
20+ answer = 0
21+
22+ for n in range (len (prices )):
23+ min_val = min (min_val ,prices [n ]) #Update min value of the prices list
24+ answer = max (prices [n ]- min_val ,answer ) #Update max value of prices[n] - min value
25+
26+ return answer
27+
Original file line number Diff line number Diff line change 1+ #해석
2+ #encode함수: 매개변수 strs 리스트를 join 메소드와 특정 매개변수를 사용해 하나의 string인 answer로 전환
3+ #decode함수: 매개변수 string s를 split 메소드를 사용해 특정 매개변수를 기점으로 나누어 list로 전환하여 return한다.
4+ # 만약 strs가 비어있을때는 특정 string을 주입하여 decode 에서 해당 string을 인식하여 빈 배열([])를 return한다.
5+
6+
7+ #Big O
8+ #N: 리스트 strs의 길이 (element 갯수)
9+ #L: strs의 각 element 평균 길이 (문자열의 길이)
10+ #M: string s 의 길이
11+
12+ #Time Complexity:
13+ #-encode: O(N*L)
14+ #-- join(strs): 리스트에 있는 N개의 element와 각 문자열의 길이 L을 합산하여 문자열 생성 -> O(N * L)
15+ #-decode: O(M):
16+ #- split('구분자'): split 메서드는 구분자를 찾는 과정에서 string s를 순회하므로 -> O(M)
17+
18+
19+
20+ #Space Complexity:
21+ #-encode: O(N*L)
22+ #-- answer: join 메서드로 생성되는 문자열은 strs 리스트의 모든 문자열을 합친 값이므로 -> O(N * L)
23+ #-decode: O(M)
24+ #-- answer:split 메서드로 생성되는 리스트는 string s의 길이에 비례하여 메모리를 차지 -> O(M)
25+
26+
27+
28+ class Solution :
29+
30+
31+ def encode (self , strs : List [str ]) -> str :
32+ answer = '!@#$%123456789' .join (strs )
33+ if len (strs ) == 0 :
34+ answer = "I am empty"
35+ return answer
36+
37+ def decode (self , s : str ) -> List [str ]:
38+ answer = s .split ('!@#$%123456789' )
39+ if s == "I am empty" :
40+ answer = []
41+ return answer
42+
43+
44+
Original file line number Diff line number Diff line change 1+ #해석
2+ #tempDict = defaultdict(list) 로 자동으로 빈 리스트를 생성하는 딕셔너리
3+ #for loop 로 strs의 각 element를 순회
4+ #key=tuple(sorted(s)) 각 element인 s를 정렬하여 tuple로 변환하여 key로 저장한다 -> key는 변경 불가능 해야하므로 리스트 대신 tuple이 적합
5+ #tempDict[key].append(s) 로 key를 기준으로 element를 value값으로 tempDict에 저장한다.
6+ #tempDict의 value값만 return하여 같은 key를 가지는 value가 list로 묶인 이중 list를 return한다.
7+
8+ #Big O
9+ #- N: strs 리스트의 element 갯수
10+ #- K: 각 element의 길이
11+
12+ #Time Complexity: O(N∗K∗Log(K)) = O(N) * O(K*Log(K))
13+ #- sorted(s) : sort,sorted알고리즘은 Timsort 알고리즘이므로 정렬 대상 길이(K)에 영향받음 -> O(K∗Log(K))
14+ #- for loop: strs의 element갯수만큼 순회 -> O(N)
15+
16+
17+
18+ #Space Complexity: O(N∗K) = O(N) * O(N)
19+ #- tempDict key : 각 키는 최대 K 크기의 tuple로 저장 -> O(K)
20+ #- tempDict value: strs에 각 고유한 element만 있다면 tempDict의 value의 최댓값은 N개 -> O(N)
21+
22+
23+ class Solution (object ):
24+ def groupAnagrams (self , strs ):
25+ """
26+ :type strs: List[str]
27+ :rtype: List[List[str]]
28+ """
29+ tempDict = defaultdict (list )
30+
31+ for s in strs :
32+ key = tuple (sorted (s ))
33+ tempDict [key ].append (s )
34+
35+ return list (tempDict .values ())
36+
37+
You can’t perform that action at this time.
0 commit comments