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+

โ€Žclimbing-stairs/haung921209.md

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+

โ€Žcombination-sum/Tessa1217.java

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+

โ€Ždecode-ways/printjin-gmailcom.py

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)

โ€Žmaximum-subarray/Tessa1217.java

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)