Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions combination-sum/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
vector<vector<int>> ans;
vector<int> candids;
int t;

void dfs(int idx, int sum, vector<int> s) {
if(sum > t) return;
else if (sum == t) ans.push_back(s);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sum == t일 때도 곧바로 return 하는 것이 더 좋을 것 같습니다!


for(int i = idx; i < candids.size(); i++) {
auto next = vector<int>(s);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매 호출마다 벡터를 복사하면 O(len(s)) 만큼의 비용이 각 호출마다 발생합니다.

주어진 s 벡터에 대해 dfs() 호출 앞뒤로 push_back()pop_back()을 하여 동적으로 벡터를 조작한다면 더 효율적이지 않을까요??

next.push_back(candids[i]);
dfs(i, sum+candids[i], next);
}
};

vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
candids = candidates;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가로 주어진 candidates 벡터를 정렬하면 pruning을 할 수 있어 풀이를 더 최적화 할 수 있습니다!

t = target;
dfs(0, 0, {});
return ans;
}
};
38 changes: 38 additions & 0 deletions decode-ways/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public:
int mem[100];
string str;

bool isValid(string s) {
if(s.size() == 1) {
return '1' <= s[0] && s[0] <= '9';
}
else if(s.size() == 2) {
if(s[0] == '1') return '0' <= s[1] && s[1] <= '9';
else if(s[0] == '2') return '0' <= s[1] && s[1] <= '6';
}
return false;
}

int dfs(int idx) {
if(idx >= str.size()) return 1;

int& ret = mem[idx];
if(ret != -1) return ret;
ret = 0;

if (isValid(str.substr(idx, 1))){
ret += dfs(idx+1);
}
if (idx < str.size() - 1 && isValid(str.substr(idx, 2))){
ret += dfs(idx+2);
}
return ret;
}

int numDecodings(string s) {
str = s;
fill(mem, mem+100, -1);
return dfs(0);
}
};
15 changes: 15 additions & 0 deletions maximum-subarray/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int dp[100000];
dp[0] = max(nums[0], -(1<<30));
for(int i = 1; i < nums.size(); i++) {
dp[i] = max(dp[i-1] + nums[i], nums[i]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dp[i]를 구하기 위해 dp[i - 1]만 확인하므로 DP 리스트가 아닌 하나의 변수로 트래킹한다면 공간 복잡도를 O(1)로 최적화 할 수 있을 것 같습니다!

}
int m = dp[0];
for(int i = 1; i < nums.size(); i++) {
m = max(dp[i], m);
}
return m;
}
};
6 changes: 6 additions & 0 deletions number-of-1-bits/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
public:
int hammingWeight(int n) {
return popcount((unsigned int)n);
}
};
22 changes: 22 additions & 0 deletions valid-palindrome/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
bool isPalindrome(string s) {
string p;
for(char const& c: s) {
if('A' <= c && c <= 'Z') {
p.push_back(c - 'A' + 'a');
}
else if('a' <= c && c <= 'z') {
p.push_back(c);
}
else if ('0' <= c && c <= '9') {
p.push_back(c);
}
}
int start = 0, end = p.size()-1;
while(start <= end) {
if(p[start++] != p[end--]) return false;
}
return true;
}
};