Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Sep 13, 2025

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • LeetCode 209번 문제 해결: 최소 크기 부분 배열의 합 구현

  • 투 포인터 알고리즘을 활용한 효율적인 해결책 제시


@uyeon0 uyeon0 added the leetcode Leetcode 문제 풀이 label Sep 13, 2025
@github-actions
Copy link

PR Reviewer Guide 🔍

🧪 PR contains tests
⚡ Recommended focus areas for review

성능 최적화

현재 구현은 O(n) 시간 복잡도를 가지지만, 내부 while 루프에서 sum >= target 조건을 체크할 때마다 불필요한 반복이 발생할 수 있습니다. 예를 들어 [1,1,1,1,1]와 target=10 같은 케이스에서 비효율이 발생합니다.

while (sum >= target) {
  minLength = Math.min(minLength, right - left);
  sum -= nums[left];
  left++;
}
엣지 케이스

음수 값이 포함된 배열(예: [1,-2,3,4])이나 target이 음수인 경우에 대한 처리가 명확하지 않습니다. 또한 빈 배열이나 단일 요소 배열([5], target=5)에 대한 처리도 검증이 필요합니다.

function minSubArrayLen(target: number, nums: number[]): number {
  let minLength = Infinity;
  let left = 0,
    right = left;
  let sum = 0;

  while (right < nums.length) {
    if (sum < target) {
      sum += nums[right];
      right++;
    }
    while (sum >= target) {
      minLength = Math.min(minLength, right - left);
      sum -= nums[left];
      left++;
    }
  }

  return minLength === Infinity ? 0 : minLength;
}
메모리 사용

현재 구현은 O(1) 공간 복잡도를 가지지만, 큰 숫자의 합을 다룰 때 sum 변수의 오버플로우 가능성이 있습니다. 예: [Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]

let sum = 0;

@github-actions
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
슬라이딩 윈도우 로직 단순화

슬라이딩 윈도우 로직을 더 명확하게 개선하세요. 현재 중첩된 while 문은 가독성이 떨어집니다. 외부 while 문에서 모든 로직을 처리하고,
조건문을 단순화하여 코드 흐름을 개선할 수 있습니다.

Leetcode/209.minimum-size-subarray-sum.ts [6-21]

 let minLength = Infinity;
-let left = 0,
-  right = left;
+let left = 0;
 let sum = 0;
 
-while (right < nums.length) {
-  if (sum < target) {
-    sum += nums[right];
-    right++;
-  }
+for (let right = 0; right < nums.length; right++) {
+  sum += nums[right];
+  
   while (sum >= target) {
-    minLength = Math.min(minLength, right - left);
+    minLength = Math.min(minLength, right - left + 1);
     sum -= nums[left];
     left++;
   }
 }
Suggestion importance[1-10]: 8

__

Why: Converting nested while loops to a for-loop with a single while loop significantly improves code readability and maintainability. The improved version also fixes a bug in the window size calculation by adding + 1.

Medium
변수 선언 개선

변수 초기화를 더 명확하게 분리하세요. 현재 rightleft에 기반하여 초기화하는 것은 불필요하며, 각 변수를 독립적으로 선언하는 것이 더
명확합니다.

Leetcode/209.minimum-size-subarray-sum.ts [7-8]

-let left = 0,
-  right = left;
+let left = 0;
+let right = 0;
Suggestion importance[1-10]: 4

__

Why: Separating variable declarations makes the code slightly more readable, though the impact is minor since both approaches are valid. The current initialization of right = left is unnecessary as both are 0.

Low

@yoouyeon yoouyeon self-assigned this Sep 13, 2025
@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Sep 13, 2025
@yoouyeon yoouyeon added ready-to-merge pr을 머지해주세요 and removed ready-to-merge pr을 머지해주세요 labels Sep 13, 2025
@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Sep 13, 2025
@uyeon0 uyeon0 merged commit e94a5db into main Sep 13, 2025
8 checks passed
@uyeon0 uyeon0 deleted the upload branch September 13, 2025 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

leetcode Leetcode 문제 풀이 ready-to-merge pr을 머지해주세요

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants