algorithm & data structure

[모두의 알고리즘] 최댓값 찾기

참고 교재: 모두의 알고리즘 with 파이썬

❓ 문제

주어진 숫자 n개 중 가장 큰 숫자를 찾는 알고리즘

입력: 17, 92, 18, 33, 58, 7, 33, 42
출력: 92

 

❗️ 풀이

python

def find_max(numList):
    max = numList[0]
    for i in range(0, len(numList)):
        if max < numList[i]:
            max = numList[i]
    return max
 
print(find_max([1, 23, 29, 12, 39, 7, 15]))

Swift5

public func find_max(list: [Int]) -> Int {
    var max = list[0]
    for i in list {
        if max < i {
            max = i
        }
    }
    return max
}