Skip to content

Dev #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 14, 2022
Merged

Dev #80

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ set(CXX_FLAGS " -std=c++17 -fsanitize=address -ggdb3 -Wall -Wextra")
#add_executable(number_of_enclaves src/number_of_enclaves.cpp)
#add_executable(all_paths_from_source_to_target src/all_paths_from_source_to_target.cpp)
#add_executable(number_of_good_paths src/number_of_good_paths.cpp)
add_executable(satisfiability_of_equality_equations src/satisfiability_of_equality_equations.cpp)
#add_executable(satisfiability_of_equality_equations src/satisfiability_of_equality_equations.cpp)
add_executable(most_stones_removed_with_same_row_or_column src/most_stones_removed_with_same_row_or_column.cpp)
55 changes: 55 additions & 0 deletions src/graph/src/most_stones_removed_with_same_row_or_column.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Created by sajith on 11/14/22.
//

#include "base.h"

class Solution
{
vector<bool> visited;

void dfs(int idx, vector<vector<int>> &stones)
{
visited[idx] = true;
for (int i = 0; i < stones.size(); ++i)
{
if (!visited[i])
{
if (stones[idx][0] == stones[i][0] || stones[idx][1] == stones[i][1])
{
dfs(i, stones);
}
}
}
}

public:
int removeStones(vector<vector<int>> &stones)
{
visited.resize(stones.size(), false);
int cnt = 0;
for (int i = 0; i < stones.size(); ++i)
{
if (!visited[i])
{
++cnt;
dfs(i, stones);
}
}
return static_cast<int>(stones.size()) - cnt;
}
};

int main()
{
Solution sol;
vector<vector<int>> v{{0, 0},
{0, 1},
{1, 0},
{1, 2},
{2, 1},
{2, 2}};


return 0;
}
3 changes: 2 additions & 1 deletion src/stacks_queues/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ project(stacknqueues)
#add_executable(construct_target_array_with_multiple_sums src/construct_target_array_with_multiple_sums.cpp)
#add_executable(design_circular_queue src/design_circular_queue.cpp)
#add_executable(make_the_string_great src/make_the_string_great.cpp)
add_executable(online_stock_span src/online_stock_span.cpp)
#add_executable(online_stock_span src/online_stock_span.cpp)
add_executable(find_median_from_data_stream src/find_median_from_data_stream.cpp)

61 changes: 61 additions & 0 deletions src/stacks_queues/src/find_median_from_data_stream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

//
// Created by sajith on 11/12/22.
//

#include "base.h"

class MedianFinder
{
public:

priority_queue<int> smaller;

priority_queue<int, vector<int>, greater<int>> larger;


MedianFinder()
{

}

void addNum(int num)
{
smaller.push(num);
if (!smaller.empty() && !larger.empty() && smaller.top() > larger.top())
{
int val = smaller.top();
smaller.pop();
larger.push(val);
}

if (smaller.size() > larger.size() + 1)
{
int val = smaller.top();
smaller.pop();
larger.push(val);
}
else if (larger.size() > smaller.size() + 1)
{
int val = larger.top();
larger.pop();
smaller.push(val);
}
}

double findMedian()
{
if (smaller.size() == larger.size())
{
return (double(smaller.top()) + double(larger.top())) / 2;
}
else if (smaller.size() == larger.size() + 1)
{
return double(smaller.top());
}
else
{
return double(larger.top());
}
}
};
24 changes: 24 additions & 0 deletions src/strings/src/reverse_words_in_a_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by sajith on 11/13/22.
//
class Solution
{
public:
string reverseWords(string s)
{
string res;
int i = 0, j;
while (i < s.length())
{
while (i < s.length() && s[i] == ' ') ++i;
if (i >= s.length()) break;
j = i + 1;
while (j < s.length() && s[j] != ' ') ++j;

if (res.empty()) res = s.substr(i, j - i);
else res = s.substr(i, j - i) + " " + res;
i = j + 1;
}
return res;
}
};