문제 링크 Shortest Unsorted Continuous Subarray - 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 문제 유형 투 포인터, 정렬 문제 풀이 이 문제는 배열 내 모든 원소가 오름차순 정렬이 되도록 정렬되지 않은 subarray를 정렬할 때의 최소 subarray의 길이를 구하는 문제이다. 해당 문제는 두가지 방법으로 접근 가능하다. 정렬 후 비교하기 1. 입력으로 주어진 배열을 정렬해 새로운 배열에 복사한다. 2. 브루트 포스 방법으로..
문제 링크 Merge Intervals - 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 문제 유형 배열, 정렬 문제 풀이 이 문제는 어떤 숫자 배열의 간격들이 요소인 배열이 주어진다. 간격이 겹치는 배열을 합쳐서 최종적으로 겹치지 않는 배열들을 출력한다. 이 문제는 다음과 같은 방식으로 접근한다. 배열을 정렬한다. 예제 입력은 요소들이 정렬되어 있어 모든 테스트 케이스들이 정렬된 줄 알았는데 테스트 케이스에는 정렬되지 않은 배열도 포함되어 있었다. 따라서, 왼..
문제 링크 Merge 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 sort 문제 풀이 이 문제는 num2 배열을 num1 배열로 합쳐서 비내림차순으로 in-place 정렬한 nums1을 반환해야 한다. 비내림차순(non-decreasing order)란 배열이 내림차순이 아닌 경우를 말한다. 즉, 배열 내에 같은 원소가 있기도 하고 그 원소들이 오름차순으로 정렬되어야 한다. 이 문제는 다음과 같은 방법으로 ..
문제 링크 Find Peak Element - 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 문제 유형 이진 탐색 (Binary Search) 문제 풀이 이 문제는 완전 탐색을 통해 배열 내의 가장 큰 값을 찾고, 전/후 값을 비교해 peak elements인지 확인하는 방법이 있다. 이 경우, 시간 복잡도는 O(n)이다. 이 문제는 O(log n)의 시간복잡도를 갖도록 구현해야 하므로 이진 탐색을 이용하여 peak elements를 찾는다면 O(log n)의 ..
문제 링크 Sort Colors - 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 sort algorithm 문제 풀이 이 문제는 아래의 다양한 방식으로 접근할 수 있다. 정렬 메서드 sort() 사용하기 javascript 엔진 v8에서는 삽입 정렬과 병합 정렬을 합친 TimSort를 사용해 정렬 메서드를 구현한다고 한다. 따라서, sort() 메서드를 사용해 정렬을 하였을 때, O(nlogn)의 시간 복잡도를 ..
문제 링크 Minimum Size Subarray 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 문제 유형 배열 (Array), 투 포인터 (Two Pointer) 문제 풀이 해당 문제는 배열 내의 어떤 부분 배열의 원소의 전체 합이 target값과 같거나 큰 부분 배열의 최소 길이를 출력해야 한다. 이는 투 포인터의 개념을 이용하여 해결하면 된다. 투 포인터는 1차원 배열을 순차적으로 접근할 때, 2개의 포인터를 사용하여 문제를 처리한다. 따라서, ..
문제 링크 Find Pivot Index - 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 문제 유형 배열 (Array) 문제 풀이 처음에는 브루트 포스로 현재 피봇의 양옆의 값들의 합을 비교하는 방법이 떠올랐다. 그러나 이렇게 되면 양옆의 값을 더할 때 O(n)의 시간복잡도를 갖게 되고, 이것이 n개 만큼 반복되어 결국 O(n^2)의 시간복잡도를 갖게 되므로 다른 방법으로 접근해야 한다. 이 문제는 배열 접근 방법 중 하나인 sliding 기법을 사용하면 된다..
문제 링크 Move Zeroes - 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 문제 유형 배열 (Array) 문제 풀이 이 문제는 0을 옮긴다고 생각하기 보다 0이 아닌 숫자를 옮기는 방식으로 접근하면 된다. 배열을 순회하면서 현재 요소가 0이 아닌 숫자이면 idx 인덱스의 0과 스왑하고 idx를 한칸 옮긴다. 코드 var moveZeroes = function(nums) { let idx = 0; // 요소의 값이 0인 인덱스를 저장한다. for (let ..