1. ๋ฌธ์ œ : https://leetcode.com/problems/subarray-sum-equals-k/

 

2. ํ’€์ด

๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ™œ์šฉํ•œ ํ’€์ด๋ฒ•

 

3. ์ฝ”๋“œ

class Solution {
    public int subarraySum(int[] nums, int k) {
        
        // 1. dp
        int len = nums.length;
        int cnt = 0;

        int[] dp = new int[len+1];
        for (int i = 0; i < len; i++) { // dp[i]๋Š” ํ˜„์žฌ ์ธ๋ฑ์Šค ์ „(i-1)๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ
            dp[i+1] = dp[i]+nums[i];
        }
        for (int i = 0; i < dp.length; i++) {
            for (int j = i+1; j < dp.length; j++) {
                if(dp[j]-dp[i] == k){
                    cnt++;
                }
            }
        }
        return cnt;

    }
}

'๐Ÿ’ป Coding Problems Solving > Dynamic Programming' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ 1103] ๊ฒŒ์ž„  (0) 2023.08.07
[BOJ 1890] ์ ํ”„ (java)  (0) 2023.04.20
[LeetCode] Longest Increasing Subsequence  (0) 2023.03.30
[LeetCode] Coin Change  (0) 2023.03.29
[LeetCode] Maximum Product Subarray  (0) 2023.03.28
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