From 18fef7d7cee7d63222412725a235954629f73555 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:53:38 +0700 Subject: [PATCH 01/73] Create Assignment --- Topic1_Arrays/Day2207/Assignment | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/Assignment diff --git a/Topic1_Arrays/Day2207/Assignment b/Topic1_Arrays/Day2207/Assignment new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/Assignment @@ -0,0 +1 @@ + From bd073dba423a028ee184f54d8fa695dd1c808a5a Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:55:08 +0700 Subject: [PATCH 02/73] Delete Topic1_Arrays/Day2207/Assignment --- Topic1_Arrays/Day2207/Assignment | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Topic1_Arrays/Day2207/Assignment diff --git a/Topic1_Arrays/Day2207/Assignment b/Topic1_Arrays/Day2207/Assignment deleted file mode 100644 index 8b13789..0000000 --- a/Topic1_Arrays/Day2207/Assignment +++ /dev/null @@ -1 +0,0 @@ - From 36360290a9f6e22b60cd616724dcc8ddf06e0e21 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:57:18 +0700 Subject: [PATCH 03/73] Create 1.txt --- Topic1_Arrays/Day2207/AssignmentsHomework/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/AssignmentsHomework/1.txt diff --git a/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt b/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt @@ -0,0 +1 @@ + From 5700f0ad08b49c3068f0c0123f62dc0cf08291cc Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 16:01:16 +0700 Subject: [PATCH 04/73] Update 1.txt --- .../Day2207/AssignmentsHomework/1.txt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt b/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt index 8b13789..f825bcf 100644 --- a/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt +++ b/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt @@ -1 +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; + } +}; From 1e3bd3a8a859a5572a12b49fb27a6fde6b92a86a Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 16:01:41 +0700 Subject: [PATCH 05/73] Create 2.txt --- Topic1_Arrays/Day2207/AssignmentsHomework/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/AssignmentsHomework/2.txt diff --git a/Topic1_Arrays/Day2207/AssignmentsHomework/2.txt b/Topic1_Arrays/Day2207/AssignmentsHomework/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/AssignmentsHomework/2.txt @@ -0,0 +1 @@ + From cd7b8a812d1fb781f38b6e4dc72c6d22e0c02fe4 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 16:48:35 +0700 Subject: [PATCH 06/73] Update 2.txt --- .../Day2207/AssignmentsHomework/2.txt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Topic1_Arrays/Day2207/AssignmentsHomework/2.txt b/Topic1_Arrays/Day2207/AssignmentsHomework/2.txt index 8b13789..87e950f 100644 --- a/Topic1_Arrays/Day2207/AssignmentsHomework/2.txt +++ b/Topic1_Arrays/Day2207/AssignmentsHomework/2.txt @@ -1 +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; + } +}; From b97fc785c05beb882c076a74b778c5775b315a49 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 16:50:52 +0700 Subject: [PATCH 07/73] Create 3.txt --- Topic1_Arrays/Day2207/AssignmentsHomework/3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/AssignmentsHomework/3.txt diff --git a/Topic1_Arrays/Day2207/AssignmentsHomework/3.txt b/Topic1_Arrays/Day2207/AssignmentsHomework/3.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/AssignmentsHomework/3.txt @@ -0,0 +1 @@ + From c3a60abfd9295220b277d54d1502b08348f5f864 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 16:52:45 +0700 Subject: [PATCH 08/73] Update 3.txt --- .../Day2207/AssignmentsHomework/3.txt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Topic1_Arrays/Day2207/AssignmentsHomework/3.txt b/Topic1_Arrays/Day2207/AssignmentsHomework/3.txt index 8b13789..ace7d3b 100644 --- a/Topic1_Arrays/Day2207/AssignmentsHomework/3.txt +++ b/Topic1_Arrays/Day2207/AssignmentsHomework/3.txt @@ -1 +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++]; + } + } + +}; From 406d9fdda12aff7f5b9054872e49c40a60d01b85 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:55:09 +0700 Subject: [PATCH 09/73] Create 1.txt --- Topic1_Arrays/Day2207/TrungHau/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/TrungHau/1.txt diff --git a/Topic1_Arrays/Day2207/TrungHau/1.txt b/Topic1_Arrays/Day2207/TrungHau/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/TrungHau/1.txt @@ -0,0 +1 @@ + From 21d76c0f900bfdbd2f38dd0045443445f500d573 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:55:32 +0700 Subject: [PATCH 10/73] Update 1.txt --- Topic1_Arrays/Day2207/TrungHau/1.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Topic1_Arrays/Day2207/TrungHau/1.txt b/Topic1_Arrays/Day2207/TrungHau/1.txt index 8b13789..b8ba4d3 100644 --- a/Topic1_Arrays/Day2207/TrungHau/1.txt +++ b/Topic1_Arrays/Day2207/TrungHau/1.txt @@ -1 +1,23 @@ +//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; + } +}; From 4cd907c6e26bb0a9098185f76632a02309bb3149 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:55:55 +0700 Subject: [PATCH 11/73] Create 2.txt --- Topic1_Arrays/Day2207/TrungHau/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/TrungHau/2.txt diff --git a/Topic1_Arrays/Day2207/TrungHau/2.txt b/Topic1_Arrays/Day2207/TrungHau/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/TrungHau/2.txt @@ -0,0 +1 @@ + From 16517aaccfe21d10652a3d7903a1463186cb9115 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:56:31 +0700 Subject: [PATCH 12/73] Create 3.txt --- Topic1_Arrays/Day2207/TrungHau/3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/TrungHau/3.txt diff --git a/Topic1_Arrays/Day2207/TrungHau/3.txt b/Topic1_Arrays/Day2207/TrungHau/3.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/TrungHau/3.txt @@ -0,0 +1 @@ + From fdd3ee4cde1de854dd0cd48c244232a4133f847d Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:57:35 +0700 Subject: [PATCH 13/73] Update 2.txt --- Topic1_Arrays/Day2207/TrungHau/2.txt | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Topic1_Arrays/Day2207/TrungHau/2.txt b/Topic1_Arrays/Day2207/TrungHau/2.txt index 8b13789..d703f96 100644 --- a/Topic1_Arrays/Day2207/TrungHau/2.txt +++ b/Topic1_Arrays/Day2207/TrungHau/2.txt @@ -1 +1,37 @@ +// 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; + } +}; From 36399bd78db0b21e47c0f0a7a489760fd79f5cc2 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:58:25 +0700 Subject: [PATCH 14/73] Update 3.txt --- Topic1_Arrays/Day2207/TrungHau/3.txt | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Topic1_Arrays/Day2207/TrungHau/3.txt b/Topic1_Arrays/Day2207/TrungHau/3.txt index 8b13789..ace7d3b 100644 --- a/Topic1_Arrays/Day2207/TrungHau/3.txt +++ b/Topic1_Arrays/Day2207/TrungHau/3.txt @@ -1 +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++]; + } + } + +}; From 12b3a342d89f9725a1a8ce7c44d513634bb20d60 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:59:08 +0700 Subject: [PATCH 15/73] Delete Topic1_Arrays/Day2207/TrungHau directory --- Topic1_Arrays/Day2207/TrungHau/1.txt | 23 ----------------- Topic1_Arrays/Day2207/TrungHau/2.txt | 37 ---------------------------- Topic1_Arrays/Day2207/TrungHau/3.txt | 29 ---------------------- 3 files changed, 89 deletions(-) delete mode 100644 Topic1_Arrays/Day2207/TrungHau/1.txt delete mode 100644 Topic1_Arrays/Day2207/TrungHau/2.txt delete mode 100644 Topic1_Arrays/Day2207/TrungHau/3.txt diff --git a/Topic1_Arrays/Day2207/TrungHau/1.txt b/Topic1_Arrays/Day2207/TrungHau/1.txt deleted file mode 100644 index b8ba4d3..0000000 --- a/Topic1_Arrays/Day2207/TrungHau/1.txt +++ /dev/null @@ -1,23 +0,0 @@ - -//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 deleted file mode 100644 index d703f96..0000000 --- a/Topic1_Arrays/Day2207/TrungHau/2.txt +++ /dev/null @@ -1,37 +0,0 @@ - -// 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 deleted file mode 100644 index ace7d3b..0000000 --- a/Topic1_Arrays/Day2207/TrungHau/3.txt +++ /dev/null @@ -1,29 +0,0 @@ -// 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++]; - } - } - -}; From 715d37ab278f830809028eacdddb7d3c00d53de2 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:01:46 +0700 Subject: [PATCH 16/73] Create 1.txt --- Topic1_Arrays/Day2207/TrungHau/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/TrungHau/1.txt diff --git a/Topic1_Arrays/Day2207/TrungHau/1.txt b/Topic1_Arrays/Day2207/TrungHau/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/TrungHau/1.txt @@ -0,0 +1 @@ + From c56562a7ef1b490cf1c5ec1093eddfa56eaf60aa Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:01:59 +0700 Subject: [PATCH 17/73] Create 2.txt --- Topic1_Arrays/Day2207/TrungHau/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/TrungHau/2.txt diff --git a/Topic1_Arrays/Day2207/TrungHau/2.txt b/Topic1_Arrays/Day2207/TrungHau/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/TrungHau/2.txt @@ -0,0 +1 @@ + From 59c8c4c65f57f1385b3eebb1a28d0717d9d52509 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:02:12 +0700 Subject: [PATCH 18/73] Create 3.txt --- Topic1_Arrays/Day2207/TrungHau/3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2207/TrungHau/3.txt diff --git a/Topic1_Arrays/Day2207/TrungHau/3.txt b/Topic1_Arrays/Day2207/TrungHau/3.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2207/TrungHau/3.txt @@ -0,0 +1 @@ + From 50637a7bd5ca65c6ca887bb1cffab79cea487c5b Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:02:38 +0700 Subject: [PATCH 19/73] Update 1.txt --- Topic1_Arrays/Day2207/TrungHau/1.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Topic1_Arrays/Day2207/TrungHau/1.txt b/Topic1_Arrays/Day2207/TrungHau/1.txt index 8b13789..f825bcf 100644 --- a/Topic1_Arrays/Day2207/TrungHau/1.txt +++ b/Topic1_Arrays/Day2207/TrungHau/1.txt @@ -1 +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; + } +}; From 2ba34071b83a9a43b92c33f724a97c502b49b6f5 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:02:55 +0700 Subject: [PATCH 20/73] Update 2.txt --- Topic1_Arrays/Day2207/TrungHau/2.txt | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Topic1_Arrays/Day2207/TrungHau/2.txt b/Topic1_Arrays/Day2207/TrungHau/2.txt index 8b13789..87e950f 100644 --- a/Topic1_Arrays/Day2207/TrungHau/2.txt +++ b/Topic1_Arrays/Day2207/TrungHau/2.txt @@ -1 +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; + } +}; From b9a02449e341d9bb8dc7ecc85abf7c9dbbce40d6 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:03:16 +0700 Subject: [PATCH 21/73] Update 3.txt --- Topic1_Arrays/Day2207/TrungHau/3.txt | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Topic1_Arrays/Day2207/TrungHau/3.txt b/Topic1_Arrays/Day2207/TrungHau/3.txt index 8b13789..ace7d3b 100644 --- a/Topic1_Arrays/Day2207/TrungHau/3.txt +++ b/Topic1_Arrays/Day2207/TrungHau/3.txt @@ -1 +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++]; + } + } + +}; From 5dbeeecc56bd7ac8373b0a17261d5ae3d283e507 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:03:34 +0700 Subject: [PATCH 22/73] Delete Topic1_Arrays/Day2207/AssignmentsHomework directory --- .../Day2207/AssignmentsHomework/1.txt | 22 ------------ .../Day2207/AssignmentsHomework/2.txt | 36 ------------------- .../Day2207/AssignmentsHomework/3.txt | 29 --------------- 3 files changed, 87 deletions(-) delete mode 100644 Topic1_Arrays/Day2207/AssignmentsHomework/1.txt delete mode 100644 Topic1_Arrays/Day2207/AssignmentsHomework/2.txt delete mode 100644 Topic1_Arrays/Day2207/AssignmentsHomework/3.txt diff --git a/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt b/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt deleted file mode 100644 index f825bcf..0000000 --- a/Topic1_Arrays/Day2207/AssignmentsHomework/1.txt +++ /dev/null @@ -1,22 +0,0 @@ -//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/AssignmentsHomework/2.txt b/Topic1_Arrays/Day2207/AssignmentsHomework/2.txt deleted file mode 100644 index 87e950f..0000000 --- a/Topic1_Arrays/Day2207/AssignmentsHomework/2.txt +++ /dev/null @@ -1,36 +0,0 @@ -// 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/AssignmentsHomework/3.txt b/Topic1_Arrays/Day2207/AssignmentsHomework/3.txt deleted file mode 100644 index ace7d3b..0000000 --- a/Topic1_Arrays/Day2207/AssignmentsHomework/3.txt +++ /dev/null @@ -1,29 +0,0 @@ -// 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++]; - } - } - -}; From c41316431cf5ae56a745c2fa85b65e4c30079397 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 23 Jul 2024 13:00:47 +0700 Subject: [PATCH 23/73] Create 1.txt --- Topic1_Arrays/Day2307/TrungHau/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2307/TrungHau/1.txt diff --git a/Topic1_Arrays/Day2307/TrungHau/1.txt b/Topic1_Arrays/Day2307/TrungHau/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2307/TrungHau/1.txt @@ -0,0 +1 @@ + From c09dbf3ab3bc3993cf165e49114ca26cb2146b15 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 23 Jul 2024 13:01:09 +0700 Subject: [PATCH 24/73] Create 2.txt --- Topic1_Arrays/Day2307/TrungHau/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2307/TrungHau/2.txt diff --git a/Topic1_Arrays/Day2307/TrungHau/2.txt b/Topic1_Arrays/Day2307/TrungHau/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2307/TrungHau/2.txt @@ -0,0 +1 @@ + From aa55ba6485cce1e9f7a08cdea9a233606a6b4ee9 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 23 Jul 2024 13:01:19 +0700 Subject: [PATCH 25/73] Create 3.txt --- Topic1_Arrays/Day2307/TrungHau/3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2307/TrungHau/3.txt diff --git a/Topic1_Arrays/Day2307/TrungHau/3.txt b/Topic1_Arrays/Day2307/TrungHau/3.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2307/TrungHau/3.txt @@ -0,0 +1 @@ + From 4bb127867f19c6ccae9d53028493fac8c0ba838c Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:38:03 +0700 Subject: [PATCH 26/73] Update 1.txt --- Topic1_Arrays/Day2307/TrungHau/1.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Topic1_Arrays/Day2307/TrungHau/1.txt b/Topic1_Arrays/Day2307/TrungHau/1.txt index 8b13789..f5fdc6a 100644 --- a/Topic1_Arrays/Day2307/TrungHau/1.txt +++ b/Topic1_Arrays/Day2307/TrungHau/1.txt @@ -1 +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(); + } +}; From 7bc3208a8512205e8532e4a3e67980c195049ae7 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 23 Jul 2024 18:18:15 +0700 Subject: [PATCH 27/73] Update 2.txt --- Topic1_Arrays/Day2307/TrungHau/2.txt | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Topic1_Arrays/Day2307/TrungHau/2.txt b/Topic1_Arrays/Day2307/TrungHau/2.txt index 8b13789..6fb2d4a 100644 --- a/Topic1_Arrays/Day2307/TrungHau/2.txt +++ b/Topic1_Arrays/Day2307/TrungHau/2.txt @@ -1 +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; + } +}; From 45ade47cf24ccafca5194897ae991ef7323779f3 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 23 Jul 2024 18:22:56 +0700 Subject: [PATCH 28/73] Update 3.txt --- Topic1_Arrays/Day2307/TrungHau/3.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Topic1_Arrays/Day2307/TrungHau/3.txt b/Topic1_Arrays/Day2307/TrungHau/3.txt index 8b13789..a1d5aff 100644 --- a/Topic1_Arrays/Day2307/TrungHau/3.txt +++ b/Topic1_Arrays/Day2307/TrungHau/3.txt @@ -1 +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; + } +}; From bd08c544cb4316302e1f05cb7fce973279f94270 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:39:49 +0700 Subject: [PATCH 29/73] Create 1.txt --- Topic1_Arrays/Day2407/TrungHau/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2407/TrungHau/1.txt diff --git a/Topic1_Arrays/Day2407/TrungHau/1.txt b/Topic1_Arrays/Day2407/TrungHau/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2407/TrungHau/1.txt @@ -0,0 +1 @@ + From cd6857a57a011461c0b160c5f5d779aaf936dfd1 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:40:00 +0700 Subject: [PATCH 30/73] Create 2.txt --- Topic1_Arrays/Day2407/TrungHau/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2407/TrungHau/2.txt diff --git a/Topic1_Arrays/Day2407/TrungHau/2.txt b/Topic1_Arrays/Day2407/TrungHau/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2407/TrungHau/2.txt @@ -0,0 +1 @@ + From 22b73dcad00306b02ec9237193f5ca90efab63de Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:40:14 +0700 Subject: [PATCH 31/73] Create 3.txt --- Topic1_Arrays/Day2407/TrungHau/3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2407/TrungHau/3.txt diff --git a/Topic1_Arrays/Day2407/TrungHau/3.txt b/Topic1_Arrays/Day2407/TrungHau/3.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2407/TrungHau/3.txt @@ -0,0 +1 @@ + From b6e2f4d77be134748676800ffdc0c35529162b47 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:31:40 +0700 Subject: [PATCH 32/73] Update 1.txt --- Topic1_Arrays/Day2407/TrungHau/1.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Topic1_Arrays/Day2407/TrungHau/1.txt b/Topic1_Arrays/Day2407/TrungHau/1.txt index 8b13789..6e3755f 100644 --- a/Topic1_Arrays/Day2407/TrungHau/1.txt +++ b/Topic1_Arrays/Day2407/TrungHau/1.txt @@ -1 +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; + } +}; From 35281fd95f59fe40e1eb834d6f8f55e2273e7d26 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 24 Jul 2024 16:10:42 +0700 Subject: [PATCH 33/73] Update 2.txt --- Topic1_Arrays/Day2407/TrungHau/2.txt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Topic1_Arrays/Day2407/TrungHau/2.txt b/Topic1_Arrays/Day2407/TrungHau/2.txt index 8b13789..22b0567 100644 --- a/Topic1_Arrays/Day2407/TrungHau/2.txt +++ b/Topic1_Arrays/Day2407/TrungHau/2.txt @@ -1 +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]; + } +}; From bd0e5073055429e9da7d6bf545c982abc0728672 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 25 Jul 2024 17:49:59 +0700 Subject: [PATCH 34/73] Update 3.txt --- Topic1_Arrays/Day2407/TrungHau/3.txt | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Topic1_Arrays/Day2407/TrungHau/3.txt b/Topic1_Arrays/Day2407/TrungHau/3.txt index 8b13789..d891695 100644 --- a/Topic1_Arrays/Day2407/TrungHau/3.txt +++ b/Topic1_Arrays/Day2407/TrungHau/3.txt @@ -1 +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; + } +}; From fab457a9242f722bf8d02265cb73118d50c204b9 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 25 Jul 2024 17:50:21 +0700 Subject: [PATCH 35/73] Create 1.txt --- Topic1_Arrays/Day2507/TrungHau/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2507/TrungHau/1.txt diff --git a/Topic1_Arrays/Day2507/TrungHau/1.txt b/Topic1_Arrays/Day2507/TrungHau/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2507/TrungHau/1.txt @@ -0,0 +1 @@ + From d3c8160ddbbc23d194a317bcdabd31c4f48093cd Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 25 Jul 2024 19:02:08 +0700 Subject: [PATCH 36/73] Create 2.txt --- Topic1_Arrays/Day2507/TrungHau/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2507/TrungHau/2.txt diff --git a/Topic1_Arrays/Day2507/TrungHau/2.txt b/Topic1_Arrays/Day2507/TrungHau/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2507/TrungHau/2.txt @@ -0,0 +1 @@ + From 537d8ef3466bacc1214f8c8cd873e0268adfda6c Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 25 Jul 2024 19:08:16 +0700 Subject: [PATCH 37/73] Update 1.txt --- Topic1_Arrays/Day2507/TrungHau/1.txt | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Topic1_Arrays/Day2507/TrungHau/1.txt b/Topic1_Arrays/Day2507/TrungHau/1.txt index 8b13789..5ec823a 100644 --- a/Topic1_Arrays/Day2507/TrungHau/1.txt +++ b/Topic1_Arrays/Day2507/TrungHau/1.txt @@ -1 +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; + } +}; From 2fb134f279e6278e68b93b0308b9edc6b42a3fb4 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Fri, 26 Jul 2024 08:35:54 +0700 Subject: [PATCH 38/73] Update 2.txt --- Topic1_Arrays/Day2507/TrungHau/2.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Topic1_Arrays/Day2507/TrungHau/2.txt b/Topic1_Arrays/Day2507/TrungHau/2.txt index 8b13789..91dc6ae 100644 --- a/Topic1_Arrays/Day2507/TrungHau/2.txt +++ b/Topic1_Arrays/Day2507/TrungHau/2.txt @@ -1 +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; + } +}; From 9a6ce7340c400805ea0b65e7c7e7297cf72fdc38 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:07:31 +0700 Subject: [PATCH 39/73] Create 3.txt --- Topic1_Arrays/Day2507/TrungHau/3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2507/TrungHau/3.txt diff --git a/Topic1_Arrays/Day2507/TrungHau/3.txt b/Topic1_Arrays/Day2507/TrungHau/3.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2507/TrungHau/3.txt @@ -0,0 +1 @@ + From a3800355b55dc0a5bc3a3d0633f557cee9d19260 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:31:00 +0700 Subject: [PATCH 40/73] Update 3.txt --- Topic1_Arrays/Day2507/TrungHau/3.txt | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Topic1_Arrays/Day2507/TrungHau/3.txt b/Topic1_Arrays/Day2507/TrungHau/3.txt index 8b13789..5d0cd5e 100644 --- a/Topic1_Arrays/Day2507/TrungHau/3.txt +++ b/Topic1_Arrays/Day2507/TrungHau/3.txt @@ -1 +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; + } +}; From 0b2f5dd975a79e67e94724c42b4d9e87df9d608b Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:35:43 +0700 Subject: [PATCH 41/73] Create 1.txt --- Topic1_Arrays/Day2607/TrungHau/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2607/TrungHau/1.txt diff --git a/Topic1_Arrays/Day2607/TrungHau/1.txt b/Topic1_Arrays/Day2607/TrungHau/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2607/TrungHau/1.txt @@ -0,0 +1 @@ + From 4e737db59543958eb2c6d2b23da112deeea6a66b Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Fri, 26 Jul 2024 21:12:43 +0700 Subject: [PATCH 42/73] Update 1.txt --- Topic1_Arrays/Day2607/TrungHau/1.txt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Topic1_Arrays/Day2607/TrungHau/1.txt b/Topic1_Arrays/Day2607/TrungHau/1.txt index 8b13789..0025779 100644 --- a/Topic1_Arrays/Day2607/TrungHau/1.txt +++ b/Topic1_Arrays/Day2607/TrungHau/1.txt @@ -1 +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; + } +}; From 0cd9174c01ecb42c7816fffc90dc876792b88374 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Sat, 27 Jul 2024 16:02:41 +0700 Subject: [PATCH 43/73] Create 2.txt --- Topic1_Arrays/Day2607/TrungHau/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2607/TrungHau/2.txt diff --git a/Topic1_Arrays/Day2607/TrungHau/2.txt b/Topic1_Arrays/Day2607/TrungHau/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2607/TrungHau/2.txt @@ -0,0 +1 @@ + From 44e6c3cd510426a66d47d81d5e0bdfeb855a995e Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Sat, 27 Jul 2024 16:57:33 +0700 Subject: [PATCH 44/73] Update 2.txt --- Topic1_Arrays/Day2607/TrungHau/2.txt | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Topic1_Arrays/Day2607/TrungHau/2.txt b/Topic1_Arrays/Day2607/TrungHau/2.txt index 8b13789..9530700 100644 --- a/Topic1_Arrays/Day2607/TrungHau/2.txt +++ b/Topic1_Arrays/Day2607/TrungHau/2.txt @@ -1 +1,32 @@ +// Time complexity: O(n * log(n)) +// Memory complexity: ... +// Description: Using two loop to find index to convert +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; + } + } + } +}; From 8eab98308bebbdc4a93848169085c94d5a446adc Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Sat, 27 Jul 2024 17:06:46 +0700 Subject: [PATCH 45/73] Update 2.txt --- Topic1_Arrays/Day2607/TrungHau/2.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Topic1_Arrays/Day2607/TrungHau/2.txt b/Topic1_Arrays/Day2607/TrungHau/2.txt index 9530700..c5dc96d 100644 --- a/Topic1_Arrays/Day2607/TrungHau/2.txt +++ b/Topic1_Arrays/Day2607/TrungHau/2.txt @@ -1,6 +1,6 @@ // Time complexity: O(n * log(n)) // Memory complexity: ... -// Description: Using two loop to find index to convert +// Description: Using two loop to find index to convert...According to https://www.geeksforgeeks.org/next-permutation/. class Solution { public: From ce9a8ccb0f42ff2d155cfe42165e46cd304bc9e2 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Sun, 28 Jul 2024 08:30:25 +0700 Subject: [PATCH 46/73] Create 3.txt --- Topic1_Arrays/Day2607/TrungHau/3.txt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Topic1_Arrays/Day2607/TrungHau/3.txt 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_; + } +}; From 9abeab7b0e393b7a95655a695b06cb750305aaef Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Sun, 28 Jul 2024 08:46:48 +0700 Subject: [PATCH 47/73] Create 1.txt --- Topic1_Arrays/Day2707/TrungHau/1.txt | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Topic1_Arrays/Day2707/TrungHau/1.txt 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_; + } +}; From 850f6f9af2411e68a1547afa4c4d304309821a9d Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 30 Jul 2024 16:07:39 +0700 Subject: [PATCH 48/73] Create 2.txt --- Topic1_Arrays/Day2707/TrungHau/2.txt | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Topic1_Arrays/Day2707/TrungHau/2.txt 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; + } +}; From 7fd5a2bb7687b4576849acc1d9a4a7c3722231fe Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 30 Jul 2024 16:41:50 +0700 Subject: [PATCH 49/73] Create 3.txt --- Topic1_Arrays/Day2707/TrungHau/3.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Topic1_Arrays/Day2707/TrungHau/3.txt 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; + } +}; From 6fdd4eaff5565257d10d3e989dad788f90137184 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 30 Jul 2024 16:42:11 +0700 Subject: [PATCH 50/73] Create 1.txt --- Topic1_Arrays/Day2807/TrungHau/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2807/TrungHau/1.txt diff --git a/Topic1_Arrays/Day2807/TrungHau/1.txt b/Topic1_Arrays/Day2807/TrungHau/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2807/TrungHau/1.txt @@ -0,0 +1 @@ + From 9683d1d5afcf42fe717d9636163d928d290b78ef Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 30 Jul 2024 22:32:40 +0700 Subject: [PATCH 51/73] Update 1.txt --- Topic1_Arrays/Day2807/TrungHau/1.txt | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Topic1_Arrays/Day2807/TrungHau/1.txt b/Topic1_Arrays/Day2807/TrungHau/1.txt index 8b13789..70438e7 100644 --- a/Topic1_Arrays/Day2807/TrungHau/1.txt +++ b/Topic1_Arrays/Day2807/TrungHau/1.txt @@ -1 +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_; + } +}; From dc8222f995c7c8e843db9c7de5216a5608142f95 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 30 Jul 2024 23:25:01 +0700 Subject: [PATCH 52/73] Create 2.txt --- Topic1_Arrays/Day2807/TrungHau/2.txt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Topic1_Arrays/Day2807/TrungHau/2.txt 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; + } +}; From 00031e23a1abcaffb02ff69668195c11ce9a4e0a Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 30 Jul 2024 23:39:13 +0700 Subject: [PATCH 53/73] Create 3.txt --- Topic1_Arrays/Day2807/TrungHau/3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic1_Arrays/Day2807/TrungHau/3.txt diff --git a/Topic1_Arrays/Day2807/TrungHau/3.txt b/Topic1_Arrays/Day2807/TrungHau/3.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic1_Arrays/Day2807/TrungHau/3.txt @@ -0,0 +1 @@ + From d84622869bfeb5188fd70f2f38e8126a07ae5025 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 30 Jul 2024 23:43:12 +0700 Subject: [PATCH 54/73] Update 3.txt From 4748cd0b970ff54e1239549fda0959063f4736c4 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:00:00 +0700 Subject: [PATCH 55/73] Update 3.txt --- Topic1_Arrays/Day2807/TrungHau/3.txt | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Topic1_Arrays/Day2807/TrungHau/3.txt b/Topic1_Arrays/Day2807/TrungHau/3.txt index 8b13789..b0fa9f4 100644 --- a/Topic1_Arrays/Day2807/TrungHau/3.txt +++ b/Topic1_Arrays/Day2807/TrungHau/3.txt @@ -1 +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; + } +}; From e29a0c566b6606fa6d0c0aa42fa8959eb0af2029 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:00:22 +0700 Subject: [PATCH 56/73] Create 1.txt --- Topic2_LinkedList/TrungHau/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic2_LinkedList/TrungHau/1.txt diff --git a/Topic2_LinkedList/TrungHau/1.txt b/Topic2_LinkedList/TrungHau/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic2_LinkedList/TrungHau/1.txt @@ -0,0 +1 @@ + From e3f89d3292c20298c5f0d253961dc45934165c8a Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:04:01 +0700 Subject: [PATCH 57/73] Update 1.txt --- Topic2_LinkedList/TrungHau/1.txt | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Topic2_LinkedList/TrungHau/1.txt b/Topic2_LinkedList/TrungHau/1.txt index 8b13789..8e36205 100644 --- a/Topic2_LinkedList/TrungHau/1.txt +++ b/Topic2_LinkedList/TrungHau/1.txt @@ -1 +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; + } +}; From 02fc5e0ea4ae102a2a89fcca64ee44215d33cbd1 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:06:08 +0700 Subject: [PATCH 58/73] Create 2.txt --- Topic2_LinkedList/TrungHau/2.txt | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Topic2_LinkedList/TrungHau/2.txt 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; + } +}; From 92ed3f68e6eacc111de9a2dd9d4967b9891cd364 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:37:31 +0700 Subject: [PATCH 59/73] Create 3.txt --- Topic2_LinkedList/TrungHau/3.txt | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Topic2_LinkedList/TrungHau/3.txt 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; + } + + } +}; From d227609937f65f316a80e18684d92511eb200a1a Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:39:59 +0700 Subject: [PATCH 60/73] Create 4.txt --- Topic2_LinkedList/TrungHau/4.txt | 57 ++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Topic2_LinkedList/TrungHau/4.txt 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; + + } +}; From e95b7594b2660893fffca5972136b1632369b5c8 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Sat, 17 Aug 2024 20:26:13 +0700 Subject: [PATCH 61/73] Create 5.txt --- Topic2_LinkedList/TrungHau/5.txt | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Topic2_LinkedList/TrungHau/5.txt 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; + } +}; From 55e02b56ec6c035eaf27ed9ed365957ddce99347 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 20 Aug 2024 21:51:30 +0700 Subject: [PATCH 62/73] Create 7.txt --- Topic2_LinkedList/TrungHau/7.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Topic2_LinkedList/TrungHau/7.txt 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; + } +}; From caed617f6fc88b7a93fac2d48facaa5e555855b8 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Tue, 20 Aug 2024 21:51:49 +0700 Subject: [PATCH 63/73] Create 6.txt --- Topic2_LinkedList/TrungHau/6.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic2_LinkedList/TrungHau/6.txt diff --git a/Topic2_LinkedList/TrungHau/6.txt b/Topic2_LinkedList/TrungHau/6.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic2_LinkedList/TrungHau/6.txt @@ -0,0 +1 @@ + From 3181beecace566a3ea713fa73b29ee2944ed47c5 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:34:42 +0700 Subject: [PATCH 64/73] Update 6.txt --- Topic2_LinkedList/TrungHau/6.txt | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/Topic2_LinkedList/TrungHau/6.txt b/Topic2_LinkedList/TrungHau/6.txt index 8b13789..3ca0496 100644 --- a/Topic2_LinkedList/TrungHau/6.txt +++ b/Topic2_LinkedList/TrungHau/6.txt @@ -1 +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); + } +}; From a51388866c06aef4619a8464ee06e40648f5e0a2 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:35:02 +0700 Subject: [PATCH 65/73] Create 1.txt --- Topic3_StackQueue/TrungHau/1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic3_StackQueue/TrungHau/1.txt diff --git a/Topic3_StackQueue/TrungHau/1.txt b/Topic3_StackQueue/TrungHau/1.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic3_StackQueue/TrungHau/1.txt @@ -0,0 +1 @@ + From a0272060f500a68d4a2bb931de33ffc3e2e3c778 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:35:26 +0700 Subject: [PATCH 66/73] Update 1.txt --- Topic3_StackQueue/TrungHau/1.txt | 36 +++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/Topic3_StackQueue/TrungHau/1.txt b/Topic3_StackQueue/TrungHau/1.txt index 8b13789..9bff865 100644 --- a/Topic3_StackQueue/TrungHau/1.txt +++ b/Topic3_StackQueue/TrungHau/1.txt @@ -1 +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; + } +}; From ed20d101854b46b8fe268f4d9190fc146bfbafe0 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:35:35 +0700 Subject: [PATCH 67/73] Create 2.txt --- Topic3_StackQueue/TrungHau/2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Topic3_StackQueue/TrungHau/2.txt diff --git a/Topic3_StackQueue/TrungHau/2.txt b/Topic3_StackQueue/TrungHau/2.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Topic3_StackQueue/TrungHau/2.txt @@ -0,0 +1 @@ + From 31ca668bdf3e970012b469b422e92819d1fa7b1b Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:52:29 +0700 Subject: [PATCH 68/73] Update 2.txt --- Topic3_StackQueue/TrungHau/2.txt | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Topic3_StackQueue/TrungHau/2.txt b/Topic3_StackQueue/TrungHau/2.txt index 8b13789..673c98b 100644 --- a/Topic3_StackQueue/TrungHau/2.txt +++ b/Topic3_StackQueue/TrungHau/2.txt @@ -1 +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(); + */ From 3519da5f96feb01c71b825cfb704913daf706283 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 22 Aug 2024 21:35:02 +0700 Subject: [PATCH 69/73] Create 3.txt --- Topic3_StackQueue/TrungHau/3.txt | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Topic3_StackQueue/TrungHau/3.txt 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; + } +}; From a2d378d517ba78fadc07a1b6570ab0a1dc01b679 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 22 Aug 2024 21:46:11 +0700 Subject: [PATCH 70/73] Create 4.txt --- Topic3_StackQueue/TrungHau/4.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Topic3_StackQueue/TrungHau/4.txt 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; + } +}; From 73de1d11259f954a042cdc0b9c1c8e9d647ad835 Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+23020061@users.noreply.github.com> Date: Thu, 22 Aug 2024 21:47:17 +0700 Subject: [PATCH 71/73] Create 5.txt --- Topic3_StackQueue/TrungHau/5.txt | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Topic3_StackQueue/TrungHau/5.txt 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; + } +}; From c6007f48d931dbb9be4c06415ed3444d0d0df5ec Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+TrungHau2056@users.noreply.github.com> Date: Fri, 23 Aug 2024 20:21:39 +0700 Subject: [PATCH 72/73] Create 7.txt --- Topic3_StackQueue/TrungHau/7.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Topic3_StackQueue/TrungHau/7.txt 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(); + } +}; From 73dce2cbba7962c3aa419af1dfc66e16553dfecb Mon Sep 17 00:00:00 2001 From: TrungHau <144112290+TrungHau2056@users.noreply.github.com> Date: Sun, 25 Aug 2024 15:42:46 +0700 Subject: [PATCH 73/73] Create 8.txt --- Topic3_StackQueue/TrungHau/8.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Topic3_StackQueue/TrungHau/8.txt 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; + } +};