Skip to content

Commit

Permalink
Merge branch 'neetcode-gh:main' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
tharunkanna14 authored Oct 22, 2022
2 parents 4863935 + e7fbd4b commit 840e429
Show file tree
Hide file tree
Showing 47 changed files with 1,257 additions and 182 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/build-readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@ jobs:
Build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x]

steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
fetch-depth: 1

- name: Use Node.js (dependency)
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
node-version: 16

- name: Completion Table
run: node ./.github/workflows/updateCompletionTable.js;

- name: Check for modified files
id: git-check
run: echo ::set-output name=modified::$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)
run: echo modified=$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi) >> $GITHUB_OUTPUT

- name: Push
if: steps.git-check.outputs.modified == 'true'
Expand Down
32 changes: 16 additions & 16 deletions README.md

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions c/4-Median-of-Two-Sorted-Arrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Time: log(min(n, m))
Space: O(1)
*/

#define min(x, y) ((x < y) ? (x) : (y))
#define max(x, y) ((x > y) ? (x) : (y))

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
int* A = nums1;
int* B = nums2;
int ASize = nums1Size;
int BSize = nums2Size;

int total = nums1Size + nums2Size;
int half = total / 2;

if(nums2Size < nums1Size)
{
A = nums2;
B = nums1;
ASize = nums2Size;
BSize = nums1Size;
}

int l = 0;
int r = ASize - 1;

while(true)
{
int i = l + ((r - l - 2 + 1) / 2); // A Mid, round down instead of rounding towards 0
int j = half - i - 2; // B Mid


int Aleft = i >= 0 ? A[i] : INT_MIN;
int Aright = (i + 1) < ASize ? A[i + 1] : INT_MAX;
int Bleft = j >= 0 ? B[j] : INT_MIN;
int Bright = (j + 1) < BSize ? B[j + 1] : INT_MAX;

// partition is correct
if(Aleft <= Bright && Bleft <= Aright)
{
// Odd
if(total % 2)
{
return min(Aright, Bright);
}

// Even
return (max(Aleft, Bleft) + min(Aright, Bright)) / 2.0;
}
else if(Aleft > Bright)
{
r = i - 1;
}
else
{
l = i + 1;
}
}

}
39 changes: 39 additions & 0 deletions cpp/1209-Remove-All-Adjacent-Duplicates-in-String-II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time and space complexity is O(n) where n is the size of the input string.
class Solution {
public:
string removeDuplicates(string s, int k) {
stack<pair<char , int>> st;

for(int i = 0 ; i < s.size(); i++)
{
int count = 1;
if(!st.empty() && st.top().first == s[i])
{
count += st.top().second;
st.pop();
}

st.push({s[i] , count});

if(count == k) st.pop();

}

string ans = "";
while(!st.empty())
{
int freq = st.top().second;
int c = st.top().first;
while(freq > 0)
{
ans += c;
freq--;
}

st.pop();
}

reverse(ans.begin() , ans.end());
return ans;
}
};
59 changes: 59 additions & 0 deletions cpp/14-4Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Solution {
public:
vector<vector<int>> res;

vector<vector<int>> fourSum(vector<int>& nums, int target) {

if(nums.size() < 4) return res;

vector<int>quad;
sort(nums.begin() , nums.end());
kSum(0,4,target,nums,quad);
return res;
}


void kSum (int index , int k , long long target, vector<int> nums , vector<int>&q)
{

if(k == 2)
{
twoSum(index , target, q , nums);
return;
}

for(int i = index ; i < nums.size() - k + 1; i++)
{
if(i > index && nums[i] == nums[i-1]) continue;
q.push_back(nums[i]);
kSum(i+1 , k-1 , target-nums[i] , nums , q);
q.pop_back();
}

}

void twoSum (int start,long long target,vector<int>&ans,vector<int>& nums)
{
int lo = start;
int hi = nums.size()-1;

while(lo < hi)
{
int sum = nums[lo]+nums[hi];
if(sum > target) hi--;
else if (sum < target) lo++;

else
{
ans.insert(ans.end() , {nums[lo] , nums[hi]});
res.push_back(ans);

ans.pop_back();
ans.pop_back();

lo++;
while (lo < hi && nums[lo] == nums[lo - 1]) lo++;
}
}
}
};
2 changes: 1 addition & 1 deletion cpp/338-Counting-Bits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Differ by 1 bit, by removing LSB: f(x) = f(x / 2) + (x mod 2)
Time: O(n)
Space: O(n)
Space: O(1), the output array does not count towards space
*/

class Solution {
Expand Down
37 changes: 37 additions & 0 deletions cpp/402-Remove-K-Digits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time Complexity is O(N) where N is the size of the input string.
// Space complexity is O(N) as well
class Solution {
public:
string removeKdigits(string num, int k) {
int n = num.size();

stack<char>s;
int count = k;

for(int i = 0 ; i < n; i++)
{
while(!s.empty() && count > 0 && s.top() > num[i])
{
s.pop();
count--;
}
s.push(num[i]);
}

// In case the num was already in a non increasing order (e.x: 123456)
while(s.size() != n - k) s.pop();

string res = "";
while(!s.empty())
{
res += s.top();
s.pop();
}
reverse(res.begin() , res.end());
// Remove the zeros from the left if they exist.
while (res[0] == '0') res.erase(0 , 1);


return (res == "") ? "0": res;
}
};
22 changes: 22 additions & 0 deletions cpp/83-Remove-Duplicates-from-Sorted-List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time Complexity is O(N).
// Space Complexity is O(1).

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode * fast = head;
ListNode * slow = head;

while(slow != NULL)
{
while(fast != NULL && slow->val == fast->val)
fast = fast -> next;

slow->next = fast;
slow = slow -> next;
}

return head;

}
};
38 changes: 38 additions & 0 deletions cpp/neetcode_150/05_binary_search/search_a_2d_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,41 @@ class Solution {
return false;
}
};

// Same approach with implicit array linearization
// T(n) = O(log_2(mn)) = O(log_2(m) + log_2(n))
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {

int left = 0;
int m = matrix.size();
int n = matrix[0].size();
int right = m * n - 1;

while (left <= right){

int middle = right + (left - right) / 2;

// compute sub-indices using matrix structure
int row = middle / n;
int col = middle % n;


//ordinary binary search
int middle_x = matrix[row][col];
if (target > middle_x){
left = ++middle;
} else if (target < middle_x){
right = --middle;
} else {
return true;
}


}

return false;

}
};
Loading

0 comments on commit 840e429

Please sign in to comment.