Skip to content

Commit e5e172a

Browse files
authoredApr 25, 2025
Merge pull request #1314 from crumbs22/main
[crumbs22] Week 03 solutions
2 parents 162c276 + a28d380 commit e5e172a

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
 

โ€Žmaximum-subarray/crumbs22.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <climits>
4+
5+
using namespace std;
6+
7+
8+
/*
9+
TC: O(n)
10+
SC: O(1)
11+
ํ’€์ด ๋ฐฉ๋ฒ•:
12+
- sum์— ํ˜„์žฌ ๊ฐ’์„ ๋”ํ•ด๊ฐ€๋ฉด์„œ ์ตœ๋Œ€๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค
13+
- ๋ˆ„์ ํ•ฉ์ด ์Œ์ˆ˜๊ฐ€ ๋์„ ๋•Œ 0์œผ๋กœ ๋ฆฌ์…‹ํ•œ๋‹ค
14+
15+
๊ณ ๋ฏผํ–ˆ๋˜ ์ผ€์ด์Šค(left์™€ right ํฌ์ธํ„ฐ๋ฅผ ๋‘๊ณ  ํ’€์—ˆ์„ ๋•Œ):
16+
[-2, -1]
17+
[-1, -2]
18+
[-2, 1]
19+
[-1, 1, 2, 1]
20+
*/
21+
class Solution {
22+
public:
23+
int maxSubArray(vector<int>& nums) {
24+
25+
int max = nums[0];
26+
int sum = 0;
27+
28+
for (int i = 0; i < nums.size(); i++)
29+
{
30+
sum += nums[i];
31+
if (sum > max)
32+
max = sum;
33+
if (sum < 0)
34+
sum = 0;
35+
}
36+
return (max);
37+
}
38+
};

โ€Žnumber-of-1-bits/crumbs22.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
/*
6+
TC: O(1)
7+
SC: O(1)
8+
ํ’€์ด๋ฐฉ๋ฒ•:
9+
- n์˜ ๋น„ํŠธ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™์‹œํ‚ค๋ฉด์„œ ์ตœํ•˜์œ„ ๋น„ํŠธ๊ฐ€ 1์ธ์ง€ ํ™•์ธํ•œ๋‹ค
10+
- n์ด intํ˜•์ด๋ฏ€๋กœ cnt๋Š” 32๋ฅผ ๋„˜์„ ์ˆ˜ ์—†๋‹ค
11+
*/
12+
13+
class Solution {
14+
public:
15+
int hammingWeight(int n) {
16+
int cnt = 0;
17+
18+
while (n && cnt <= 31)
19+
{
20+
if (n & 1)
21+
cnt++;
22+
n >>= 1;
23+
}
24+
return (cnt);
25+
}
26+
};

โ€Žvalid-palindrome/crumbs22.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <cctype>
4+
5+
using namespace std;
6+
7+
/*
8+
TC: O(n)
9+
start, end ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ๊ฐ ํ•œ ๋ฒˆ์”ฉ ์ „์ฒด ๋ฌธ์ž์—ด์„ ์Šค์บ”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—
10+
๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ตœ๋Œ€ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๊ฒ€์‚ฌํ•œ๋‹ค
11+
SC: O(1)
12+
ํ’€์ด๋ฐฉ๋ฒ•:
13+
- ์–‘ ์ชฝ์—์„œ ํฌ์ธํ„ฐ๊ฐ€ ์ด๋™ํ•˜๋ฉด์„œ ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๋‘ ๋ฌธ์ž๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค
14+
๊ณ ๋ฏผํ–ˆ๋˜ ์ผ€์ด์Šค:
15+
- 0P
16+
*/
17+
18+
class Solution {
19+
public:
20+
bool isPalindrome(string s) {
21+
int start = 0;
22+
int end = s.size() - 1;
23+
24+
while (start < end)
25+
{
26+
// ascii ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ๊ตฌ๊ฐ„ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
27+
while (start < end && !isalnum(s[start]))
28+
start++;
29+
while (start < end && !isalnum(s[end]))
30+
end--;
31+
32+
if (tolower(s[start]) != tolower(s[end]))
33+
return (false);
34+
start++;
35+
end--;
36+
}
37+
return (true);
38+
}
39+
};

0 commit comments

Comments
 (0)