mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
Leetcode day1
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
@@ -0,0 +1,18 @@
|
||||
class Solution {
|
||||
public:
|
||||
int lengthOfLongestSubstring(string s) {
|
||||
int n =s.length();
|
||||
vector<int>charIndex(128,-1);
|
||||
int left = 0;
|
||||
int maxlen =0;
|
||||
for(int right =0;right<n;right++){
|
||||
if(charIndex[s[right]]>=left){
|
||||
left = charIndex[s[right]]+1;
|
||||
}
|
||||
charIndex[s[right]] = right;
|
||||
maxlen = max(maxlen, right-left+1);
|
||||
}
|
||||
return maxlen;
|
||||
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
class Solution{
|
||||
public:
|
||||
int LengthOfLongestSubString(string s){
|
||||
int n = s.length();
|
||||
int maxlen = 0;
|
||||
unordered_set<char> charset;
|
||||
int left = 0;
|
||||
|
||||
for(int right = 0;right<n;right++){
|
||||
if(charset.count(s[right])==0){
|
||||
charset.insert(s[right]);
|
||||
maxlen = max(maxlen, right-left+1);
|
||||
}while(charset.count(s[right])){
|
||||
charset.erase(s[left]);
|
||||
left++;
|
||||
}
|
||||
}
|
||||
return maxlen;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
My first sliding window problem.
|
||||
Sliding Window is an algorithmic technique used to process a contiguous subarray or substring by maintaining a moving window over the input, where thewindow expands and/or shrinks while traversing the data to efficiently compute results in linear time.
|
||||
|
||||
When to use Sliding Window :
|
||||
|
||||
>Subarray / substring problems
|
||||
|
||||
>Problems asking for:
|
||||
|
||||
1)longest / shortest window
|
||||
|
||||
2)maximum / minimum sum
|
||||
|
||||
3)unique elements in a range
|
||||
|
||||
4)Continuous range constraints
|
||||
@@ -0,0 +1,40 @@
|
||||
class Solution {
|
||||
public:
|
||||
double findMedianSortedArrays(vector<int>& nums1, vector<int>& 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]<nums2[j]){
|
||||
m1= nums1[i++];
|
||||
}
|
||||
else{
|
||||
m1 = nums2[j++];
|
||||
}
|
||||
}
|
||||
else if(i<a){
|
||||
m1 = nums1[i++];
|
||||
}
|
||||
else {
|
||||
m1 = nums2[j++];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
if((a+b)%2==1){
|
||||
return (double)m1;
|
||||
}
|
||||
else{
|
||||
return (m1+m2)/2.0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
class Solution{
|
||||
public :
|
||||
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2){
|
||||
int n = nums1.size();
|
||||
int m = nums2.size();
|
||||
vector<int> 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;
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user