Skip to content

Commit a7acca0

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents b630222 + 8b133b9 commit a7acca0

File tree

20 files changed

+647
-0
lines changed

20 files changed

+647
-0
lines changed

β€Ž3sum/haung921209.mdβ€Ž

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
```cpp
2+
class Solution {
3+
public:
4+
vector<vector<int>> threeSum(vector<int>& nums) {
5+
sort(nums.begin(), nums.end());
6+
set<vector<int>> res;
7+
for(int i=0;i<nums.size();i++){
8+
int l = i+1, r = nums.size()-1;
9+
10+
while(l<r){
11+
int sum = nums[i]+nums[l]+nums[r];
12+
13+
if(sum<0){
14+
l++;
15+
}else if(sum>0){
16+
r--;
17+
}else{
18+
res.insert({nums[i], nums[l], nums[r]});
19+
l++;
20+
r--;
21+
}
22+
}
23+
}
24+
25+
return vector<vector<int>>(res.begin(), res.end());
26+
}
27+
};
28+
```
29+
30+
- set -> vector μ‚¬μš© μ΄μœ λŠ” 쀑볡 제거λ₯Ό μœ„ν•¨
31+
32+
```cpp
33+
class Solution {
34+
public:
35+
vector<vector<int>> threeSum(vector<int>& nums) {
36+
sort(nums.begin(), nums.end());
37+
set<vector<int>> res;
38+
for(int i=0;i<nums.size();i++){
39+
if(i != 0 && nums[i] == nums[i-1]) continue;
40+
41+
int l = i+1, r = nums.size()-1;
42+
43+
while(l<r){
44+
int sum = nums[i]+nums[l]+nums[r];
45+
46+
if(sum<0){
47+
l++;
48+
}else if(sum>0){
49+
r--;
50+
}else{
51+
res.insert({nums[i], nums[l], nums[r]});
52+
l++;
53+
r--;
54+
}
55+
}
56+
}
57+
58+
return vector<vector<int>>(res.begin(), res.end());
59+
60+
}
61+
};
62+
```
63+
64+
- `if(i != 0 && nums[i] == nums[i-1]) continue;` λ₯Ό ν†΅ν•œ 탐색 λ²”μœ„ 쀄이기 μ΅œμ ν™” μ •λ„μ˜ 차이둜 상 / ν•˜μœ„ κ°ˆλ¦¬λŠ” 정도
65+
- λ‹¨μˆœ 2 pointer둜 μ²˜λ¦¬ν•΄λ„ 무방
66+
67+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
```cpp
2+
class Solution {
3+
public:
4+
int climbStairs(int n) {
5+
vector<int> dp = {0, 1, 2};
6+
dp.resize(n+1);
7+
for(int i=3;i<=n;i++){
8+
dp[i] = dp[i-1] + dp[i-2];
9+
}
10+
return dp[n];
11+
}
12+
};
13+
```
14+
- O(n)
15+
- dp
16+
17+
18+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.List;
2+
import java.util.ArrayList;
3+
/**
4+
쀑볡 λ˜μ§€ μ•Šμ€ μš”μ†Œλ“€μ΄ λ“€μ–΄μžˆλŠ” candidates 배열이 μ£Όμ–΄μ§€κ³  target 값이 μ£Όμ–΄μ§„λ‹€.
5+
합이 targetκ³Ό 같은 μ€‘λ³΅λ˜μ§€ μ•Šμ€ 쑰합을 λͺ¨λ‘ λ°˜ν™˜ν•˜μ‹œμ˜€.
6+
*/
7+
class Solution {
8+
9+
List<List<Integer>> answer = new ArrayList<>();
10+
11+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
12+
combination(0, candidates, target, 0, new ArrayList<>());
13+
return answer;
14+
}
15+
16+
// μ‹œκ°„λ³΅μž‘λ„ O(2^target)
17+
public void combination(int idx, int[] candidates, int target, int currentSum, List<Integer> comb) {
18+
// λˆ„μ  합이 λ„˜μœΌλ©΄
19+
if (currentSum > target) {
20+
return;
21+
}
22+
23+
// λˆ„μ  합이 νƒ€κ²Ÿ κ°’κ³Ό κ°™μœΌλ©΄
24+
if (currentSum == target) {
25+
answer.add(new ArrayList<>(comb));
26+
return;
27+
}
28+
29+
for (int i = idx; i < candidates.length; i++) {
30+
comb.add(candidates[i]);
31+
combination(i, candidates, target, currentSum + candidates[i], comb);
32+
comb.remove(comb.size() - 1);
33+
}
34+
}
35+
}
36+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def combinationSum(self, candidates, target):
3+
output, nums = [], []
4+
def dfs(start, total):
5+
if total > target:
6+
return
7+
if total == target:
8+
return output.append(nums[:])
9+
for i in range(start, len(candidates)):
10+
num = candidates[i]
11+
nums.append(num)
12+
dfs(i, total + num)
13+
nums.pop()
14+
dfs(0, 0)
15+
return output

