티스토리 뷰
Problem
Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return the shortest such subarray and output its length.
Example 1:
Input: nums = [2,6,4,8,10,9,15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
Example 2:
Input: nums = [1,2,3,4]
Output: 0
Example 3:
Input: nums = [1]
Output: 0
Constraints:
- 1 <= nums.length <= 10^4
- -10^5 <= nums[i] <= 10^5
Follow up: Can you solve it in O(n) time complexity?
Solution
정수 배열이 주어질 때 연속된 부분 배열 중 오름차순으로 정렬했을 때 전체가 오름차순이 되게 하는 부분 배열을 구하는 문제입니다.
부분 배열 중 가장 짧은 부분배열의 길이를 반환해야 합니다.
import java.util.Stack;
public class Solution {
public int findUnsortedSubarray(int[] nums) {
Stack<Integer> stack = new Stack<>();
int left = nums.length, right = 0;
for (int i = 0; i < nums.length; i++) {
while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) {
left = Math.min(left, stack.pop());
}
stack.push(i);
}
stack.clear();
for (int i = nums.length - 1; i >= 0; i--) {
while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) {
right = Math.max(right, stack.pop());
}
stack.push(i);
}
return right - left > 0 ? right - left + 1 : 0;
}
}
스택을 이용하여 오른쪽부터 최솟값을, 왼쪽부터 최댓값을 구해 두 인덱스의 차이를 반환하면 됩니다.
Test
package io.lcalmsky.leetcode.shortest_unsorted_continuous_subarray;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class SolutionTest {
@Test
public void givenArray_whenFindTheShortestSubarrayLength_thenCorrect() {
assertAll(
() -> test(new int[]{2, 6, 4, 8, 10, 9, 15}, 5),
() -> test(new int[]{1, 2, 3, 4}, 0)
);
}
private void test(int[] given, int expected) {
// when
Solution shortestUnsortedContinuousSubarray = new Solution();
int actual = shortestUnsortedContinuousSubarray.findUnsortedSubarray(given);
// then
assertEquals(expected, actual);
}
}
'Algorithm > LeetCode' 카테고리의 다른 글
216. Combination Sum III (0) | 2022.05.17 |
---|---|
17. Letter Combinations of a Phone Number (0) | 2022.05.14 |
1679. Max Number of K-Sum Pairs (0) | 2022.05.10 |
99. Recover Binary Search Tree (0) | 2022.05.07 |
844. Backspace String Compare (0) | 2022.05.06 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- JSON
- 헥사고날 아키텍처
- 스프링부트
- Jackson
- Spring Boot Tutorial
- QueryDSL
- Linux
- r
- JPA
- 스프링 부트 튜토리얼
- 클린 아키텍처
- 스프링 부트
- proto3
- Java
- Spring Boot JPA
- spring boot jwt
- leetcode
- 스프링 부트 회원 가입
- spring boot application
- 스프링 부트 애플리케이션
- 함께 자라기
- 함께 자라기 후기
- Spring Data JPA
- spring boot app
- @ManyToOne
- Spring Boot
- 알고리즘
- intellij
- gRPC
- 스프링 데이터 jpa
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
글 보관함