Last active
October 29, 2022 03:15
-
-
Save Merce0897/20020e68e2da0aa7b51f219daec8039a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Time: O(n) -- Space: O(1) -- Pair with target Sum | |
| const pair_with_targetsum = function ( | |
| arr: number[], | |
| target_sum: number, | |
| ): number[] { | |
| // Create two pointers | |
| let i = 0 | |
| let j = arr.length - 1 | |
| // Loop the array | |
| while (i < j) { | |
| let sum = arr[i] + arr[j] | |
| // return if equal target | |
| if (sum === target_sum) return [i,j] | |
| // Increase lower pointer if sum < target | |
| // Decrease higher pointer if sum > target | |
| if (sum > target_sum) { | |
| j-- | |
| } else if (sum < target_sum) { | |
| i++ | |
| } | |
| } | |
| return [-1, -1] | |
| }; | |
| // Time: O(n) -- Space: O(1) -- Remove Duplicate | |
| const remove_duplicates = function (arr: number[]): number { | |
| // Create two pointers | |
| let i = 0 | |
| let j = 0 | |
| // Loop the array | |
| while (j < arr.length) { | |
| // if found duplicate element, swap the element to start of array and count | |
| if (arr[j] !== arr[i]) { | |
| i++ | |
| arr[i] = arr[j] | |
| } | |
| j++ | |
| } | |
| return i + 1; | |
| }; | |
| // Time: O(n) -- Space: O(n) -- Squaring a sorted array | |
| const make_squares = function (nums: number[]): number[] { | |
| // set two pointers | |
| let i = 0 | |
| let j = nums.length - 1 | |
| // make result array and its pointer | |
| let result : number[] = [] | |
| result.length = nums.length | |
| let k = result.length - 1 | |
| // Loop the array | |
| while(i <= j) { | |
| // Create already square of elements | |
| let a = nums[i] * nums[i] | |
| let b = nums[j] * nums[j] | |
| // compare square of two pointer if higher will push to result array | |
| if(a > b) { | |
| result[k] = a | |
| k-- | |
| i++ | |
| } else { | |
| result[k] = b | |
| k-- | |
| j-- | |
| } | |
| } | |
| return result; | |
| }; | |
| // Time: O(n) -- Space: O(n ) -- Valid Palindrome | |
| function isPalindrome(s: string): boolean { | |
| // Create alphanumeric array from s | |
| let arr: string[] = [] | |
| // Push all alphanumeric element by Lower Case to array | |
| for(let i = 0; i < s.length; i++) { | |
| if(s[i].toLocaleLowerCase() !== s[i].toLocaleUpperCase() || !isNaN(parseInt(s[i]))) { | |
| arr.push(s[i].toLocaleLowerCase()) | |
| } | |
| } | |
| // Create pointers | |
| let i = 0 | |
| let j = arr.length - 1 | |
| // Check if array is palindrome | |
| while (i <= j) { | |
| if (arr[i] !== arr[j]) { | |
| return false | |
| } else { | |
| i++ | |
| j-- | |
| } | |
| } | |
| return true | |
| }; | |
| // Time: O(n) -- Space: O(1) -- Reverse String | |
| function reverseString(s: string[]): void { | |
| // Create pointers | |
| let i = 0 | |
| let j = s.length - 1 | |
| // Swap the element of array | |
| while (i <= j) { | |
| [s[i],s[j]] = [s[j],s[i]] | |
| i++ | |
| j-- | |
| } | |
| }; | |
| // Time: O(n) -- Space: O(1) -- Merge Sorted array | |
| function merge(nums1: number[], m: number, nums2: number[], n: number): void { | |
| // Create pointer to push element and use m,n as pointer to check | |
| let k = nums1.length - 1 | |
| m -= 1 | |
| n -= 1 | |
| // Loop the array | |
| while (k >= 0) { | |
| // Check when nums2 array already push all | |
| if (n < 0) { | |
| break; | |
| } | |
| // Compare element of m,n if higher push to k | |
| if (nums1[m] >= nums2[n]) { | |
| nums1[k] = nums1[m] | |
| m-- | |
| } else { | |
| nums1[k] = nums2[n] | |
| n-- | |
| } | |
| k-- | |
| } | |
| console.log(nums1); | |
| }; | |
| // Time: O(n) -- Space: O(1) -- Move Zeroes | |
| function moveZeroes(nums: number[]): void { | |
| // Create pointers | |
| let i = 0 | |
| let j = 0 | |
| // Loop the nums array | |
| while (i < nums.length) { | |
| // Check one pointer for the array | |
| // one pointer for the zero and swap | |
| if (nums[i] !== 0 && nums[j] === 0) { | |
| [nums[i], nums[j]] = [nums[j], nums[i]] | |
| j++ | |
| } | |
| if (nums[j] !== 0) { | |
| j++ | |
| } | |
| i++ | |
| } | |
| }; | |
| // Time: O(n) -- Space: O(1) -- Remove Duplicates from Sorted Array | |
| function removeDuplicates(nums: number[]): number { | |
| // Create pointers | |
| let i = 0 | |
| let j = 0 | |
| // Loop the nums array | |
| while (j < nums.length) { | |
| // One pointer to loop the array | |
| // One pointer to check the duplicate and swap | |
| if (nums[j] !== nums[i]) { | |
| i++ | |
| nums[i] = nums[j] | |
| } | |
| j++ | |
| } | |
| return i + 1; | |
| }; | |
| // Time: O(n) -- Space: O(1) -- Container with most water | |
| function maxArea(height: number[]): number { | |
| // Create pointers and the output | |
| let max = 0 | |
| let i = 0 | |
| let j = height.length - 1 | |
| // Loop the array | |
| while (i < j) { | |
| // Create area var and its parameters | |
| let h = Math.min(height[i],height[j]) | |
| let w = j - i | |
| let area = h * w | |
| // Check the area if higher output so assign to output | |
| if (max < area) { max = area } | |
| // Check the height of two pointer if smaller pointer will move | |
| if (height[i] < height[j]) { | |
| i++ | |
| } else { | |
| j-- | |
| } | |
| } | |
| return max; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/Merce0897/20020e68e2da0aa7b51f219daec8039a#file-two_pointer-ts-L203