β€Žcombination-sum/uraflower.jsβ€Ž

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* μ£Όμ–΄μ§„ λ°°μ—΄μ˜ μ›μ†Œ μ‘°ν•©(쀑볡 ν—ˆμš©)의 합이 target인 λͺ¨λ“  경우λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜
3+
* @param {number[]} candidates
4+
* @param {number} target
5+
* @return {number[][]}
6+
*/
7+
const combinationSum = function(candidates, target) {
8+
const sortedCandidates = candidates.filter((x) => x <= target).sort((a, b) => Number(a) - Number(b));
9+
const answer = [];
10+
11+
if (sortedCandidates.length === 0) {
12+
return answer;
13+
}
14+
15+
function search(currentIdx, combination, total) {
16+
if (total === target) {
17+
answer.push([...combination]); // λ°°μ—΄ 자체λ₯Ό λ„£μœΌλ©΄ μ°Έμ‘° λ•Œλ¬Έμ— 값이 λ³€κ²½λ˜λ―€λ‘œ, λ³΅μ‚¬ν•΄μ„œ λ„£μ–΄μ•Ό 함
18+
return;
19+
}
20+
21+
if (total > target) {
22+
return; // backtracking
23+
}
24+
25+
combination.push(sortedCandidates[currentIdx]);
26+
search(currentIdx, combination, total + sortedCandidates[currentIdx]);
27+
combination.pop();
28+
29+
if (total + sortedCandidates[currentIdx] > target) {
30+
return; // backtracking
31+
}
32+
33+
if (currentIdx + 1 < sortedCandidates.length) {
34+
search(currentIdx + 1, combination, total);
35+
}
36+
}
37+
38+
search(0, [], 0);
39+
return answer;
40+
};
41+
42+
// t: target
43+
// μ‹œκ°„λ³΅μž‘λ„: O(2^t)
44+
// κ³΅κ°„λ³΅μž‘λ„: O(t)

β€Ždecode-ways/Tessa1217.javaβ€Ž

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
μ£Όμ–΄μ§„ λ¬Έμžμ—΄μ„ λ³΅ν˜Έν™” ν•  수 μžˆλŠ” 경우의 수λ₯Ό λ°˜ν™˜ν•˜μ‹œμ˜€.
3+
λ¬Έμžμ—΄μ€ A-ZκΉŒμ§€ 숫자 1-26으둜 μΉ˜ν™˜
4+
μ˜ˆμ‹œ: "AAJF" => (1, 1, 10, 6), (11, 10, 6)...
5+
*/
6+
class Solution {
7+
8+
// μ‹œκ°„λ³΅μž‘λ„: O(n), κ³΅κ°„λ³΅μž‘λ„: O(n)
9+
public int numDecodings(String s) {
10+
11+
int[] dp = new int[s.length() + 1];
12+
13+
// contain leading zero(s)
14+
if (s.charAt(0) == '0') {
15+
return 0;
16+
}
17+
18+
dp[0] = 1;
19+
dp[1] = 1;
20+
21+
for (int i = 2; i <= s.length(); i++) {
22+
23+
// 1자리수 검사
24+
int one = Integer.parseInt(Character.toString(s.charAt(i - 1)));
25+
26+
if (one != 0) {
27+
dp[i] += dp[i - 1];
28+
}
29+
30+
// 2자리수 검사
31+
int two = Integer.parseInt(Character.toString(s.charAt(i - 2))) * 10 + one;
32+
33+
if (two >= 10 && two <= 26) {
34+
dp[i] += dp[i - 2];
35+
}
36+
37+
}
38+
39+
return dp[s.length()];
40+
}
41+
42+
}
43+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def numDecodings(self, s):
3+
if not s:
4+
return 0
5+
dp = [0] * (len(s) + 1)
6+
dp[0] = 1
7+
dp[1] = 1 if s[0] != '0' else 0
8+
for i in range(2, len(s) + 1):
9+
if '1' <= s[i - 1] <= '9':
10+
dp[i] += dp[i - 1]
11+
if '10' <= s[i - 2:i] <= '26':
12+
dp[i] += dp[i - 2]
13+
return dp[len(s)]

