Skip to content

Commit 282cee5

Browse files
committed
2022 sf tech solved.
1 parent a343ffb commit 282cee5

File tree

12 files changed

+437
-0
lines changed

12 files changed

+437
-0
lines changed

Others/2022-sf-tech/A/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(A)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(A main.cpp)

Others/2022-sf-tech/A/main.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/// Source : https://leetcode.cn/contest/sf-tech/problems/EUpcmh/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
#include <set>
9+
#include <cassert>
10+
11+
using namespace std;
12+
13+
14+
/// DFS Cycle Finding
15+
/// Time Complexity: O(|s| + n)
16+
/// Space Compelxity: O(n)
17+
class Solution {
18+
public:
19+
bool hasCycle(string graph) {
20+
21+
vector<string> edges_str = get_edges_str(graph);
22+
23+
map<int, set<int>> g;
24+
for(const string& edge_str: edges_str){
25+
int u, v;
26+
get_uv_from_edge_str(edge_str, u, v);
27+
g[u].insert(v);
28+
}
29+
30+
set<int> visited;
31+
bool has_cycle = false;
32+
for(const pair<int, set<int>>& p: g){
33+
int u = p.first;
34+
if(!visited.count(u)){
35+
set<int> stack;
36+
has_cycle = dfs(g, u, stack, visited);
37+
if(has_cycle) break;
38+
}
39+
}
40+
return has_cycle;
41+
}
42+
43+
private:
44+
bool dfs(const map<int, set<int>>& g, int u, set<int>& stack, set<int>& visited){
45+
46+
stack.insert(u);
47+
if(g.count(u)){
48+
for(int v: g.at(u)){
49+
if(stack.count(v)) return true;
50+
if(!visited.count(v) && dfs(g, v, stack, visited)) return true;
51+
}
52+
}
53+
stack.erase(u);
54+
return false;
55+
}
56+
57+
void get_uv_from_edge_str(const string& e, int& u, int& v){
58+
59+
int arrow_pos = e.find("->");
60+
assert(arrow_pos != string::npos);
61+
62+
u = atoi(e.substr(0, arrow_pos).c_str());
63+
v = atoi(e.substr(arrow_pos + 2).c_str());
64+
}
65+
66+
vector<string> get_edges_str(const string& g){
67+
68+
vector<string> res;
69+
for(int start = 0, i = 1; i <= g.size(); i ++)
70+
if(i == g.size() || g[i] == ','){
71+
res.push_back(g.substr(start, i - start));
72+
start = i + 1;
73+
i = start + 1;
74+
}
75+
return res;
76+
}
77+
};
78+
79+
80+
int main() {
81+
82+
cout << Solution().hasCycle("1->2,2->3,3->1") << '\n';
83+
// 1
84+
85+
cout << Solution().hasCycle("1->4,2->5,3->6,3->7,4->8,5->8,5->9,6->9,6->11,7->11,8->12,9->12,9->13,10->13,10->14,11->10,11->14") << '\n';
86+
// 0
87+
88+
cout << Solution().hasCycle("1->4,2->5,3->6,3->7,4->8,5->8,5->9,6->9,6->11,7->11,8->12,9->12,9->13,10->6,10->13,10->14,11->10,11->14") << '\n';
89+
// 1
90+
91+
return 0;
92+
}

Others/2022-sf-tech/B/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(B)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(B main.cpp)

Others/2022-sf-tech/B/main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// Source : https://leetcode.cn/contest/sf-tech/problems/cINqyA/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <cassert>
8+
9+
using namespace std;
10+
11+
12+
/// Backpack DP
13+
/// Time complexity: O(C * n)
14+
/// Space Complexity: O(C)
15+
class Solution {
16+
public:
17+
int minRemainingSpace(vector<int>& V, int C) {
18+
19+
vector<bool> dp(C + 1, false);
20+
dp[0] = true;
21+
for(int v: V){
22+
for(int c = C; c >= v; c --)
23+
if(dp[c - v]) dp[c] = true;
24+
}
25+
26+
for(int i = C; i >= 0; i --)
27+
if(dp[i]) return C - i;
28+
assert(false);
29+
return -1;
30+
}
31+
};
32+
33+
34+
int main() {
35+
36+
return 0;
37+
}

