1. ๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/178870
2. ํ์ด
ํฌํฌ์ธํฐ ํ์ฉ ๋๊ฐ๋ก ์์ํด์ K๊ฐ ๊ธฐ์ค์ผ๋ก L,R์์ง์ฌ์ฃผ๋ ํ์ด๋ฐฉ์
3. ์ฝ๋
class Solution {
public int[] solution(int[] sequence, int k) {
int left = 0;
int right = 0;
int sum = 0;
int size = sequence.length;
int ans1 = 0;
int ans2 = 0;
for(right=0; right<sequence.length; right++) {
sum += sequence[right];
while(sum > k) {
sum -= sequence[left];
left++;
}
if(sum == k) {
if(size > right-left) {
size = right-left;
ans1 = left;
ans2 = right;
}
else if(size == right-left) {
ans1 = Math.min(ans1, left);
ans2 = Math.min(ans2, right);
}
}
}
return new int[] {ans1, ans2};
}
}
'๐ป Coding Problems Solving > Two Pointers | Binary Search| LinkedList' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] Remove Duplicates from Sorted List II (0) | 2023.07.12 |
---|---|
[LeetCode] Linked List Cycle II (0) | 2023.07.10 |
[LeetCode] 3Sum (0) | 2023.04.12 |
[BOJ 1806] ๋ถ๋ถํฉ (java) (0) | 2023.04.05 |
[LeetCode] Container With Most Water (0) | 2023.03.28 |
์ต๊ทผ๋๊ธ