Skip to content

Commit f2839e7

Browse files
author
Jie Feng
committed
hh
1 parent 8fc8e82 commit f2839e7

File tree

7 files changed

+139
-13
lines changed

7 files changed

+139
-13
lines changed

algorithm-puzzle/1

119 KB
Binary file not shown.

algorithm-puzzle/1.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <algorithm>
4+
#include <map>
5+
#include <climits>
6+
using namespace std;
7+
8+
struct node{
9+
int pos;
10+
int num;
11+
node(int p, int n):pos(p), num(n){};
12+
bool operator<(const node& other)const{
13+
return num < other.num;
14+
}
15+
};
16+
int solution(vector<int> &A) {
17+
vector<node> B;
18+
for (int i = 0; i < A.size(); i++){
19+
B.push_back(node(i, A[i]));
20+
}
21+
sort(B.begin(), B.end());
22+
int minPos = INT_MAX, maxDis = 0;
23+
int i = 0;
24+
for (unsigned i = 0; i < B.size(); i++){
25+
minPos = min(minPos, B[i].pos);
26+
maxDis = max(maxDis, B[i].pos - minPos);
27+
}
28+
return maxDis;
29+
}
30+
31+
int main(){
32+
int A[] = {5,3,6,3,4,2};
33+
vector<int> arr(A, A + sizeof(A)/4);
34+
cout << solution(arr) << endl;
35+
}

algorithm-puzzle/1.o

225 KB
Binary file not shown.

algorithm-puzzle/2.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
// you can also use includes, for example:
7+
// #include <algorithm>
8+
bool isPeriod(vector<bool>& bits, unsigned int pe){
9+
for (unsigned int i = 0; i < bits.size() - pe; i++){
10+
if (bits[i] != bits[i+pe]) return false;
11+
}
12+
return true;
13+
}
14+
int solution(int N) {
15+
if (N == 0) return 1;
16+
vector<bool> bits;
17+
while(N != 0){
18+
bits.push_back(N & 1);
19+
N >>= 1;
20+
}
21+
reverse(bits.begin(), bits.end());
22+
for (unsigned int pe = bits.size()/2; pe >= 1; pe--){
23+
if (isPeriod(bits, pe)) return pe;
24+
}
25+
return -1;
26+
}
27+
int main(){
28+
cout << solution(955) << endl;
29+
}

algorithm-puzzle/3.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
7+
// you can also use includes, for example:
8+
// #include <algorithm>
9+
#define ui unsigned int
10+
#define inside(x,L,R) (L<x&&x<R)
11+
const int dir[4][2] = {{0,1}, {1,0}, {0,-1}, {-1,0}};
12+
inline bool cross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
13+
if (x1 > x2) swap(x1, x2);
14+
if (y1 > y2) swap(y1, y2);
15+
if (x3 > x4) swap(x3, x4);
16+
if (y3 > y4) swap(y3, y4);
17+
18+
if (x1 == x2){
19+
if (x3 == x4){
20+
if (x1 == x3){
21+
return !(y3 >= y2 || y1 >= y4);
22+
}else return false;
23+
}else{
24+
return inside(y3,y1,y2) && inside(x1,x3,x4);
25+
}
26+
}else{
27+
if (x3 == x4){
28+
return inside(x3,x1,x2) && inside(y1,y3,y4);
29+
}else{
30+
if (y1 == y3){
31+
return !(x3 >= x2 || x1 >= x4);
32+
}else return false;
33+
}
34+
}
35+
}
36+
int solution(const vector<int> &A) {
37+
int x1 = 0, y1 = 0, x2, y2, d = 0;
38+
for (ui i = 0; i < A.size(); i++){
39+
40+
41+
x2 = x1 + dir[d][0] * A[i];
42+
y2 = y1 + dir[d][1] * A[i];
43+
44+
45+
int x3 = 0, y3 = 0, x4, y4, d1 = 0;
46+
for (ui j = 0; j < i; j++){
47+
x4 = x3 + dir[d1][0] * A[j];
48+
y4 = y3 + dir[d1][1] * A[j];
49+
if (cross(x1, y1, x2, y2, x3, y3, x4, y4)) return i+1;
50+
if (++d1 == 4) d1 = 0;
51+
x3 = x4;
52+
y3 = y4;
53+
}
54+
if (++d == 4) d = 0;
55+
x1 = x2;
56+
y1 = y2;
57+
}
58+
return -1;
59+
}
60+
61+
62+
63+
int main(){
64+
int a[] = {1,3,2,5,4,4,6,3,2};
65+
vector<int> A(a, a+sizeof(a)/4);
66+
cout << solution(A) << endl;
67+
}

algorithm-puzzle/in

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
1-
7 8
2-
2 0 0 0 0 0 0
3-
0 0 0 0 0 0 0
4-
0 0 0 0 0 0 0
5-
0 0 0 0 0 0 0
6-
0 0 0 0 0 0 0
7-
0 0 0 0 0 0 0
8-
0 0 0 0 0 0 0
9-
3 0 0 0 0 1 1
1+
4
2+
6 5 5 6

algorithm-puzzle/makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
target= quora-nearby
2-
#target= temp
1+
target= 1
32

43
all:
5-
g++ -o $(target) -g $(target).cpp
4+
g++ -o $(target) -g $(target).cpp -std=c++11 -Wno-deprecated
65

7-
.PHONY: run debug
6+
.PHONY: run debug read
87

98
run:
109
./${target} < in | tee out
1110

1211
debug:
1312
gdb ${target}
13+
14+
read:
15+
vim -R ${target}.cpp

0 commit comments

Comments
 (0)