File tree 5 files changed +188
-0
lines changed
5 files changed +188
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ target + 1개의 크기를 가지는 삼중벡터 dp를 만든다
4
+ dp[n] = dp[n - candidate]의 각 조합에 candidate를 추가하는 로직으로 쌓아나갈 것이다
5
+ dp[n - c]가 [comb1, comb2]일 때 dp[n]은 [comb1.push_back(c), comb2.push_back[2]]
6
+
7
+ dp[0]은 연산을 위해 빈 이중 벡터로 초기화 ( dp[n] = dp[n - n] = dp[0] --> [[].push_back(n)])
8
+
9
+ target크기 : T, candidate 갯수 : N
10
+
11
+ TC : O(T * N)
12
+
13
+ SC : O(T * N)
14
+ */
15
+
16
+ #include < vector>
17
+ using namespace std ;
18
+
19
+ class Solution {
20
+ public:
21
+ vector<vector<int >> combinationSum (vector<int >& candidates, int target) {
22
+ vector<vector<vector<int >>> dp (target + 1 );
23
+ dp[0 ] = {{}};
24
+ for (int candidate : candidates)
25
+ {
26
+ for (int num = candidate; num <= target; num++)
27
+ {
28
+ for (auto & combination : dp[num - candidate])
29
+ {
30
+ vector<int > new_comb = combination;
31
+ new_comb.push_back (candidate);
32
+ dp[num].push_back (new_comb);
33
+ }
34
+ }
35
+ }
36
+ return dp[target];
37
+ }
38
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ i 번째 연산을 시작 전 cur는 i + 1에서 시작하는 경우의 수, nxt에는 i + 2에서 시작하는 경우의 수 저장돼있다
4
+ i 번째 연산이 끝난 후 cur는 i에서 시작하는 경우, nxt에는 i + 1에서 시작하는 경우의 수 저장되도록 한다
5
+ s의 길이가 1일 때 무조건 1개의 경우의 수를 가지므로 cur 1로 초기화
6
+
7
+ 세가지 경우의 수
8
+ 1. s[i]가 '0' 일때 0으로 시작하는 문자열은 해석가능한 수가 없으므로 cur를 0으로 한다
9
+ 2. s[i]로 시작하는 두 자리 수가 숫자로 변환하면 27보다 작으면, 1자리로 변환하는 경우의 수(cur) + 2자리로 변환하는 경우의 수(nxt)로 cur 변경
10
+ 3. 그 외에는 1자리로 변환하는 경우의 수 밖에 없으므로 cur 그대로
11
+
12
+ 문자열 끝에서 조건에 맞춰 업데이트 하면서 문자열 처음까지 순회하고 cur 리턴한다
13
+
14
+ 문자열 길이 N
15
+
16
+ TC : O(N)
17
+ 문자열 한번 순회
18
+
19
+ SC : O(1)
20
+ */
21
+
22
+ #include < string>
23
+ using namespace std ;
24
+
25
+ class Solution {
26
+ public:
27
+ int numDecodings (string s) {
28
+ int cur = 1 ;
29
+ int nxt = 0 ;
30
+ int tmp;
31
+
32
+ for (int i = s.size () - 1 ; i >= 0 ; i--)
33
+ {
34
+ tmp = nxt;
35
+ if (s[i] == ' 0' )
36
+ {
37
+ nxt = cur;
38
+ cur = 0 ;
39
+ }
40
+ else if (i < s.size () - 1 && stoi (s.substr (i, 2 )) < 27 )
41
+ {
42
+ nxt = cur;
43
+ cur = cur + tmp;
44
+ }
45
+ else
46
+ {
47
+ nxt = cur;
48
+ }
49
+ }
50
+ return cur;
51
+ }
52
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ max_sum은 INT_MIN으로 초기화
4
+
5
+ 수를 순회하면서 sum에 num을 더한다(subarray의 합)
6
+ max_sum과 현재 subarray합을 비교해 업데이트한다
7
+
8
+ subarray의 합이 0보다 작으면 새로운 subarray를 시작하기 위해 sum을 0으로 초기화한다
9
+
10
+ nums의 길이 N
11
+
12
+ TC : O(N)
13
+
14
+ SC : O(1)
15
+ */
16
+
17
+ #include < vector>
18
+ #include < limits.h>
19
+ using namespace std ;
20
+
21
+ class Solution {
22
+ public:
23
+ int maxSubArray (vector<int >& nums) {
24
+ int max_sum = INT_MIN;
25
+ int sum = 0 ;
26
+
27
+ for (auto & num : nums)
28
+ {
29
+ sum += num;
30
+ if (max_sum < sum)
31
+ max_sum = sum;
32
+ if (sum < 0 )
33
+ sum = 0 ;
34
+ }
35
+ return max_sum;
36
+ }
37
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ 쉬프트 연산으로 n을 감소 시키면서 n 과 1의 & 연산이 true인 갯수를 세서 리턴
4
+
5
+ TC : O(1)
6
+ n이 커도 최대 32번의 반복문
7
+
8
+ SC : O(1)
9
+ */
10
+
11
+ class Solution {
12
+ public:
13
+ int hammingWeight (int n) {
14
+ int cnt = 0 ;
15
+ while (n > 0 )
16
+ {
17
+ if (n & 1 )
18
+ cnt++;
19
+ n = n >> 1 ;
20
+ }
21
+ return cnt;
22
+ }
23
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ alnum인 문자들만 추출해서 소문자로 만들어 alnu_s를 만듬
4
+ 양 끝이 같은 문자일 동안 left++, right-- 실시
5
+ 같지 않으면 false, 양 끝이 수렴되서 서로 만난다면 true
6
+
7
+ 문자 길이 N
8
+
9
+ TC : O(N)
10
+
11
+ SC : O(N)
12
+ */
13
+
14
+ #include < string>
15
+ using namespace std ;
16
+
17
+ class Solution {
18
+ public:
19
+ bool isPalindrome (string s) {
20
+ string alnu_s = " " ;
21
+
22
+ for (char c : s)
23
+ {
24
+ if (isalnum (c))
25
+ alnu_s += tolower (c);
26
+ }
27
+ int left = 0 ;
28
+ int right = alnu_s.size () - 1 ;
29
+ while (left < right)
30
+ {
31
+ if (alnu_s[left] != alnu_s[right])
32
+ return false ;
33
+ left++;
34
+ right--;
35
+ }
36
+ return true ;
37
+ }
38
+ };
You can’t perform that action at this time.
0 commit comments