-
Notifications
You must be signed in to change notification settings - Fork 15
Tuan dev #9
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
hmintusn
wants to merge
60
commits into
nguyenson2012:main
Choose a base branch
from
hmintusn:tuan_dev
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
Tuan dev #9
Changes from all commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
cc396e7
Add files via upload
hmintusn 7ec5886
Update 2_NumberOfIslands.txt
hmintusn 6130412
Add files via upload
hmintusn b4cc797
Delete Topic1_Arrays/Day2207/Day2207_Tuan directory
hmintusn 70d148d
Delete Topic1_Arrays/Day2307/Day2307_Tuan directory
hmintusn ad715e7
first init
hmintusn 1e3ada1
Add files via upload
hmintusn eab714d
b1_linkedlist
hmintusn 6589dac
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn 8968e73
Merge branch 'tuan_dev' of https://github.com/ligmaDbolzz/AlgorithmGr…
hmintusn aa9ec43
some update
hmintusn 3a4a52b
problem 2
hmintusn 5613347
update time complexity of b2
hmintusn f20bae1
b3_linkedlist
hmintusn ab1bcb5
b4_linkedlist
hmintusn 71d484f
b6_linkedlist
hmintusn c74990c
b7_linkedlist
hmintusn e97d622
b5_linkedlist
hmintusn 50bf27c
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn 57ced6d
b1_StackQueue
hmintusn 552931e
b2_StackQueue + update file extension .cpp
hmintusn e6a8766
b3_StackQueue
hmintusn 94650e7
b4_StackQueue
hmintusn f2a70d0
b5_StackQueue
hmintusn 3a99a5d
b1_arrays_2407
hmintusn 3a09cfa
b8_StackQueue
hmintusn 3f60b91
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn 1700b6b
b7_StackQueue
hmintusn 63edba3
Merge branch 'tuan_dev' of https://github.com/ligmaDbolzz/AlgorithmGr…
hmintusn d63a313
r1
hmintusn 48fad96
update_RecursionBacktrack
hmintusn 028bf5e
r2_RecursionBacktrack
hmintusn fc3bfa5
update RB
hmintusn 63692c3
b3_BR
hmintusn d468a81
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn e0ef704
b1_BiSearch
hmintusn 4710b37
b2_BiSearch
hmintusn 29ee43a
b3_BiSearch
hmintusn fef4eb4
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn a5eeaff
b1_Sorting
hmintusn 1d4f597
b3_Sorting
hmintusn e20a908
b5b8_Sorting
hmintusn a47a7ad
b4_Sorting
hmintusn 384f1b0
b9_SortList
hmintusn 6b5ef76
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn f184c16
b4_BiSearch
hmintusn ad66e75
b1_HashTable
hmintusn 9097a21
b3_HashTable
hmintusn 93e8c0f
b7_HashTable
hmintusn 8c3ac26
b5_BiSearch
hmintusn e87edff
r5_RecursionBacktrack
hmintusn b3c4656
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn 3760215
b1_TreeDFSBFS
hmintusn a0a2d90
b2_TreeDFSBFS
hmintusn f4834b4
b4b5_TreeDFSBFS
hmintusn 802b85c
b6_TreeDFSBFS
hmintusn f6f14b1
3sum
hmintusn 2007c47
common char
hmintusn 7fb1bd2
max subarray
hmintusn 6676e7c
arr
hmintusn 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,16 @@ | ||
| //Complexity: O(n) | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| unordered_map<int, int> mp; | ||
| for(int i = 0; i < nums.size(); i++){ | ||
| if(mp.find(target - nums[i]) == mp.end()){ | ||
| mp[nums[i]] = i; | ||
| }else{ | ||
| return {mp[target-nums[i]], i}; | ||
| } | ||
| } | ||
| return {-1, -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,41 @@ | ||
| //Complexity: O(n*m) | ||
|
|
||
| class Solution { | ||
| public: | ||
| int numIslands(vector<vector<char>>& grid) { | ||
| queue<pair<int, int>> q; | ||
|
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. Add more meaningful variable name |
||
| int rows = grid.size(); | ||
| int cols = grid[0].size(); | ||
| int cnt = 0; | ||
| vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; | ||
| for(int i = 0; i < rows; i++){ | ||
| for(int j = 0; j < cols; j++){ | ||
| if(grid[i][j] == '1'){ | ||
| pair<int, int> p; | ||
| p.first = i; | ||
| p.second = j; | ||
| q.push(p); | ||
| grid[i][j] = '0'; | ||
| ++cnt; | ||
| cout << cnt << "\n"; | ||
| while(!q.empty()){ | ||
| pair<int, int> peek = q.front(); | ||
| q.pop(); | ||
| cout << peek.first << " " << peek.second << "\n"; | ||
| for(pair<int, int> dir : directions){ | ||
| int neighboor_x = peek.first + dir.first; | ||
| int neighboor_y = peek.second + dir.second; | ||
| if(neighboor_x >= 0 && neighboor_x < rows && neighboor_y >= 0 | ||
| && neighboor_y < cols && grid[neighboor_x][neighboor_y] == '1'){ | ||
| pair<int, int> neighboor = {neighboor_x, neighboor_y}; | ||
| q.push(neighboor); | ||
| grid[neighboor_x][neighboor_y] = '0'; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return cnt; | ||
| } | ||
| }; | ||
18 changes: 18 additions & 0 deletions
18
Topic1_Arrays/Day2207/Day2207_Tuan/3_MergeSortedArrays.txt
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,18 @@ | ||
| //Complexity: O(n+m) | ||
|
|
||
| class Solution { | ||
| public: | ||
| void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { | ||
| int i = m - 1, j = n - 1, k = n + m - 1; | ||
| while(j>=0){ | ||
| if(i >= 0 && nums1[i] > nums2[j]){ | ||
| nums1[k] = nums1[i]; | ||
| i--; k--; | ||
| } | ||
| else{ | ||
| nums1[k] = nums2[j]; | ||
| j--; k--; | ||
| } | ||
| } | ||
| } | ||
| }; |
30 changes: 30 additions & 0 deletions
30
Topic1_Arrays/Day2307/Day2307_Tuan/1_RandomPickWithWeight.txt
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,30 @@ | ||
| // Time complexity: O(N + log N) | ||
| // Using the interval between 2 index of cumulative sum to represent the Selection Possibility | ||
| class Solution { | ||
| private: | ||
| vector<int> cumulativeSum; | ||
| public: | ||
| Solution(vector<int>& w) { | ||
| // Calculate cumulative sum according to weight[i] | ||
| int sum = 0; | ||
| for(int weight : w){ | ||
| sum += weight; | ||
| cumulativeSum.push_back(sum); | ||
| } | ||
| } | ||
| // Using binary search to find the possibility of random weight | ||
| int pickIndex() { | ||
| // +1 at the end of randWeight to include the last element | ||
| int randWeight = rand() % cumulativeSum.back() + 1; | ||
| int low = 0, high = cumulativeSum.size()-1; | ||
| while(low < high){ | ||
| int mid = (low + high) / 2; | ||
| if(randWeight <= cumulativeSum[mid]){ | ||
| high = mid; | ||
| } else { | ||
| low = mid + 1; | ||
| } | ||
| } | ||
| return low; | ||
| } | ||
| }; |
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,22 @@ | ||
| //Complexity: O(n) | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<vector<int>> merge(vector<vector<int>>& intervals) { | ||
| sort(intervals.begin(), intervals.end()); | ||
|
|
||
| vector<vector<int>> merged; | ||
| vector<int> previous = intervals[0]; | ||
| for(int i = 1; i < intervals.size(); i++){ | ||
| if(previous[1] >= intervals[i][0]){ | ||
| previous[1] = max(previous[1], intervals[i][1]); | ||
| } else { | ||
| merged.push_back(previous); | ||
| previous = intervals[i]; | ||
| } | ||
| } | ||
| merged.push_back(previous); | ||
|
|
||
| return merged; | ||
| } | ||
| }; |
17 changes: 17 additions & 0 deletions
17
Topic1_Arrays/Day2307/Day2307_Tuan/3_BestTimeToBuyAndSellStock.txt
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,17 @@ | ||
| //Complexity: O(n) | ||
|
|
||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& prices) { | ||
| int length = prices.size(); | ||
| int buyPrice = prices[0]; | ||
| int profit = 0; | ||
| for(int i = 1; i < length; i++){ | ||
| if(prices[i] < buyPrice){ | ||
| buyPrice = prices[i]; | ||
| } | ||
| profit = max(profit, prices[i] - buyPrice); | ||
| } | ||
| return profit; | ||
| } | ||
| }; |
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,22 @@ | ||
| // Time complexity: O(n) | ||
| // Space complexity: O(n) - Using map | ||
| // Explanation: Because all the anagrams have the same sorted string. | ||
| // So, using map to store <sorted string, vector<string>>. | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| unordered_map<string, vector<string>> mp; | ||
| for(string str : strs){ | ||
| string word = str; | ||
| sort(word.begin(), word.end()); | ||
| mp[word].push_back(str); | ||
| } | ||
|
|
||
| vector<vector<string>> ans; | ||
| for(auto key : mp){ | ||
| ans.push_back(key.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,38 @@ | ||
| // Time complexity: O(n^2) | ||
| // Space complexity: O(1) | ||
| // Explanation: Using 1 pivot i and 2 pointer j and k | ||
| // Skip the duplicate elements | ||
|
|
||
| class Solution { | ||
| public List<List<Integer>> threeSum(int[] nums) { | ||
| List<List<Integer>> res = new ArrayList<>(); | ||
| Arrays.sort(nums); | ||
| int len = nums.length; | ||
| for(int i = 0; i < len - 2; i++){ | ||
| // Skip duplicate elements in nums array to avoid duplicate triplets | ||
| if(i > 0 && nums[i] == nums[i-1]) | ||
| continue; | ||
|
|
||
| // i is like the pivot | ||
| // j/k is left/right ptr of the rest interval | ||
| int j = i + 1, k = len - 1; | ||
|
|
||
| while(j < k){ | ||
| int sum = nums[i] + nums[j] + nums[k]; | ||
| if(sum > 0){ | ||
| --k; | ||
| }else if(sum < 0){ | ||
| ++j; | ||
| }else{ | ||
| res.add(Arrays.asList(nums[i], nums[j], nums[k])); | ||
| ++j; | ||
|
|
||
| // Avoid duplicate nums[j] if nums[j] == nums[j-1] | ||
| while(nums[j] == nums[j-1] && j < k) | ||
| ++j; | ||
| } | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
Topic1_Arrays/Day2507/Day2507_Tuan/3_FindCommonCharacters.txt
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 @@ | ||
| // Time complexity: O(n) | ||
| // Space complexity: O(n) - n: words.length | ||
| // Explanation: Using 2d array to store the frequency of each word | ||
|
|
||
| class Solution { | ||
| public List<String> commonChars(String[] words) { | ||
| int[][] freq = new int[words.length][26]; | ||
| for (int i = 0; i < words.length; i++) { | ||
| for (char c : words[i].toCharArray()) { | ||
| freq[i][c - 'a']++; | ||
| } | ||
| } | ||
| List<String> res = new ArrayList<>(); | ||
| for (int i = 'a'; i <= 'z'; i++) { | ||
| int min = Integer.MAX_VALUE; | ||
| for (int j = 0; j < freq.length; j++) { | ||
| min = Math.min(min, freq[j][i-'a']); | ||
| } | ||
| for (int j = 0; j < min; j++) { | ||
| res.add(String.valueOf((char) i)); | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
| } |
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,17 @@ | ||
| // Time complexity: O(n) | ||
| // Space complexity: O(n) | ||
| // Explanation: Using array tracking the current sum to compare the previous sum and | ||
| // Compare the previous sum and current num | ||
|
|
||
| class Solution { | ||
| public int maxSubArray(int[] nums) { | ||
| int res = nums[0]; | ||
| int[] currSum = new int[nums.length]; | ||
| currSum[0] = nums[0]; | ||
| for(int i = 1; i < nums.length; i++){ | ||
| currSum[i] = Math.max(nums[i], nums[i] + currSum[i-1]); | ||
| res = Math.max(currSum[i], res); | ||
| } | ||
| return res; | ||
| } | ||
| } |
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,17 @@ | ||
| // Time complexity: O(n) | ||
| // Space complexity: O(n) | ||
| // Explanation: Using hash map to store frequency | ||
|
|
||
| class Solution { | ||
| public int majorityElement(int[] nums) { | ||
| HashMap<Integer, Integer> hm = new HashMap<>(); | ||
| int res = 0; | ||
| for(int num : nums){ | ||
| hm.put(num, hm.getOrDefault(num, 0) + 1); | ||
| if(hm.get(num) > nums.length/2){ | ||
| res = num; | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
Topic2_LinkedList/LinkedList_HMTuan/1_ReverseLinkedList.txt
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,30 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * struct ListNode { | ||
| * int val; | ||
| * ListNode *next; | ||
| * ListNode() : val(0), next(nullptr) {} | ||
| * ListNode(int x) : val(x), next(nullptr) {} | ||
| ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
| }; | ||
| */ | ||
|
|
||
| // Time Complexity: O(n) | ||
| // Space Complexity: O(1) | ||
| // Explanation: Using *temp to store the next pointer of head(head->next). | ||
| // Because after that, the head->next will point to *rev pointer. | ||
| // Expected: rev <- 1 <- 2 <- 3 | ||
|
|
||
| class Solution { | ||
| public: | ||
| ListNode *reverseList(ListNode *head) { | ||
| ListNode *rev = nullptr; | ||
| while(head != nullptr){ | ||
| ListNode *temp = head->next; | ||
| head->next = rev; | ||
| rev = head; | ||
| head = temp; | ||
| } | ||
| return rev; | ||
| } | ||
| }; |
47 changes: 47 additions & 0 deletions
47
Topic2_LinkedList/LinkedList_HMTuan/2_Merge2SortedLists.txt
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,47 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * struct ListNode { | ||
| * int val; | ||
| * ListNode *next; | ||
| * ListNode() : val(0), next(nullptr) {} | ||
| * ListNode(int x) : val(x), next(nullptr) {} | ||
| * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
| * }; | ||
| */ | ||
|
|
||
| // Time complexity: O(n+m)- n: size of list1, m: size of list2 | ||
| // Space complexity: O(1) - The algorithm merges the lists by rearranging the 'next' pointers of node. | ||
| // It does not create a new nodes -> no use additional data structure for storing nodes. | ||
| // Explaination: Use a dummy node as starting point. | ||
| // Then traverse 2 linked list and pick the smallest element to push back at each iterate. | ||
|
|
||
| class Solution { | ||
| public: | ||
| ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { | ||
| // Dummy node to act as the starting point | ||
| ListNode dummy; | ||
| ListNode *tail = &dummy; | ||
|
|
||
| // Traverse both lists | ||
| while (list1 != nullptr && list2 != nullptr) { | ||
| if (list1->val <= list2->val) { | ||
| tail->next = list1; | ||
| list1 = list1->next; | ||
| } else { | ||
| tail->next = list2; | ||
| list2 = list2->next; | ||
| } | ||
| tail = tail->next; | ||
| } | ||
|
|
||
| // Attach the remaining nodes | ||
| if (list1 != nullptr) { | ||
| tail->next = list1; | ||
| } else { | ||
| tail->next = list2; | ||
| } | ||
|
|
||
| // Return the merged list, starting from dummy.next | ||
| return dummy.next; | ||
| } | ||
| }; |
Oops, something went wrong.
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.
Add the way you think to solve the problem, it will help you improve skill to explain your idea. It helps when you take the interview.
Please add more meaningful variable name