From 1855c9b4a1ccf0605c36dbee7999fd339657aa89 Mon Sep 17 00:00:00 2001 From: Undefined-dev74 Date: Thu, 8 Aug 2024 09:15:06 +0530 Subject: [PATCH 1/7] Add optimized twoSum function with hashmap for O(n) time complexity --- src/App.tsx | 2 ++ src/practice/index.tsx | 9 +++++++++ src/practice/twoSum.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 src/practice/index.tsx create mode 100644 src/practice/twoSum.ts diff --git a/src/App.tsx b/src/App.tsx index 0896ba7..521ae75 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -51,6 +51,7 @@ import { } from "./leetcode/sortArrayOfZeronOnes"; import { unionIntersection } from "./leetcode/unionIntersection"; import { unionOfSortedArray } from "./leetcode/unionOfSortedArray"; +import Practice from "./practice"; import SortingVisualizer from "./views/SortingVisualizer"; function App() { @@ -163,6 +164,7 @@ function App() { return ( <> +

Vite + React

Radix Sort{" "} diff --git a/src/practice/index.tsx b/src/practice/index.tsx new file mode 100644 index 0000000..17253af --- /dev/null +++ b/src/practice/index.tsx @@ -0,0 +1,9 @@ +import { twoSum, twoSumOptimized } from "./twoSum"; + +const Practice = () => { + console.log(twoSum([1, 2, 3, 4, 5, 6, 7], 10)); + console.log(twoSumOptimized([1, 2, 3, 4, 5, 6, 7], 10)); + return
Practice
; +}; + +export default Practice; diff --git a/src/practice/twoSum.ts b/src/practice/twoSum.ts new file mode 100644 index 0000000..636b2a5 --- /dev/null +++ b/src/practice/twoSum.ts @@ -0,0 +1,28 @@ +export const twoSum = (nums: number[], target: number): number[] => { + let left = 0; + let right = left + 1; + const result = []; + while (left < nums.length) { + if (nums[left] + nums[right] === target) { + result.push(left, right); + break; + } else if (right >= nums.length) { + left++; + right = left + 1; + } else right++; + } + return result; +}; + +export const twoSumOptimized = (nums: number[], target: number): number[] => { + const map = new Map(); + console.log("MAP", map); + for (let i = 0; i < nums.length; i++) { + const diff = target - nums[i]; + if (map.has(diff)) { + return [map.get(diff), i]; + } + map.set(nums[i], i); + } + return []; +}; From 58cb7230f66dd97717e0f2cc64557b365ab28511 Mon Sep 17 00:00:00 2001 From: Undefined-dev74 Date: Thu, 8 Aug 2024 10:06:20 +0530 Subject: [PATCH 2/7] Add twoSumSorted function with O(n) time complexity and O(1) space complexity --- src/practice/twoSumSorted.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/practice/twoSumSorted.ts diff --git a/src/practice/twoSumSorted.ts b/src/practice/twoSumSorted.ts new file mode 100644 index 0000000..7298c1f --- /dev/null +++ b/src/practice/twoSumSorted.ts @@ -0,0 +1,17 @@ +export const twoSumSorted = (numbers: number[], target: number): number[] => { + let left = 0; + let right = numbers.length - 1; + while (left < right) { + const sum = numbers[left] + numbers[right]; + if (sum === target) { + return [left, right]; + } else if (sum > target) { + right--; + } else { + left++; + } + } + return []; +}; +// this is the best optimized solution for twoSumSorted +// Time Complexity: O(n) Space Complexity: O(1) where n is the length of the input array From ff959f9d7cc08c9e98ea3017394b2a5aaadd8064 Mon Sep 17 00:00:00 2001 From: Undefined-dev74 Date: Sun, 11 Aug 2024 23:23:53 +0530 Subject: [PATCH 3/7] feat: Add solution for threeSumClosest problem --- src/practice/threeSumClosest.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/practice/threeSumClosest.ts diff --git a/src/practice/threeSumClosest.ts b/src/practice/threeSumClosest.ts new file mode 100644 index 0000000..6ff8df5 --- /dev/null +++ b/src/practice/threeSumClosest.ts @@ -0,0 +1,30 @@ +const threeSumClosest = (nums: number[], target: number): number => { + let closest = Infinity; + nums.sort((a, b) => a - b); + + for (let k = 0; k < nums.length - 2; k++) { + let i = k + 1; + let j = nums.length - 1; + + while (i < j) { + const sum = nums[k] + nums[i] + nums[j]; + + if (Math.abs(target - sum) < Math.abs(target - closest)) { + closest = sum; + } + + if (sum < target) { + i++; + } else { + j--; + } + } + } + return closest; +}; + +const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +console.log(threeSumClosest(nums, 20)); // 21 + +// Time Complexity: O(n^2) - n is the length of the input array +// Space Complexity: O(1) - no extra space is used From b6c1687219b2734c45ef8414a65611768ea4daad Mon Sep 17 00:00:00 2001 From: Undefined-dev74 Date: Mon, 12 Aug 2024 07:43:13 +0530 Subject: [PATCH 4/7] refactor: Improve threeSumClosest algorithm efficiency --- src/practice/threeSumClosest.ts | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/practice/threeSumClosest.ts b/src/practice/threeSumClosest.ts index 6ff8df5..228e6b8 100644 --- a/src/practice/threeSumClosest.ts +++ b/src/practice/threeSumClosest.ts @@ -1,23 +1,25 @@ const threeSumClosest = (nums: number[], target: number): number => { - let closest = Infinity; + let closest = Infinity; // first we will define it as infinity, since we have to find the closest sum + // step 1: sort the array nums.sort((a, b) => a - b); - - for (let k = 0; k < nums.length - 2; k++) { - let i = k + 1; - let j = nums.length - 1; - - while (i < j) { - const sum = nums[k] + nums[i] + nums[j]; - + // step 2: iterate through the array + // we will iterate till the 3rd last element, since we have to find the sum of 3 element + // left pointer will be i + 1, since i will be the first element starting from 0th index + for (let i = 0; i < nums.length - 2; i++) { + // step 3: define left and right pointer + // right pointer will be the last element + let left = i + 1; + let right = nums.length - 1; + while (left < right) { + // step 4: find the sum of 3 elements + const sum = nums[i] + nums[left] + nums[right]; if (Math.abs(target - sum) < Math.abs(target - closest)) { - closest = sum; + // if the sum is closer to the target than the closest sum + closest = sum; // update the closest sum } - if (sum < target) { - i++; - } else { - j--; - } + left++; + } else right--; } } return closest; From 80d2d74f91fb849532160e44f80512c58d2500fc Mon Sep 17 00:00:00 2001 From: Undefined-dev74 Date: Mon, 12 Aug 2024 21:40:13 +0530 Subject: [PATCH 5/7] feat: Add factorial function --- src/practice/factorial.ts | 4 ++++ src/practice/index.tsx | 2 ++ 2 files changed, 6 insertions(+) create mode 100644 src/practice/factorial.ts diff --git a/src/practice/factorial.ts b/src/practice/factorial.ts new file mode 100644 index 0000000..68b9d30 --- /dev/null +++ b/src/practice/factorial.ts @@ -0,0 +1,4 @@ +export const factorial = (n: number): number => { + if (n === 1) return 1; + return n * factorial(n - 1); +}; diff --git a/src/practice/index.tsx b/src/practice/index.tsx index 17253af..5fc2eff 100644 --- a/src/practice/index.tsx +++ b/src/practice/index.tsx @@ -1,8 +1,10 @@ +import { factorial } from "./factorial"; import { twoSum, twoSumOptimized } from "./twoSum"; const Practice = () => { console.log(twoSum([1, 2, 3, 4, 5, 6, 7], 10)); console.log(twoSumOptimized([1, 2, 3, 4, 5, 6, 7], 10)); + console.log(factorial(5)); return
Practice
; }; From 8a6f33740b8a7cce2a5a7b828c50c4ebda4f0dbb Mon Sep 17 00:00:00 2001 From: Undefined-dev74 Date: Mon, 12 Aug 2024 21:59:50 +0530 Subject: [PATCH 6/7] feat: Add reverseStringRecursion function using recursion method --- src/practice/index.tsx | 2 ++ src/practice/reverseStringRecursion.ts | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 src/practice/reverseStringRecursion.ts diff --git a/src/practice/index.tsx b/src/practice/index.tsx index 5fc2eff..6eaf983 100644 --- a/src/practice/index.tsx +++ b/src/practice/index.tsx @@ -1,10 +1,12 @@ import { factorial } from "./factorial"; +import { reverseStringRecursion } from "./reverseStringRecursion"; import { twoSum, twoSumOptimized } from "./twoSum"; const Practice = () => { console.log(twoSum([1, 2, 3, 4, 5, 6, 7], 10)); console.log(twoSumOptimized([1, 2, 3, 4, 5, 6, 7], 10)); console.log(factorial(5)); + console.log(reverseStringRecursion("hello")); return
Practice
; }; diff --git a/src/practice/reverseStringRecursion.ts b/src/practice/reverseStringRecursion.ts new file mode 100644 index 0000000..1dba62d --- /dev/null +++ b/src/practice/reverseStringRecursion.ts @@ -0,0 +1,10 @@ +const reverse = (str: string, idx: number): string => { + if (idx < 0) { + return ""; + } + return str[idx] + reverse(str, idx - 1); +}; + +export const reverseStringRecursion = (str: string): string => { + return reverse(str, str.length - 1); +}; From e81473e43bf5c4e87afe7a7b7d0a3cb3c9dd09d5 Mon Sep 17 00:00:00 2001 From: Undefined-dev74 Date: Sat, 6 Sep 2025 00:10:02 +0530 Subject: [PATCH 7/7] refactor: Update import statements and use createRoot for rendering --- src/main.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index 3d7150d..f38d4f6 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,10 +1,10 @@ -import React from 'react' -import ReactDOM from 'react-dom/client' -import App from './App.tsx' -import './index.css' +import * as React from "react"; +import { createRoot } from "react-dom/client"; +import App from "./App.tsx"; +import "./index.css"; -ReactDOM.createRoot(document.getElementById('root')!).render( +createRoot(document.getElementById("root")!).render( - , -) + +);