코딩테스트(Kotlin)/LeetCode Top Interview 150 25

[1주차] 189. Rotate Array

https://leetcode.com/problems/rotate-array/description/?envType=study-plan-v2&envId=top-interview-150 문제 주어진 nums 배열을 오른쪽으로 k 만큼 회전시켜라. 단 k >= 0 이다. 풀이 보자마자 LinkedList를 이용한 Queue 자료구조가 생각났고 해결했다. 좀더 직관적으로 알아볼 수 있게 Deque를 사용했다. fun rotate(nums: IntArray, k: Int): Unit { val queue: Deque = LinkedList() nums.forEach { queue.addLast(it) } repeat(k % nums.size) { queue.addFirst(queue.removeLast()) } ..

[1주차] 169. Majority Element

https://leetcode.com/problems/majority-element/?envType=study-plan-v2&envId=top-interview-150 문제 주어진 정수배열 nums 에서 nums.size / 2 보다 많이 나오는 원소를 리턴한다. 단, 조건에 맞는 원소는 반드시 존재한다. 해결 문제를 보자마자 생각난 방식 fun majorityElement(nums: IntArray): Int { val map = mutableMapOf() nums.forEach { val prev = map.getOrDefault(it, 0) map[it] = prev + 1 } map.forEach { if (it.value > nums.size / 2) { return it.key } } retu..

[1주차] 80. Remove Duplicates from Sorted Array II

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/?envType=study-plan-v2&envId=top-interview-150 Remove Duplicates from Sorted Array II - LeetCode Can you solve this real interview question? Remove Duplicates from Sorted Array II - Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place [https://en.wikipedia.org/wiki/In-place_algorithm] such..

[1주차] 27. Remove Element

https://leetcode.com/problems/remove-element/?envType=study-plan-v2&envId=top-interview-150 Remove Element - LeetCode Can you solve this real interview question? Remove Element - Given an integer array nums and an integer val, remove all occurrences of val in nums in-place [https://en.wikipedia.org/wiki/In-place_algorithm]. The order of the elements may be changed. Then r leetcode.com 📝 문제 오름차순으..

[1주차] 88. Merge Sorted Array

https://leetcode.com/problems/merge-sorted-array/ 처음에는 단순히 sort 라이브러리를 이용하여 O(nlogn) 시간에 풀었다. for (i in nums2.indices) { nums1[m + i] = nums2[i] } nums1.sort() m, n = 0 && p2 >= 0) { if (nums1[p1] > nums2[p2]) { nums1[p3--] = nums1[p1--] } else { nums1[p3--] = nums2[p2--] } } if (p1 >= 0) { while (p1 >= 0) nums1[p3--] = nums1[p1--] } if (p2 >= 0) { while (p2 >= 0) nums1[p3--] = nums2[p2--] } p1이..