본문 바로가기

분류 전체보기

(139)
[Queue] Reveal Cards in Increasing Order - LeetCode Swift 문제 : https://leetcode.com/problems/reveal-cards-in-increasing-order/submissions/ Reveal Cards In Increasing Order - LeetCode 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 코드 class Solution { func deckRevealedIncreasing(_ deck: [Int]) -> [Int] { var q = deck.sorted(by:
[투포인터] 빗물 - backjoon Swift 문제 : https://www.acmicpc.net/problem/14719 14719번: 빗물 첫 번째 줄에는 2차원 세계의 세로 길이 H과 2차원 세계의 가로 길이 W가 주어진다. (1 ≤ H, W ≤ 500) 두 번째 줄에는 블록이 쌓인 높이를 의미하는 0이상 H이하의 정수가 2차원 세계의 맨 왼쪽 위치 www.acmicpc.net 풀이방법 벽을 2차원 배열로 만들고 row 한줄씩 본다. 1과 1사이를 두개의 포인터로 지정후 가운데를 2로 채운다. 2의 갯수를 센다. 코드 import Foundation func checkRainWater() -> Int { var mapInfo = readLine()!.components(separatedBy: " ").map{Int($0)!} let wall =..
[배열] QueensThatCanAttackTheKing - LeetCode Swift 문제 : https://leetcode.com/problems/queens-that-can-attack-the-king/submissions/ Queens That Can Attack the King - LeetCode 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 풀이방법 : 1상하좌우 king으로부터 가까운순으로 돈다. 대각선 상하좌우 king으로부터 가까운순으로 돈다. Queen(1)을 찾게 되면 그 라인은 더이상 안돈다. 음.. 공통된 코드가 많이 보여서 좀 아쉬운 코드..
[해쉬] Group -anagrams - LeetCode Swift 문제 : https://leetcode.com/problems/group-anagrams/ Group Anagrams - LeetCode 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 풀이 방법 : 정렬해서 같은 것이 해쉬테이블 키값에 있는지 없는지 검사 코드 class Solution { func groupAnagrams(_ strs: [String]) -> [[String]] { var myStrs = strs var result = [String:[String]]() va..
[LinkedList] remove-nth-node-from-end-of-list - LeetCode Swift 문제 : https://leetcode.com/problems/remove-nth-node-from-end-of-list/submissions/ Remove Nth Node From End of List - LeetCode 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 링크드리스트에 대한 개념이 없을 때 풀이방법 링크드리스트를 배열로 변환 배열에서 뒤에서 n번째 요소 삭제 배열을 다시 링크드리스트로 변환 결과 : 속도는 기본적인 링크드리스트와 비슷했지만 메모리에서 엄청 낮은 점..
[완전탐색] 카펫 - 프로그래머스 Swift 문제 : https://programmers.co.kr/learn/courses/30/lessons/42842 코딩테스트 연습 - 카펫 Leo는 카펫을 사러 갔다가 아래 그림과 같이 중앙에는 노란색으로 칠해져 있고 테두리 1줄은 갈색으로 칠해져 있는 격자 모양 카펫을 봤습니다. Leo는 집으로 돌아와서 아까 본 카펫의 노란색과 programmers.co.kr 풀이방법 brown + yellow의 수의 모든 약수를 완전탐색으로 알아냄 count가 홀수 일때는 중간 약수 값을 리턴함 [중간약수, 중간약수] count가 짝수 일때는 노란색 수가 맞는지 일단 확인한다. 노란색 수를 구하는 식은 테두리가 모두 갈색이므로 노란색 수 = (카펫 가로길이 - 2) * (카펫 세로길이 - 2 ) 확인 후 같으면 해당하는..
[DFS] Sum Root To Leaf Numbers - LeetCode Swift 문제 : https://leetcode.com/problems/sum-root-to-leaf-numbers/submissions/ Sum Root to Leaf Numbers - LeetCode 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 시도했던 방법 처음에 레벨순회(BFS)로 TreeNode를 배열로 바꾼후 부모노드인덱스 = 자식노드인덱스/2 를 이용하려고 함 부모노드인덱스 = 자식노드인덱스/2의 특징은 완전이진트리에서만 쓸 수 있었다. 이 문제는 그냥 이진트리이다. 이것..
[정렬] 병합정렬 Swift 병합정렬 핵심 나누기 : 배열을 원소가 1개 남을때 까지 반으로 쪼갠다. 정렬 & 합치기 : 왼쪽과 오른쪽으로 쪼개진 배열을 비교해서 하나의 배열로 합친다. Swift 코드 // 참고 : 아이작 코드로 공부했다. v 재귀함수 동작에 대해 공부할 필요성을 느꼇다. import Foundation //이때 l과 r은 정렬이 된 상태여야 한다. func merged(l: [Int], r: [Int]) -> [Int] { var currentLeftIndex = 0 var currentRightIndex = 0 var sorted = [Int]() //왼쪽 오른쪽 배열을 비교해서 큰 숫자 차례로 담음 while currentLeftIndex < l.count && currentRightIndex < r.coun..