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 |
์ต๊ทผ๋๊ธ