Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions Topic1_Arrays/Day2207/Tung/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Description: Use two Loop
// Time complexity: O(n ^ 2)
// Memory complexity: O(1)

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> kq ;
Copy link
Owner

Choose a reason for hiding this comment

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

Use English variable name. Sample "result"
Try to find better solution instead of brute force solution

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;

}
};

1 change: 1 addition & 0 deletions Topic1_Arrays/Day2307/Tung/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

29 changes: 29 additions & 0 deletions Topic1_Arrays/Day2307/Tung/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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 complexity: O (n log(n))
// Memory complexity: O (n)
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
Copy link
Owner

Choose a reason for hiding this comment

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

Good!

sort(intervals.begin() , intervals.end());

vector<vector<int>> 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;

}
};
27 changes: 27 additions & 0 deletions Topic1_Arrays/Day2307/Tung/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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<int>& 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;
}
};
23 changes: 23 additions & 0 deletions Topic1_Arrays/Day2407/Tung/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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<vector<string>> groupAnagrams(vector<string>& strs) {
map<string, vector<string> >mp;

for(auto str:strs){
string s = str;
sort(s.begin(), s.end());
mp[s].push_back(str);
}

vector<vector<string>> ans;
for(auto it = mp.begin(); it != mp.end(); it++){
ans.push_back(it->second);
}
return ans;
}
};
19 changes: 19 additions & 0 deletions Topic1_Arrays/Day2407/Tung/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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<int>& nums, int k) {
priority_queue<int , vector<int> , greater<int>> pq;
for(int i = 0 ; i < nums.size() ; i++)
{
pq.push(nums[i]);
if(pq.size() > k)
{
pq.pop();
}
}
return pq.top();
return 0;
}
};
42 changes: 42 additions & 0 deletions Topic1_Arrays/Day2407/Tung/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Description: Use pointer left and right.
// Time complexity: O(n ^ 2)
// Memory complexity: O(k)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> 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;
}
};
27 changes: 27 additions & 0 deletions Topic1_Arrays/Day2507/Tung/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Description: Use pointer left and right.
// Time complexity: O(n).
// Memory complexity: O(1).
class Solution {
public:
int maxArea(vector<int>& 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;
}
};
33 changes: 33 additions & 0 deletions Topic1_Arrays/Day2507/Tung/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Description: Use unordered_map.
// Time complexity: O(n).
// Memory complexity: O(n).
#include <vector>
#include <unordered_map>
using namespace std;

class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map<int, int> 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).
31 changes: 31 additions & 0 deletions Topic1_Arrays/Day2507/Tung/3.txt
Original file line number Diff line number Diff line change
@@ -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<string> commonChars(vector<string>& words) {
vector<string> result;
vector<int> min_fred(26 , 1000);
for(string word : words)
{
vector<int> 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;
}
};