Skip to content

Add C++ solutions in algorithms/cpp #288

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add C++ solution in algorithms/cpp/courseSchedule/SunnyLin/
  • Loading branch information
royeLin committed Jun 5, 2023
commit d28b6b5b72d32a7686d117cd06f50c2e64a7e9f8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Source : https://oj.leetcode.com/problems/binary-tree-level-order-traversal/
// Author : Hao Chen
// Date : 2014-07-17
// Author : Hao Chen, Sunny Lin
// Date : 2023-06-05

/**********************************************************************************
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Source : https://leetcode.com/problems/binary-tree-right-side-view/
// Author : David Lin
// Author : Sunny Lin
// Date : 2023-05-18

/**********************************************************************************
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Source : https://leetcode.com/problems/count-complete-tree-nodes/
// Author : David Lin
// Author : Sunny Lin
// Date : 2023-05-18

/**********************************************************************************
Expand Down
87 changes: 87 additions & 0 deletions algorithms/cpp/courseSchedule/SunnyLin/CourseSchedule.II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Source : https://leetcode.com/problems/course-schedule/
// Author : Sunny Lin
// Date : 2023-06-05

/**********************************************************************************
*
* There are a total of n courses you have to take, labeled from 0 to n - 1.
*
* Some courses may have prerequisites, for example to take course 0 you have to first take course 1,
* which is expressed as a pair: [0,1]
*
* Given the total number of courses and a list of prerequisite pairs, return the ordering of courses
* you should take to finish all courses.
*
* There may be multiple correct orders, you just need to return one of them. If it is impossible to
* finish all courses, return an empty array.
*
* For example:
* 2, [[1,0]]
* There are a total of 2 courses to take. To take course 1 you should have finished course 0.
* So the correct course order is [0,1]
*
* 4, [[1,0],[2,0],[3,1],[3,2]]
* There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2.
* Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3].
* Another correct ordering is[0,2,1,3].
*
* Note:
* The input prerequisites is a graph represented by a list of edges, not adjacency matrices.
* Read more about how a graph is represented.
*
* click to show more hints.
*
* Hints:
*
* - This problem is equivalent to finding the topological order in a directed graph. If a cycle exists,
* no topological ordering exists and therefore it will be impossible to take all courses.
*
* - Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining
* the basic concepts of Topological Sort.
*
* - Topological sort could also be done via BFS.
*
*
**********************************************************************************/

#include <stdio.h>

#include <stack>
#include <vector>
using std::stack;
using std::vector;
using std::pair;

bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> adjlist(numCourses);
vector<int> indegree(numCourses, 0);

for(auto& pair: prerequisites){
adjlist[pair[1]].push_back(pair[0]);
indegree[pair[0]]++;
}

stack<int> stack;
for(int i{}; i < indegree.size(); i++){
if(indegree[i] == 0){
stack.push(i);
}
}

int count{};
while (!stack.empty())
{
int topo_index = stack.top();
stack.pop();
count++;

for(auto& item: adjlist[topo_index]){
indegree[item]--;
if(indegree[item] == 0){
stack.push(item);
}
}
}
if(count != numCourses) return false;
return true;
}
106 changes: 106 additions & 0 deletions algorithms/cpp/courseSchedule/SunnyLin/CourseSchedule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Source : https://leetcode.com/problems/course-schedule/
// Author : Sunny Lin
// Date : 2023-06-05

/**********************************************************************************
*
* There are a total of n courses you have to take, labeled from 0 to n - 1.
*
* Some courses may have prerequisites, for example to take course 0 you have to first take course 1,
* which is expressed as a pair: [0,1]
*
* Given the total number of courses and a list of prerequisite pairs, return the ordering of courses
* you should take to finish all courses.
*
* There may be multiple correct orders, you just need to return one of them. If it is impossible to
* finish all courses, return an empty array.
*
* For example:
* 2, [[1,0]]
* There are a total of 2 courses to take. To take course 1 you should have finished course 0.
* So the correct course order is [0,1]
*
* 4, [[1,0],[2,0],[3,1],[3,2]]
* There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2.
* Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3].
* Another correct ordering is[0,2,1,3].
*
* Note:
* The input prerequisites is a graph represented by a list of edges, not adjacency matrices.
* Read more about how a graph is represented.
*
* click to show more hints.
*
* Hints:
*
* - This problem is equivalent to finding the topological order in a directed graph. If a cycle exists,
* no topological ordering exists and therefore it will be impossible to take all courses.
*
* - Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining
* the basic concepts of Topological Sort.
*
* - Topological sort could also be done via BFS.
*
*
**********************************************************************************/

