Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
18fef7d
Create Assignment
TrungHau2056 Jul 22, 2024
bd073db
Delete Topic1_Arrays/Day2207/Assignment
TrungHau2056 Jul 22, 2024
3636029
Create 1.txt
TrungHau2056 Jul 22, 2024
5700f0a
Update 1.txt
TrungHau2056 Jul 22, 2024
1e3bd3a
Create 2.txt
TrungHau2056 Jul 22, 2024
cd7b8a8
Update 2.txt
TrungHau2056 Jul 22, 2024
b97fc78
Create 3.txt
TrungHau2056 Jul 22, 2024
c3a60ab
Update 3.txt
TrungHau2056 Jul 22, 2024
406d9fd
Create 1.txt
TrungHau2056 Jul 22, 2024
21d76c0
Update 1.txt
TrungHau2056 Jul 22, 2024
4cd907c
Create 2.txt
TrungHau2056 Jul 22, 2024
16517aa
Create 3.txt
TrungHau2056 Jul 22, 2024
fdd3ee4
Update 2.txt
TrungHau2056 Jul 22, 2024
36399bd
Update 3.txt
TrungHau2056 Jul 22, 2024
12b3a34
Delete Topic1_Arrays/Day2207/TrungHau directory
TrungHau2056 Jul 22, 2024
715d37a
Create 1.txt
TrungHau2056 Jul 22, 2024
c56562a
Create 2.txt
TrungHau2056 Jul 22, 2024
59c8c4c
Create 3.txt
TrungHau2056 Jul 22, 2024
50637a7
Update 1.txt
TrungHau2056 Jul 22, 2024
2ba3407
Update 2.txt
TrungHau2056 Jul 22, 2024
b9a0244
Update 3.txt
TrungHau2056 Jul 22, 2024
5dbeeec
Delete Topic1_Arrays/Day2207/AssignmentsHomework directory
TrungHau2056 Jul 22, 2024
c413164
Create 1.txt
TrungHau2056 Jul 23, 2024
c09dbf3
Create 2.txt
TrungHau2056 Jul 23, 2024
aa55ba6
Create 3.txt
TrungHau2056 Jul 23, 2024
4bb1278
Update 1.txt
TrungHau2056 Jul 23, 2024
7bc3208
Update 2.txt
TrungHau2056 Jul 23, 2024
45ade47
Update 3.txt
TrungHau2056 Jul 23, 2024
bd08c54
Create 1.txt
TrungHau2056 Jul 24, 2024
cd6857a
Create 2.txt
TrungHau2056 Jul 24, 2024
22b73dc
Create 3.txt
TrungHau2056 Jul 24, 2024
b6e2f4d
Update 1.txt
TrungHau2056 Jul 24, 2024
35281fd
Update 2.txt
TrungHau2056 Jul 24, 2024
bd0e507
Update 3.txt
TrungHau2056 Jul 25, 2024
fab457a
Create 1.txt
TrungHau2056 Jul 25, 2024
d3c8160
Create 2.txt
TrungHau2056 Jul 25, 2024
537d8ef
Update 1.txt
TrungHau2056 Jul 25, 2024
2fb134f
Update 2.txt
TrungHau2056 Jul 26, 2024
9a6ce73
Create 3.txt
TrungHau2056 Jul 26, 2024
a380035
Update 3.txt
TrungHau2056 Jul 26, 2024
0b2f5dd
Create 1.txt
TrungHau2056 Jul 26, 2024
4e737db
Update 1.txt
TrungHau2056 Jul 26, 2024
0cd9174
Create 2.txt
TrungHau2056 Jul 27, 2024
44e6c3c
Update 2.txt
TrungHau2056 Jul 27, 2024
8eab983
Update 2.txt
TrungHau2056 Jul 27, 2024
ce9a8cc
Create 3.txt
TrungHau2056 Jul 28, 2024
9abeab7
Create 1.txt
TrungHau2056 Jul 28, 2024
850f6f9
Create 2.txt
TrungHau2056 Jul 30, 2024
7fd5a2b
Create 3.txt
TrungHau2056 Jul 30, 2024
6fdd4ea
Create 1.txt
TrungHau2056 Jul 30, 2024
9683d1d
Update 1.txt
TrungHau2056 Jul 30, 2024
dc8222f
Create 2.txt
TrungHau2056 Jul 30, 2024
00031e2
Create 3.txt
TrungHau2056 Jul 30, 2024
d846228
Update 3.txt
TrungHau2056 Jul 30, 2024
07c9595
Merge branch 'nguyenson2012:main' into brand1
TrungHau2056 Aug 5, 2024
4748cd0
Update 3.txt
TrungHau2056 Aug 7, 2024
e29a0c5
Create 1.txt
TrungHau2056 Aug 7, 2024
e3f89d3
Update 1.txt
TrungHau2056 Aug 7, 2024
02fc5e0
Create 2.txt
TrungHau2056 Aug 7, 2024
92ed3f6
Create 3.txt
TrungHau2056 Aug 7, 2024
d227609
Create 4.txt
TrungHau2056 Aug 7, 2024
a095975
Merge branch 'nguyenson2012:main' into brand1
TrungHau2056 Aug 17, 2024
e95b759
Create 5.txt
TrungHau2056 Aug 17, 2024
f3be41d
Merge branch 'nguyenson2012:main' into brand1
TrungHau2056 Aug 20, 2024
55e02b5
Create 7.txt
TrungHau2056 Aug 20, 2024
caed617
Create 6.txt
TrungHau2056 Aug 20, 2024
3181bee
Update 6.txt
TrungHau2056 Aug 22, 2024
a513888
Create 1.txt
TrungHau2056 Aug 22, 2024
a027206
Update 1.txt
TrungHau2056 Aug 22, 2024
ed20d10
Create 2.txt
TrungHau2056 Aug 22, 2024
31ca668
Update 2.txt
TrungHau2056 Aug 22, 2024
3519da5
Create 3.txt
TrungHau2056 Aug 22, 2024
a2d378d
Create 4.txt
TrungHau2056 Aug 22, 2024
73de1d1
Create 5.txt
TrungHau2056 Aug 22, 2024
c6007f4
Create 7.txt
TrungHau2056 Aug 23, 2024
73dce2c
Create 8.txt
TrungHau2056 Aug 25, 2024
0ddedb4
Merge branch 'nguyenson2012:main' into brand1
TrungHau2056 Aug 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Topic1_Arrays/Day2207/TrungHau/1.txt
Original file line number Diff line number Diff line change
@@ -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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add esimtate for time and space complexity

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector <int> 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;
}
};
36 changes: 36 additions & 0 deletions Topic1_Arrays/Day2207/TrungHau/2.txt
Original file line number Diff line number Diff line change
@@ -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<vector<char>> &temp){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose meaningful function name

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<vector<char>>& 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;
}
};
29 changes: 29 additions & 0 deletions Topic1_Arrays/Day2207/TrungHau/3.txt
Original file line number Diff line number Diff line change
@@ -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<int>& nums1, int m, vector<int>& nums2, int n) {
vector <int> 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++];
}
}

};
27 changes: 27 additions & 0 deletions Topic1_Arrays/Day2307/TrungHau/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time complexity: O(n * 3 + log(n))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time complexity O(3n + log(n)) same as time complexity O(n)
In this question, space complexity is O(n) also

