Skip to content

Commit 976c003

Browse files
committed
8/5/23
- Buy and sell single share of stock (C++/Py) - Buy and sell two shares of stock (C++/Py) - Alternating array - Is string palindromic - Print all primes between 0 and n - Anki updates
1 parent d1f18be commit 976c003

12 files changed

+182
-27
lines changed

anki/Competitive Programming.apkg

2.34 KB
Binary file not shown.

elements-of-programming-interviews/cpp/alternating_array.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
#include "test_framework/timed_executor.h"
88
using std::vector;
99
void Rearrange(vector<int>* A_ptr) {
10-
// TODO - you fill in here.
10+
vector<int>& A = *A_ptr;
11+
if (A.size() == 0) {
12+
return;
13+
}
14+
for (size_t i = 0; i < A.size() - 1; i++) {
15+
if (((i % 2 == 0) && A[i] > A[i + 1]) || ((i % 2) && A[i] < A[i + 1])) {
16+
int tmp = A[i];
17+
A[i] = A[i + 1];
18+
A[i + 1] = tmp;
19+
}
20+
}
1121
return;
1222
}
1323
void CheckAnswer(const vector<int>& A) {

elements-of-programming-interviews/cpp/buy_and_sell_stock.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
#include "test_framework/generic_test.h"
44
using std::vector;
55
double BuyAndSellStockOnce(const vector<double>& prices) {
6-
// TODO - you fill in here.
7-
return 0.0;
6+
double bestBuy = prices[0];
7+
double bestSell = 0.0;
8+
for (auto price : prices) {
9+
bestBuy = (bestBuy < price) ? bestBuy : price;
10+
bestSell = (bestSell > price - bestBuy) ? bestSell : price - bestBuy;
11+
}
12+
return bestSell;
813
}
914

1015
int main(int argc, char* argv[]) {

elements-of-programming-interviews/cpp/buy_and_sell_stock_twice.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
#include <limits>
12
#include <vector>
23

34
#include "test_framework/generic_test.h"
45
using std::vector;
56
double BuyAndSellStockTwice(const vector<double>& prices) {
6-
// TODO - you fill in here.
7-
return 0.0;
7+
double negative_infinity = -std::numeric_limits<double>::infinity();
8+
double firstBuy = negative_infinity;
9+
double firstSell = 0.0;
10+
double secondBuy = negative_infinity;
11+
double secondSell = 0.0;
12+
for (auto price : prices) {
13+
firstBuy = (firstBuy > -price) ? firstBuy : -price;
14+
firstSell = (firstSell > (price + firstBuy)) ? firstSell : price + firstBuy;
15+
secondBuy =
16+
(secondBuy > (firstSell - price)) ? secondBuy : firstSell - price;
17+
secondSell =
18+
(secondSell > (secondBuy + price)) ? secondSell : secondBuy + price;
19+
}
20+
return secondSell;
821
}
922

1023
int main(int argc, char* argv[]) {

elements-of-programming-interviews/cpp/is_string_palindromic.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22

33
#include "test_framework/generic_test.h"
44
using std::string;
5+
56
bool IsPalindromic(const string& s) {
6-
// TODO - you fill in here.
7+
if (s.empty()) {
8+
return false;
9+
}
10+
int l = 0, r = s.size() - 1;
11+
while (l <= r) {
12+
if (s[l++] != s[r--]) {
13+
return false;
14+
}
15+
}
16+
717
return true;
818
}
919

elements-of-programming-interviews/cpp/prime_sieve.cc

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,41 @@
22

33
#include "test_framework/generic_test.h"
44
using std::vector;
5-
// Given n, return all primes up to and including n.
5+
// Brute force method. Given n return all primes up to and including n.
6+
vector<int> GeneratePrimesBruteForce(int n) {
7+
int counter = 0;
8+
if (n == 0 || n == 1) {
9+
return {};
10+
}
11+
vector<int> primes{2};
12+
for (int i = 2; i <= n; i++) {
13+
int j = 0;
14+
for (; j < primes.size(); j++) {
15+
if (!(i % primes[j])) {
16+
break;
17+
}
18+
}
19+
if (j == primes.size()) {
20+
primes.push_back(i);
21+
}
22+
}
23+
return primes;
24+
}
25+
626
vector<int> GeneratePrimes(int n) {
7-
// TODO - you fill in here.
8-
return {};
27+
// Sieve of Eratosthenes
28+
vector<int> primes;
29+
vector<bool> table(n + 1, false);
30+
for (size_t i = 2; i <= n; i++) {
31+
if (table[i] == true) {
32+
continue;
33+
}
34+
primes.push_back(i);
35+
for (size_t j = 0; j <= n; j += i) {
36+
table[j] = true;
37+
}
38+
}
39+
return primes;
940
}
1041

1142
int main(int argc, char* argv[]) {

elements-of-programming-interviews/problem_mapping.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,57 +256,57 @@ problem_mapping = {
256256
},
257257
"5.06 Buy and sell a stock once": {
258258
"C++: buy_and_sell_stock.cc": {
259-
"passed": 0,
259+
"passed": 402,
260260
"total": 402
261261
},
262262
"Java: BuyAndSellStock.java": {
263263
"passed": 0,
264264
"total": 402
265265
},
266266
"Python: buy_and_sell_stock.py": {
267-
"passed": 0,
267+
"passed": 402,
268268
"total": 402
269269
}
270270
},
271271
"5.07 Buy and sell a stock twice": {
272272
"C++: buy_and_sell_stock_twice.cc": {
273-
"passed": 0,
273+
"passed": 402,
274274
"total": 402
275275
},
276276
"Java: BuyAndSellStockTwice.java": {
277277
"passed": 0,
278278
"total": 402
279279
},
280280
"Python: buy_and_sell_stock_twice.py": {
281-
"passed": 0,
281+
"passed": 402,
282282
"total": 402
283283
}
284284
},
285285
"5.08 Computing an alternation": {
286286
"C++: alternating_array.cc": {
287-
"passed": 0,
287+
"passed": 203,
288288
"total": 203
289289
},
290290
"Java: AlternatingArray.java": {
291291
"passed": 0,
292292
"total": 203
293293
},
294294
"Python: alternating_array.py": {
295-
"passed": 0,
295+
"passed": 203,
296296
"total": 203
297297
}
298298
},
299299
"5.09 Enumerate all primes to n": {
300300
"C++: prime_sieve.cc": {
301-
"passed": 0,
301+
"passed": 24,
302302
"total": 24
303303
},
304304
"Java: PrimeSieve.java": {
305305
"passed": 0,
306306
"total": 24
307307
},
308308
"Python: prime_sieve.py": {
309-
"passed": 0,
309+
"passed": 24,
310310
"total": 24
311311
}
312312
},
@@ -468,15 +468,15 @@ problem_mapping = {
468468
"Chapter 06: Strings": {
469469
"6.00 Bootcamp: Strings": {
470470
"C++: is_string_palindromic.cc": {
471-
"passed": 0,
471+
"passed": 10000,
472472
"total": 10000
473473
},
474474
"Java: IsStringPalindromic.java": {
475475
"passed": 0,
476476
"total": 10000
477477
},
478478
"Python: is_string_palindromic.py": {
479-
"passed": 0,
479+
"passed": 10000,
480480
"total": 10000
481481
}
482482
},

elements-of-programming-interviews/python/alternating_array.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@
77

88

99
def rearrange(A: List[int]) -> None:
10-
# TODO - you fill in here.
10+
A.sort()
11+
i = 1
12+
while i < len(A)-1:
13+
tmp = A[i]
14+
A[i] = A[i+1]
15+
A[i+1] = tmp
16+
i += 2
17+
return
18+
19+
20+
def rearrange(A: List[int]) -> None:
21+
for i in range(len(A)-1):
22+
if ((i % 2 == 0) and A[i] > A[i+1]) or (i % 2 and A[i] < A[i+1]):
23+
tmp = A[i]
24+
A[i] = A[i+1]
25+
A[i+1] = tmp
26+
i += 2
1127
return
1228

1329

elements-of-programming-interviews/python/buy_and_sell_stock.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,41 @@
33
from test_framework import generic_test
44

55

6+
def buy_and_sell_stock_once_reversed(prices: List[float]) -> float:
7+
highest = prices[-1]
8+
profit = 0.0
9+
for price in prices[::-1]:
10+
highest = max(highest, price)
11+
profit = max(profit, highest-price)
12+
return profit
13+
14+
15+
def buy_and_sell_stock_once_explicit_min_price(prices: List[float]) -> float:
16+
lowest_price = float('inf')
17+
best_profit = 0
18+
for price in prices:
19+
lowest_price = min(price, lowest_price)
20+
best_profit = max(best_profit, price-lowest_price)
21+
return best_profit
22+
23+
624
def buy_and_sell_stock_once(prices: List[float]) -> float:
7-
# TODO - you fill in here.
8-
return 0.0
25+
"""
26+
On every day given the stock price, we can do one of 3 things:
27+
3) Sell 1st share. Profit: price - lowest price so far.
28+
4) Buy 1st share. Profit: -price.
29+
5) Nothing so far. Profit: 0.
30+
31+
To get the best possible sale for each, use the max function.
32+
"""
33+
first_buy = -float('inf')
34+
first_sell = 0
35+
36+
for price in prices:
37+
first_buy = max(first_buy, -price)
38+
first_sell = max(first_sell, price + first_buy)
39+
40+
return first_sell
941

1042

1143
if __name__ == '__main__':

elements-of-programming-interviews/python/buy_and_sell_stock_twice.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,31 @@
44

55

66
def buy_and_sell_stock_twice(prices: List[float]) -> float:
7-
# TODO - you fill in here.
8-
return 0.0
7+
"""
8+
On every day given the stock price, we can do one of 5 things:
9+
1) Sell 2nd share. Profit: profit from 2nd buy + price.
10+
2) Buy 2nd share. Profit: profit from first sale - price.
11+
3) Sell 1st share. Profit: price - lowest price so far.
12+
4) Buy 1st share. Profit: -price.
13+
5) Nothing so far. Profit: 0.
14+
15+
To get the best possible sale for each, use the max function. The ordering we do the updates in actually doesn't
16+
matter and still produces the correct result for the tests; however, the since the best second sell day depends
17+
on the best second buy day, which in turn depends on the best first sell day, the order should be first_buy -> first_sell ->
18+
second_buy -> second_sell.
19+
"""
20+
first_buy = -float('inf')
21+
first_sell = 0
22+
second_buy = -float('inf')
23+
second_sell = 0
24+
25+
for price in prices:
26+
first_buy = max(first_buy, -price)
27+
first_sell = max(first_sell, price + first_buy)
28+
second_buy = max(second_buy, first_sell - price)
29+
second_sell = max(second_sell, second_buy + price)
30+
31+
return second_sell
932

1033

1134
if __name__ == '__main__':

0 commit comments

Comments
 (0)