Skip to content
Open
Show file tree
Hide file tree
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 Jul 23, 2024
7ec5886
Update 2_NumberOfIslands.txt
hmintusn Jul 23, 2024
6130412
Add files via upload
hmintusn Jul 27, 2024
b4cc797
Delete Topic1_Arrays/Day2207/Day2207_Tuan directory
hmintusn Jul 27, 2024
70d148d
Delete Topic1_Arrays/Day2307/Day2307_Tuan directory
hmintusn Jul 27, 2024
ad715e7
first init
hmintusn Jul 27, 2024
1e3ada1
Add files via upload
hmintusn Aug 3, 2024
eab714d
b1_linkedlist
hmintusn Aug 5, 2024
6589dac
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn Aug 5, 2024
8968e73
Merge branch 'tuan_dev' of https://github.com/ligmaDbolzz/AlgorithmGr…
hmintusn Aug 5, 2024
aa9ec43
some update
hmintusn Aug 5, 2024
3a4a52b
problem 2
hmintusn Aug 6, 2024
5613347
update time complexity of b2
hmintusn Aug 6, 2024
f20bae1
b3_linkedlist
hmintusn Aug 7, 2024
ab1bcb5
b4_linkedlist
hmintusn Aug 8, 2024
71d484f
b6_linkedlist
hmintusn Aug 9, 2024
c74990c
b7_linkedlist
hmintusn Aug 9, 2024
e97d622
b5_linkedlist
hmintusn Aug 10, 2024
50bf27c
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn Aug 13, 2024
57ced6d
b1_StackQueue
hmintusn Aug 13, 2024
552931e
b2_StackQueue + update file extension .cpp
hmintusn Aug 14, 2024
e6a8766
b3_StackQueue
hmintusn Aug 15, 2024
94650e7
b4_StackQueue
hmintusn Aug 15, 2024
f2a70d0
b5_StackQueue
hmintusn Aug 16, 2024
3a99a5d
b1_arrays_2407
hmintusn Aug 16, 2024
3a09cfa
b8_StackQueue
hmintusn Aug 18, 2024
3f60b91
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn Aug 19, 2024
1700b6b
b7_StackQueue
hmintusn Aug 19, 2024
63edba3
Merge branch 'tuan_dev' of https://github.com/ligmaDbolzz/AlgorithmGr…
hmintusn Aug 19, 2024
d63a313
r1
hmintusn Aug 20, 2024
48fad96
update_RecursionBacktrack
hmintusn Aug 20, 2024
028bf5e
r2_RecursionBacktrack
hmintusn Aug 21, 2024
fc3bfa5
update RB
hmintusn Aug 22, 2024
63692c3
b3_BR
hmintusn Aug 24, 2024
d468a81
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn Aug 27, 2024
e0ef704
b1_BiSearch
hmintusn Aug 27, 2024
4710b37
b2_BiSearch
hmintusn Aug 28, 2024
29ee43a
b3_BiSearch
hmintusn Aug 30, 2024
fef4eb4
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn Sep 3, 2024
a5eeaff
b1_Sorting
hmintusn Sep 4, 2024
1d4f597
b3_Sorting
hmintusn Sep 5, 2024
e20a908
b5b8_Sorting
hmintusn Sep 6, 2024
a47a7ad
b4_Sorting
hmintusn Sep 7, 2024
384f1b0
b9_SortList
hmintusn Sep 7, 2024
6b5ef76
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn Sep 10, 2024
f184c16
b4_BiSearch
hmintusn Sep 10, 2024
ad66e75
b1_HashTable
hmintusn Sep 10, 2024
9097a21
b3_HashTable
hmintusn Sep 10, 2024
93e8c0f
b7_HashTable
hmintusn Sep 12, 2024
8c3ac26
b5_BiSearch
hmintusn Sep 13, 2024
e87edff
r5_RecursionBacktrack
hmintusn Sep 13, 2024
b3c4656
Merge branch 'nguyenson2012:main' into tuan_dev
hmintusn Sep 19, 2024
3760215
b1_TreeDFSBFS
hmintusn Sep 19, 2024
a0a2d90
b2_TreeDFSBFS
hmintusn Sep 20, 2024
f4834b4
b4b5_TreeDFSBFS
hmintusn Sep 21, 2024
802b85c
b6_TreeDFSBFS
hmintusn Sep 22, 2024
f6f14b1
3sum
hmintusn Oct 10, 2024
2007c47
common char
hmintusn Oct 11, 2024
7fb1bd2
max subarray
hmintusn Oct 13, 2024
6676e7c
arr
hmintusn Oct 14, 2024
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
16 changes: 16 additions & 0 deletions Topic1_Arrays/Day2207/Day2207_Tuan/1_TwoSum.txt
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;
Copy link
Owner

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

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};
}
};
41 changes: 41 additions & 0 deletions Topic1_Arrays/Day2207/Day2207_Tuan/2_NumberOfIslands.txt
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;
Copy link
Owner

Choose a reason for hiding this comment

The 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 Topic1_Arrays/Day2207/Day2207_Tuan/3_MergeSortedArrays.txt
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 Topic1_Arrays/Day2307/Day2307_Tuan/1_RandomPickWithWeight.txt
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;
}
};
22 changes: 22 additions & 0 deletions Topic1_Arrays/Day2307/Day2307_Tuan/2_MergeIntervals.txt
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 Topic1_Arrays/Day2307/Day2307_Tuan/3_BestTimeToBuyAndSellStock.txt
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;
}
};
22 changes: 22 additions & 0 deletions Topic1_Arrays/Day2407/Day2407_Tuan/1_GroupAnagrams.txt
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;
}
};
38 changes: 38 additions & 0 deletions Topic1_Arrays/Day2407/Day2407_Tuan/3_3Sum.txt
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 Topic1_Arrays/Day2507/Day2507_Tuan/3_FindCommonCharacters.txt
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;
}
}
17 changes: 17 additions & 0 deletions Topic1_Arrays/Day2607/Day2607_Tuan/3_MaximumSubarray.txt
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;
}
}
17 changes: 17 additions & 0 deletions Topic1_Arrays/Day2707/2707_Tuan/3_MajorityElement.txt
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 Topic2_LinkedList/LinkedList_HMTuan/1_ReverseLinkedList.txt
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 Topic2_LinkedList/LinkedList_HMTuan/2_Merge2SortedLists.txt
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;
}
};
Loading