Skip to content

[rlawjd10] WEEK 01 solutions #1718

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
Jul 26, 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
14 changes: 14 additions & 0 deletions contains-duplicate/rlawjd10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
int compare(const void* a, const void* b) {
return (*(int*)a - *(int*)b); // 오름차순
}

bool containsDuplicate(int* nums, int numsSize) {
qsort(nums, numsSize, sizeof(int), compare);

for (int i = 1; i < numsSize; i++) {
if (nums[i] == nums[i-1])
return 1;
}
return 0;
}

19 changes: 19 additions & 0 deletions house-robber/rlawjd10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int rob(vector<int>& nums) {
if (nums.empty()) return 0;
if (nums.size() == 1) return nums[0];

int prev2 = 0; // 두 칸 전 (현재 집 털기)
int prev1 = 0; // 한 칸 전 (현재 집 안털기)

for (int num : nums) {
int current = max(prev1, prev2 + num);
prev2 = prev1;
prev1 = current;
}

return prev1;
}
};

28 changes: 28 additions & 0 deletions longest-consecutive-sequence/rlawjd10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_map<int, int> count;
// nums에 해당하는 index는 1을 표시
for (int num: nums) {
count[num] = 1;
}

vector<pair<int, int>> order(count.begin(), count.end());
sort(order.begin(), order.end(), [](auto& a, auto& b) {return a.first < b.first;});

int length = 1;
int currentLength = 1;
if(order.empty()) return 0;
for (int i = 1; i < order.size(); i++) {
// 연속적인지 확인
if (order[i].first == order[i-1].first +1)
currentLength++;
else
currentLength = 1;
length = max(length, currentLength);
}

return length;
}
};

26 changes: 26 additions & 0 deletions top-k-frequent-elements/rlawjd10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> count;

// 1. nums의 각 원소 count
for (int num : nums) {
count[num]++; // count에 num에 해당하는 value에는 1증가
}

// 2. 많이 나온 순서대로 정렬
vector<pair<int, int>> freqs(count.begin(), count.end());
sort(freqs.begin(), freqs.end(), [](pair<int, int> a, pair<int, int> b) {
return a.second > b.second;
});

// 3. 상위 k개 추출
vector<int> max(k);
for (int i = 0; i < k; i++) {
max[i] = freqs[i].first;
}

return max;
}
};

13 changes: 13 additions & 0 deletions two-sum/rlawjd10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
int compare(const void* a, const void* b) {
return (*(int*)a - *(int*)b); // 오름차순
}

bool containsDuplicate(int* nums, int numsSize) {
qsort(nums, numsSize, sizeof(int), compare);

for (int i = 1; i < numsSize; i++) {
if (nums[i] == nums[i-1])
return 1;
}
return 0;
}