当前位置:网站首页>[Li Kou] curriculum series
[Li Kou] curriculum series
2022-06-12 06:58:00 【ToLoveToFeel】
【 Power button 】 Curriculum series
Leetcode 0207 The curriculum
Title Description :Leetcode 0207 The curriculum

analysis
The test site of this question : A topological sort .
For topology sorting, please refer to : website .
Code
- C++
class Solution {
public:
bool canFinish(int n, vector<vector<int>> &edges) {
vector<vector<int>> g(n);
vector<int> d(n); // Storage penetration
for (auto &e : edges) {
int b = e[0], a = e[1];
g[a].push_back(b);
d[b]++;
}
queue<int> q;
for (int i = 0; i < n; i++)
if (d[i] == 0)
q.push(i);
int cnt = 0;
while (q.size()) {
auto a = q.front(); q.pop();
cnt++;
for (auto b : g[a])
if (--d[b] == 0)
q.push(b);
}
return cnt == n;
}
};
- Java
class Solution {
public boolean canFinish(int n, int[][] edges) {
List<List<Integer>> g = new ArrayList<>();
for (int i = 0; i < n; i++) g.add(new ArrayList<>());
int[] d = new int[n]; // Storage penetration
for (int[] e : edges) {
int b = e[0], a = e[1];
g.get(a).add(b);
d[b]++;
}
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++)
if (d[i] == 0)
q.add(i);
int cnt = 0;
while (!q.isEmpty()) {
int a = q.remove();
cnt++;
for (int b : g.get(a))
if (--d[b] == 0)
q.add(b);
}
return cnt == n;
}
}
Time and space complexity analysis
Time complexity : O ( n ) O(n) O(n),
nNumber of courses .Spatial complexity : O ( n ) O(n) O(n).
Leetcode 0210 The curriculum II
Title Description :Leetcode 0210 The curriculum II

analysis
The test site of this question : A topological sort .
For topology sorting, please refer to : website .
This question is relative to Leetcode 0207 The curriculum , Let's find out the scheme of topological sorting . The out of queue order of the elements in the queue is a legal scheme , Just write it down
Code
- C++
class Solution {
public:
vector<int> findOrder(int n, vector<vector<int>> &edges) {
vector<vector<int>> g(n);
vector<int> d(n); // The degree of
for (auto &e : edges) {
auto b = e[0], a = e[1];
g[a].push_back(b);
d[b]++;
}
queue<int> q;
for (int i = 0; i < n; i++)
if (d[i] == 0)
q.push(i);
vector<int> res;
while (q.size()) {
auto a = q.front();
q.pop();
res.push_back(a);
for (auto b : g[a])
if (--d[b] == 0)
q.push(b);
}
if (res.size() < n) res = {
};
return res;
}
};
- Java
class Solution {
public int[] findOrder(int n, int[][] edges) {
List<List<Integer>> g = new ArrayList<>();
for (int i = 0; i < n; i++) g.add(new ArrayList<>());
int[] d = new int[n]; // Storage penetration
for (int[] e : edges) {
int b = e[0], a = e[1];
g.get(a).add(b);
d[b]++;
}
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++)
if (d[i] == 0)
q.add(i);
int[] res = new int[n];
int cnt = 0;
while (!q.isEmpty()) {
int a = q.remove();
res[cnt++] = a;
for (int b : g.get(a))
if (--d[b] == 0)
q.add(b);
}
if (cnt < n) return new int[]{
};
return res;
}
}
Time and space complexity analysis
Time complexity : O ( n ) O(n) O(n),
nNumber of courses .Spatial complexity : O ( n ) O(n) O(n).
Leetcode 0630 The curriculum III
Title Description :Leetcode 0630 The curriculum III

analysis
The test site of this question : greedy 、 Pile up .
Sort by deadline from small to large , Look at each course from front to back , If you can choose this course, choose , If not, you must delete a course , Delete the longest course containing the current course .
Choose if you can : That is, if the total time taken after joining this course does not exceed the current deadline .
Code
- C++
class Solution {
public:
int scheduleCourse(vector<vector<int>> &courses) {
sort(courses.begin(), courses.end(), [](vector<int> &a, vector<int> &b) {
return a[1] < b[1];
});
priority_queue<int> heap;
int tot = 0; // Selected course time
for (auto &c : courses) {
tot += c[0];
heap.push(c[0]);
if (tot > c[1]) {
tot -= heap.top();
heap.pop();
}
}
return heap.size();
}
};
Time and space complexity analysis
Time complexity : O ( n × l o g ( n ) ) O(n \times log(n)) O(n×log(n)),
nIs array length .Spatial complexity : O ( n ) O(n) O(n).
边栏推荐
- Detailed explanation of convirt paper (medical pictures)
- How to build your own website (using the pagoda panel)
- Beginners can't tell the difference between framework and class library
- 如何更新 Kubernetes 证书
- Can official account also bring goods?
- leetcode. 39 --- combined sum
- Leetcode: Sword finger offer 66 Build product array [application of pre and post infix]
- 初中学历,从不到3K,到月薪30K+,不设限的人生有多精彩
- Tomato learning notes-stm32 SPI introduction and Tim synchronization
- Throw away the ugly toast. The movable toast is more interesting
猜你喜欢

(14)Blender源码分析之闪屏窗口显示软件版本号

数据库语法相关问题,求解一个正确语法

Postman splice replacement parameter loop call interface
![Leetcode: offer 60 Points of N dice [math + level DP + cumulative contribution]](/img/2b/41bd6a213892062f4c12721b5d4e8d.png)
Leetcode: offer 60 Points of N dice [math + level DP + cumulative contribution]

【图像去噪】基于偏微分方程(PDE)实现图像去噪附matlab代码

8. 表单标签

CL210OpenStack操作的故障排除--章节实验

Install MySQL tutorial

lambda 函数完美使用指南
![Leetcode: Sword finger offer 67 Convert string to integer [simulation + segmentation + discussion]](/img/32/16751c0a783cc3121eddfe265e2f4f.png)
Leetcode: Sword finger offer 67 Convert string to integer [simulation + segmentation + discussion]
随机推荐
1. Foundation of MySQL database (1- installation and basic operation)
platform driver
ConVIRT论文详解(医疗图片)
5、 El expression & JSTL tag library
六月集训 第七日 ——哈希表
Redis supports data structure types
推荐17个提升开发效率的“轮子”
张驰咨询:流程是一剂万能良药吗?
Process when solving vagrant up_ builder. rb:43:in `join‘: incompatible character encodings: GBK and UTF-8
六月集训 第九日——位运算
lambda 函数完美使用指南
12.13-12.19 summary
Tomato learning notes -seq2seq
A journey of database full SQL analysis and audit system performance optimization
Scons编译IMGUI
Recommend 17 "wheels" to improve development efficiency
Dépannage de l'opération cl210openstack - chapitre expérience
[image denoising] salt and pepper noise image denoising based on Gaussian filter, mean filter, median filter and bilateral filter with matlab code attached
Interview intelligence questions
Drawing grid navigation by opencv map reading