Others/2022-sf-tech/B/main2.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/// Source : https://leetcode.cn/contest/sf-tech/problems/cINqyA/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <set>
8+
9+
using namespace std;
10+
11+
12+
/// Meet in the middle
13+
/// Time complexity: O(n * 2^(n / 2))
14+
/// Space Complexity: O(2^(n/2))
15+
class Solution {
16+
public:
17+
int minRemainingSpace(vector<int>& V, int C) {
18+
19+
int n = V.size();
20+
if(n == 1) return max(C - V[0], 0);
21+
22+
int left_n = n / 2, right_n = n - left_n;
23+
vector<int> left(1 << left_n, 0), right(1 << right_n, 0);
24+
for(int state = 1; state < (1 << left_n); state ++){
25+
int p = __builtin_ffs(state) - 1;
26+
left[state] = left[state - (1 << p)] + V[p];
27+
}
28+
for(int state = 1; state < (1 << right_n); state ++){
29+
int p = __builtin_ffs(state) - 1;
30+
right[state] = right[state - (1 << p)] + V[left_n + p];
31+
}
32+
33+
set<int> left_set(left.begin(), left.end());
34+
set<int> right_set(right.begin(), right.end());
35+
36+
int res = C;
37+
for(int e1: left){
38+
if(e1 > C) break;
39+
auto iter = right_set.upper_bound(C - e1);
40+
iter --;
41+
res = min(res, C - e1 - *iter);
42+
}
43+
44+
return res;
45+
}
46+
};
47+
48+
49+
int main() {
50+
51+
return 0;
52+
}

Others/2022-sf-tech/C/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(C)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(C main.cpp)

Others/2022-sf-tech/C/main.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// Source : https://leetcode.cn/contest/sf-tech/problems/8oimK4/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Linear Scan
12+
/// Time Compelxity: O(n)
13+
/// Space Complexity: O(1)
14+
class Solution {
15+
public:
16+
int findMaxCI(vector<int>& nums) {
17+
18+
int n = nums.size();
19+
int res = 0;
20+
for(int start = 0, i = 1; i <= n; i ++)
21+
if(i == n || nums[i] <= nums[i - 1]){
22+
res = max(res, i - start);
23+
start = i;
24+
i = start;
25+
}
26+
return res;
27+
}
28+
};
29+
30+
31+
int main() {
32+
33+
return 0;
34+
}

Others/2022-sf-tech/D/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(D)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(D main.cpp)

