-
Notifications
You must be signed in to change notification settings - Fork 15
TrungHau 22/07 Algorithm #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
18fef7d
bd073db
3636029
5700f0a
1e3bd3a
cd7b8a8
b97fc78
c3a60ab
406d9fd
21d76c0
4cd907c
16517aa
fdd3ee4
36399bd
12b3a34
715d37a
c56562a
59c8c4c
50637a7
2ba3407
b9a0244
5dbeeec
c413164
c09dbf3
aa55ba6
4bb1278
7bc3208
45ade47
bd08c54
cd6857a
22b73dc
b6e2f4d
35281fd
bd0e507
fab457a
d3c8160
537d8ef
2fb134f
9a6ce73
a380035
0b2f5dd
4e737db
0cd9174
44e6c3c
8eab983
ce9a8cc
9abeab7
850f6f9
7fd5a2b
6fdd4ea
9683d1d
dc8222f
00031e2
d846228
07c9595
4748cd0
e29a0c5
e3f89d3
02fc5e0
92ed3f6
d227609
a095975
e95b759
f3be41d
55e02b5
caed617
3181bee
a513888
a027206
ed20d10
31ca668
3519da5
a2d378d
73de1d1
c6007f4
73dce2c
0ddedb4
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,22 @@ | ||
| //Using two loop to solve problem. That way is save memory so much but i think it isn't the best solution. | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| vector <int> TwoNumber; | ||
| for(int i = 0; i < nums.size(); i++) | ||
| { | ||
| for(int j = i + 1; j < nums.size(); j++) | ||
| { | ||
| if(nums[i] + nums[j] == target) | ||
| { | ||
| TwoNumber.push_back(i); | ||
| TwoNumber.push_back(j); | ||
| break; | ||
| } | ||
| } | ||
| if(TwoNumber.size() == 2) break; | ||
| } | ||
| return TwoNumber; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Using two loop to get each element in vector and check if this is '1', i will use Try function to delete all character beside that. | ||
| // But this is a common way and not quickly and not efficiently | ||
| class Solution { | ||
| public: | ||
| void Try(int i, int j, vector<vector<char>> &temp){ | ||
|
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. Choose meaningful function name |
||
| if(i + 1 < temp.size() && temp[i + 1][j] == '1') { | ||
| temp[i + 1][j] = '0'; | ||
| Try(i + 1, j, temp); | ||
| } | ||
| if(i - 1 >= 0 && temp[i - 1][j] == '1') { | ||
| temp[i - 1][j] = '0'; | ||
| Try(i - 1, j, temp); | ||
| } | ||
| if(j - 1 >= 0 && temp[i][j - 1] == '1') { | ||
| temp[i][j - 1] = '0'; | ||
| Try(i, j - 1, temp); | ||
| } | ||
| if(j + 1 < temp[i].size() && temp[i][j + 1] == '1') { | ||
| temp[i][j + 1] = '0'; | ||
| Try(i, j + 1, temp); | ||
| } | ||
| } | ||
|
|
||
| int numIslands(vector<vector<char>>& grid) { | ||
| int count = 0; | ||
| for(int i = 0; i < grid.size(); i++){ | ||
| for(int j = 0; j < grid[i].size(); j++){ | ||
| if(grid[i][j] == '1'){ | ||
| count ++; | ||
| Try(i, j, grid); | ||
| } | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Using the way same as Merge Sort to solve the problem. | ||
| // I think that is the popular solution. | ||
|
|
||
| class Solution { | ||
| public: | ||
| void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { | ||
| vector <int> temp_nums1(nums1); | ||
| int indexNums1 = 0, indexNums2 = 0, index_res = 0; | ||
| while(indexNums1 < m && indexNums2 < n) | ||
| { | ||
| if(temp_nums1[indexNums1] > nums2[indexNums2]){ | ||
| nums1[index_res] = nums2[indexNums2]; | ||
| indexNums2++; | ||
| } | ||
| else { | ||
| nums1[index_res] = temp_nums1[indexNums1]; | ||
| indexNums1++; | ||
| } | ||
| index_res++; | ||
| } | ||
| while(indexNums1 < m){ | ||
| nums1[index_res++] = temp_nums1[indexNums1++]; | ||
| } | ||
| while(indexNums2 < n){ | ||
| nums1[index_res++] = nums2[indexNums2++]; | ||
| } | ||
| } | ||
|
|
||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Time complexity: O(n * 3 + log(n)) | ||
|
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. Time complexity O(3n + log(n)) same as time complexity O(n) |
||
| // Memory compexity: (i don't know how to calculate) | ||
| // Using array to seperate rate. | ||
|
|
||
| class Solution { | ||
| public: | ||
| int sum = 0; | ||
| vector <double> percent; | ||
| Solution(vector<int>& w) { | ||
| srand(time(NULL)); | ||
| for(int i = 0; i < w.size(); i++){ | ||
| sum += w[i]; | ||
| } | ||
| for(int i = 0; i < w.size(); i++){ | ||
| percent.push_back(w[i] * 1.0 / (sum * 1.0) * 100.0); | ||
| } | ||
| for(int i = 1; i < percent.size(); i++){ | ||
| percent[i] =percent[i] + percent[i - 1]; | ||
| } | ||
|
|
||
| } | ||
| int pickIndex() { | ||
| double temp = (rand() % 100); | ||
| auto a = upper_bound(percent.begin(), percent.end(), temp); | ||
| return a - percent.begin(); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Time complexity: O(n) | ||
|
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. Time complexity here is O(n^2), not O(n) |
||
| // Memory complexity: | ||
| // Using two pointer to compare end previous and start next. If it satisfied about end previous is larger than start next. I will erase index that and update index present. But that is not the best effecient way. | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> merge(vector<vector<int>>& intervals) { | ||
| sort(intervals.begin(), intervals.end()); | ||
| vector<vector<int>> res; | ||
| for(int i = 0; i < intervals.size(); i++){ | ||
| for(int j = i + 1; j < intervals.size(); j++){ | ||
| if(intervals[i][1] >= intervals[j][0]){ | ||
| intervals[i][1] = max(intervals[j][1], intervals[i][1]); | ||
| intervals.erase(intervals.begin() + j); | ||
| j--; | ||
| } | ||
| else break; | ||
| } | ||
| res.push_back(intervals[i]); | ||
| } | ||
| return res; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Time complexity: O(n) | ||
|
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. Please include leetcode link of this question in the answer also |
||
| // Memory complexity: | ||
| // Description: I compare each element to find the space largest between two element in the array and update to res variable. | ||
|
|
||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& prices) { | ||
| int num_min = prices[0]; | ||
| int res = 0; | ||
| for(int i = 0; i < prices.size(); i++){ | ||
| if(num_min > prices[i]) num_min = prices[i]; | ||
| if(res < prices[i] - num_min) res = prices[i] - num_min; | ||
| } | ||
| return res; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Time complexity: O(n * log(n) + n) | ||
| // Memory complexity: | ||
| // With each string element, i sort all character in string and push it in map | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| vector<vector<string>> res; | ||
| unordered_map<string, vector<string>> anagramGroups; | ||
|
|
||
| for (const string &s : strs) { | ||
| string sortedStr = s; | ||
| sort(sortedStr.begin(), sortedStr.end()); | ||
| anagramGroups[sortedStr].emplace_back(s); | ||
| } | ||
|
|
||
| for (const auto& group : anagramGroups) { | ||
| res.emplace_back(group.second); | ||
| } | ||
| return res; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Time complexity: O(log(n)) | ||
| // Memory complexity: ... | ||
| // I has a way to solve that don't use sort. It is use two loop to find index max in the array but it isn't as effectively way as | ||
| // sort way. | ||
| class Solution { | ||
| public: | ||
| int findKthLargest(vector<int>& nums, int k) { | ||
| sort(nums.begin(), nums.end()); | ||
| return nums[nums.size() - k]; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Time complexity: O(n * log(n)) | ||
| // Memory complexity: ... | ||
| // Description: Using two pointer to find nums[j] and nums[k] and use set container to avoid duplicate. | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<vector<int>> threeSum(vector<int>& nums) { | ||
| vector <vector<int>> res; | ||
| set <pair<int, int>> check; | ||
| sort(nums.begin(), nums.end()); | ||
|
|
||
| for(int i = 0; i < nums.size(); i++){ | ||
| int j = i + 1; | ||
| int k = nums.size() - 1; | ||
| while(j < k){ | ||
| if(nums[i] + nums[j] + nums[k] == 0){ | ||
| check.insert(make_pair(max(nums[i], max(nums[j], nums[k])), min(nums[i], min(nums[j], nums[k])))); | ||
| j++; | ||
| k--; | ||
| } | ||
| else if(nums[i] + nums[j] + nums[k] > 0) | ||
| { | ||
| k--; | ||
| } else j++; | ||
| } | ||
| } | ||
| for(const auto& a : check){ | ||
| vector<int> temp{a.first, a.second, -(a.first + a.second)}; | ||
| res.emplace_back(temp); | ||
| } | ||
| return res; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Time complexity: O(n) | ||
| // Memory complexity: .... (i don't know to calulate) | ||
| // Description: Using two pointer to consider start and end. Choose pointer has smaller value to move. Moving pointer to point has higher value than previous pointer. And we continue loop until pointer left higher or equal than pointer right. | ||
| class Solution { | ||
| public: | ||
| int maxArea(vector<int>& height) { | ||
| int ptr_left = 0; | ||
| int ptr_right = height.size() - 1; | ||
| int max_water = 0; | ||
| while(ptr_left < ptr_right){ | ||
| int sum = min(height[ptr_left], height[ptr_right]) * (ptr_right - ptr_left); | ||
| if(height[ptr_left] < height[ptr_right]){ | ||
| int i = ptr_left + 1; | ||
| while(height[i] < height[ptr_left]){i++;} | ||
| ptr_left = i; | ||
| } | ||
| else { | ||
| int i = ptr_right - 1; | ||
| while(height[i] < height[ptr_right]){i--;} | ||
| ptr_right = i; | ||
| } | ||
| max_water = max(max_water, sum); | ||
| } | ||
| return max_water; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Time complexity: O(n^2) | ||
| // Memory compleity: ... | ||
| // Description: Using prefix array. | ||
|
|
||
| class Solution { | ||
| public: | ||
| int subarraySum(vector<int>& nums, int k) { | ||
| vector <int> sum(nums.size() + 1); | ||
| sum[0] = 0; | ||
| for(int i = 0; i < nums.size(); i++){ | ||
| sum[i + 1] = sum[i] + nums[i]; | ||
| // cout << sum[i + 1] << ' '; | ||
| } | ||
| int count = 0; | ||
| for(int i = 1; i < sum.size(); i++){ | ||
| for(int j = i - 1; j >= 0; j--){ | ||
| if(sum[j] == sum[i] - k) count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Time complexity: O(n ^ 3) | ||
| // Memory complexity: .... | ||
| // Description: Using loop to compare each element together. | ||
|
|
||
| #include <string> | ||
| class Solution { | ||
| public: | ||
| vector<string> commonChars(vector<string>& words) { | ||
| string a = words[0]; | ||
| string b = ""; | ||
| for(int i = 1; i < words.size(); i++){ | ||
| string b = words[i]; | ||
| string temp = a; | ||
| for(int j = 0; j < temp.length(); j++){ | ||
| bool has = false; | ||
| for(int k = 0; k < b.length(); k++){ | ||
| if(temp[j] == b[k]){ | ||
| b.erase(b.begin() + k); | ||
| has = true; | ||
| break; | ||
| } | ||
| } | ||
| if(has == false){ | ||
| temp.erase(temp.begin() + j); | ||
| j--; | ||
| } | ||
| } | ||
| a = temp; | ||
| if(a.length() == 0) break; | ||
| } | ||
| vector <string> res(a.length()); | ||
| if(a.length() != 0) { | ||
| for(int i = 0; i < a.length(); i++){ | ||
| res[i] = a[i]; | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Time complextiy: O(n) | ||
| // Memory complexity: | ||
| /* Description: Using prefix sum, let 0 as -1 and 1 as 1. Calculating sum at the each index, and update (sum + nums.size()) index. | ||
| */ | ||
|
|
||
|
|
||
| class Solution { | ||
| public: | ||
| int findMaxLength(vector<int>& nums) { | ||
| vector<int> arr(2*nums.size()+1, -2); | ||
|
|
||
| arr[nums.size()] = -1; | ||
| int ans = 0, count = 0; | ||
|
|
||
| for(int i = 0; i < nums.size(); i++){ | ||
| count += (nums[i] == 0 ? -1 : 1); | ||
| if(arr[count + nums.size()] >= -1){ | ||
| ans = max(ans, i-arr[count+nums.size()]); | ||
| }else{ | ||
| arr[count+nums.size()] = i; | ||
| } | ||
| } | ||
|
|
||
| return ans; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Time complexity: O(n * log(n)) | ||
| // Memory complexity: ... | ||
| // Description: Using two loop to find index to convert...According to https://www.geeksforgeeks.org/next-permutation/. | ||
|
|
||
| class Solution { | ||
| public: | ||
| public: | ||
| void nextPermutation(vector<int>& nums) { | ||
| int n=nums.size(); | ||
| if(n==1){ | ||
| return; | ||
| } | ||
| int temp; | ||
| for(int i=n-1;i>=0;i--){ | ||
| int flag=0; | ||
| for(int j=i+1;j<n;j++){ | ||
| if(nums[j]>nums[i]){ | ||
| flag=1; | ||
| temp=nums[j]; | ||
| nums[j]=nums[i]; | ||
| nums[i]=temp; | ||
| break; | ||
| } | ||
| } | ||
| if(flag==0){ | ||
| sort(nums.begin()+i,nums.end()); | ||
| }else{ | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Time complexity: O(n) | ||
| // Memory complexity: O(1) | ||
| // Description: Using a variable to save value of subarray which has max value at index present. And comparing with max_ variable to update that. | ||
| class Solution { | ||
| public: | ||
| int maxSubArray(vector<int>& nums) { | ||
| int max_ = nums[0]; | ||
| int sum_previous = nums[0]; | ||
| for(int i = 1; i < nums.size(); i++){ | ||
| if(sum_previous + nums[i] < nums[i]){ | ||
| sum_previous = nums[i]; | ||
| } | ||
| else { | ||
| sum_previous += nums[i]; | ||
| } | ||
| max_ = max(sum_previous, max_); | ||
| } | ||
| return max_; | ||
| } | ||
| }; |
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.
Add esimtate for time and space complexity