알고리즘

· 알고리즘
안녕하세요. 린다입니다.오늘은 간단하게 알고리즘의 기초인 정렬 알고리즘 중버블, 선택, 삽입, 퀵 정렬에 대해서 정리해볼게요! 버블 정렬데이터의 인접요소끼리 비교하여 swap 연산을 수행하여 정렬하는 기법루프를 돌면서 연산 수행시간복잡도 O(n^2)let array = [99, 98, 97, 96]func bubbleSort(_ array: [Int]) -> [Int] { var bubbleArray = array let n = bubbleArray.count for i in 0.. bubbleArray[j+1] { bubbleArray.swapAt(j, j+1)// let temp = bubbleArray[j]// ..
· 알고리즘
백준 큐, 스택 알고리즘 풀이 (24511) https://www.acmicpc.net/problem/24511  문제를 손으로 직접 써가면서 풀다보면Stack은 변화가 없고 Queue에서만 변화가 있다는 걸 알 수 있음그래서 Queue만 처리해주면 됨! "Queue는 선입선출 + 들어오는 값의 크기인 n만큼 Output도 생성됨"1-4까지 뒤부터 차례대로 Queue에 먼저 들어가 있는 원소들이 빠지기 때문에 그 값들을 처리하고 생각하면 됩니다. 그래서 ..첫번째 원소가 4인 큐에 있는 원소부터 빠지기 때문에answer = [Int]() 배열이 있을 때Queue안에 있는 원소들을 모두 추가하고answer의 크기가 input 크기에 비해 부족하다면 그 값 만큼 input 에서 removefirst()를 해..
· 알고리즘
https://leetcode.com/problems/maximum-depth-of-binary-tree/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 트리를 구현이 가능한지 불가능한지를 판단할 수 있는 기본 문제 제시해준 TreeNode를 사용하여 재귀함수로 풀이하였습니다. 우선 깊이는 1부터 시작하구요. (문제 예시를 보면 알 수 있음) 루트 노드를 기..
· 알고리즘
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 간단한 트리 문제, 가장 낮은 공통 조상 노드를 찾는 문제 조상 노드가 될 수 있는 경우의 수는 4가지 노드에 어떤 값도 들어오지 않았을 때 → 전달해줄 값이 없음 노드가 주어진 p 또는..
· 알고리즘
https://www.acmicpc.net/problem/1181 1181번: 단어 정렬 첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다. www.acmicpc.net 정말 간단한 문제인데, Dictionary, Sort 개념 잡기 좋은 문제 딕셔너리를 활용하여 입력된 단어들의 등장 횟수를 세는 예제입니다. import Foundation let n = Int(readLine()!)! var dict = [String : Int]() for _ in 0.. [Swift] Dictionary 의 subscript 3개 / KeyValuePairs 문서를..
· 알고리즘
https://leetcode.com/problems/intersection-of-two-linked-lists/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 요약: 두개의 linked list를 순회하면서 교차점을 찾아서 반환하는 문제 Set으로 하고 싶은데 안 되고 while headA != nil, headB != nil로 하기엔 길이가 다르니까 co..
서연(linda)
'알고리즘' 카테고리의 글 목록 (2 Page)