Others/2022-sf-tech/D/main.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/// Source : https://leetcode.cn/contest/sf-tech/problems/uWWzsv/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <complex>
8+
9+
#define X real()
10+
#define Y imag()
11+
12+
using namespace std;
13+
14+
15+
typedef double C;
16+
typedef complex<C> P;
17+
18+
const double EPS = 1e-6;
19+
20+
/// Cross Product
21+
/// Time Complexity: O(t)
22+
/// Space Complexity: O(1)
23+
C cross_product(P a, P b){
24+
return (conj(a) * b).Y; // a.X * b.Y - b.X * a.Y
25+
}
26+
27+
bool is_zero(C x){
28+
return abs(x) < EPS;
29+
}
30+
// 1 : left
31+
// 0 : touch
32+
// -1 : right
33+
int test_point_loc(P p, P s1, P s2){
34+
C res = cross_product(p - s1, p - s2);
35+
if(is_zero(res)) return 0;
36+
return res > 0 ? 1 : -1;
37+
}
38+
39+
// 0 : on boundary
40+
// 1 : intersection
41+
// -1 : no intersection
42+
int intersect(P p1, P p2, P p3, P p4){
43+
44+
C x1 = p1.X, y1 = p1.Y, x2 = p2.X, y2 = p2.Y, x3 = p3.X, y3 = p3.Y, x4 = p4.X, y4 = p4.Y;
45+
46+
int loc1 = test_point_loc(p1, p3, p4), loc2 = test_point_loc(p2, p3, p4);
47+
int loc3 = test_point_loc(p3, p1, p2), loc4 = test_point_loc(p4, p1, p2);
48+
49+
if(loc1 == 0 && min(x3, x4) <= x1 && x1 <= max(x3, x4)
50+
&& min(y3, y4) <= y1 && y1 <= max(y3, y4)) return 0;
51+
52+
if(loc2 == 0 && min(x3, x4) <= x2 && x2 <= max(x3, x4)
53+
&& min(y3, y4) <= y2 && y2 <= max(y3, y4)) return 0;
54+
55+
if(loc3 == 0 && min(x1, x2) <= x3 && x3 <= max(x1, x2)
56+
&& min(y1, y2) <= y3 && y3 <= max(y1, y2)) return 0;
57+
58+
if(loc4 == 0 && min(x1, x2) <= x4 && x4 <= max(x1, x2)
59+
&& min(y1, y2) <= y4 && y4 <= max(y1, y2)) return 0;
60+
61+
if(!loc1 || !loc2 || !loc3 || !loc4) return -1;
62+
63+
if(loc1 * loc2 > 0 || loc3 * loc4 > 0) return -1;
64+
return 1;
65+
}
66+
67+
//long long gcd(long long a, long long b)
68+
//{
69+
// if(a > b) swap(a, b);
70+
// if (a == 0) return b;
71+
// return gcd(b % a, a);
72+
//}
73+
74+
int in_polygon(int n, const vector<P>& polygon, P p){
75+
76+
for(int i = 1; i < n; i ++){
77+
int loc = test_point_loc(p, polygon[i], polygon[i - 1]);
78+
if(loc == 0 && min(polygon[i].X, polygon[i - 1].X) <= p.X && p.X <= max(polygon[i].X, polygon[i - 1].X)
79+
&& min(polygon[i].Y, polygon[i - 1].Y) <= p.Y && p.Y <= max(polygon[i].Y, polygon[i - 1].Y))
80+
return 0;
81+
}
82+
83+
int loc = test_point_loc(p, polygon[n - 1], polygon[0]);
84+
if(loc == 0 && min(polygon[n - 1].X, polygon[0].X) <= p.X && p.X <= max(polygon[n - 1].X, polygon[0].X)
85+
&& min(polygon[n - 1].Y, polygon[0].Y) <= p.Y && p.Y <= max(polygon[n - 1].Y, polygon[0].Y))
86+
return 0;
87+
88+
// 保证 p2.X - p1.X 和 p2.Y - p1.Y 互质,
89+
double INF = 1e9 + 7;
90+
91+
P p1 = p, p2 = {p.X + INF, p.Y + 1};
92+
int cnt = 0;
93+
for(int i = 1; i < n; i ++)
94+
cnt += intersect(p1, p2, polygon[i], polygon[i - 1]) >= 0;
95+
cnt += intersect(p1, p2, polygon[n - 1], polygon[0]) >= 0;
96+
97+
return (cnt & 1) ? -1 : 1;
98+
}
99+
100+
class Solution {
101+
public:
102+
bool isPointInPolygon(double x, double y, vector<double>& coords) {
103+
104+
vector<P> polygon;
105+
for(int i = 0; i < coords.size(); i += 2)
106+
polygon.push_back({coords[i], coords[i + 1]});
107+
108+
P p = {x, y};
109+
return in_polygon(polygon.size(), polygon, p) < 0;
110+
}
111+
};
112+
113+
int main() {
114+
115+
return 0;
116+
}

Others/2022-sf-tech/E/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(E)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(E main.cpp)

0 commit comments

Comments
 (0)