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
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