From ee7617904c6d83f9d08ae92de43d0e701e320e5d Mon Sep 17 00:00:00 2001 From: Hoang Minh Tuan Date: Sat, 28 Sep 2024 21:48:00 +0700 Subject: [PATCH 1/8] 25_9 --- .../2509/HMTuan/KthLargestElementInAnArray.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt diff --git a/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt b/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt new file mode 100644 index 0000000..82a3256 --- /dev/null +++ b/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt @@ -0,0 +1,15 @@ +// 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) +// *note: initial case where prefixSum = 0 at index -1 + +class Solution { +public: + int findKthLargest(vector& nums, int k) { + priority_queue pq(nums.begin(), nums.end()); + for (int i = 0; i < k - 1; i++) { + pq.pop(); + } + return pq.top(); + } +}; \ No newline at end of file From dcef39129c12b123fdae2c5cb20474506f938441 Mon Sep 17 00:00:00 2001 From: Hoang Minh Tuan <104216461+hmintusn@users.noreply.github.com> Date: Mon, 30 Sep 2024 07:23:53 +0700 Subject: [PATCH 2/8] Update KthLargestElementInAnArray.txt --- Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt b/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt index 82a3256..28e7680 100644 --- a/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt +++ b/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt @@ -1,7 +1,7 @@ // 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) -// *note: initial case where prefixSum = 0 at index -1 + class Solution { public: @@ -12,4 +12,4 @@ public: } return pq.top(); } -}; \ No newline at end of file +}; From 5ce8bdee92b7100c6102ca7ac1d8b9c2c4886e09 Mon Sep 17 00:00:00 2001 From: Hoang Minh Tuan Date: Mon, 30 Sep 2024 07:51:50 +0700 Subject: [PATCH 3/8] excersises --- .../HMTuan/KthLargestElementInAnArray.txt | 2 +- .../2609/HMTuan/ContainerWithMostWater.txt | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 Topic1_Array_2309/2609/HMTuan/ContainerWithMostWater.txt diff --git a/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt b/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt index 82a3256..9562d90 100644 --- a/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt +++ b/Topic1_Array_2309/2509/HMTuan/KthLargestElementInAnArray.txt @@ -1,7 +1,7 @@ // 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) -// *note: initial case where prefixSum = 0 at index -1 +// class Solution { public: diff --git a/Topic1_Array_2309/2609/HMTuan/ContainerWithMostWater.txt b/Topic1_Array_2309/2609/HMTuan/ContainerWithMostWater.txt new file mode 100644 index 0000000..1a6e544 --- /dev/null +++ b/Topic1_Array_2309/2609/HMTuan/ContainerWithMostWater.txt @@ -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; + } +} \ No newline at end of file From 592fcaabdc6e33da603e615c930e32d078ac510d Mon Sep 17 00:00:00 2001 From: Hoang Minh Tuan Date: Wed, 2 Oct 2024 09:08:46 +0700 Subject: [PATCH 4/8] contiguous arr --- .../2709/HMTuan/ContiguousArray.txt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Topic1_Array_2309/2709/HMTuan/ContiguousArray.txt diff --git a/Topic1_Array_2309/2709/HMTuan/ContiguousArray.txt b/Topic1_Array_2309/2709/HMTuan/ContiguousArray.txt new file mode 100644 index 0000000..7684cf3 --- /dev/null +++ b/Topic1_Array_2309/2709/HMTuan/ContiguousArray.txt @@ -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 + HashMap 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; + } +} \ No newline at end of file From 1059683d54c740a93f896fe7179e4c250df73520 Mon Sep 17 00:00:00 2001 From: Hoang Minh Tuan Date: Fri, 4 Oct 2024 07:34:31 +0700 Subject: [PATCH 5/8] array --- .../2709/HMTuan/NextPermutation.txt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Topic1_Array_2309/2709/HMTuan/NextPermutation.txt diff --git a/Topic1_Array_2309/2709/HMTuan/NextPermutation.txt b/Topic1_Array_2309/2709/HMTuan/NextPermutation.txt new file mode 100644 index 0000000..e1aefc1 --- /dev/null +++ b/Topic1_Array_2309/2709/HMTuan/NextPermutation.txt @@ -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& 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()); + } + } +}; \ No newline at end of file From 03a16cc4edafb1209a85258b97d0d27b317bb4ac Mon Sep 17 00:00:00 2001 From: Hoang Minh Tuan Date: Fri, 4 Oct 2024 15:58:08 +0700 Subject: [PATCH 6/8] array --- .../HMTuan/LongestConsecutiveSequence.txt | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Topic1_Array_2309/2809/HMTuan/LongestConsecutiveSequence.txt diff --git a/Topic1_Array_2309/2809/HMTuan/LongestConsecutiveSequence.txt b/Topic1_Array_2309/2809/HMTuan/LongestConsecutiveSequence.txt new file mode 100644 index 0000000..0882925 --- /dev/null +++ b/Topic1_Array_2309/2809/HMTuan/LongestConsecutiveSequence.txt @@ -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 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; + } +} \ No newline at end of file From c627f4f55b6807dc65cd728acb065002eda84a08 Mon Sep 17 00:00:00 2001 From: Hoang Minh Tuan Date: Sun, 6 Oct 2024 08:48:06 +0700 Subject: [PATCH 7/8] array 2809 --- .../2809/HMTuan/TopKFrequentElements.txt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Topic1_Array_2309/2809/HMTuan/TopKFrequentElements.txt diff --git a/Topic1_Array_2309/2809/HMTuan/TopKFrequentElements.txt b/Topic1_Array_2309/2809/HMTuan/TopKFrequentElements.txt new file mode 100644 index 0000000..b87ec10 --- /dev/null +++ b/Topic1_Array_2309/2809/HMTuan/TopKFrequentElements.txt @@ -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 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[] 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 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]; + } +} \ No newline at end of file From 9bff6611547665dfff0b56580c1c8c68387d1b49 Mon Sep 17 00:00:00 2001 From: Hoang Minh Tuan Date: Mon, 7 Oct 2024 07:34:17 +0700 Subject: [PATCH 8/8] bagoftokens --- Topic1_Array_2309/2909/HMTuan/BagOfTokens.txt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Topic1_Array_2309/2909/HMTuan/BagOfTokens.txt diff --git a/Topic1_Array_2309/2909/HMTuan/BagOfTokens.txt b/Topic1_Array_2309/2909/HMTuan/BagOfTokens.txt new file mode 100644 index 0000000..c9f9cd9 --- /dev/null +++ b/Topic1_Array_2309/2909/HMTuan/BagOfTokens.txt @@ -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; + } +} \ No newline at end of file