mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 07:44:07 +00:00
ok i'm back: 210-course-schedule-ii, 43-multiply-strings
This commit is contained in:
parent
6df68a24f2
commit
44dd48d78b
13
210-course-schedule-ii/driver.cpp
Normal file
13
210-course-schedule-ii/driver.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int numCourses = 4;
|
||||||
|
vector<vector<int>> prerequisites{{0, 1}, {0, 2}, {3, 0}};
|
||||||
|
Solution soln;
|
||||||
|
cout << "Ordering of courses:" << endl;
|
||||||
|
for (int num : soln.findOrder(numCourses, prerequisites))
|
||||||
|
cout << "->" << num;
|
||||||
|
cout << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
43
210-course-schedule-ii/soln.cpp
Normal file
43
210-course-schedule-ii/soln.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
vector<int> Solution::findOrder(int numCourses, vector<vector<int>> &prerequisites)
|
||||||
|
{
|
||||||
|
vector<vector<int>> adj(numCourses); // adjacency list
|
||||||
|
vector<int> ans_order;
|
||||||
|
vector<int> in_degrees(numCourses);
|
||||||
|
|
||||||
|
for (vector<int> &p : prerequisites)
|
||||||
|
{
|
||||||
|
adj[p[1]].push_back(p[0]); // [1,0] -> course 1 will have incoming edge from 0
|
||||||
|
in_degrees[p[0]]++; // increment in degree of course 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// find starting nodes, which don't hv prereqs
|
||||||
|
// so this will be 0 alone
|
||||||
|
queue<int> bfs_q;
|
||||||
|
for (int i = 0; i < numCourses; i++)
|
||||||
|
{
|
||||||
|
if (in_degrees[i] == 0)
|
||||||
|
bfs_q.push(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// BFS
|
||||||
|
while (bfs_q.size())
|
||||||
|
{
|
||||||
|
int cur = bfs_q.front();
|
||||||
|
bfs_q.pop();
|
||||||
|
ans_order.push_back(cur);
|
||||||
|
for (int out_node : adj[cur])
|
||||||
|
{
|
||||||
|
// check if no more prereqs
|
||||||
|
// if yes, bfs on that
|
||||||
|
if (in_degrees[out_node] == 1)
|
||||||
|
bfs_q.push(out_node);
|
||||||
|
in_degrees[out_node]--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if solution exists, answer will have all courses
|
||||||
|
if (ans_order.size() == numCourses)
|
||||||
|
return ans_order;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
7
210-course-schedule-ii/soln.hpp
Normal file
7
210-course-schedule-ii/soln.hpp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
vector<int> findOrder(int numCourses, vector<vector<int>> &prerequisites);
|
||||||
|
};
|
||||||
9
43-multiply-strings/driver.cpp
Normal file
9
43-multiply-strings/driver.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
string num1 = "123", num2 = "456", answer = "56088";
|
||||||
|
Solution soln;
|
||||||
|
cout << "Multiplied strings correctly? " << soln.test(num1, num2, answer) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
33
43-multiply-strings/soln.cpp
Normal file
33
43-multiply-strings/soln.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
string Solution::multiply(string num1, string num2)
|
||||||
|
{
|
||||||
|
// product can't have more digits than sum of individual num's digits
|
||||||
|
string prod(num1.size() + num2.size(), '0');
|
||||||
|
int carry, tmp;
|
||||||
|
|
||||||
|
// standard multiplication
|
||||||
|
// one digit of num1 with all of num2, accumulate
|
||||||
|
for (int i = num1.size() - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
carry = 0;
|
||||||
|
for (int j = num2.size() - 1; j >= 0; j--)
|
||||||
|
{
|
||||||
|
tmp = (num1[i] - '0') * (num2[j] - '0') + (prod[i + j + 1] - '0') + carry;
|
||||||
|
prod[i + j + 1] = (tmp % 10) + '0';
|
||||||
|
carry = tmp / 10;
|
||||||
|
}
|
||||||
|
prod[i] += carry;
|
||||||
|
}
|
||||||
|
|
||||||
|
// strip leading zeroes
|
||||||
|
int start_pos = prod.find_first_not_of("0");
|
||||||
|
if (start_pos != -1)
|
||||||
|
return prod.substr(start_pos);
|
||||||
|
return "0";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Solution::test(string num1, string num2, string answer)
|
||||||
|
{
|
||||||
|
return multiply(num1, num2) == answer;
|
||||||
|
}
|
||||||
8
43-multiply-strings/soln.hpp
Normal file
8
43-multiply-strings/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
string multiply(string num1, string num2);
|
||||||
|
bool test(string num1, string num2, string answer);
|
||||||
|
};
|
||||||
9
template/driver.cpp
Normal file
9
template/driver.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
Solution soln;
|
||||||
|
cout << "? " << soln.test(, answer) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
2
template/soln.cpp
Normal file
2
template/soln.cpp
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
8
template/soln.hpp
Normal file
8
template/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
bool test(, bool answer);
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user