#include <stdio.h>

#include <queue>
#include <vector>
#include <unordered_set>

using std::queue;
using std::vector;
using std::pair;

bool dfs(int head, vector<vector<int>>& adjlist, vector<bool>& seen){

if (seen[head] == false){
seen[head] = true;
}
else{
return false;
}

for(auto& adj: adjlist[head]){

if( not dfs(adj, adjlist, seen)){
return false;
}
}
return true;

}


bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> adjlist(numCourses);
int current{};

queue<int> q;
for(auto& pair: prerequisites){
adjlist[pair[1]].push_back(pair[0]);
}

for(int v{}; v < adjlist[v].size(); v++){
vector<bool> seen(numCourses, false);
for(auto& adj_v : adjlist[v]){
q.push(adj_v);
}

while (!q.empty())
{
current = q.front();
if(current == v)return false;
q.pop();
seen[current] = true;
for(auto& adj_current: adjlist[current]){
if(~seen[adj_current]){
q.push(adj_current);
}
}
}
}
return true;
}
104 changes: 104 additions & 0 deletions algorithms/cpp/numberOfIslands/NumberOfIslands.II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Source : https://leetcode.com/problems/number-of-islands/
// Author : David Lin
// Date : 2023-05-20

/**********************************************************************************
*
* Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
* An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
* You may assume all four edges of the grid are all surrounded by water.
*
* Example 1:
* 11110
* 11010
* 11000
* 00000
* Answer: 1
*
* Example 2:
* 11000
* 11000
* 00100
* 00011
* Answer: 3
*
* Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.
*
**********************************************************************************/

#include <iostream>
#include <vector>
using namespace std;

void dfs(vector<vector<char> >& grid, int row, int col, int row_boarder, int col_boarder){

if (row < 0
|| col < 0
|| row >= row_boarder
|| col >= col_boarder
|| grid[row][col]=='0') return;
grid[row][col] = '0';


dfs(grid, row + 1, col, row_boarder, col_boarder);
dfs(grid, row, col + 1, row_boarder, col_boarder);
dfs(grid, row, col - 1, row_boarder, col_boarder);
dfs(grid, row -1, col, row_boarder, col_boarder);
}

int numIslands(vector<vector<char> >& grid) {
if (grid.size()== 0) return 0;
int count = 0;
int row_boarder = grid.size();
int col_boarder = grid[0].size();
for (int row = 0; row < grid.size(); row++){
for (int col = 0; col < grid[0].size(); col++){
if (grid[row][col] == '1'){
count++;
dfs(grid, row, col, row_boarder, col_boarder);
}
}
}
return count;
}

void initGrid( string g[], int len, vector<vector<char> >& grid )
{
for (int i=0; i<len; i++){
grid.push_back(vector<char>(g[i].begin(), g[i].end()));
}
}

int main(void)
{
vector< vector<char> > grid;
grid.push_back( vector<char>(1, '1') );

cout << numIslands(grid) << endl;


grid.clear();

string g1[] = { "11110",
"11010",
"11000",
"00000" };

initGrid(g1, 4, grid);
cout << numIslands(grid) << endl;



grid.clear();

string g2[] = { "11000",
"11000",
"00100",
"00011" };

initGrid(g2, 4, grid);
cout << numIslands(grid) << endl;


return 0;
}
2 changes: 1 addition & 1 deletion algorithms/cpp/rottingOranges/rottingOranges.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Source : https://leetcode.com/problems/rotting-oranges/
// Author : David Lin
// Author : Sunny Lin
// Date : 2023-05-18

/**********************************************************************************
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Source : https://leetcode.com/problems/time-needed-to-inform-all-employees/
// Author : David Lin
// Author : Sunny Lin
// Date : 2023-05-22

/***************************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion algorithms/cpp/wallAndGates/wallAndGates.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Source : https://leetcode.com/problems/walls-and-gates/
// Author : David Lin
// Author : Sunny Lin
// Date : 2023-05-20

/**********************************************************************************
Expand Down