→ Problems

· → Problems
📠 문제Maximum Product Subarray난이도 Medium부분배열중 가장 곱이 높은 배열을 찾는 문제Input: nums = [-2,0,-1]Output: 0Explanation: The result cannot be 2, because [-2,-1] is not a subarray.💡 풀이이 문제의 고민은 만약에 부분합이라면 투포인터를 사용할 수 있지만 부분 곱셈에서는 조금 까다로운 문제가 있었다.배열의 요소 중 하나라도 0이 나왔을 땐 무조건 곱이 0이 되므로 포인터를 움직이며 비교할 수 없다는 것.음수는 같은 음수를 만나면 오히려 더 높은 숫자가 나올 수 있다는 것.결국 아래와 같이 maxBuffer, minBuffer를 두어 해결하고자 했다. 또한 0일 경우를 고려해서 아무것도 곱하..
· → Problems
📠 문제Remove Nth Node From End of List난이도: Medium연결리스트에서 끝에서 n번째의 요소를 제거하는 문제Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5]💡 풀이요즘 연결리스트 문제를 자주 푸는 것 같다. 이 문제는 먼저 n번 노드를 뒤로 보낸 후에 같이 이동하면서 next가 nil이면 start.next = start.next.next 로 연결을 끊어주면 된다.func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { let zero: ListNode? = ListNode(0) zero?.next = head var start = zero var end..
· → Problems
📠 문제Reorder List난이도: Medium주어진 리스트를 재배열하는 문제이다.You are given the head of a singly linked-list. The list can be represented as:L0 → L1 → … → Ln - 1 → LnReorder the list to be on the following form:L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …You may not modify the values in the list's nodes. Only nodes themselves may be changed.💡 풀이아래와 같은 조건이 있었기 때문에 기존의 리스트를 가지고 재배열을 해야했다.You may not modify the valu..
· → Problems
📠 문제3Sum난이도: Medium배열중 3개를 뽑아 더하면 0이 되는 배열을 겹치지 않게 뽑는 문제Input: nums = [-1,0,1,2,-1,-4]Output: [[-1,-1,2],[-1,0,1]]Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.The distinct triplets are [-1,0,1] and [-1,-1,2].Notice that the order of the output and the order of the triplets does not ma..
· → Problems
📠 문제Missing Number난이도: Easy주어진 0부터 n까지 있는 배열에서 비어있는 숫자를 찾는 문제Input: nums = [3,0,1]Output: 2Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.💡 풀이이 문제는 아주 단순하게 풀린다. 모든 수를 더하고 배열을 순회하면서 마이너스하면 결국 없는 숫자가 남게 된다. 한줄로 해결도 가능하다! 🎉func missingNumber(_ nums: [Int]) -> Int { nums.reduce((nums.cou..
· → Problems
📠 문제Linked List Cycle난이도: Easy연결리스트 사이클이 있는지 찾는 문제Input: head = [3,2,0,-4], pos = 1Output: trueExplanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).💡 풀이솔직히 이 문제가 왜 Easy인지 모르겠다. 아래처럼 ListNode 배열이나 ObjectIdentifier 집합을 사용하여 풀면 풀리긴 하지만... 이런 문제를 풀 때 아주아주 유명한 알고리즘이 있다.func hasCycle(_ head: ListNode?) -> Bool { var visited: [ListNode] = [] var ..
· → Problems
📠 문제Word Break난이도: Medium단어 사전에 있는 단어 조합으로 주어진 문자열을 모두 대체할 수 있는지 찾는 문제Input: s = "leetcode", wordDict = ["leet","code"]Output: trueExplanation: Return true because "leetcode" can be segmented as "leet code".🚧 시행착오아래와 같이 풀면 되지 않을까? 생각했다. 앞에서부터 순회하지 않는 이유는 word, words 같은 두 개의 문자가 있을 때 words를 대체해야 하기 때문에 앞에서부터 하면 word를 하고 s가 남지 않을까? 해서였다.func wordBreak(_ s: String, _ wordDict: [String]) -> Bool {..
· → Problems
📠 문제Container With Most Water난이도: Medium주어진 배열에서 양 끝점으로 한 가장 큰 직사각형의 넓이를 구하는 문제Input: height = [1,8,6,2,5,4,8,3,7]Output: 49Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.💡 풀이첫 번째로 가장 무식하게 모든 경우의 수를 체크하는 방법이 있다. 물론 n의 입력값이 10^5이기 때문에 시간초과!func maxArea(_ height: [Int]) -> Int ..
Swift librarian
'→ Problems' 카테고리의 글 목록