Skip to content
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
13 changes: 13 additions & 0 deletions 0349-intersection-of-two-arrays/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### step1

nums1とnums2をsetに入れて、制約である0-1000までの値の中で両方のsetに
時間計算量はnums1, nums2の長さをN,MとしてO(NlogN + MlogM + 1000 * (logN + logM))になると思います。

### step2

step1のコードをGPTにより簡潔で高速なコードに修正してもらいました。
時間計算量はO(N + M)に改善されると思います。

### step3

step2のコードを3回通すまで書き直し。
19 changes: 19 additions & 0 deletions 0349-intersection-of-two-arrays/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
set<int> set1, set2;
for (int num1 : nums1) {
set1.insert(num1);
}
for (int num2 : nums2) {
set2.insert(num2);
}
vector<int> answer;
for (int i = 0; i <= 1000; i++) {
Copy link

Choose a reason for hiding this comment

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

これでも制約が保証されているならばいいですが、だいたいこういう制約は気がつくと破られているものなので、破られても耐えられるものにしておくといいでしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

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

承知しました!

if (set1.contains(i) && set2.contains(i)) {
answer.push_back(i);
}
}
return answer;
}
};
15 changes: 15 additions & 0 deletions 0349-intersection-of-two-arrays/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> set_of_nums1(nums1.begin(), nums1.end());
unordered_set<int> answer;

for (int num2 : nums2) {
if (set_of_nums1.count(num2)) {
answer.insert(num2);
}
}

return vector<int>(answer.begin(), answer.end());
}
};
15 changes: 15 additions & 0 deletions 0349-intersection-of-two-arrays/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> set_of_nums1(nums1.begin(), nums1.end());
unordered_set<int> answer;

for (int num2 : nums2) {
if (set_of_nums1.count(num2)) {
answer.insert(num2);
}
}

return vector<int>(answer.begin(), answer.end());
}
};