본문 바로가기

ALGORITHM

(71)
[Swift] LeetCode: Remove Duplicates from Sorted Array (inout) https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Remove Duplicates from Sorted Array - 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 영어를 안읽고 .. 문제가 제일 중요하단건 알지만 .. in-place로 unique한 element가 한번만 배열에 나타나도록 하는 문제였다. 주어진 문제에서 inout 파라미터가 등장했는데 아래에서 알아보자 inout 원래 함수..
[Swift] 9081 단어 맞추기 (next permutation) https://www.acmicpc.net/problem/9081 9081번: 단어 맞추기 입력의 첫 줄에는 테스트 케이스의 개수 T (1 ≤ T ≤ 10)가 주어진다. 각 테스트 케이스는 하나의 단어가 한 줄로 주어진다. 단어는 알파벳 A~Z 대문자로만 이루어지며 항상 공백이 없는 연속된 알 www.acmicpc.net 처음에는 재귀를 사용하여 구현하였는데 시간초과가 나서 검색을 뒤지다가 next permutation 이라는 저 기억 건너편에 있던게 생각이 났다... 왜 여태까지 next permutation 이 재귀로 구현된 것이라고 생각했는지 모를 노릇이지만.. 이제라도 알았다는 사실이 다행이다. next permutation 은 재귀로 구하던 순열보다 훨씬 짧은 시간인 O(n) 시간안에 구할 수 ..
[Swift] LeetCode: Longest Common Prefix (문자열) https://leetcode.com/problems/longest-common-prefix/submissions/ Longest Common Prefix - 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 XCode 없이 풀다가 별 문법 틀린 것 없이 한번에 success 가 떠서 대박! 했던 문제 String index 에 조금 적응한 기분 갬덩 ㅠ...ㅠ 문제 문제 제목 그대로 모든 문자열의 가장 긴 Prefix 접두사를 구하는 문제 생각 주어진 String..
[Swift] LeetCode: Roman to Integer (문자열) https://leetcode.com/problems/roman-to-integer/ Roman to Integer - 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 처음 문제를 풀었을 때 dictionary로 돌면서 미리 넣어둔 symbol값을 숫자값 + " " 로 바꿔주고 split으로 단어를 나눠서 모두 더하도록 했다. XIV -> 10 4 -> 10 + 4 = 14 간과하고 있었던 사실은 dictionary에 순서가 없다는 것이었다. 내가 짠 코드는 IV..
[Swift] LeetCode: Palindrome Number https://leetcode.com/problems/palindrome-number/ Palindrome Number - 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 c++, java 를 풀면서도 팰린드롬 문제를 다양하게 풀어봤는데 대부분 문자열로 풀었었다. 근데 Swift는 문자열로 푸는게 더 어려울 것 같다는 생각을 했는데 마침 문제 아래에 Follow up: Could you solve it without converting the integer to ..
[Swift] 2234 성곽 (BFS) https://www.acmicpc.net/problem/2234 2234번: 성곽 첫째 줄에 두 정수 N, M이 주어진다. 다음 M개의 줄에는 N개의 정수로 벽에 대한 정보가 주어진다. 벽에 대한 정보는 한 정수로 주어지는데, 서쪽에 벽이 있을 때는 1을, 북쪽에 벽이 있을 때는 2를, www.acmicpc.net 생각 [ struct wall ] wall은 벽에대한 정보보다는 그 위치의 대한 정보라서 - 위치 r, c - 동서남북 벽의 유무 - 방의 번호 : 벽을 허물었을 때 현재 방의 넓이를 알 수 있도록 로 선언하였다. c++ 로 풀 때는 Bool { switch direction { case 0: return west case 1: return north case 2: return east ca..
[Swift] LeetCode: Two Sum https://leetcode.com/problems/two-sum/ Two Sum - 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 twoSum(_ nums: [Int], _ target: Int) -> [Int] { var result: [Int] = [] for firstIndex in 0.. [Int] { var map = [Int: Int]() for (i, n) in nums.en..
[Swift] 14889 스타트와 링크 (조합) https://www.acmicpc.net/problem/14889 14889번: 스타트와 링크 예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다. www.acmicpc.net 생각 스타트 팀과 링크 팀 두개의 팀으로 나누기 때문에 두개의 팀 모두 인원은 전체인원 / 2 명 -> 조합으로 경우의 수를 모두 구해서 팀원의 능력치가 가장 작은 방법을 구합니다. 능력치를 구하는 방법은 같은 팀일 때만 (여기서는 visited 배열을 사용) 능력치를 더해주었습니다. 조합문제를 Swift로 푸는 것은 처음인데 알고리즘을 푸는 방법은 동일해서 코드도 언어만 다르지 동일 백준문제에서도 요즘은 Swift로..