β€Ždecode-ways/uraflower.jsβ€Ž

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* μ£Όμ–΄μ§„ λ¬Έμžμ—΄μ„ λ³΅ν˜Έν™”ν•˜λŠ” 경우의 수λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜
3+
* @param {string} s
4+
* @return {number}
5+
*/
6+
const numDecodings = function(s) {
7+
const dp = {};
8+
9+
function decode(idx) {
10+
if (s[idx] === '0') {
11+
return 0;
12+
}
13+
14+
if (idx === s.length) {
15+
return 1;
16+
}
17+
18+
if (dp[idx]) {
19+
return dp[idx];
20+
}
21+
22+
let result = 0;
23+
result += decode(idx + 1); // ν˜„μž¬ 문자만 μ“°λŠ” 경우: λ‹€μŒ λ¬ΈμžλΆ€ν„° 탐색
24+
if (s[idx + 1] && Number(s[idx] + s[idx+1]) <= 26) {
25+
result += decode(idx + 2); // ν˜„μž¬ λ¬Έμžμ™€ λ‹€μŒ 문자 λΆ™μ—¬μ„œ μ“°λŠ” 경우: λ‹€λ‹€μŒ λ¬ΈμžλΆ€ν„° 탐색
26+
}
27+
28+
dp[idx] = result;
29+
return result;
30+
}
31+
32+
return decode(0);
33+
};
34+
35+
// μ‹œκ°„λ³΅μž‘λ„: O(n) (λ©”λͺ¨μ΄μ œμ΄μ…˜ μ•ˆν•˜λ©΄ λ§€ μΈλ±μŠ€λ§ˆλ‹€ μ΅œλŒ€ 2개의 ν•˜μœ„ 호좜이 λ°œμƒν•˜μ—¬ O(2^n))
36+
// κ³΅κ°„λ³΅μž‘λ„: O(n)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
μ •μˆ˜ 배열이 μ£Όμ–΄μ§ˆ λ•Œ λΆ€λΆ„ μˆ˜μ—΄μ˜ κ°€μž₯ 큰 합을 κ΅¬ν•˜μ‹œμ˜€.
3+
*/
4+
class Solution {
5+
6+
// μ‹œκ°„λ³΅μž‘λ„: O(n), κ³΅κ°„λ³΅μž‘λ„: O(1)
7+
public int maxSubArray(int[] nums) {
8+
9+
int sum = nums[0];
10+
11+
for (int i = 1; i < nums.length; i++) {
12+
// 수 μ΄μ–΄μ„œ 더할지 μ•„λ‹ˆλ©΄ ν˜„μž¬ κ°’μœΌλ‘œ μ΄ˆκΈ°ν™”ν• μ§€ μ—¬λΆ€ νŒλ‹¨
13+
nums[i] = Math.max(nums[i], nums[i] + nums[i - 1]);
14+
sum = Math.max(nums[i], sum);
15+
}
16+
17+
return sum;
18+
}
19+
20+
}
21+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def maxSubArray(self, nums):
3+
max_sum = current_sum = nums[0]
4+
for num in nums[1:]:
5+
current_sum = max(num, current_sum + num)
6+
max_sum = max(max_sum, current_sum)
7+
return max_sum

0 commit comments

Comments
Β (0)