From 5a3e3fe5f61222879ef858124c4aae4249c01d0e Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Fri, 26 Jul 2024 20:18:20 +0700 Subject: [PATCH 01/21] Create 1.txt --- Topic1_Arrays/Day2207/Tung/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/Tung/1.txt diff --git a/Topic1_Arrays/Day2207/Tung/1.txt b/Topic1_Arrays/Day2207/Tung/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/Tung/1.txt @@ -0,0 +1 @@ + From 186e51cf2ff1c292bd9933a62835125d45b3bfd6 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Fri, 26 Jul 2024 20:33:54 +0700 Subject: [PATCH 02/21] Update 1.txt --- Topic1_Arrays/Day2207/Tung/1.txt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Topic1_Arrays/Day2207/Tung/1.txt b/Topic1_Arrays/Day2207/Tung/1.txt index 8b13789..3c499dc 100644 --- a/Topic1_Arrays/Day2207/Tung/1.txt +++ b/Topic1_Arrays/Day2207/Tung/1.txt @@ -1 +1,25 @@ +// Description: Use two Loop +// Time complexity: O(n ^ 2) +// Memory complexity: O(1) + +class Solution { +public: + vector twoSum(vector& nums, int target) { + vector 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; + + } +}; From 6bf6a13e9ffbc448cfd5f34e840198e4905909f4 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Sat, 27 Jul 2024 14:44:48 +0700 Subject: [PATCH 03/21] Create Tung --- Topic1_Arrays/Day2307/Tung | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2307/Tung diff --git a/Topic1_Arrays/Day2307/Tung b/Topic1_Arrays/Day2307/Tung new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2307/Tung @@ -0,0 +1 @@ + From fe2962a83934293a4af22ba335e4efba266db001 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Sat, 27 Jul 2024 14:45:02 +0700 Subject: [PATCH 04/21] Delete Topic1_Arrays/Day2307/Tung --- Topic1_Arrays/Day2307/Tung | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Topic1_Arrays/Day2307/Tung diff --git a/Topic1_Arrays/Day2307/Tung b/Topic1_Arrays/Day2307/Tung deleted file mode 100644 index 8b13789..0000000 --- a/Topic1_Arrays/Day2307/Tung +++ /dev/null @@ -1 +0,0 @@ - From abde8e65a39cdc15eb15f9bd5743f561ea95b583 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Sat, 27 Jul 2024 14:45:31 +0700 Subject: [PATCH 05/21] Create 1.txt --- Topic1_Arrays/Day2307/Tung/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2307/Tung/1.txt diff --git a/Topic1_Arrays/Day2307/Tung/1.txt b/Topic1_Arrays/Day2307/Tung/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2307/Tung/1.txt @@ -0,0 +1 @@ + From 4542d594b6d769953049db447fe801a2a45c5627 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Sun, 28 Jul 2024 10:37:41 +0700 Subject: [PATCH 06/21] Create 2.txt --- Topic1_Arrays/Day2307/Tung/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2307/Tung/2.txt diff --git a/Topic1_Arrays/Day2307/Tung/2.txt b/Topic1_Arrays/Day2307/Tung/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2307/Tung/2.txt @@ -0,0 +1 @@ + From 7354fe109edd6b15984cd855dbcbe7a0bd4f0eb8 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Sun, 28 Jul 2024 11:10:00 +0700 Subject: [PATCH 07/21] Update 2.txt --- Topic1_Arrays/Day2307/Tung/2.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Topic1_Arrays/Day2307/Tung/2.txt b/Topic1_Arrays/Day2307/Tung/2.txt index 8b13789..dfdac8d 100644 --- a/Topic1_Arrays/Day2307/Tung/2.txt +++ b/Topic1_Arrays/Day2307/Tung/2.txt @@ -1 +1,5 @@ - +// 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 memory: O (n log(n)) +// From 5847f5283f480e7cded185e09497188b815d964d Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Sun, 28 Jul 2024 11:11:44 +0700 Subject: [PATCH 08/21] Update 2.txt --- Topic1_Arrays/Day2307/Tung/2.txt | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Topic1_Arrays/Day2307/Tung/2.txt b/Topic1_Arrays/Day2307/Tung/2.txt index dfdac8d..3aa011d 100644 --- a/Topic1_Arrays/Day2307/Tung/2.txt +++ b/Topic1_Arrays/Day2307/Tung/2.txt @@ -1,5 +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 memory: O (n log(n)) -// +// Time complexity: O (n log(n)) +// Memory complexity: O (n) +class Solution { +public: + vector> merge(vector>& intervals) { + sort(intervals.begin() , intervals.end()); + + vector> 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; + + } +}; From e3f1f0cc46e360f94234935189c6a12762d767a3 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Sun, 28 Jul 2024 11:41:21 +0700 Subject: [PATCH 09/21] Create 3.txt --- Topic1_Arrays/Day2307/Tung/3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2307/Tung/3.txt diff --git a/Topic1_Arrays/Day2307/Tung/3.txt b/Topic1_Arrays/Day2307/Tung/3.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2307/Tung/3.txt @@ -0,0 +1 @@ + From 16d254027a32c2374d3392d961c1819e92770a6b Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Sun, 28 Jul 2024 11:45:39 +0700 Subject: [PATCH 10/21] Update 3.txt --- Topic1_Arrays/Day2307/Tung/3.txt | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Topic1_Arrays/Day2307/Tung/3.txt b/Topic1_Arrays/Day2307/Tung/3.txt index 8b13789..a24bbc5 100644 --- a/Topic1_Arrays/Day2307/Tung/3.txt +++ b/Topic1_Arrays/Day2307/Tung/3.txt @@ -1 +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& 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; + } +}; From 213c228dd121e27ee6653a3993c369702293ac9f Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:32:52 +0700 Subject: [PATCH 11/21] Create 1.txt --- Topic1_Arrays/Day2407/Tung/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2407/Tung/1.txt diff --git a/Topic1_Arrays/Day2407/Tung/1.txt b/Topic1_Arrays/Day2407/Tung/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2407/Tung/1.txt @@ -0,0 +1 @@ + From 28b61823be0b493c9a09af7f275dae8b00358012 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:37:23 +0700 Subject: [PATCH 12/21] Update 1.txt --- Topic1_Arrays/Day2407/Tung/1.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Topic1_Arrays/Day2407/Tung/1.txt b/Topic1_Arrays/Day2407/Tung/1.txt index 8b13789..136a129 100644 --- a/Topic1_Arrays/Day2407/Tung/1.txt +++ b/Topic1_Arrays/Day2407/Tung/1.txt @@ -1 +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> groupAnagrams(vector& strs) { + map >mp; + for(auto str:strs){ + string s = str; + sort(s.begin(), s.end()); + mp[s].push_back(str); + } + + vector> ans; + for(auto it = mp.begin(); it != mp.end(); it++){ + ans.push_back(it->second); + } + return ans; + } +}; From f25c38480227e9b2df088aadb278c6277565b2c1 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:37:45 +0700 Subject: [PATCH 13/21] Create 2.txt --- Topic1_Arrays/Day2407/Tung/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2407/Tung/2.txt diff --git a/Topic1_Arrays/Day2407/Tung/2.txt b/Topic1_Arrays/Day2407/Tung/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2407/Tung/2.txt @@ -0,0 +1 @@ + From 4563e3248af3e998528fb08aedeed6621bcf633c Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:54:21 +0700 Subject: [PATCH 14/21] Update 2.txt --- Topic1_Arrays/Day2407/Tung/2.txt | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Topic1_Arrays/Day2407/Tung/2.txt b/Topic1_Arrays/Day2407/Tung/2.txt index 8b13789..be0b820 100644 --- a/Topic1_Arrays/Day2407/Tung/2.txt +++ b/Topic1_Arrays/Day2407/Tung/2.txt @@ -1 +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& nums, int k) { + priority_queue , greater> pq; + for(int i = 0 ; i < nums.size() ; i++) + { + pq.push(nums[i]); + if(pq.size() > k) + { + pq.pop(); + } + } + return pq.top(); + return 0; + } +}; From 26d58a58a06821433905a675e76ec399673d3df9 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:54:36 +0700 Subject: [PATCH 15/21] Create 3.txt --- Topic1_Arrays/Day2407/Tung/3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2407/Tung/3.txt diff --git a/Topic1_Arrays/Day2407/Tung/3.txt b/Topic1_Arrays/Day2407/Tung/3.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2407/Tung/3.txt @@ -0,0 +1 @@ + From d29ff899f9290ccae310d678d38d1b69ec804529 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 19:15:19 +0700 Subject: [PATCH 16/21] Update 3.txt --- Topic1_Arrays/Day2407/Tung/3.txt | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Topic1_Arrays/Day2407/Tung/3.txt b/Topic1_Arrays/Day2407/Tung/3.txt index 8b13789..b3195d0 100644 --- a/Topic1_Arrays/Day2407/Tung/3.txt +++ b/Topic1_Arrays/Day2407/Tung/3.txt @@ -1 +1,42 @@ +// Description: Use pointer left and right. +// Time complexity: O(n ^ 2) +// Memory complexity: O(k) +class Solution { +public: + vector> threeSum(vector& nums) { + vector> 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; + } +}; From 349bb9b0bd0bcdc0e6d4375d30fea14457658424 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 19:41:33 +0700 Subject: [PATCH 17/21] Create 1.txt --- Topic1_Arrays/Day2507/Tung/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2507/Tung/1.txt diff --git a/Topic1_Arrays/Day2507/Tung/1.txt b/Topic1_Arrays/Day2507/Tung/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2507/Tung/1.txt @@ -0,0 +1 @@ + From 8594a1a034ec7755898fd184f4bdcb3d8a985032 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 19:43:58 +0700 Subject: [PATCH 18/21] Update 1.txt --- Topic1_Arrays/Day2507/Tung/1.txt | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Topic1_Arrays/Day2507/Tung/1.txt b/Topic1_Arrays/Day2507/Tung/1.txt index 8b13789..9fb46bc 100644 --- a/Topic1_Arrays/Day2507/Tung/1.txt +++ b/Topic1_Arrays/Day2507/Tung/1.txt @@ -1 +1,27 @@ - +// Description: Use pointer left and right. +// Time complexity: O(n). +// Memory complexity: O(1). +class Solution { +public: + int maxArea(vector& 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; + } +}; From 6a5790ec9663c6ed5b6486e7fe01d61237ced6b9 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 20:09:26 +0700 Subject: [PATCH 19/21] Create 2.txt --- Topic1_Arrays/Day2507/Tung/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2507/Tung/2.txt diff --git a/Topic1_Arrays/Day2507/Tung/2.txt b/Topic1_Arrays/Day2507/Tung/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2507/Tung/2.txt @@ -0,0 +1 @@ + From d4aa29d0a37bc0302d603777ce86f797aa1e85ff Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Mon, 29 Jul 2024 20:19:31 +0700 Subject: [PATCH 20/21] Update 2.txt --- Topic1_Arrays/Day2507/Tung/2.txt | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Topic1_Arrays/Day2507/Tung/2.txt b/Topic1_Arrays/Day2507/Tung/2.txt index 8b13789..c847ffc 100644 --- a/Topic1_Arrays/Day2507/Tung/2.txt +++ b/Topic1_Arrays/Day2507/Tung/2.txt @@ -1 +1,33 @@ +// Description: Use unordered_map. +// Time complexity: O(n). +// Memory complexity: O(n). +#include +#include +using namespace std; +class Solution { +public: + int subarraySum(vector& nums, int k) { + unordered_map 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). From 3da85c1820e0690da37920ff20cb828766d59d75 Mon Sep 17 00:00:00 2001 From: Tungcutenhoem <160585498+Tungcutenhoem@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:17:17 +0700 Subject: [PATCH 21/21] Create 3.txt --- Topic1_Arrays/Day2507/Tung/3.txt | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Topic1_Arrays/Day2507/Tung/3.txt diff --git a/Topic1_Arrays/Day2507/Tung/3.txt b/Topic1_Arrays/Day2507/Tung/3.txt new file mode 100644 index 0000000..01571fd --- /dev/null +++ b/Topic1_Arrays/Day2507/Tung/3.txt @@ -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 commonChars(vector& words) { + vector result; + vector min_fred(26 , 1000); + for(string word : words) + { + vector 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; + } +};