Skip to content

Commit 2c85396

Browse files
Merge pull request codemistic#470 from paras3624/main
121 Leetcode Solution
2 parents a9deda7 + f516bb6 commit 2c85396

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

121 Leetcode/121 leetcode.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
maxProfit = 0
4+
currentMax = 0
5+
6+
for i in reversed(prices):
7+
currentMax = max(currentMax, i)
8+
profit = currentMax - i
9+
maxProfit = max(profit, maxProfit)
10+
return maxProfit

BFS in C++/BFS.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
#include <list>
3+
4+
using namespace std;
5+
6+
class Graph {
7+
int numVertices;
8+
list<int>* adjLists;
9+
bool* visited;
10+
11+
public:
12+
Graph(int vertices);
13+
void addEdge(int src, int dest);
14+
void BFS(int startVertex);
15+
};
16+
17+
Graph::Graph(int vertices) {
18+
numVertices = vertices;
19+
adjLists = new list<int>[vertices];
20+
}
21+
void Graph::addEdge(int src, int dest) {
22+
adjLists[src].push_back(dest);
23+
adjLists[dest].push_back(src);
24+
}
25+
26+
void Graph::BFS(int startVertex) {
27+
visited = new bool[numVertices];
28+
for (int i = 0; i < numVertices; i++)
29+
visited[i] = false;
30+
31+
list<int> queue;
32+
33+
visited[startVertex] = true;
34+
queue.push_back(startVertex);
35+
36+
list<int>::iterator i;
37+
38+
while (!queue.empty()) {
39+
int currVertex = queue.front();
40+
cout << "Visited " << currVertex << " ";
41+
queue.pop_front();
42+
43+
for (i = adjLists[currVertex].begin(); i != adjLists[currVertex].end(); ++i) {
44+
int adjVertex = *i;
45+
if (!visited[adjVertex]) {
46+
visited[adjVertex] = true;
47+
queue.push_back(adjVertex);
48+
}
49+
}
50+
}
51+
}
52+
53+
int main() {
54+
Graph g(4);
55+
g.addEdge(0, 1);
56+
g.addEdge(0, 2);
57+
g.addEdge(1, 2);
58+
g.addEdge(2, 0);
59+
g.addEdge(2, 3);
60+
g.addEdge(3, 3);
61+
g.BFS(2);
62+
return 0;
63+
}

0 commit comments

Comments
 (0)