-
-
Notifications
You must be signed in to change notification settings - Fork 337
[hwi-middle] WEEK 08 solutions #2570
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| // Definition for a Node. | ||
| 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; | ||
| } | ||
| }; | ||
| */ | ||
|
|
||
| class Solution { | ||
| public: | ||
| Node* cloneGraph(Node* node) { | ||
| if (node == nullptr) | ||
| { | ||
| return node; | ||
| } | ||
|
|
||
| if (nodeMap.contains(node)) | ||
| { | ||
| return nodeMap[node]; | ||
| } | ||
|
|
||
| Node* copied = new Node(node->val); | ||
| nodeMap[node] = copied; | ||
| for (auto n : node->neighbors) | ||
| { | ||
| copied->neighbors.push_back(cloneGraph(n)); | ||
| } | ||
|
|
||
| return copied; | ||
| } | ||
|
|
||
| private: | ||
| unordered_map<Node*, Node*> nodeMap; | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution { | ||
| public: | ||
| int longestCommonSubsequence(string text1, string text2) { | ||
| vector<int> d(text1.length(), 0); | ||
| int ans = 0; | ||
|
|
||
| for (auto c : text2) | ||
| { | ||
| int cur = 0; | ||
| for (int i = 0; i < d.size(); i++) | ||
| { | ||
| if (cur < d[i]) | ||
| { | ||
| cur = d[i]; | ||
| } | ||
| else if (c == text1[i]) | ||
| { | ||
| d[i] = cur + 1; | ||
| ans = max(ans, cur + 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ans; | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| class Solution { | ||
| public: | ||
| int characterReplacement(string s, int k) { | ||
| unordered_map<char, int> freq; | ||
| int res = 0; | ||
| int i = 0; | ||
| int maxFreq = 0; | ||
|
|
||
| for (int j = 0; j < s.size(); j++) | ||
| { | ||
| freq[s[j]]++; | ||
| maxFreq = max(maxFreq, freq[s[j]]); | ||
|
|
||
| while ((j - i + 1) - maxFreq > k) | ||
| { | ||
| freq[s[i]]--; | ||
| i++; | ||
| } | ||
|
|
||
| res = max(res, j - i + 1); | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| class Solution { | ||
| public: | ||
| int countSubstrings(string s) { | ||
| int n = s.size(); | ||
| if (n == 0) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| vector<vector<int>> dp(n, vector<int>(n)); | ||
|
|
||
| int ans = n; | ||
| for (int i = 0; i < n; ++i) | ||
| { | ||
| dp[i][i] = true; | ||
| } | ||
|
|
||
| for (int i = 0; i < n - 1; ++i) | ||
| { | ||
| dp[i][i + 1] = (s[i] == s[i + 1]); | ||
| ans += dp[i][i + 1]; | ||
| } | ||
|
|
||
| for (int l = 3; l <= n; ++l) | ||
| { | ||
| for (int i = 0, j = i + l - 1; j < n; ++i, ++j) | ||
| { | ||
| dp[i][j] = dp[i + 1][j - 1] && (s[i] == s[j]); | ||
| ans += dp[i][j]; | ||
| } | ||
| } | ||
|
|
||
| return ans; | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution { | ||
| public: | ||
| int reverseBits(int n) { | ||
| int r = 0; | ||
|
|
||
| for (int i = 0; i <= 31; ++i) | ||
| { | ||
| int pot = 1 << i; | ||
| if ((n & pot) != 0) | ||
| { | ||
| r |= (1 << (31 - i)); | ||
| } | ||
| } | ||
|
|
||
| return r; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석