diff --git a/Topic1_Arrays/Day2207/TrungHau/1.txt b/Topic1_Arrays/Day2207/TrungHau/1.txt new file mode 100644 index 0000000..f825bcf --- /dev/null +++ b/Topic1_Arrays/Day2207/TrungHau/1.txt @@ -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 twoSum(vector& nums, int target) { + vector 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; + } +}; diff --git a/Topic1_Arrays/Day2207/TrungHau/2.txt b/Topic1_Arrays/Day2207/TrungHau/2.txt new file mode 100644 index 0000000..87e950f --- /dev/null +++ b/Topic1_Arrays/Day2207/TrungHau/2.txt @@ -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> &temp){ + 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>& 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; + } +}; diff --git a/Topic1_Arrays/Day2207/TrungHau/3.txt b/Topic1_Arrays/Day2207/TrungHau/3.txt new file mode 100644 index 0000000..ace7d3b --- /dev/null +++ b/Topic1_Arrays/Day2207/TrungHau/3.txt @@ -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& nums1, int m, vector& nums2, int n) { + vector 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++]; + } + } + +}; diff --git a/Topic1_Arrays/Day2307/TrungHau/1.txt b/Topic1_Arrays/Day2307/TrungHau/1.txt new file mode 100644 index 0000000..f5fdc6a --- /dev/null +++ b/Topic1_Arrays/Day2307/TrungHau/1.txt @@ -0,0 +1,27 @@ +// Time complexity: O(n * 3 + log(n)) +// Memory compexity: (i don't know how to calculate) +// Using array to seperate rate. + +class Solution { +public: + int sum = 0; + vector percent; + Solution(vector& 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(); + } +}; diff --git a/Topic1_Arrays/Day2307/TrungHau/2.txt b/Topic1_Arrays/Day2307/TrungHau/2.txt new file mode 100644 index 0000000..6fb2d4a --- /dev/null +++ b/Topic1_Arrays/Day2307/TrungHau/2.txt @@ -0,0 +1,22 @@ +// Time complexity: 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> merge(vector>& intervals) { + sort(intervals.begin(), intervals.end()); + vector> 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; + } +}; diff --git a/Topic1_Arrays/Day2307/TrungHau/3.txt b/Topic1_Arrays/Day2307/TrungHau/3.txt new file mode 100644 index 0000000..a1d5aff --- /dev/null +++ b/Topic1_Arrays/Day2307/TrungHau/3.txt @@ -0,0 +1,16 @@ +// Time complexity: O(n) +// 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& 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; + } +}; diff --git a/Topic1_Arrays/Day2407/TrungHau/1.txt b/Topic1_Arrays/Day2407/TrungHau/1.txt new file mode 100644 index 0000000..6e3755f --- /dev/null +++ b/Topic1_Arrays/Day2407/TrungHau/1.txt @@ -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> groupAnagrams(vector& strs) { + vector> res; + unordered_map> 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; + } +}; diff --git a/Topic1_Arrays/Day2407/TrungHau/2.txt b/Topic1_Arrays/Day2407/TrungHau/2.txt new file mode 100644 index 0000000..22b0567 --- /dev/null +++ b/Topic1_Arrays/Day2407/TrungHau/2.txt @@ -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& nums, int k) { + sort(nums.begin(), nums.end()); + return nums[nums.size() - k]; + } +}; diff --git a/Topic1_Arrays/Day2407/TrungHau/3.txt b/Topic1_Arrays/Day2407/TrungHau/3.txt new file mode 100644 index 0000000..d891695 --- /dev/null +++ b/Topic1_Arrays/Day2407/TrungHau/3.txt @@ -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> threeSum(vector& nums) { + vector > res; + set > 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 temp{a.first, a.second, -(a.first + a.second)}; + res.emplace_back(temp); + } + return res; + } +}; diff --git a/Topic1_Arrays/Day2507/TrungHau/1.txt b/Topic1_Arrays/Day2507/TrungHau/1.txt new file mode 100644 index 0000000..5ec823a --- /dev/null +++ b/Topic1_Arrays/Day2507/TrungHau/1.txt @@ -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& 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; + } +}; diff --git a/Topic1_Arrays/Day2507/TrungHau/2.txt b/Topic1_Arrays/Day2507/TrungHau/2.txt new file mode 100644 index 0000000..91dc6ae --- /dev/null +++ b/Topic1_Arrays/Day2507/TrungHau/2.txt @@ -0,0 +1,22 @@ +// Time complexity: O(n^2) +// Memory compleity: ... +// Description: Using prefix array. + +class Solution { +public: + int subarraySum(vector& nums, int k) { + vector 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; + } +}; diff --git a/Topic1_Arrays/Day2507/TrungHau/3.txt b/Topic1_Arrays/Day2507/TrungHau/3.txt new file mode 100644 index 0000000..5d0cd5e --- /dev/null +++ b/Topic1_Arrays/Day2507/TrungHau/3.txt @@ -0,0 +1,39 @@ +// Time complexity: O(n ^ 3) +// Memory complexity: .... +// Description: Using loop to compare each element together. + +#include +class Solution { +public: + vector commonChars(vector& 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 res(a.length()); + if(a.length() != 0) { + for(int i = 0; i < a.length(); i++){ + res[i] = a[i]; + } + } + return res; + } +}; diff --git a/Topic1_Arrays/Day2607/TrungHau/1.txt b/Topic1_Arrays/Day2607/TrungHau/1.txt new file mode 100644 index 0000000..0025779 --- /dev/null +++ b/Topic1_Arrays/Day2607/TrungHau/1.txt @@ -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& nums) { + vector 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; + } +}; diff --git a/Topic1_Arrays/Day2607/TrungHau/2.txt b/Topic1_Arrays/Day2607/TrungHau/2.txt new file mode 100644 index 0000000..c5dc96d --- /dev/null +++ b/Topic1_Arrays/Day2607/TrungHau/2.txt @@ -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& 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;jnums[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; + } + } + } +}; diff --git a/Topic1_Arrays/Day2607/TrungHau/3.txt b/Topic1_Arrays/Day2607/TrungHau/3.txt new file mode 100644 index 0000000..71baddd --- /dev/null +++ b/Topic1_Arrays/Day2607/TrungHau/3.txt @@ -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& 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_; + } +}; diff --git a/Topic1_Arrays/Day2707/TrungHau/1.txt b/Topic1_Arrays/Day2707/TrungHau/1.txt new file mode 100644 index 0000000..343af90 --- /dev/null +++ b/Topic1_Arrays/Day2707/TrungHau/1.txt @@ -0,0 +1,23 @@ +// Time complexity: O(nlog(n)) +// Memory complexity: O(1) +// Desciption: Using sort to solve this problem. + +class Solution { +public: + int longestConsecutive(vector& nums) { + if(nums.size() == 0) return 0; + sort(nums.begin(), nums.end()); + int count = 1; + int max_ = 1; + for(int i = 1; i < nums.size(); i++){ + if(nums[i] == nums[i - 1] + 1){ + count++; + max_ = max(max_, count); + } + else { + if(nums[i] != nums[i - 1]) count = 1; + } + } + return max_; + } +}; diff --git a/Topic1_Arrays/Day2707/TrungHau/2.txt b/Topic1_Arrays/Day2707/TrungHau/2.txt new file mode 100644 index 0000000..7ad624c --- /dev/null +++ b/Topic1_Arrays/Day2707/TrungHau/2.txt @@ -0,0 +1,30 @@ +// Time complexity: O(nlog(n)) +// Space complexity: (3n) +// Description: Count the frequently of each number and fill to temp container and compare it. push to save container. +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + vector save; + vector > temp; + sort(nums.begin(), nums.end()); + int count = 1; + for(int i = 1; i < nums.size(); i++){ + if(nums[i] == nums[i - 1]){ + count++; + } + else{ + temp.push_back(make_pair(count, nums[i - 1])); + count = 1; + } + } + temp.push_back(make_pair(count, nums[nums.size() - 1])); + + sort(temp.begin(), temp.end()); + + for(int i = 0; i < k; i++){ + save.push_back(temp[temp.size() - 1- i].second); + } + + return save; + } +}; diff --git a/Topic1_Arrays/Day2707/TrungHau/3.txt b/Topic1_Arrays/Day2707/TrungHau/3.txt new file mode 100644 index 0000000..567816f --- /dev/null +++ b/Topic1_Arrays/Day2707/TrungHau/3.txt @@ -0,0 +1,21 @@ +// Time complexity: O(n) +// Space complexity: O(1) +// Description: Creating a count variable to count the element. Because majority element appear more than n / 2. + +class Solution { +public: + int majorityElement(vector& nums) { + int res = 0, count = 0; + for(int i = 0; i < nums.size(); i++){ + if(count > 0){ + if(nums[i] == res) count++; + else count--; + } + else { + res = nums[i]; + count = 1; + } + } + return res; + } +}; diff --git a/Topic1_Arrays/Day2807/TrungHau/1.txt b/Topic1_Arrays/Day2807/TrungHau/1.txt new file mode 100644 index 0000000..70438e7 --- /dev/null +++ b/Topic1_Arrays/Day2807/TrungHau/1.txt @@ -0,0 +1,36 @@ +// Time complexity: O(n) +// Space complexity: O(1); +// Description: Consider each instance. Here below code: + +class Solution { +public: + int bagOfTokensScore(vector& tokens, int power) { + sort(tokens.begin(), tokens.end()); + int beginPtr = 0, endPtr = tokens.size() - 1; + int point = 0, max_ = 0; + while(beginPtr <= endPtr){ + if(point > 0){ + if(power >= tokens[beginPtr]){ + power -= tokens[beginPtr]; + point++; + beginPtr++; + } + else{ + point--; + power = power + tokens[endPtr]; + endPtr--; + } + } + else{ + if(power >= tokens[beginPtr]){ + power -= tokens[beginPtr]; + point++; + beginPtr++; + } + else break; + } + max_ = max(max_, point); + } + return max_; + } +}; diff --git a/Topic1_Arrays/Day2807/TrungHau/2.txt b/Topic1_Arrays/Day2807/TrungHau/2.txt new file mode 100644 index 0000000..f5d83d2 --- /dev/null +++ b/Topic1_Arrays/Day2807/TrungHau/2.txt @@ -0,0 +1,24 @@ +// Time complexity: O(nlog(n)) +// Space complexity: .. +// Description: Using.... + +class Solution { +public: + bool checkSubarraySum(vector& nums, int k) { + vector sum(nums.size ()); + map index; + sum[0] = nums[0]; + index[sum[0] % k] = 0; + for(int i = 1; i < nums.size(); i++){ + sum[i] = sum[i - 1] + nums[i]; + if(sum[i] % k == 0) return true; + if(index.find(sum[i] % k) == index.end()){ + index[sum[i] % k] = i; + } + else{ + if(i - index[sum[i] % k] >= 2) return true; + } + } + return false; + } +}; diff --git a/Topic1_Arrays/Day2807/TrungHau/3.txt b/Topic1_Arrays/Day2807/TrungHau/3.txt new file mode 100644 index 0000000..b0fa9f4 --- /dev/null +++ b/Topic1_Arrays/Day2807/TrungHau/3.txt @@ -0,0 +1,32 @@ +// Time complexity: O(nlog(n)) +// Space complexity: O(n) +// Description: Using...... + +class Solution { +public: + vector deckRevealedIncreasing(vector& deck) { + sort(deck.begin(), deck.end()); + int i = 0; + vector save(deck.size(), 0); + bool check = true; + int j = 0; + while(j < deck.size()){ + if(save[i] == 0) + { + if(check == true){ + save[i] = deck[j]; + j++; + check = false; + } + else{ + check = true; + } + } + i++; + if(i == save.size()){ + i = 0; + } + } + return save; + } +}; diff --git a/Topic2_LinkedList/TrungHau/1.txt b/Topic2_LinkedList/TrungHau/1.txt new file mode 100644 index 0000000..8e36205 --- /dev/null +++ b/Topic2_LinkedList/TrungHau/1.txt @@ -0,0 +1,28 @@ +// Time complexity: O(n) +// Space complexity: O(n) +// Description: Using a new ListNode to save new Node. And combinate push Node to new ListNode. +/** + * 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) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + ListNode* head_temp = NULL; + ListNode* temp = head; + + while(temp != NULL) + { + ListNode* newNode = new ListNode(temp->val, head_temp); + head_temp = newNode; + temp = temp->next; + } + return head_temp; + } +}; diff --git a/Topic2_LinkedList/TrungHau/2.txt b/Topic2_LinkedList/TrungHau/2.txt new file mode 100644 index 0000000..86f704b --- /dev/null +++ b/Topic2_LinkedList/TrungHau/2.txt @@ -0,0 +1,60 @@ +// Time complexity: O(2n) +// Space complexity: O(n) +// Description: Create 1 new ListNode to merge 2 ListNode. + +/** + * 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) {} + * }; + */ +class Solution { +public: + ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { + ListNode* head1 = list1; + ListNode* head2 = list2; + ListNode* a = new ListNode; + a->next = nullptr; + ListNode* head = a; + ListNode *temp = head; + while(head1 != NULL && head2 != NULL) + { + ListNode* newNode = new ListNode; + if(head1->val < head2->val) + { + newNode->val = head1->val; + head1 = head1->next; + } + else + { + newNode->val = head2->val; + head2 = head2->next; + } + temp->next = newNode; + temp = temp->next; + } + while(head1 != NULL) + { + ListNode *newNode = new ListNode; + newNode->next = nullptr; + newNode->val = head1->val; + temp->next = newNode; + temp = temp->next; + head1 = head1->next; + } + while(head2 != NULL) + { + ListNode *newNode = new ListNode; + newNode->next = nullptr; + newNode->val = head2->val; + temp->next = newNode; + temp = temp->next; + head2 = head2->next; + } + return head->next; + } +}; diff --git a/Topic2_LinkedList/TrungHau/3.txt b/Topic2_LinkedList/TrungHau/3.txt new file mode 100644 index 0000000..e98857e --- /dev/null +++ b/Topic2_LinkedList/TrungHau/3.txt @@ -0,0 +1,55 @@ +// Time complexity: O(3n) +// Space complexity: O(n) +// Description: Seperate ListNode to two equally part. Reverse behind part. Add to each main Node +/** + * 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) {} + * }; + */ +class Solution { +public: + void reorderList(ListNode* head) { + ListNode* first = head; + ListNode* second = head; + ListNode* temp = head; + while(second->next != NULL){ + temp = first; + if(first->next != NULL) first = first->next; + if(second->next != NULL) + { + second = second->next; + if(second->next != NULL) + second = second->next; + } + } + if(first == second) return; + temp->next = NULL; + ListNode* newNode = NULL; + while(first != NULL){ + ListNode* tempNode = new ListNode(first->val); + if(newNode == NULL){ + newNode = tempNode; + } + else{ + tempNode->next = newNode; + newNode = tempNode; + } + first = first->next; + } + ListNode* currentNode = head; + while(currentNode != NULL && newNode != NULL) + { + ListNode* tempNode = new ListNode(newNode->val, currentNode->next); + currentNode->next = tempNode; + currentNode = currentNode->next; + if(currentNode->next != NULL) currentNode = currentNode->next; + newNode = newNode->next; + } + + } +}; diff --git a/Topic2_LinkedList/TrungHau/4.txt b/Topic2_LinkedList/TrungHau/4.txt new file mode 100644 index 0000000..df5213a --- /dev/null +++ b/Topic2_LinkedList/TrungHau/4.txt @@ -0,0 +1,57 @@ +// Time complexity: O(n) +// Space complexity: O(n) +// Description: ... + +/** + * 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) {} + * }; + */ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode* temp = head; + int size = 0; + while(temp != NULL) + { + size++; + temp = temp->next; + } + + //cout << size << '\n'; + int pos = 1; + ListNode* tempNode = head; + while(pos < size - n) + { + tempNode = tempNode->next; + pos++; + } + if(n == size) + { + head = head->next; + } + else + { + //cout << pos << '\n'; + ListNode* cur = NULL; + if(tempNode->next != NULL) + { + cur = tempNode->next->next; + } + else + { + cur = NULL; + } + + tempNode->next = cur; + } + + return head; + + } +}; diff --git a/Topic2_LinkedList/TrungHau/5.txt b/Topic2_LinkedList/TrungHau/5.txt new file mode 100644 index 0000000..1ff9aa9 --- /dev/null +++ b/Topic2_LinkedList/TrungHau/5.txt @@ -0,0 +1,53 @@ +// Definition for a Node. +// class Node { +// public: +// int val; +// Node* next; +// Node* random; + +// Node(int _val) { +// val = _val; +// next = NULL; +// random = NULL; +// } +// }; + +class Solution { +public: + Node* copyRandomList(Node* head) { + if (!head) return nullptr; + + // Step 1: Create new nodes and insert them next to original nodes + Node* temp = head; + while (temp) { + Node* newNode = new Node(temp->val); + newNode->next = temp->next; + temp->next = newNode; + temp = newNode->next; + } + + // Step 2: Assign random pointers for the new nodes + temp = head; + while (temp) { + if (temp->random) { + temp->next->random = temp->random->next; + } + temp = temp->next->next; + } + + // Step 3: Separate the original list and the copied list + Node* newHead = head->next; + Node* copy = newHead; + temp = head; + while (temp) { + temp->next = temp->next->next; + if (copy->next) { + copy->next = copy->next->next; + } + temp = temp->next; + copy = copy->next; + } + + return newHead; + } +}; diff --git a/Topic2_LinkedList/TrungHau/6.txt b/Topic2_LinkedList/TrungHau/6.txt new file mode 100644 index 0000000..3ca0496 --- /dev/null +++ b/Topic2_LinkedList/TrungHau/6.txt @@ -0,0 +1,54 @@ +/** + * 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) {} + * }; + */ + #include +class Solution { +public: + + ListNode* reverse(ListNode* head) { + ListNode* prev = nullptr; + ListNode* curr = head; + while (curr != nullptr) { + ListNode* next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + } + return prev; + } + ListNode* addTwoNumbers(ListNode* li1, ListNode* li2) { + ListNode* revLi1 = li1; + ListNode* revLi2 = li2; + + ListNode* res = nullptr; + int pre = 0; + while(revLi1 != nullptr || revLi2 != nullptr || pre != 0) + { + int valueLi1 = (revLi1 != nullptr) ? revLi1->val : 0; + int valueLi2 = (revLi2 != nullptr) ? revLi2->val : 0; + int temp = (valueLi1 + valueLi2 + pre); + pre = temp / 10; + temp = temp % 10; + ListNode* newNode = new ListNode(temp); + if(res == nullptr) + { + res = newNode; + } + else { + newNode->next = res; + res = newNode; + } + if(revLi1 != nullptr)revLi1 = revLi1->next; + if(revLi2 != nullptr)revLi2 = revLi2->next; + } + + return reverse(res); + } +}; diff --git a/Topic2_LinkedList/TrungHau/7.txt b/Topic2_LinkedList/TrungHau/7.txt new file mode 100644 index 0000000..5c4d2aa --- /dev/null +++ b/Topic2_LinkedList/TrungHau/7.txt @@ -0,0 +1,26 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + bool hasCycle(ListNode *head) { + ListNode* first = head; + ListNode* second = head; + ListNode* temp = head; + bool check = true; + if(head == nullptr) return false; + if(head -> next == NULL) return false; + while(1){ + if(first!= NULL) first = first->next; else { break;} + if(second!= NULL) second = second->next; else { break;} + if(second!= NULL) second = second->next; else { break;} + if(first == second) return true; + } + return false; + } +}; diff --git a/Topic3_StackQueue/TrungHau/1.txt b/Topic3_StackQueue/TrungHau/1.txt new file mode 100644 index 0000000..9bff865 --- /dev/null +++ b/Topic3_StackQueue/TrungHau/1.txt @@ -0,0 +1,35 @@ +class Solution { +public: + bool isValid(string s) { + bool check = true; + vector a; + for(int i = 0; i < s.size(); i++) + { + if(s[i] == '(' || s[i] == '[' || s[i] == '{') + { + a.push_back(s[i]); + } + else if(a.size() > 0) + { + if(s[i] == ')') + { + if(a.back() == '(') a.pop_back(); + else return false; + } + if(s[i] == ']') + { + if(a.back() == '[') a.pop_back(); + else return false; + } + if(s[i] == '}') + { + if(a.back() == '{') a.pop_back(); + else return false; + } + } + else return false; + } + if(a.size() != 0) return false; + return true; + } +}; diff --git a/Topic3_StackQueue/TrungHau/2.txt b/Topic3_StackQueue/TrungHau/2.txt new file mode 100644 index 0000000..673c98b --- /dev/null +++ b/Topic3_StackQueue/TrungHau/2.txt @@ -0,0 +1,45 @@ +class MinStack { +public: + vector a; + vector Min; + MinStack() { + } + void push(int val) { + a.push_back(val); + if(Min.size() == 0) + { + Min.push_back(val); + } + else { + if(Min[Min.size() - 1] < val){ + Min.push_back(Min[Min.size() - 1]); + } + else + { + Min.push_back(val); + } + } + } + + void pop() { + a.pop_back(); + Min.pop_back(); + } + + int top() { + return a[a.size() - 1]; + } + + int getMin() { + return Min[Min.size() - 1]; + } +}; + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack* obj = new MinStack(); + * obj->push(val); + * obj->pop(); + * int param_3 = obj->top(); + * int param_4 = obj->getMin(); + */ diff --git a/Topic3_StackQueue/TrungHau/3.txt b/Topic3_StackQueue/TrungHau/3.txt new file mode 100644 index 0000000..2e50d4e --- /dev/null +++ b/Topic3_StackQueue/TrungHau/3.txt @@ -0,0 +1,41 @@ +#include +class Solution { +public: + int evalRPN(vector& tokens) { + stack temp; + int res = 0; + bool check = false; + for(int i = 0; i < tokens.size(); i++){ + if (isdigit(tokens[i].back()) || (tokens[i].size() > 1 && tokens[i][0] == '-')){ + int x = stoi(tokens[i]); + temp.push(x); + } + else{ + int b = temp.top(); temp.pop(); + int a = temp.top(); temp.pop(); + switch(tokens[i][0]){ + case '+': + res = a + b; + temp.push(res); + break; + case '-': + res = a - b; + temp.push(res); + break; + case '*': + res = a * b; + temp.push(res); + break; + case '/': + res = a / b; + temp.push(res); + break; + default: + break; + } + } + } + if(tokens.size() <= 1) res = stoi(tokens[0]); + return res; + } +}; diff --git a/Topic3_StackQueue/TrungHau/4.txt b/Topic3_StackQueue/TrungHau/4.txt new file mode 100644 index 0000000..66b9a1b --- /dev/null +++ b/Topic3_StackQueue/TrungHau/4.txt @@ -0,0 +1,22 @@ +class Solution { +public: + vector dailyTemperatures(vector& t) { + vector res(t.size(), 0); + stack > temp; + for(int i = 0; i < t.size(); i++){ + if(temp.empty() == true){ + temp.push(make_pair(t[i], i)); + } + else + { + while(temp.empty() == false && temp.top().first < t[i]) + { + res[temp.top().second] = i - temp.top().second; + temp.pop(); + } + temp.push(make_pair(t[i], i)); + } + } + return res; + } +}; diff --git a/Topic3_StackQueue/TrungHau/5.txt b/Topic3_StackQueue/TrungHau/5.txt new file mode 100644 index 0000000..f2fd822 --- /dev/null +++ b/Topic3_StackQueue/TrungHau/5.txt @@ -0,0 +1,28 @@ +class Solution { +public: + vector deckRevealedIncreasing(vector& deck) { + sort(deck.begin(), deck.end()); + int i = 0; + vector save(deck.size(), 0); + bool check = true; + int j = 0; + while(j < deck.size()){ + if(save[i] == 0) + { + if(check == true){ + save[i] = deck[j]; + j++; + check = false; + } + else{ + check = true; + } + } + i++; + if(i == save.size()){ + i = 0; + } + } + return save; + } +}; diff --git a/Topic3_StackQueue/TrungHau/7.txt b/Topic3_StackQueue/TrungHau/7.txt new file mode 100644 index 0000000..c5068ae --- /dev/null +++ b/Topic3_StackQueue/TrungHau/7.txt @@ -0,0 +1,22 @@ +class Solution { +public: + int findTheWinner(int n, int k) { + vector build; + for(int i = 1; i <= n; i++){ + build.push_back(i); + } + int count = 0; + while(build.size() > 1){ + for(int i = 0; i < build.size(); i++){ + count++; + if(count == k){ + count = 0; + build.erase(build.begin() + i); + i--; + if(build.size() <= 1) return build.back(); + } + } + } + return build.back(); + } +}; diff --git a/Topic3_StackQueue/TrungHau/8.txt b/Topic3_StackQueue/TrungHau/8.txt new file mode 100644 index 0000000..2897e61 --- /dev/null +++ b/Topic3_StackQueue/TrungHau/8.txt @@ -0,0 +1,17 @@ +class Solution { +public: + int longestSubarray(vector& nums, int limit) { + multiset save; + int j = 0; + int ans = 1; + for(int i = 0; i < nums.size(); i++){ + save.insert(nums[i]); + while(!save.empty() && *save.rbegin() - *save.begin() > limit){ + save.erase(save.find(nums[j])); + j++; + } + ans = max(ans, i - j + 1); + } + return ans; + } +};