Skip to content

Commit d41b451

Browse files
committed
Some tiny edits
1 parent 1afef8d commit d41b451

16 files changed

+226
-42
lines changed

ACM/Divide and Conquer/11378_Bey_Battle/11378.cc

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
#include <iostream>
2-
#include <vector>
32
#include <algorithm>
43
#include <fstream>
54
#include <map>
65

76
using namespace std;
87

98
typedef long long ll;
10-
// typedef vector<ll> vi;
11-
// typedef vector<vi> vvi;
129
typedef map<ll, ll> dict;
1310

1411
ll N;
15-
//vvi plane(100000, vi(2));
1612
dict plane;
1713

18-
// bool compare(vi p1, vi p2) {
19-
// return p1[0]<p1[0];
20-
// }
21-
2214
bool cover(ll L) {
2315
if (L==1) return true;
2416

@@ -44,34 +36,24 @@ int main() {
4436
// streambuf *cinbuf = std::cin.rdbuf(); //save old buf
4537
// cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
4638

47-
// ofstream out("out.txt");
48-
// streambuf *coutbuf = std::cout.rdbuf(); //save old buf
49-
// cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
50-
5139
while (cin >> N) {
5240
plane.clear();
5341
for (ll i=0; i<N; ++i) {
5442
ll x,y;
5543
cin >> x >> y;
5644
plane[x] = y;
5745
}
58-
//sort(plane.begin(), plane.begin()+N, compare);
5946

6047
ll L=0, R=4000001, mid;
6148
while (R - L != 1) {
6249
mid = (L+R) >> 1;
63-
//cout << R << " " << L<< " " << mid << endl;
6450
if (cover(mid)) {
6551
L = mid;
6652
} else {
6753
R = mid;
6854
}
6955
}
70-
// for (ll i=0; i<N; ++i) {
71-
// // ll tmp = max(abs(plane[i][0] - plane[i+1][0]), abs(plane[i][1] -
72-
// // plane[i+1][1]));
73-
// // Max = min(tmp, Max);
74-
// }
56+
7557
cout << L << endl;
7658
}
7759
return 0;

ACM/Divide and Conquer/11378_Bey_Battle/in.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,4 @@
506506
-20901 -17385
507507
-1343 -49230
508508
5037 -46873
509-
-4285 57582
509+
-4285 57582
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
3138
2+
2576
3+
2167
4+
1376
5+
1945
6+
833
7+
2486
8+
1179
9+
1987
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
3138
2+
2576
3+
2167
4+
1376
5+
1945
6+
833
7+
2486
8+
1179
9+
1987

ACM/Greedy Algorithms/10718/10718

9.28 KB
Binary file not shown.

ACM/Greedy Algorithms/10718/10718.cc

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Algorithm design:
3+
4+
first we locate the valid bits of the number N. For example, for 100 50 60,
5+
the valid bits are the last four bits. Becasue we have:
6+
7+
100 - 1100100
8+
50 - 110010
9+
60 - 111100
10+
11+
We can notice that 50 and 60 has the first bits in common.
12+
13+
Let's substract 32 from them.
14+
50-32-16 = 2 - 0010
15+
60-32-16 = 12 - 1100
16+
17+
So we only need to focus on the last four digits of 100, which is:
18+
0100
19+
20+
So the best possible four bits are:
21+
1011
22+
23+
Now we scan from the leftmost (greediest),
24+
25+
can we have 1?
26+
- 1000 is in range [0010, 1100]. yes
27+
can we have 0?
28+
- 1000 is in range [0010, 1100]. yes
29+
can we have 1?
30+
- 1010 is in range [0010, 1100]. yes
31+
can we have 1?
32+
- 1011 is in range [0010, 1100]. yes
33+
34+
Now let's change to another situation. What if the range is [0010, 1000]
35+
36+
can we have 1?
37+
- 1000 is in range [0010, 1000]. yes
38+
can we have 0?
39+
- 1000 is in range [0010, 1000]. yes
40+
can we have 1?
41+
- 1010 is not in range [0010, 1000]. no
42+
So the solution is 1010 plue the first two bits. the number is 111010 = 58
43+
*/
44+
45+
#include <iostream>
46+
#include <fstream>
47+
48+
using namespace std;
49+
50+
typedef long long ll;
51+
52+
int main() {
53+
54+
}

ACM/Greedy Algorithms/10718/10718.pdf

12.4 KB
Binary file not shown.

ACM/Greedy Algorithms/11054/11054

15 KB
Binary file not shown.

ACM/Greedy Algorithms/11054/11054.cc

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
typedef long long ll;
8+
typedef vector<ll> vi;
9+
10+
ll n;
11+
vi a(100000);
12+
13+
int main() {
14+
// Unit tests:
15+
16+
// ifstream in("in.txt");
17+
// streambuf *cinbuf = std::cin.rdbuf(); //save old buf
18+
// cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
19+
20+
while (cin>>n) {
21+
if (n==0) return 0;
22+
23+
for (ll i=0; i<n; ++i) {
24+
cin>>a[i];
25+
}
26+
27+
ll work = 0;
28+
for (ll i=0; i<n; i++) {
29+
if (a[i] != 0) {
30+
a[i+1] += a[i];
31+
work += abs(a[i]);
32+
}
33+
}
34+
35+
cout << work << endl;
36+
}
37+
return 0;
38+
}

ACM/Greedy Algorithms/11054/11054.pdf

13.4 KB
Binary file not shown.

ACM/Greedy Algorithms/11054/in.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
10
2+
100 200 300 400 500 -500 -400 -300 -200 -100
3+
10
4+
100 200 300 400 500 -100 -200 -300 -400 -500
5+
10
6+
100 -100 200 -200 300 -300 400 -400 500 -500
7+
3
8+
-10 20 -10
9+
10
10+
100 200 300 400 500 600 700 800 900 -4500
11+
10
12+
-2100 500 400 300 200 500 800 400 600 -1600
13+
15
14+
-1 -2 -3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15
15+
0

Cracking_the_coding_interview/1.1.py

+87-11
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,114 @@
33
What if you can not use additional data structures?
44
'''
55

6-
'''Algorithm Design
6+
'''
77
Confusions:
88
1. What do you mean by "all unique characters"? I assume it is ACSII string with
99
value 0~255.
1010
2. What do you mean by "cannot use addtional data structures"? I assume I can
1111
only use string.
12+
3. Does the string need to include all 256 characters? Or just need to have no
13+
duplicates. (assume the later one)
1214
Examples:
1315
1) agshdhfkd -> False
1416
2) agsdhfk -> True
15-
Design:
16-
Create a hash function to map all unique characters
1717
'''
1818

19-
def hash_function(char):
20-
return ord(char)
21-
19+
# Idea: hash table
2220
# time: O(n)
2321
# space: O(1) - fixed size 256
24-
def all_unique_string(string):
22+
def all_unique_character_1(string):
2523
table = [0 for x in range(256)]
2624
for char in string:
27-
index = hash_function(char)
25+
# ord returns the integer ordinal of a one-character string
26+
index = ord(char)
2827
if table[index] == 0:
2928
table[index] = 1
3029
else:
3130
return False
3231
return True
3332

34-
if __name__ == '__main__':
35-
print all_unique_string('agshdhfkd')
36-
print all_unique_string('agsdhfk')
33+
# Idea: hash table
34+
# time: ?
35+
# space: O(1)
36+
def all_unique_character_2(string):
37+
table = []
38+
for char in string:
39+
if char in table:
40+
return False
41+
table.append(char)
42+
return True
3743

44+
# Idea: use bit vector, since Mac OS is in 64 bit, we need 4 vectors
45+
# time: O(n)
46+
# space: O(1)
47+
def all_unique_character_3(string):
48+
checker_1 = checker_2 = checker_3 = checker_4 = 0
49+
for char in string:
50+
order = ord(char)
51+
if order < 64:
52+
if (checker_1 & (1<<order)) != 0:
53+
return False
54+
checker_1 |= 1<<order
55+
elif order < 128:
56+
order -= 64
57+
if (checker_2 & (1<<order)) != 0:
58+
return False
59+
checker_2 |= 1<<order
60+
elif order < 192:
61+
order -= 128
62+
if (checker_3 & (1<<order)) != 0:
63+
return False
64+
checker_3 |= 1<<order
65+
else:
66+
order -= 192
67+
if (checker_4 & (1<<order)) != 0:
68+
return False
69+
checker_4 |= 1<<order
70+
return True
71+
72+
73+
''' Below are Unit Tests '''
74+
75+
def unit_test_1(func):
76+
input_string = 'agshdhfkd'
77+
expected = False
78+
if func(input_string) == expected:
79+
print "Pass: Unit Test 1"
80+
else:
81+
print "Fail: Unit Test 1"
3882

83+
def unit_test_2(func):
84+
input_string = 'agsdhfk'
85+
expected = True
86+
if func(input_string) == expected:
87+
print "Pass: Unit Test 2"
88+
else:
89+
print "Fail: Unit Test 2"
3990

91+
def unit_test_3(func):
92+
input_string = 'agsdhfkA123BGW'
93+
expected = True
94+
if func(input_string) == expected:
95+
print "Pass: Unit Test 3"
96+
else:
97+
print "Fail: Unit Test 3"
4098

99+
def unit_test_4(func):
100+
input_string = 'agsdhfkA123BGW278'
101+
expected = False
102+
if func(input_string) == expected:
103+
print "Pass: Unit Test 4"
104+
else:
105+
print "Fail: Unit Test 4"
106+
107+
def run_unit_tests(func):
108+
unit_test_1(func)
109+
unit_test_2(func)
110+
unit_test_3(func)
111+
unit_test_4(func)
112+
113+
if __name__ == '__main__':
114+
run_unit_tests(all_unique_character_1)
115+
run_unit_tests(all_unique_character_2)
116+
run_unit_tests(all_unique_character_3)

Cracking_the_coding_interview/1.2_reverse_c_style_string.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@
77
terminator. A null terminator means that the string ends with a '\0' character
88
(which has ASCII code 0)
99
10-
It is almost the same as revert a linked list
10+
It is almost the same as reverting a linked list
1111
1212
'''
1313

14-
1514
# clarification questions:
1615
# 1) input is a string ending with '\0'
1716
# 2) i cannot use .reverse()
1817

1918
class Node:
20-
def __init__(self,data,next):
19+
def __init__(self,data,successor):
2120
self.data = data
22-
self.next = next
21+
self.next = successor
2322

23+
def reverseString(node):
24+
head = Node('\0',node)
25+
pre = None
26+
while head != None:
27+
head.next, pre, head = pre, head, head.next
28+
return pre.next
2429

2530
def reverseNodeList(node,previous):
2631
if node.next == None:
@@ -52,7 +57,7 @@ def reverseCStyleString(c_str):
5257
node2 = Node('c',node1)
5358
node3 = Node('b',node2)
5459
node4 = Node('a',node3)
55-
node = reverseCStyleString(node4)
60+
node = reverseString(node4)
5661
while True:
5762
print node.data
5863
if node.next==None:break

NAVDIA/sort_a_linked_list.py

-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def merge(lst1,lst2):
4444
else:
4545
return [lst2[0]] + merge(lst2[1:],lst1)
4646

47-
4847
if __name__ == '__main__':
4948
node1 = Node(4,None)
5049
node2 = Node(2,node1)
@@ -55,8 +54,3 @@ def merge(lst1,lst2):
5554
print node.data
5655
if node.next==None:break
5756
node = node.next
58-
59-
60-
61-
62-

SmartUQ/range_string_compression.py

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
44
Question: given a string "3:8,5,2: 5,11:22,5,7, 4,100", compress to "2:8,11:22,100"
55
6+
7+
[1:4],[2:6],7 --> [1:7]
8+
69
Limitations:
710
1) cannot deal with bad input such as '1,3,2:::::::5,,::::,,'
811

0 commit comments

Comments
 (0)