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
19 changes: 19 additions & 0 deletions Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Time complexity: O(nlogn) - 1 element insertion into heap is log n
// Space complexity: O(n)
// Explanation: Using priority_queue (built on top of the max heap)
<<<<<<< HEAD
//
=======

>>>>>>> 984e867d74654f4f3d11443740c2cdb345a31c91

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> pq(nums.begin(), nums.end());
for (int i = 0; i < k - 1; i++) {
pq.pop();
}
return pq.top();
}
};
24 changes: 24 additions & 0 deletions Topic1_Array_2309/2609/HMTuan/ContainerWithMostWater.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Time complexity: O(n)
// Space complexity: O(1)
// Explanation: Using 2 pointer left and right.
// Greedy: Fix the pointer (left or right) with highest high, and move the another one

class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;

int maxWater = 0;
while(left < right){
int dis = right - left;
int currWater = Math.min(height[left], height[right]) * dis;
maxWater = Math.max(maxWater, currWater);
if(height[left] > height[right]){
--right;
}else{
++left;
}
}
return maxWater;
}
}
30 changes: 30 additions & 0 deletions Topic1_Array_2309/2709/HMTuan/ContiguousArray.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time complexity: O(n)
// Space complexity: O(n)
// Explanation: Using hash map to store different between number of zeros and ones at the current index

class Solution {
public int findMaxLength(int[] nums) {
// Hashmap to store <difference between 0s and 1s, index>
HashMap<Integer, Integer> hm = new HashMap<>();

int zeroNum = 0, oneNum = 0, maxLen = 0;
hm.put(0, -1); // base case
for(int i = 0; i < nums.length; i++){
if(nums[i] == 0)
++zeroNum;
else
++oneNum;

int diff = oneNum - zeroNum;
if(hm.containsKey(diff)){
// If the current difference has been encountered before
// calculate the length of the subarray
int currLen = i - hm.get(diff);
maxLen = Math.max(maxLen, currLen);
}else{
hm.put(diff, i);
}
}
return maxLen;
}
}
27 changes: 27 additions & 0 deletions Topic1_Array_2309/2709/HMTuan/NextPermutation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time complexity: O(n)
// Space complexity: O(1)
// Explanation: Find the first decreasing element from the end.
// If, none -> last permutation -> reverse all array
// Otherwise, swap it with the next larger element and reverse the suffix

class Solution {
public:
void nextPermutation(vector<int>& nums) {
if(nums.size() <= 1) return;

int i = nums.size() - 2;
while(i >= 0 && nums[i] >= nums[i+1]){
--i;
}
if(i == -1){
// last case: the last permutation
return reverse(nums.begin(), nums.end());
}
else{
int j = nums.size() - 1;
while(nums[i] >= nums[j]) --j;
swap(nums[i], nums[j]);
reverse(nums.begin() + i + 1, nums.end());
}
}
};
31 changes: 31 additions & 0 deletions Topic1_Array_2309/2809/HMTuan/LongestConsecutiveSequence.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Time complexity: O(n) - n + n = time of pushing element into set + time to handle task
// Space complexity: O(n)
// Explanation: Using Set to store the element of array
// Iterate through array, If set do not contains currNum -1 -> currNum is not the start of ConSeq
// Otherwise, while loop until the next element (currNum + 1) is contained or not

class Solution {
public int longestConsecutive(int[] nums) {
// if(nums.length == 0) return 0;
Set<Integer> s = new HashSet<>();
for(int num : nums)
s.add(num);

int longestConSeq = 0;
for(int num : nums){
// if set do not contains num - 1 -> num is start number
if(!s.contains(num-1)){
int currNum = num;
int currConSeq = 1;

// if the next element is in the set -> increase currConSeq and currNum
while(s.contains(currNum+1)){
++currConSeq;
++currNum;
}
longestConSeq = Math.max(longestConSeq, currConSeq);
}
}
return longestConSeq;
}
}
37 changes: 37 additions & 0 deletions Topic1_Array_2309/2809/HMTuan/TopKFrequentElements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time complexity: O(n)
// Space complexity: O(n)
// Explanation: Using hash map to count frequency of each element

class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for(int num : nums){
map.put(num, map.getOrDefault(num, 0) + 1);
}

// Using freq array to store the value of element that has the same freq
List<Integer>[] freq = new ArrayList[nums.length + 1];
for (int i = 0; i < freq.length; i++) {
freq[i] = new ArrayList<>();
}

// Iterate through hashmap to add list to freq array
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int frequency = entry.getValue();
freq[frequency].add(entry.getKey());
}

int[] res = new int[k];
int idx = 0;
for (int i = freq.length - 1; i >= 0; i--) {
for (int num : freq[i]) {
res[idx++] = num;
if (idx == k) {
return res;
}
}
}

return new int[0];
}
}
27 changes: 27 additions & 0 deletions Topic1_Array_2309/2909/HMTuan/BagOfTokens.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time complexity: O(nlogn + n) - time of sorting + time of iterating
// Space complexity: O(1)
// Explanation: Using Greedy algo for ascending-sorted array (where the left is min, right is max)
// Play min token (left) whenever possible

class Solution {
public int bagOfTokensScore(int[] tokens, int power) {
int scr = 0, maxScr = 0;
Arrays.sort(tokens);
int l = 0, r = tokens.length -1;
while(l <= r){
if(power >= tokens[l]){
power -= tokens[l];
scr++;
maxScr = Math.max(scr, maxScr);
++l;
}else if(scr >= 1){
power += tokens[r];
scr--;
--r;
}else{
++l;
}
}
return maxScr;
}
}