Skip to content

[sungjinwi] Week03 #1324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 22, 2025
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
38 changes: 38 additions & 0 deletions combination-sum/sungjinwi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
풀이 :
target + 1개의 크기를 가지는 삼중벡터 dp를 만든다
dp[n] = dp[n - candidate]의 각 조합에 candidate를 추가하는 로직으로 쌓아나갈 것이다
dp[n - c]가 [comb1, comb2]일 때 dp[n]은 [comb1.push_back(c), comb2.push_back[2]]

dp[0]은 연산을 위해 빈 이중 벡터로 초기화 ( dp[n] = dp[n - n] = dp[0] --> [[].push_back(n)])

target크기 : T, candidate 갯수 : N

TC : O(T * N)

SC : O(T * N)
*/

#include <vector>
using namespace std;

class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<vector<int>>> dp(target + 1);
dp[0] = {{}};
for (int candidate : candidates)
{
for (int num = candidate; num <= target; num++)
{
for (auto& combination : dp[num - candidate])
{
vector<int> new_comb = combination;
new_comb.push_back(candidate);
dp[num].push_back(new_comb);
}
}
}
return dp[target];
}
};
52 changes: 52 additions & 0 deletions decode-ways/sungjinwi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
풀이 :
i 번째 연산을 시작 전 cur는 i + 1에서 시작하는 경우의 수, nxt에는 i + 2에서 시작하는 경우의 수 저장돼있다
i 번째 연산이 끝난 후 cur는 i에서 시작하는 경우, nxt에는 i + 1에서 시작하는 경우의 수 저장되도록 한다
s의 길이가 1일 때 무조건 1개의 경우의 수를 가지므로 cur 1로 초기화

세가지 경우의 수
1. s[i]가 '0' 일때 0으로 시작하는 문자열은 해석가능한 수가 없으므로 cur를 0으로 한다
2. s[i]로 시작하는 두 자리 수가 숫자로 변환하면 27보다 작으면, 1자리로 변환하는 경우의 수(cur) + 2자리로 변환하는 경우의 수(nxt)로 cur 변경
3. 그 외에는 1자리로 변환하는 경우의 수 밖에 없으므로 cur 그대로

문자열 끝에서 조건에 맞춰 업데이트 하면서 문자열 처음까지 순회하고 cur 리턴한다

문자열 길이 N

TC : O(N)
문자열 한번 순회

SC : O(1)
*/

#include <string>
using namespace std;

class Solution {
public:
int numDecodings(string s) {
int cur = 1;
int nxt = 0;
int tmp;

for (int i = s.size() - 1; i >= 0; i--)
{
tmp = nxt;
if (s[i] == '0')
{
nxt = cur;
cur = 0;
}
else if(i < s.size() - 1 && stoi(s.substr(i, 2)) < 27)
{
nxt = cur;
cur = cur + tmp;
}
else
{
nxt = cur;
}
}
return cur;
}
};
37 changes: 37 additions & 0 deletions maximum-subarray/sungjinwi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
풀이 :
max_sum은 INT_MIN으로 초기화

수를 순회하면서 sum에 num을 더한다(subarray의 합)
max_sum과 현재 subarray합을 비교해 업데이트한다

subarray의 합이 0보다 작으면 새로운 subarray를 시작하기 위해 sum을 0으로 초기화한다

nums의 길이 N

TC : O(N)

SC : O(1)
*/

#include <vector>
#include <limits.h>
using namespace std;

class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max_sum = INT_MIN;
int sum = 0;

for (auto& num : nums)
{
sum += num;
if (max_sum < sum)
max_sum = sum;
if (sum < 0)
sum = 0;
}
return max_sum;
}
};
23 changes: 23 additions & 0 deletions number-of-1-bits/sungjinwi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
풀이 :
쉬프트 연산으로 n을 감소 시키면서 n 과 1의 & 연산이 true인 갯수를 세서 리턴

TC : O(1)
n이 커도 최대 32번의 반복문

SC : O(1)
*/

class Solution {
public:
int hammingWeight(int n) {
int cnt = 0;
while (n > 0)
{
if (n & 1)
cnt++;
n = n >> 1;
}
return cnt;
}
};
38 changes: 38 additions & 0 deletions valid-palindrome/sungjinwi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
풀이 :
Copy link
Contributor

Choose a reason for hiding this comment

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

설명 너무 깔끔하고 잘 정리하셨네요. 변수명도 다 잘 읽혀서 쉽게 이해했습니다!

alnum인 문자들만 추출해서 소문자로 만들어 alnu_s를 만듬
양 끝이 같은 문자일 동안 left++, right-- 실시
같지 않으면 false, 양 끝이 수렴되서 서로 만난다면 true

문자 길이 N

TC : O(N)

SC : O(N)
*/

#include <string>
using namespace std;

class Solution {
public:
bool isPalindrome(string s) {
string alnu_s = "";

for (char c : s)
{
if (isalnum(c))
alnu_s += tolower(c);
}
int left = 0;
int right = alnu_s.size() - 1;
while (left < right)
{
if (alnu_s[left] != alnu_s[right])
return false;
left++;
right--;
}
return true;
}
};