inblog logo
|
stupefyee
    알고리즘문제풀기

    [JAVA 문제 풀이] 393. Maximum Erasure Value

    [최대 삭제 값]
    Stupefyee's avatar
    Stupefyee
    Jul 22, 2025
    [JAVA 문제 풀이] 393. Maximum Erasure Value
    Contents
    내가 작성한 코드
    Maximum Erasure Value - LeetCode
    Can you solve this real interview question? Maximum Erasure Value - You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements. Return the maximum score you can get by erasing exactly one subarray. An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r).   Example 1: Input: nums = [4,2,4,5,6] Output: 17 Explanation: The optimal subarray here is [2,4,5,6]. Example 2: Input: nums = [5,2,1,2,5,2,1,2,5] Output: 8 Explanation: The optimal subarray here is [5,2,1] or [1,2,5].   Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 104
    Maximum Erasure Value - LeetCode
    https://leetcode.com/problems/maximum-erasure-value/description/
    Maximum Erasure Value - LeetCode
    notion image
    notion image
    양의 정수 'nums' 배열이 주어지면 고유한 요소가 포함된 하위 배열을 지우려고 합니다. 하위 배열을 지우면 해당 요소의 합과 같습니다. 정확히 하나의 하위 배열을 지워서 얻을 수 있는 최대 점수를 반환하세요. 배열 'b'가 a의 연속적인 부분열을 형성하는 경우, 즉 어떤 '(l,r)'에 대해 'a[l], a[l+1], ..., a[r]'와 같다면 'a'의 부분열이라고 합니다. 제약 조건: * 1 <= nums.length <= 105 * 1 <= nums [i] <= 104
     

    내가 작성한 코드

    import java.util.*; class Solution { public int maximumUniqueSubarray(int[] nums) { Set<Integer> set = new HashSet<>(); // 현재 서브배열의 원소 집합 int left = 0; // 왼쪽 포인터 int right = 0; // 오른쪽 포인터 int sum = 0; // 포인터 내부 합 int maxSum = 0; // 최대 값 while (right < nums.length) { // 중복이 아니면 // 포인터를 오른쪽을 이동시키며 값들 증가 if (!set.contains(nums[right])) { set.add(nums[right]); sum += nums[right]; maxSum = Math.max(maxSum, sum); right++; } else { // 중복이 생기면 왼쪽 포인터를 이동시키며 값들 감소 set.remove(nums[left]); sum -= nums[left]; left++; } } return maxSum; } }
    Share article
    Contents
    내가 작성한 코드

    stupefyee

    RSS·Powered by Inblog