[수업목표]
1. 개발자들에게 알고리즘 공부가 필요한 이유 이해
2. 알고리즘을 학습하기 위한 기본 코드 구현력 높이기
3. 시간 복잡도 및 공간 복잡도에 대한 이해
input = "Hello My name is Nathan"
def find_max_occurred_alphabet(string):
alphabet_array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
max_occurence = 0
max_alphabet = alphabet_array[0]
for alphabet in alphabet_array:
occurrence = 0
for char in string:
if char == alphabet:
occurrence += 1
if occurrence > max_occurence:
max_occurence = occurrence
max_alphabet = alphabet
return max_alphabet
26 * (1 + 2*N + 3) = 52N + 104
[N만큼의 시간 소요] def find_max_occurred_alphabet(string):
alphabet_occurrence_array = [0] * 26
for i in string:
if i.isalpha() is True:
alphabet_occurrence_array[ord(i)-ord('a')] += 1
max_occurrence = 0
max_alphabet_index = 0
for index in range(len(alphabet_occurrence_array)):
alphabet_occurrence = alphabet_occurrence_array[index]
if alphabet_occurrence > max_occurrence:
max_alphabet_index = index
max_occurrence = alphabet_occurrence
return chr(max_alphabet_index + ord('a'))
N * (1 + 1) + 2 + 26 * (1 + 3) = 2N + 106
[N만큼의 시간 소요] N의길이 | 답안1(52N + 104) | 답안2(2N+106) | N^2 |
---|---|---|---|
1 | 156 | 108 | 1 |
10 | 624 | 126 | 100 |
100 | 5304 | 306 | 100000 |
1000 | 52104 | 2106 | 1000000 |
(52N + 104)
든 (2N+106)
이든 N^2
에 비해서는 아무 것도 아니다.최악의 성능
이 나올 때 어느 정도의 연산량이 걸릴 것인지 나타냄최선의 성능
이 나올 때 어느 정도의 연산량이 걸릴 것인지 나타냄