Skip to content

[crumbs22] Week08 Solutions #1510

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions clone-graph/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <vector>
#include <iostream>

using namespace std;

class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
/*
std::vector<T> v;

v.emplace_back(arg1, arg2, ...); // 생성자 인자를 바로 전달해 컨테이너 안에서 직접 T 객체를 생성
*/

#include <unordered_map>
#include <queue>

class Solution {
public:
Node* cloneGraph(Node* node) {
if (!node)
return (nullptr);
unordered_map<Node*, Node*> m;
queue<Node*> q;

m[node] = new Node(node->val); // 시작 노드를 복제하고 맵과 큐에 등록
q.push(node);

// BFS
while (!q.empty()) {
Node* cur = q.front();
q.pop();

for (Node* nei : cur->neighbors) {

// 아직 복제하지 않은 노드일 때
if (!m.count(nei)) {
m[nei] = new Node(nei->val);
q.push(nei);
}
m[cur]->neighbors.push_back(m[nei]); // 현재 복제본에 이 이웃의 복제본을 연결
}
}
return m[node];
}
};
34 changes: 34 additions & 0 deletions longest-repeating-character-replacement/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <algorithm>
using namespace std;

/*
시간 복잡도 : O(n)
공간 복잡도 : O(1)

구간 길이 windowSize = (right - left + 1)에서,
windowSize - maxCount <= k 이면 이 구간을 모두 같은 문자로 만들 수 있다
windowSize - maxCount > k 이면 교체가능한 횟수가 초과되었으므로 left를 한 칸 밀어서 구간을 줄인다
*/
class Solution {
public:
int characterReplacement(string s, int k) {
int left = 0;
int maxCount = 0;
int answer = 0;
int count[26] = {0};

for (int right = 0; right < s.size(); right++) {

count[s[right] - 'A']++;
maxCount = max(maxCount, count[s[right] - 'A']); // 윈도우 안에서 가장 많은 빈도를 갱신
while (right - left + 1 - maxCount > k) {
count[s[left] - 'A']--;
left++;
}
answer = max(answer, right - left + 1);

}
return (answer);
}
};
30 changes: 30 additions & 0 deletions palindromic-substrings/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>

using namespace std;

/*
중심 확장 방식
전체 문자열길이 n에 대해서
- 홀수 길이용 n개
- 짝수 길이용 n - 1개
전체 2n - 1개의 중심에서 각 양쪽으로 확장하며 카운트
*/
class Solution {
public:
int countSubstrings(string s) {
int answer = 0;
int n = s.size();

for (int center = 0; center < 2 * n - 1; center++) {
int left = center / 2;
int right = left + (center % 2);

while (left >= 0 && right < n && s[left] == s[right]) {
answer++;
left--;
right++;
}
}
return (answer);
}
};
14 changes: 14 additions & 0 deletions reverse-bits/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
unsigned int reverse = 0;
for (int i = 0; i < 32; i++) {
reverse <<= 1; // 한 비트를 왼쪽으로 당겨서 공간을 만들고
reverse |= (n & 1); // n의 최하위 비트를 reverse의 최하위 비트에 저장
n >>= 1; // n의 다음 비트를 최하위 비트로 당겨온다
}
return (reverse);
}
};