-
Notifications
You must be signed in to change notification settings - Fork 15
Create 1.txt #7
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
Tungcutenhoem
wants to merge
21
commits into
nguyenson2012:main
Choose a base branch
from
Tungcutenhoem:LeetCode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Create 1.txt #7
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5a3e3fe
Create 1.txt
Tungcutenhoem 186e51c
Update 1.txt
Tungcutenhoem 6bf6a13
Create Tung
Tungcutenhoem fe2962a
Delete Topic1_Arrays/Day2307/Tung
Tungcutenhoem abde8e6
Create 1.txt
Tungcutenhoem 4542d59
Create 2.txt
Tungcutenhoem 7354fe1
Update 2.txt
Tungcutenhoem 5847f52
Update 2.txt
Tungcutenhoem e3f1f0c
Create 3.txt
Tungcutenhoem 16d2540
Update 3.txt
Tungcutenhoem 213c228
Create 1.txt
Tungcutenhoem 28b6182
Update 1.txt
Tungcutenhoem f25c384
Create 2.txt
Tungcutenhoem 4563e32
Update 2.txt
Tungcutenhoem 26d58a5
Create 3.txt
Tungcutenhoem d29ff89
Update 3.txt
Tungcutenhoem 349bb9b
Create 1.txt
Tungcutenhoem 8594a1a
Update 1.txt
Tungcutenhoem 6a5790e
Create 2.txt
Tungcutenhoem d4aa29d
Update 2.txt
Tungcutenhoem 3da85c1
Create 3.txt
Tungcutenhoem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Description: Use two Loop | ||
| // Time complexity: O(n ^ 2) | ||
| // Memory complexity: O(1) | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| vector<int> kq ; | ||
| for(int i = 0 ; i < nums.size() - 1 ; i++) | ||
| { | ||
| for(int j = i + 1 ; j < nums.size() ; j++) | ||
| { | ||
| if(nums[i] + nums[j] == target) | ||
| { | ||
| kq.push_back(i); | ||
| kq.push_back(j); | ||
| return kq; | ||
| } | ||
| } | ||
| } | ||
| return kq; | ||
|
|
||
| } | ||
| }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Description: Use vector and loop: | ||
| + First, Add the first element in vector intervals to the new vector; | ||
| + Second, Use loop and check the case to save result | ||
| // Time complexity: O (n log(n)) | ||
| // Memory complexity: O (n) | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> merge(vector<vector<int>>& intervals) { | ||
|
Owner
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. Good! |
||
| sort(intervals.begin() , intervals.end()); | ||
|
|
||
| vector<vector<int>> merge; | ||
|
|
||
| merge.push_back(intervals[0]); | ||
|
|
||
| for(int i = 1 ; i < intervals.size() ; i++) | ||
| { | ||
| if(merge.back()[1] >= intervals[i][0]) | ||
| { | ||
| merge.back()[1] = max(merge.back()[1] , intervals[i][1]); | ||
| } | ||
| else | ||
| { | ||
| merge.push_back(intervals[i]); | ||
| } | ||
| } | ||
| return merge; | ||
|
|
||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Desciption: Use loop : | ||
| + if prices[i] > price-min . Update price_min = prices[i]. Else update result. | ||
| // Time complexity: O(n) | ||
| // Memory complexity: O(1) | ||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& prices) { | ||
| if(prices.size() == 1) | ||
| { | ||
| return 0; | ||
| } | ||
| int price_min = prices[0]; | ||
| int result = 0; | ||
| for(int i = 0 ; i < prices.size() ; i++) | ||
| { | ||
| if(price_min > prices[i]) | ||
| { | ||
| price_min = prices[i]; | ||
| } | ||
| else | ||
| { | ||
| result = max(result , prices[i] - price_min); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Description: Use map and loop : | ||
| + First: count char of stings. | ||
| + Solve: | ||
| // Time complexity: O(n * k log(k)) | ||
| // Memory complexity: O(n * k) | ||
| class Solution { | ||
| public: | ||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| map<string, vector<string> >mp; | ||
|
|
||
| for(auto str:strs){ | ||
| string s = str; | ||
| sort(s.begin(), s.end()); | ||
| mp[s].push_back(str); | ||
| } | ||
|
|
||
| vector<vector<string>> ans; | ||
| for(auto it = mp.begin(); it != mp.end(); it++){ | ||
| ans.push_back(it->second); | ||
| } | ||
| return ans; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Description: Use priority_queue and do it have't more k elements. | ||
| // Time complexity: O(n * log(k)). | ||
| // Memory complexity: O(k). | ||
| class Solution { | ||
| public: | ||
| int findKthLargest(vector<int>& nums, int k) { | ||
| priority_queue<int , vector<int> , greater<int>> pq; | ||
| for(int i = 0 ; i < nums.size() ; i++) | ||
| { | ||
| pq.push(nums[i]); | ||
| if(pq.size() > k) | ||
| { | ||
| pq.pop(); | ||
| } | ||
| } | ||
| return pq.top(); | ||
| return 0; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Description: Use pointer left and right. | ||
| // Time complexity: O(n ^ 2) | ||
| // Memory complexity: O(k) | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> threeSum(vector<int>& nums) { | ||
| vector<vector<int>> result; | ||
| sort(nums.begin(), nums.end()); | ||
| int n = nums.size(); | ||
|
|
||
| for (int i = 0; i < n - 2; ++i) { | ||
| if (i > 0 && nums[i] == nums[i - 1]) { | ||
| continue; | ||
| } | ||
|
|
||
| int left = i + 1; | ||
| int right = n - 1; | ||
|
|
||
| while (left < right) { | ||
| int sum = nums[i] + nums[left] + nums[right]; | ||
| if (sum == 0) { | ||
| result.push_back({nums[i], nums[left], nums[right]}); | ||
|
|
||
| while (left < right && nums[left] == nums[left + 1]) { | ||
| ++left; | ||
| } | ||
| while (left < right && nums[right] == nums[right - 1]) { | ||
| --right; | ||
| } | ||
| ++left; | ||
| --right; | ||
| } else if (sum < 0) { | ||
| ++left; | ||
| } else { | ||
| --right; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Description: Use pointer left and right. | ||
| // Time complexity: O(n). | ||
| // Memory complexity: O(1). | ||
| class Solution { | ||
| public: | ||
| int maxArea(vector<int>& height) { | ||
| int result = 0; | ||
| int left = 0 ; | ||
| int right = height.size()-1; | ||
| while(left < right ) | ||
| { | ||
| int height_c = min(height[left] , height[right]); | ||
| int wight = right - left; | ||
| int area = wight * height_c; | ||
| result = max(result , area); | ||
| if(height[left] < height[right]) | ||
| { | ||
| left++; | ||
| } | ||
| else | ||
| { | ||
| right--; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Description: Use unordered_map. | ||
| // Time complexity: O(n). | ||
| // Memory complexity: O(n). | ||
| #include <vector> | ||
| #include <unordered_map> | ||
| using namespace std; | ||
|
|
||
| class Solution { | ||
| public: | ||
| int subarraySum(vector<int>& nums, int k) { | ||
| unordered_map<int, int> prefixSumCount; | ||
| int sum = 0; | ||
| int result = 0; | ||
|
|
||
| prefixSumCount[0] = 1; | ||
|
|
||
| for (int num : nums) { | ||
| sum += num; | ||
| if (prefixSumCount.find(sum - k) != prefixSumCount.end()) { | ||
|
|
||
| result += prefixSumCount[sum - k]; | ||
| } | ||
| prefixSumCount[sum]++; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| In another case: use 2 loop: | ||
| // Time complexity: O(n ^ 2). | ||
| // Memory complexity: O(1). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Description: Use ASCII and count fred of alphal. | ||
| // Time complexity: O(n * m). | ||
| // Memory complexity: O(n * m). | ||
| class Solution { | ||
| public: | ||
| vector<string> commonChars(vector<string>& words) { | ||
| vector<string> result; | ||
| vector<int> min_fred(26 , 1000); | ||
| for(string word : words) | ||
| { | ||
| vector<int> fred(26 , 0); | ||
| for(char c : word) | ||
| { | ||
| fred[c - 'a']++; | ||
| } | ||
| for(int i = 0 ; i < 26 ; i++) | ||
| { | ||
| min_fred[i] = min(min_fred[i] , fred[i]); | ||
| } | ||
| } | ||
| for(int i = 0 ; i < 26 ; i++) | ||
| { | ||
| while(min_fred[i] > 0) | ||
| { | ||
| result.push_back(string(1 , 'a' + i)); | ||
| min_fred[i]--; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Use English variable name. Sample "result"
Try to find better solution instead of brute force solution