From 245116d1813bc66d6271822c60cc093332e89d2b Mon Sep 17 00:00:00 2001 From: Wander_Lust Date: Sun, 25 Jan 2026 12:41:33 +0530 Subject: [PATCH] Leetcode day1 --- leetcode/{two-sum => lc1}/TwoPass.cpp | 0 leetcode/{two-sum => lc1}/brute.cpp | 0 leetcode/{two-sum => lc1}/notes.md | 0 leetcode/{two-sum => lc1}/onePass.cpp | 0 leetcode/lc2/addNmrs.cpp | 27 ++++++++++++++++++ leetcode/lc2/notes.md | 2 ++ leetcode/lc3/NoDuplicatesSS.cpp | 18 ++++++++++++ leetcode/lc3/UsingCharSet.cpp | 22 +++++++++++++++ leetcode/lc3/notes.md | 16 +++++++++++ leetcode/lc4/TwoPointerMed.cpp | 40 +++++++++++++++++++++++++++ leetcode/lc4/bruteMedian.cpp | 21 ++++++++++++++ leetcode/lc4/notes.md | 5 ++++ leetcode/lc9/Palindrome.cpp | 14 ++++++++++ leetcode/lc9/notes.md | 3 ++ 14 files changed, 168 insertions(+) rename leetcode/{two-sum => lc1}/TwoPass.cpp (100%) rename leetcode/{two-sum => lc1}/brute.cpp (100%) rename leetcode/{two-sum => lc1}/notes.md (100%) rename leetcode/{two-sum => lc1}/onePass.cpp (100%) create mode 100644 leetcode/lc2/addNmrs.cpp create mode 100644 leetcode/lc2/notes.md create mode 100644 leetcode/lc3/NoDuplicatesSS.cpp create mode 100644 leetcode/lc3/UsingCharSet.cpp create mode 100644 leetcode/lc3/notes.md create mode 100644 leetcode/lc4/TwoPointerMed.cpp create mode 100644 leetcode/lc4/bruteMedian.cpp create mode 100644 leetcode/lc4/notes.md create mode 100644 leetcode/lc9/Palindrome.cpp create mode 100644 leetcode/lc9/notes.md diff --git a/leetcode/two-sum/TwoPass.cpp b/leetcode/lc1/TwoPass.cpp similarity index 100% rename from leetcode/two-sum/TwoPass.cpp rename to leetcode/lc1/TwoPass.cpp diff --git a/leetcode/two-sum/brute.cpp b/leetcode/lc1/brute.cpp similarity index 100% rename from leetcode/two-sum/brute.cpp rename to leetcode/lc1/brute.cpp diff --git a/leetcode/two-sum/notes.md b/leetcode/lc1/notes.md similarity index 100% rename from leetcode/two-sum/notes.md rename to leetcode/lc1/notes.md diff --git a/leetcode/two-sum/onePass.cpp b/leetcode/lc1/onePass.cpp similarity index 100% rename from leetcode/two-sum/onePass.cpp rename to leetcode/lc1/onePass.cpp diff --git a/leetcode/lc2/addNmrs.cpp b/leetcode/lc2/addNmrs.cpp new file mode 100644 index 0000000..c086731 --- /dev/null +++ b/leetcode/lc2/addNmrs.cpp @@ -0,0 +1,27 @@ +class Solution{ + public: + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2){ + ListNode* dummyhead = new ListNode(0); + ListNode* tail = dummyhead; + bool carry = 0; + + while(l1 || l2|| carry){ + int digit1 = (l1!=nullptr) ? l1->val : 0; + int digit2 = (l2!=nullptr) ? l2->val : 0; + + int sum = digit1+digit2+carry; + int digit = sum%10; + carry = sum/10; + + ListNode* newnode = new ListNode*(digit); + tail->next = newnode; + tail = tail->next; + + l1 = (l1!=nullptr) ? l1=>next : nullptr; + l2 = (l2!=nullptr) ? l2->next : nullptr; + } + ListNode* result = dummyhead->next; + delete dummyhead; + return result; + } +} diff --git a/leetcode/lc2/notes.md b/leetcode/lc2/notes.md new file mode 100644 index 0000000..7625daf --- /dev/null +++ b/leetcode/lc2/notes.md @@ -0,0 +1,2 @@ +The intuition behind this problem kind of reminded me of the CarryLook-Ahead adder (CLA) which I funnily enough know through minecraft builds. +its very similar to pen paper additon which starts from least significant bit and we got a seperate carry bit to add it in parallel. A solid solution indeed. diff --git a/leetcode/lc3/NoDuplicatesSS.cpp b/leetcode/lc3/NoDuplicatesSS.cpp new file mode 100644 index 0000000..2fb50df --- /dev/null +++ b/leetcode/lc3/NoDuplicatesSS.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int n =s.length(); + vectorcharIndex(128,-1); + int left = 0; + int maxlen =0; + for(int right =0;right=left){ + left = charIndex[s[right]]+1; + } + charIndex[s[right]] = right; + maxlen = max(maxlen, right-left+1); + } + return maxlen; + + } +}; diff --git a/leetcode/lc3/UsingCharSet.cpp b/leetcode/lc3/UsingCharSet.cpp new file mode 100644 index 0000000..135cd25 --- /dev/null +++ b/leetcode/lc3/UsingCharSet.cpp @@ -0,0 +1,22 @@ +class Solution{ + public: + int LengthOfLongestSubString(string s){ + int n = s.length(); + int maxlen = 0; + unordered_set charset; + int left = 0; + + for(int right = 0;rightSubarray / substring problems + +>Problems asking for: + +1)longest / shortest window + +2)maximum / minimum sum + +3)unique elements in a range + +4)Continuous range constraints diff --git a/leetcode/lc4/TwoPointerMed.cpp b/leetcode/lc4/TwoPointerMed.cpp new file mode 100644 index 0000000..1187c4a --- /dev/null +++ b/leetcode/lc4/TwoPointerMed.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + int a = nums1.size(); + int b = nums2.size(); + int i=0,j=0,m1=0,m2=0; + + for(int count=0;count<=(a+b)/2;count++){ + m2=m1; + if(i!=a && j!=b){ + if(nums1[i]& nums1, vector& nums2){ + int n = nums1.size(); + int m = nums2.size(); + vector merger; + for(auto nums: nums1) + merger.push_back(nums); + for(auto nums : nums2) + merger.push_back(nums); + sort(merger.begin(), meger.end()); + + int size = meger.size(); + if(size%2==1) + return (double) merger[size/2]; + else + return (double) (merger[size/2]+ merger[size/2 - 1])/2.0; + + + } +}; diff --git a/leetcode/lc4/notes.md b/leetcode/lc4/notes.md new file mode 100644 index 0000000..7e9d5c2 --- /dev/null +++ b/leetcode/lc4/notes.md @@ -0,0 +1,5 @@ +The brute force approach is quite intuitive but doesnt meet the specified Run time complexity of O(log(m+n)) instead takes O((m+n)log(m+n)) taken by sorting + +TwoPointer method takes O(n+m) where we only merge till half and then extract the median which is the middle element + +Yet to learn binarySearch approach diff --git a/leetcode/lc9/Palindrome.cpp b/leetcode/lc9/Palindrome.cpp new file mode 100644 index 0000000..5f7285c --- /dev/null +++ b/leetcode/lc9/Palindrome.cpp @@ -0,0 +1,14 @@ +class Solution{ + public: + bool isPalindrome(int x){ + int rev = 0; + if(x<0 || x%10 == 0 && x!=0){ + return false; + } + while(x>rev){ + rev = (rev*10) + (x%10); + x/=10; + } + return ( x==rev || x = rev/10); + } +} diff --git a/leetcode/lc9/notes.md b/leetcode/lc9/notes.md new file mode 100644 index 0000000..142eac8 --- /dev/null +++ b/leetcode/lc9/notes.md @@ -0,0 +1,3 @@ +An easy problem but with a common pitfall hence a tricky one. +The idea is to only check till half of the number and not full where rev*10 exceeds int's range for larger values +Two pointer method is quite verbose as we check equality from either side using two pointers and return false if there's a mismatch