// Memory compexity: (i don't know how to calculate)
// Using array to seperate rate.

class Solution {
public:
int sum = 0;
vector <double> percent;
Solution(vector<int>& 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();
}
};
22 changes: 22 additions & 0 deletions Topic1_Arrays/Day2307/TrungHau/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time complexity: O(n)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time complexity here is O(n^2), not 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<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
vector<vector<int>> 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;
}
};
16 changes: 16 additions & 0 deletions Topic1_Arrays/Day2307/TrungHau/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Time complexity: O(n)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include leetcode link of this question in the answer also

// 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<int>& 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;
}
};
22 changes: 22 additions & 0 deletions Topic1_Arrays/Day2407/TrungHau/1.txt
Original file line number Diff line number Diff line change
@@ -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<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> 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;
}
};
11 changes: 11 additions & 0 deletions Topic1_Arrays/Day2407/TrungHau/2.txt
Original file line number Diff line number Diff line change
@@ -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<int>& nums, int k) {
sort(nums.begin(), nums.end());
return nums[nums.size() - k];
}
};
33 changes: 33 additions & 0 deletions Topic1_Arrays/Day2407/TrungHau/3.txt
Original file line number Diff line number Diff line change
@@ -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<vector<int>> threeSum(vector<int>& nums) {
vector <vector<int>> res;
set <pair<int, int>> 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<int> temp{a.first, a.second, -(a.first + a.second)};
res.emplace_back(temp);
}
return res;
}
};
26 changes: 26 additions & 0 deletions Topic1_Arrays/Day2507/TrungHau/1.txt
Original file line number Diff line number Diff line change
@@ -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<int>& 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;
}
};
22 changes: 22 additions & 0 deletions Topic1_Arrays/Day2507/TrungHau/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time complexity: O(n^2)
// Memory compleity: ...
// Description: Using prefix array.

class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
vector <int> 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;
}
};
39 changes: 39 additions & 0 deletions Topic1_Arrays/Day2507/TrungHau/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time complexity: O(n ^ 3)
// Memory complexity: ....
// Description: Using loop to compare each element together.

#include <string>
class Solution {
public:
vector<string> commonChars(vector<string>& 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 <string> res(a.length());
if(a.length() != 0) {
for(int i = 0; i < a.length(); i++){
res[i] = a[i];
}
}
return res;
}
};
26 changes: 26 additions & 0 deletions Topic1_Arrays/Day2607/TrungHau/1.txt
Original file line number Diff line number Diff line change
@@ -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<int>& nums) {
vector<int> 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;
}
};
32 changes: 32 additions & 0 deletions Topic1_Arrays/Day2607/TrungHau/2.txt
Original file line number Diff line number Diff line change
@@ -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<int>& 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;j<n;j++){
if(nums[j]>nums[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;
}
}
}
};
20 changes: 20 additions & 0 deletions Topic1_Arrays/Day2607/TrungHau/3.txt
Original file line number Diff line number Diff line change
@@ -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<int>& 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_;
}
};
Loading