├── README.md ├── HourRank_31_P1.py ├── WeekOfCode38_Problem_2.py ├── HourRank_31_P1.cpp ├── 101Hack53_P1.cpp ├── HourRank_31_P2.py ├── HourRank_31_P1.java ├── HackTheInterviewGlobal_P2.dyalog ├── HourRank29_P1.java ├── HackTheInterviewGlobal_P2.hs ├── HourRank_31_P2.cpp ├── WeekOfCode38_Problem_2.java ├── Dynamic_Programming_01_Equal.java ├── WeekOfCode38_Problem_2.cpp ├── HourRank29_P1.cpp ├── 101Hack55_P1.cpp ├── HourRank27_P1.cpp ├── Dynamic_Programming_02_CountTheArray.cpp ├── 101Hack55_P2.cpp ├── WomensCodesprint2019_StrangelyCompatible.py ├── Dynamic_Programming_01_Equal.cpp ├── RookieRank04_Problem_2 ├── WomensCodesprint2019_StrangelyCompatible.js ├── HourRank_30_P1.java ├── WeekOfCode38_Problem_3.cpp ├── WomensCodesprint2019_StrangelyCompatible.cpp ├── 101Hack53_P2.cpp ├── Dynamic_Programming_04_Abbrevition.py ├── HourRank27_P3.cpp ├── HourRank_30_P1.py ├── RookieRank04_Problem_4 ├── WeekOfCode36_Problem_4 ├── 101Hack54_P2.cpp ├── Dynamic_Programming_04_Abbrevition.cpp ├── HourRank27_P2.cpp ├── HourRank_30_P1.cpp ├── Uni_CodeSprint_5_P3.cpp ├── Dynamic_Programming_03_Sam.cpp └── 101Hack55_P3.cpp /README.md: -------------------------------------------------------------------------------- 1 | # HackerRank -------------------------------------------------------------------------------- /HourRank_31_P1.py: -------------------------------------------------------------------------------- 1 | # code_report Solution 2 | # https://youtu.be/XKQQYCScP0U 3 | 4 | def solve(h, w, l): 5 | p = max(a - b//4 for a, b in zip(w, l)) 6 | return max(0, p - h) 7 | -------------------------------------------------------------------------------- /WeekOfCode38_Problem_2.py: -------------------------------------------------------------------------------- 1 | # code_report Solution 2 | # https://youtu.be/HEizjKb7LjY 3 | 4 | def minuteToWinIt(a, k): 5 | j = 0 6 | m = dict() 7 | for e in a: 8 | m[e-j] = m.get(e-j, 0) + 1 9 | j += k 10 | return len(a) - max(m.values()) 11 | -------------------------------------------------------------------------------- /HourRank_31_P1.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/XKQQYCScP0U 3 | 4 | int solve(int h, vector w, vector l) { 5 | int p = 0; 6 | for (int i = 0; i < w.size(); ++i) 7 | p = max(p, w[i] - l[i] / 4); 8 | return max(0, p - h); 9 | } 10 | -------------------------------------------------------------------------------- /101Hack53_P1.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/xjLXiNlStMQ?t=3m26s 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | string berthType (int n) { 10 | vector v = { "SUB", "LB", "MB", "UB", "LB", "MB", "UB", "SLB" }; 11 | return v[n % 8]; 12 | } 13 | -------------------------------------------------------------------------------- /HourRank_31_P2.py: -------------------------------------------------------------------------------- 1 | # code_report Solution 2 | # https://youtu.be/E6_-mvn8xIw 3 | 4 | def avg(a, b): 5 | return (sum(a)+b)/len(a) 6 | 7 | def solve(n, a): 8 | a = sorted(a, reverse = True) 9 | on, off = a[0:n], sum(a[n:]) 10 | while on and avg(on, off) < max(on): on.pop(0) 11 | print("%.5f"% (avg(on, off))) 12 | -------------------------------------------------------------------------------- /HourRank_31_P1.java: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/XKQQYCScP0U 3 | 4 | class Result { 5 | public static int solve(int h, List w, List l) { 6 | int p = 0; 7 | for (int i = 0; i < w.size(); ++i) 8 | p = Math.max(p, w.get(i) - l.get(i)/4); 9 | return Math.max(0, p - h); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HackTheInterviewGlobal_P2.dyalog: -------------------------------------------------------------------------------- 1 | ⍝ code_report Solution 2 | ⍝ Problem Link: https://www.hackerrank.com/contests/hack-the-interview-global/challenges/heads-or-tails/problem 3 | 4 | ⍝ One liner 5 | getMaxStreaks ← {{⌈/+/¨(1,2≠/⍵)⊂⍵}(⍤1)⍉y,(⍤0)~y←'Heads'∘≡¨⍵} 6 | 7 | ⍝ More liner 8 | headsTails ← {⍉y,(⍤0)~y←'Heads'∘≡¨⍵} 9 | chunkEqual ← {(1,2≠/⍵)⊂⍵} 10 | getMaxStreaks ← {{⌈/+/¨chunkEqual}(⍤1)headsTails ⍵} 11 | -------------------------------------------------------------------------------- /HourRank29_P1.java: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/YHoEJ_UhG1Y 3 | 4 | static String solve(List> b) 5 | { 6 | int n = b.size (); 7 | for (int i = 0; i < n; ++i) { 8 | for (int j = 1; j < n; ++j) { 9 | if (b.get (i).get (j) == b.get (i).get (j - 1)) return "No"; 10 | if (b.get (j).get (i) == b.get (j - 1).get (i)) return "No"; 11 | } 12 | } 13 | return "Yes"; 14 | } 15 | -------------------------------------------------------------------------------- /HackTheInterviewGlobal_P2.hs: -------------------------------------------------------------------------------- 1 | -- code_report Solution 2 | -- Problem Link: https://www.hackerrank.com/contests/hack-the-interview-global/challenges/heads-or-tails/problem 3 | 4 | maximumOr0 [] = 0 5 | maximumOr0 xs = maximum xs 6 | 7 | streak toss = maximumOr0 8 | . map length 9 | . filter ((toss==) . head) 10 | . group 11 | 12 | getMaxStreaks tosses = [(streak "Heads" tosses), 13 | (streak "Tails" tosses)] 14 | -------------------------------------------------------------------------------- /HourRank_31_P2.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/E6_-mvn8xIw 3 | 4 | void solve(int n, vector a) { 5 | sort(begin(a), end(a), greater()); 6 | auto on = accumulate(begin(a), begin(a) + n, 0.0); 7 | auto off = accumulate(begin(a) + n, end(a), 0.0); 8 | auto i = 0; 9 | auto avg = [&]() { return (off + on) / (n - i); }; 10 | while (avg() < a[i] && i < n) on -= a[i], ++i; 11 | cout << setprecision(10) << fixed << avg() << endl; 12 | } 13 | -------------------------------------------------------------------------------- /WeekOfCode38_Problem_2.java: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/HEizjKb7LjY 3 | 4 | static int minuteToWinIt(int[] a, int k) { 5 | Integer ans = n-1; 6 | Long j = 0L; 7 | HashMap m = new HashMap (); 8 | for (int e : a) { 9 | m.put (e - j, m.getOrDefault (e - j, 0) + 1); 10 | j += k; 11 | } 12 | for (Map.Entry e : m.entrySet ()) ans = Math.min (ans, n - e.getValue ()); 13 | return ans; 14 | } 15 | -------------------------------------------------------------------------------- /Dynamic_Programming_01_Equal.java: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/rdlzz2GOcq4 3 | 4 | static int equal(int[] a) { 5 | int min = Arrays.stream(a).min().getAsInt(); 6 | int ans = Integer.MAX_VALUE; 7 | for (int i = 0; i < 5; ++i) { 8 | int ops = 0; 9 | for (int j = 0; j < a.length; ++j) { 10 | int t = a[j] - (min - i); 11 | ops += t/5 + t%5/2 + t%5%2; 12 | } 13 | ans = Math.min(ans, ops); 14 | } 15 | return ans; 16 | } 17 | -------------------------------------------------------------------------------- /WeekOfCode38_Problem_2.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/HEizjKb7LjY 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int minuteToWinIt (vector a, int k) 9 | { 10 | long long j = 0; 11 | unordered_map m; 12 | for (auto e : a) ++m[e - j], j += k; 13 | auto compare_values = [](auto a, auto b) { return a.second < b.second; }; 14 | return a.size () - max_element (m.begin (), m.end (), compare_values)->second; 15 | } 16 | -------------------------------------------------------------------------------- /HourRank29_P1.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/YHoEJ_UhG1Y 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | #define FORI(s,n) for(int i = s; i < n; i++) 10 | #define FORJ(s,n) for(int j = s; j < n; j++) 11 | 12 | string solve (vector> b) 13 | { 14 | int n = b.size (); 15 | FORI (0, n) FORJ (1, n) if (b[i][j] == b[i][j - 1]) return "No"; 16 | FORI (0, n) FORJ (1, n) if (b[j][i] == b[j - 1][i]) return "No"; 17 | return "Yes"; 18 | } 19 | -------------------------------------------------------------------------------- /101Hack55_P1.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/Zr4sMtgCsao 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int swapToSort (vector a) 10 | { 11 | if (is_sorted (a.begin (), a.end ())) return 0; 12 | int n = a.size (); 13 | for (int i = 0; i < n; ++i) { 14 | for (int j = i + 1; j < n; ++j) { 15 | swap (a[i], a[j]); 16 | if (is_sorted (a.begin (), a.end ())) return 1; 17 | swap (a[i], a[j]); 18 | } 19 | } 20 | return -1; 21 | } 22 | -------------------------------------------------------------------------------- /HourRank27_P1.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/GzJereQHbJE 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | string canModify (vector a) 10 | { 11 | if (a.size () == 1) return "YES"; 12 | 13 | vector b = a; 14 | 15 | for (int i = 1; i < a.size (); ++i) { 16 | if (a[i] < a[i - 1]) { 17 | a[i - 1] = i > 1 ? a[i - 2] : 1; 18 | b[i] = b[i - 1]; 19 | break; 20 | } 21 | } 22 | 23 | return ((is_sorted (a.begin (), a.end ()) || is_sorted (b.begin (), b.end ())) ? "YES" : "NO"); 24 | } 25 | -------------------------------------------------------------------------------- /Dynamic_Programming_02_CountTheArray.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/_nLeiMMSd4E 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int MOD = 1000000007; 9 | 10 | long countArray (int n, int k, int x) 11 | { 12 | vector b (n), a (n); 13 | 14 | a[0] = x == 1 ? 1 : 0; // ends in x 15 | b[0] = x == 1 ? 0 : 1; // doesn't end in x 16 | 17 | for (int i = 1; i < n; ++i) { 18 | a[i] = b[i - 1] % MOD; 19 | b[i] = (a[i - 1] * (k - 1) + b[i - 1] * (k - 2)) % MOD; 20 | } 21 | 22 | return a[n - 1]; 23 | } 24 | -------------------------------------------------------------------------------- /101Hack55_P2.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/xonmFdwR-Ew 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | long long minimumSum (vector l, vector r) 9 | { 10 | int a = l[0], b = r[0]; 11 | long long ans = 0; 12 | 13 | for (int i = 1; i < l.size (); ++i) { 14 | int c = l[i], d = r[i]; 15 | if (c > b) ans += c - b, a = c, b = c; 16 | else if (d < a) ans += a - d, a = d, b = d; 17 | else { 18 | if (c > a) a = c; 19 | if (d < b) b = d; 20 | } 21 | } 22 | 23 | return ans; 24 | } 25 | -------------------------------------------------------------------------------- /WomensCodesprint2019_StrangelyCompatible.py: -------------------------------------------------------------------------------- 1 | # code_report Solution 2 | # Video Link: https://youtu.be/BxSRoYSNkgM 3 | # Problem Link: https://www.hackerrank.com/contests/hackerrank-all-womens-codesprint-2019/challenges/strangely-compatible 4 | 5 | def pair_count(t): 6 | return sum((i*(i-1))//2 for i in t.values()) 7 | 8 | def strangelyCompatible(students, m): 9 | changed, same = {}, {} 10 | for s in students: 11 | same[s] = same.get(s, 0) + 1 12 | for i in range(m): 13 | t = s[:i] + '!' + s[i+1:] 14 | changed[t] = changed.get(t, 0) + 1 15 | 16 | return pair_count(changed) - pair_count(same) * m 17 | -------------------------------------------------------------------------------- /Dynamic_Programming_01_Equal.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/rdlzz2GOcq4 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int equal (vector a) { 11 | auto min_ = *min_element (a.begin (), a.end ()); 12 | auto ans = numeric_limits::max (); 13 | for (auto i = 0; i < 5; i++) { 14 | auto operations = [min_, i](auto init, auto j) { 15 | auto t = j - (min_ - i); 16 | return init + t / 5 + t % 5 / 2 + t % 5 % 2; 17 | }; 18 | ans = min (ans, accumulate (a.begin (), a.end (), 0, operations)); 19 | } 20 | return ans; 21 | } 22 | -------------------------------------------------------------------------------- /RookieRank04_Problem_2: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/jndCKH5Pp0Q?t=2m11s 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | string checkAll (int n, vector h, vector p) { 11 | 12 | string res = "NONE"; 13 | bool allfall = true; 14 | int m = 0; 15 | 16 | for (int i = 0; i < n - 1; i++) { 17 | m = max (m, h[i] + p[i]); 18 | if (m < p[i + 1]) allfall = false; 19 | } 20 | 21 | if (allfall) res = "LEFT"; 22 | 23 | allfall = true; 24 | m = 1001; 25 | 26 | for (int i = n - 1; i >= 1; i--) { 27 | m = min (m, p[i] - h[i]); 28 | if (m > p[i - 1]) allfall = false; 29 | } 30 | 31 | if (allfall) res = res == "LEFT" ? "BOTH" : "RIGHT"; 32 | 33 | return res; 34 | } 35 | -------------------------------------------------------------------------------- /WomensCodesprint2019_StrangelyCompatible.js: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // Video Link: https://youtu.be/BxSRoYSNkgM 3 | // Problem Link: https://www.hackerrank.com/contests/hackerrank-all-womens-codesprint-2019/challenges/strangely-compatible 4 | 5 | function pairCount(t) { 6 | return Object.values(t).reduce((a, b) => a + (b * (b - 1)) / 2, 0) 7 | } 8 | 9 | function strangelyCompatible(students, m) { 10 | var changed = {}, same = {} 11 | for (var j in students) { 12 | var s = students[j] 13 | same[s] = (same[s]+1) || 1 ; 14 | for (var i = 0; i < m; ++i) { 15 | var t = s.substring(0, i) + '!' + s.substring(i+1, m) 16 | changed[t] = (changed[t]+1) || 1 ; 17 | } 18 | } 19 | return pairCount(changed) - pairCount(same) * m 20 | } 21 | -------------------------------------------------------------------------------- /HourRank_30_P1.java: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/uOG3QyxIjso 3 | 4 | public static List solve(List names) { 5 | HashSet s = new HashSet<>(); 6 | HashMap m = new HashMap<>(); 7 | List l = new ArrayList<>(); 8 | for (String name : names) { 9 | if (m.containsKey(name)) { 10 | int n = m.get(name); 11 | m.put(name, n+1); 12 | l.add(name + " " + Integer.toString(n+1)); 13 | } else { 14 | m.put(name, 1); 15 | String t = ""; 16 | boolean inserted = false; 17 | for (char c : name.toCharArray()) { 18 | t += c; 19 | if (!inserted && !s.contains(t) ) { 20 | inserted = true; 21 | l.add(t); 22 | } 23 | s.add(t); 24 | } 25 | if (!inserted) l.add(t); 26 | } 27 | } 28 | return l; 29 | } 30 | -------------------------------------------------------------------------------- /WeekOfCode38_Problem_3.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/KDuPh7QRc88 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | long long leastTimeToInterview (int n, int k, int m) 12 | { 13 | vector>> v (n + 1); // first = time, second = stop # 14 | 15 | while (m--) { 16 | int i, j, t; cin >> i >> j >> t; 17 | v[i].insert ({ t, j }); 18 | v[j].insert ({ t, i }); 19 | } 20 | 21 | set> s ({ { 0,1 } }); 22 | unordered_set done; 23 | 24 | while (true) 25 | { 26 | auto cs = *s.begin (); // cs = current stop 27 | s.erase (s.begin ()); 28 | if (cs.second == n) return cs.first; 29 | done.insert (cs.second); 30 | 31 | for (auto ns : v[cs.second]) { // ns = next stop 32 | if (done.count (ns.second)) continue; 33 | auto t = cs.first + ns.first; 34 | if (ns.second != n && (t / k) % 2 == 1) t = (t / k + 1) * k; 35 | s.insert ({ t, ns.second }); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /WomensCodesprint2019_StrangelyCompatible.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // Video Link: https://youtu.be/BxSRoYSNkgM 3 | // Problem Link: https://www.hackerrank.com/contests/hackerrank-all-womens-codesprint-2019/challenges/strangely-compatible 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | template 14 | auto pair_count(T const& t) { 15 | return accumulate(t.begin(), t.end(), 0LL, 16 | [](auto a, auto b) { return a + (b.second * (b.second-1))/2; }); 17 | } 18 | 19 | auto strangelyCompatible(vector students, int m) { 20 | unordered_map changed, same; 21 | for (auto& student : students) { 22 | same[student]++; 23 | for (auto& letter : student) { 24 | auto c = letter; 25 | letter = '!'; 26 | changed[student]++; 27 | letter = c; 28 | } 29 | } 30 | return pair_count(changed) - pair_count(same) * m; 31 | } 32 | 33 | int main() 34 | { 35 | int n, m; cin >> n >> m; 36 | vector v(n); 37 | for (int i = 0; i < n; ++i) cin >> v[i]; 38 | cout << strangelyCompatible(v, m) << endl; 39 | } 40 | -------------------------------------------------------------------------------- /101Hack53_P2.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/6rEp_F7CTH0 3 | 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | void fillBoard (int n, int m, int x, int y) { 11 | 12 | bool solvable = (n*m - x - y) % 2 == 0; // if tiles to fill not even, can't solve 13 | 14 | if (solvable) { 15 | 16 | vector v; 17 | vector> res; 18 | 19 | int s = 1; // state 20 | int c = 0; // count 21 | 22 | vector> li = { { 0, m, 1 },{ m - 1, -1, -1 } }; // loop info for state 0, 1 23 | 24 | for (int i = 0; i < n; i++) { 25 | 26 | if (v.size () == 0 && i != 0) s = ((n - i) % 2 == 0); // set state for special case 27 | else s = 1 - s; // switch state 28 | 29 | for (int j = li[s][0]; j != li[s][1]; j += li[s][2]) { 30 | if (c >= x && c < n*m - y) { 31 | if (j >= m - y && i == n - 1) solvable = false; // run into Y-deleted squares 32 | v.push_back (i + 1); 33 | v.push_back (j + 1); 34 | if (v.size () == 4) { 35 | res.push_back (v); 36 | v.clear (); 37 | } 38 | } 39 | c++; 40 | } 41 | } 42 | 43 | if (solvable) { 44 | cout << "YES" << endl << ((n*m - x - y) / 2) << endl; 45 | for (const auto& v : res) { 46 | for (const auto& e : v) cout << e << ' '; 47 | cout << endl; 48 | } 49 | } 50 | } 51 | 52 | if (!solvable) cout << "NO" << endl; 53 | } 54 | -------------------------------------------------------------------------------- /Dynamic_Programming_04_Abbrevition.py: -------------------------------------------------------------------------------- 1 | # code_report Solution 2 | 3 | # Solution 1: 4 | # https://youtu.be/DW92IHf8KK8 5 | 6 | memo = set() 7 | possible = False 8 | 9 | def abbreviation(a, b): 10 | global possible, memo 11 | possible = False 12 | memo.clear() 13 | rec(a,b) 14 | return "YES" if possible else "NO" 15 | 16 | def rec(a, b): 17 | global possible, memo 18 | if (possible or len(a) < len(b)): 19 | return 20 | if (len(b) == 0): 21 | if (a.islower() or len(a) == 0): 22 | possible = True 23 | return 24 | 25 | t = a+'#'+b 26 | if (t in memo): 27 | return 28 | memo.add(t); 29 | 30 | fc = a[0:1] # first char 31 | a = a[1:] 32 | if (fc.islower()): 33 | rec(a,b) 34 | if (fc.upper() != b[0:1]): 35 | return 36 | b = b[1:] 37 | rec(a,b) 38 | 39 | return 40 | 41 | # Solution 2: 42 | # https://youtu.be/ViILdd8495M 43 | 44 | def abbreviation(a, b): 45 | n, m = len(a), len(b) 46 | dp = [[0 for i in range(m+1)] for j in range(n+1)] 47 | dp[0][0] = 1 48 | 49 | for i in range(n): 50 | for j in range(m+1): 51 | if not dp[i][j]: continue; 52 | if j < m and a[i].upper() == b[j]: dp[i+1][j+1] = 1; 53 | if not a[i].isupper(): dp[i+1][j ] = 1; 54 | 55 | return 'YES' if dp[n][m] else 'NO' 56 | -------------------------------------------------------------------------------- /HourRank27_P3.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/bfV4XhpzpBE 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main () 13 | { 14 | int n, q, x, y; 15 | cin >> n >> q; 16 | 17 | vector a (n), b (n), sum_a = { 0 }, sum_b = { 0 }; 18 | 19 | for (int i = 0; i < n; i++) 20 | { 21 | cin >> x >> y; 22 | a[i] = x + y; 23 | b[i] = x - y; 24 | } 25 | 26 | sort (a.begin (), a.end ()); 27 | sort (b.begin (), b.end ()); 28 | 29 | partial_sum (a.begin (), a.end (), back_inserter (sum_a)); 30 | partial_sum (b.begin (), b.end (), back_inserter (sum_b)); 31 | 32 | while (q--) 33 | { 34 | long long target_a, target_b, ans = 0; 35 | cin >> x >> y; 36 | 37 | target_a = x + y; 38 | target_b = x - y; 39 | 40 | int idx = distance (a.begin (), lower_bound (a.begin (), a.end (), target_a)); 41 | 42 | ans += idx * target_a - sum_a[idx]; 43 | ans += sum_a.back () - sum_a[idx] - (n - idx) * target_a; 44 | 45 | idx = distance (b.begin (), lower_bound (b.begin (), b.end (), target_b)); 46 | 47 | ans += idx * target_b - sum_b[idx]; 48 | ans += sum_b.back () - sum_b[idx] - (n - idx) * target_b; 49 | 50 | cout << ans / 2 << endl; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /HourRank_30_P1.py: -------------------------------------------------------------------------------- 1 | # code_report Solution 2 | 3 | # Solution 1: 4 | # https://youtu.be/uOG3QyxIjso 5 | 6 | import defaultdict from collections 7 | 8 | def solve(names): 9 | s = set() 10 | d = defaultdict(int) 11 | l = list() 12 | for name in names: 13 | if name in d: 14 | d[name] += 1 15 | l.append(name+" "+str(d[name])) 16 | else: 17 | d[name] = 1 18 | t = "" 19 | inserted = False; 20 | for i in range(len(name)): 21 | t += name[i:i+1] 22 | if t not in s and not inserted: 23 | inserted = True 24 | l.append(t) 25 | s.add(t) 26 | if not inserted: 27 | l.append(name) 28 | return l 29 | 30 | # Solution 2: 31 | # https://youtu.be/VsiP-dTWyG4?t=249 32 | 33 | def solve(names): 34 | trie = {} 35 | res = [] 36 | for name in names: 37 | node = trie 38 | printed = False 39 | for i, x in enumerate(name, start=1): 40 | if x not in node: 41 | if not printed: 42 | res.append(name[:i]) 43 | printed = True 44 | node[x] = defaultdict(int) 45 | node = node[x] 46 | node['count'] += 1 47 | 48 | if not printed: 49 | res.append (name + (' '+str(node['count']) if node['count']!=1 else '')) 50 | return res 51 | -------------------------------------------------------------------------------- /RookieRank04_Problem_4: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/jndCKH5Pp0Q?t=7m9s 3 | 4 | // First Solution <- THIS WILL TLE 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int winningHands (int m, int x, vector a) { 13 | 14 | int n = a.size (), ans = 0; 15 | 16 | for (int i = 1; i <= n; i++) { 17 | 18 | string bitmask (i, 1); // K leading 1's 19 | bitmask.resize (n, 0); // N-K trailing 0's 20 | 21 | do { 22 | 23 | long long res = 1; 24 | for (int j = 0; j < n; j++) if (bitmask[j]) { res *= a[j]; res %= m; } 25 | if (res == x) ans++; 26 | 27 | } while (prev_permutation (bitmask.begin (), bitmask.end ())); 28 | } 29 | 30 | return ans; 31 | } 32 | 33 | // Second solution <- THIS WILL PASS (NOT TLE 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | using namespace std; 41 | 42 | using ll = long long; 43 | 44 | ll winningHands (int m, int n, int x, vector a) { 45 | 46 | int state = 0; 47 | 48 | vector> dp (2, vector (m)); 49 | 50 | for (int i = 0; i < n; i++) { 51 | 52 | // copy one state to another 53 | dp[state] = dp[state ^ 1]; 54 | 55 | // double the count of "modulus groups" for the new element a[i] 56 | for (int j = 0; j < m; j++) dp[state][((ll) j*a[i]) % m] += dp[state ^ 1][j]; 57 | 58 | dp[state][a[i] % m]++; 59 | 60 | state ^= 1; 61 | } 62 | 63 | return dp[state ^ 1][x]; 64 | } 65 | -------------------------------------------------------------------------------- /WeekOfCode36_Problem_4: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/dehTDLmuDKk 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | using ll = long long; 11 | ll const MX = static_cast (1e5 + 1); 12 | ll heights[MX], prices[MX], dp[MX]; 13 | 14 | int main () 15 | { 16 | int n; cin >> n; 17 | for (int i = 1; i <= n; i++) cin >> heights[i]; 18 | for (int i = 2; i <= n; i++) cin >> prices [i]; 19 | 20 | stack> s; 21 | 22 | // iterating backwards 23 | for (int i = n; i >= 1; i--) 24 | { 25 | dp[i] = static_cast (1e16); // minimum sum if Mason was actually student i 26 | ll temp = static_cast (1e16); 27 | 28 | while (!s.empty () && heights[s.top ().first] <= heights[i]) // process students IF height of student in stack is <= current student height 29 | { 30 | ll next_temp = s.top ().second; 31 | dp[i] = min (dp[i], next_temp - i + heights[i]); // THESE TWO LINES WORK TOGETHER 32 | temp = min (temp, next_temp); 33 | s.pop (); 34 | } 35 | 36 | if (s.empty ()) { 37 | dp[i] = min (dp[i], n + 1ll - i); 38 | } 39 | else { 40 | auto j = s.top ().first; // index of element to the right, will always be have height > current height 41 | dp[i] = min (dp[i], j - i + dp[j] + heights[j] - heights[i] + prices[j]); // THIS LINE WORKS BY ITSELF 42 | } 43 | 44 | temp = min (temp, dp[i] + i - heights[i] + prices[i]); // THESE TWO LINES WORK TOGETHER 45 | s.push (make_pair (i * 1ll, temp)); 46 | } 47 | 48 | cout << dp[1] << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /101Hack54_P2.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/OZXNM0qK17A 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | using ld = long double; 13 | 14 | ld time_to_end (ld pos, ld stop, ld last_stop, ld v, ld t, ld w, ld s) 15 | { 16 | ld walk_time = abs (pos - stop) / s; 17 | ld time_arrive_at_stop = t + walk_time; 18 | ld bus_arrival_offset = stop / v; 19 | ld bus_number = max ((ld) 0, ceil ((time_arrive_at_stop - bus_arrival_offset) / w)); 20 | ld time_get_on_bus = bus_number * w + bus_arrival_offset; 21 | ld time_on_bus = (last_stop - stop) / v; 22 | 23 | return time_get_on_bus + time_on_bus; 24 | } 25 | 26 | void solve (vector stops, ld w, ld v, ld q) 27 | { 28 | while (q--) { 29 | ld pos, t, s; 30 | cin >> pos >> t >> s; 31 | auto ans = numeric_limits::max (); 32 | auto it = upper_bound (stops.begin (), stops.end (), pos); 33 | 34 | if (it != stops.end ()) ans = min (ans, time_to_end (pos, *it, stops.back (), v, t, w, s)); 35 | if (it != stops.begin ()) ans = min (ans, time_to_end (pos, *prev (it), stops.back (), v, t, w, s)); 36 | ans = min (ans, (stops.back () - pos) / s + t); 37 | 38 | cout << fixed << setprecision (10) << ans << endl; 39 | } 40 | } 41 | 42 | int main () 43 | { 44 | int n, q; cin >> n; 45 | vector stops (n); 46 | for (int i = 0; i < n; ++i) cin >> stops[i]; 47 | ld w, v; cin >> w >> v >> q; 48 | 49 | solve (stops, w, v, q); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Dynamic_Programming_04_Abbrevition.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | 3 | // Solution 1: 4 | // https://youtu.be/DW92IHf8KK8 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | void rec(string A, string B); 13 | 14 | unordered_set memo; 15 | bool possible; 16 | 17 | auto abbreviation(string a, string b) { 18 | possible = false; 19 | memo.clear(); 20 | rec(a, b); 21 | return possible ? "YES" : "NO"; 22 | } 23 | 24 | void rec(string A, string B) { 25 | if (possible || A.size() < B.size()) return; 26 | if (B.empty()) { 27 | if (all_of(A.begin(), A.end(), [](auto e) { return islower(e); })) 28 | possible = true; 29 | return; 30 | } 31 | 32 | auto p = memo.insert(A + '#' + B); 33 | if (!p.second) return; // return if A#B already in set 34 | 35 | auto fc = A[0]; // first char 36 | A.erase(0, 1); 37 | if (islower(fc)) rec(A, B); 38 | if (toupper(fc) != B[0]) return; 39 | B.erase(0, 1); 40 | rec(A, B); 41 | 42 | return; 43 | } 44 | 45 | // Solution 2: 46 | // https://youtu.be/ViILdd8495M 47 | 48 | auto abbreviation(string a, string b) { 49 | int n = a.size(); 50 | int m = b.size(); 51 | vector> dp(n + 1, vector(m + 1)); 52 | dp[0][0] = 1; 53 | 54 | for (int i = 0; i < n; i++) { 55 | for (int j = 0; j <= m; j++) { 56 | if (!dp[i][j]) continue; 57 | if (j < m && toupper(a[i]) == b[j]) dp[i + 1][j + 1] = 1; 58 | if (!isupper(a[i])) dp[i + 1][j ] = 1; 59 | } 60 | } 61 | 62 | return (dp[n][m] ? "YES\n" : "NO\n"); 63 | } 64 | -------------------------------------------------------------------------------- /HourRank27_P2.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/nu4NUh16Mmc 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | using ll = long long; 12 | 13 | const ll MIN = numeric_limits::min (); 14 | 15 | template 16 | auto first_less_than (const T& ms, ll val) { 17 | auto it = ms.lower_bound (val); 18 | if (it == ms.end ()) --it; 19 | while (it != ms.begin () && *it >= val) --it; 20 | return *it >= val ? ms.end () : it; 21 | } 22 | 23 | ll calc (ll a, ll b, ll c) { 24 | if (a == MIN || c == MIN) return MIN; 25 | return (a < b && b < c) ? a * b * c : MIN; 26 | } 27 | 28 | ll maximumProfit (const vector& profits) 29 | { 30 | multiset right (profits.begin (), profits.end ()); 31 | set left; 32 | 33 | ll res = MIN; 34 | 35 | for (ll b : profits) 36 | { 37 | ll a1, a2, c1, c2; 38 | right.erase (right.find (b)); 39 | 40 | if (!right.empty () && !left.empty ()) 41 | { 42 | a1 = *left.begin (); 43 | auto it = first_less_than (left, b); 44 | a2 = it != left.end () ? *it : MIN; 45 | 46 | it = right.upper_bound (b); 47 | c1 = it != right.end () ? *it : MIN; 48 | c2 = *(--right.end ()); 49 | 50 | res = max (res, calc (a1, b, c1)); 51 | res = max (res, calc (a2, b, c1)); 52 | res = max (res, calc (a1, b, c2)); 53 | res = max (res, calc (a2, b, c2)); 54 | } 55 | 56 | left.insert (b); 57 | } 58 | 59 | return res == MIN ? -1LL : res; 60 | } 61 | 62 | int main () 63 | { 64 | int n; cin >> n; 65 | vector profits (n); 66 | for (int i = 0; i < n; ++i) cin >> profits[i]; 67 | 68 | cout << maximumProfit (profits) << endl; 69 | 70 | return 0; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /HourRank_30_P1.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | 3 | // Solution 1: 4 | // https://youtu.be/uOG3QyxIjso 5 | 6 | vector solve (vector names) { 7 | unordered_set s; 8 | unordered_map m; 9 | vector ans; 10 | for (const auto& name : names) { 11 | auto it = m.find (name); 12 | if (it != m.end ()) { 13 | it->second++; 14 | ans.push_back (name + ' ' + to_string (it->second)); 15 | } 16 | else { 17 | m[name] = 1; 18 | string t; 19 | bool inserted = false; 20 | for (auto c : name) { 21 | t += c; 22 | auto p = s.insert (t); 23 | if (!inserted && p.second) { 24 | inserted = true; 25 | ans.push_back (t); 26 | } 27 | } 28 | if (!inserted) ans.push_back (t); // very tricky case 29 | } 30 | } 31 | return ans; 32 | } 33 | 34 | // Solution 2 (Trie): 35 | // https://youtu.be/VsiP-dTWyG4?t=249 36 | 37 | struct trie { 38 | unordered_map m; 39 | int count = 0; 40 | }; 41 | 42 | vector solve(vector names) { 43 | auto* t = new trie(); 44 | vector res; 45 | for (const auto& name : names) { 46 | auto* node = t; 47 | auto added = false; 48 | auto p = ""s; 49 | for (auto c : name) { 50 | p += c; // prefix 51 | if (!node->m.count(c)) { 52 | if (!added) res.push_back(p), added = true; 53 | node->m[c] = new trie(); 54 | } 55 | node = node->m[c]; 56 | } 57 | node->count++; 58 | 59 | if (!added) 60 | res.push_back(p + (node->count != 1 ? " " + to_string(node->count) : "")); 61 | } 62 | return res; 63 | } 64 | -------------------------------------------------------------------------------- /Uni_CodeSprint_5_P3.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/gE2vs81o_7g 3 | 4 | using vi = vector; 5 | using vvi = vector; 6 | using ll = long long; 7 | 8 | int dfs (const vvi& al, vi& c, int i, int group) { 9 | if (c[i] != -1) return -1; 10 | c[i] = group; 11 | for (const auto& node : al[i]) dfs (al, c, node, group); 12 | return group; 13 | } 14 | 15 | auto create_adjacency_list (int n, const vvi &edges) { 16 | vvi al (n); 17 | for (const auto& e : edges) { 18 | int x = e[0] - 1; 19 | int y = e[1] - 1; 20 | al[x].push_back (y); 21 | al[y].push_back (x); 22 | } 23 | return al; 24 | } 25 | 26 | auto determine_components (int n, const vvi &al) { 27 | vi c (n, -1); 28 | for (int i = 0; i < n; ++i) 29 | dfs (al, c, i, i); 30 | return c; 31 | } 32 | 33 | auto find_min_max_edges (int n, const vvi &al, const vi &c) { 34 | unordered_map> m; 35 | 36 | // initialize min/max for each component 37 | for (const auto& e : c) { 38 | m[e] = { 0, n }; 39 | } 40 | 41 | // find min and max edges for each component 42 | for (int i = 0; i < n; i++) { 43 | auto& p = m[c[i]]; 44 | p.first = max (p.first, (ll)al[i].size ()); 45 | p.second = min (p.second, (ll)al[i].size ()); 46 | } 47 | 48 | return m; 49 | } 50 | 51 | int solve (int n, vvi edges, int a, int b) 52 | { 53 | auto al = create_adjacency_list (n, edges); 54 | auto c = determine_components (n, al); 55 | auto m = find_min_max_edges (n, al, c); 56 | 57 | auto criteria = [&, i = 0] (const auto& edges) mutable { 58 | auto v_e = edges.size (); 59 | auto p = m[c[i]]; i++; 60 | return (a * p.second < v_e && v_e < b * p.first); 61 | }; 62 | 63 | return count_if (al.begin (), al.end (), criteria); 64 | } 65 | -------------------------------------------------------------------------------- /Dynamic_Programming_03_Sam.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/kXS66eP0T6s 3 | 4 | // SOLUTION 1 - O(n^2) 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MOD = 1000000007; 14 | 15 | auto substrings (string s) { 16 | auto ans = 0LL; 17 | vector dp; 18 | for (auto c : s) { 19 | for (auto& e : dp) e *= 10, e += c - '0', e %= MOD; 20 | dp.push_back (c - '0'); 21 | ans += accumulate (dp.begin (), dp.end (), 0LL) % MOD; 22 | } 23 | return ans % MOD; 24 | } 25 | 26 | int main () { 27 | string n; 28 | cin >> n; 29 | cout << substrings (n) << endl; 30 | } 31 | 32 | // SOLUTION 2 - O(n) - less algorithms 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | using namespace std; 39 | 40 | const int MOD = 1000000007; 41 | 42 | long substrings (string s) { 43 | int n = s.size (); 44 | vector a (n), b (n); // a = weight, b = weight sum 45 | a[0] = 1, b[0] = 1; 46 | 47 | for (int i = 1; i < n; i++) { 48 | a[i] = (10 * a[i - 1]) % MOD; 49 | b[i] = (b[i - 1] + a[i]) % MOD; 50 | } 51 | 52 | long ans = 0; 53 | for (int i = 0; i < n; i++) { 54 | ans += ((s[i] - '0') * b[n - i - 1] * (i + 1)) % MOD; 55 | ans %= MOD; 56 | } 57 | 58 | return ans; 59 | } 60 | 61 | int main () { 62 | string n; 63 | cin >> n; 64 | cout << substrings (n) << endl; 65 | } 66 | 67 | // SOLUTION 3 - O(n) - more algorithms 68 | 69 | #include 70 | #include 71 | #include 72 | #include 73 | #include 74 | 75 | using namespace std; 76 | 77 | const int MOD = 1000000007; 78 | 79 | auto substrings (string s) { 80 | int n = s.size (); 81 | vector a (n), b (n); a[0] = 1; // a = weight, b = weight sum 82 | 83 | generate (a.begin () + 1, a.end (), [&, i = 1LL] () mutable { i *= 10; i %= MOD; return i; }); 84 | partial_sum (a.begin (), a.end (), b.begin (), [&](auto i, auto j) { return (i + j) % MOD; }); 85 | 86 | auto value_add_by_digit = [&, i = 0] (auto t, auto j) mutable { 87 | ++i; return (t + ((j - '0') * b[n - i] * i)) % MOD; 88 | }; 89 | 90 | return accumulate (s.begin (), s.end (), 0LL, value_add_by_digit); 91 | } 92 | 93 | int main () { 94 | string n; 95 | cin >> n; 96 | cout << substrings (n) << endl; 97 | } 98 | -------------------------------------------------------------------------------- /101Hack55_P3.cpp: -------------------------------------------------------------------------------- 1 | // code_report Solution 2 | // https://youtu.be/czzx3pVAdw4 3 | 4 | // Original solution 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | long fewestTowers (std::vector xs, std::vector ys) 19 | { 20 | map> xy; 21 | int n = xs.size (); 22 | 23 | for (int i = 0; i < n; ++i) { 24 | int x = xs[i]; 25 | int y = ys[i]; 26 | xy[x].insert (y); 27 | } 28 | 29 | int c = xy.size (); // cols 30 | vector t, b; // top, bottom 31 | 32 | for (const auto& col : xy) { 33 | b.push_back (*col.second.begin ()); 34 | t.push_back (*col.second.rbegin ()); 35 | } 36 | 37 | auto j = distance (b.begin (), min_element (b.begin (), b.end ())); 38 | auto k = distance (t.begin (), max_element (t.begin (), t.end ())); 39 | 40 | for (int i = 1; i <= j; ++i) b[i] = min (b[i], b[i - 1]); 41 | for (int i = 1; i <= k; ++i) t[i] = max (t[i], t[i - 1]); 42 | for (int i = c-2; i >= j; --i) b[i] = min (b[i], b[i + 1]); 43 | for (int i = c-2; i >= k; --i) t[i] = max (t[i], t[i + 1]); 44 | 45 | long ans = -n; 46 | for (auto i = 0; i < c; ++i) ans += t[i] - b[i] + 1; // could be transform 47 | 48 | return ans; 49 | } 50 | 51 | // Modern solution 52 | 53 | template 54 | F group_equal (I f, I l, P p, F action) { 55 | while (f != l) { 56 | I cur_end = std::upper_bound (f, l, *f, p); 57 | action (f, cur_end); 58 | f = cur_end; 59 | } 60 | return action; 61 | } 62 | 63 | template 64 | void set_perimeter (I f, I l, P p) { 65 | using rev = std::reverse_iterator; 66 | auto limit = min_element (f, l, p); 67 | auto min_p = [p](const auto& x, const auto& y) { return p (x, y) ? x : y; }; 68 | std::partial_sum (f, limit, f, min_p); 69 | std::partial_sum (rev (l), rev (limit), rev (l), min_p); 70 | } 71 | 72 | long fewestTowers (std::vector xs, std::vector ys) 73 | { 74 | std::vector> dots (xs.size ()); 75 | std::transform (xs.begin (), xs.end (), ys.begin (), dots.begin (), [](int x, int y) { return std::make_pair (x, y); }); 76 | std::sort (dots.begin (), dots.end ()); 77 | 78 | std::vector t, b; // top, bottom 79 | group_equal (dots.begin (), dots.end (), [](const auto& dot1, const auto& dot2) { return dot1.first < dot2.first; }, 80 | [&](auto f, auto l) mutable { b.push_back (f->second); t.push_back ((--l)->second); }); 81 | 82 | set_perimeter (b.begin (), b.end (), std::less<> ()); 83 | set_perimeter (t.begin (), t.end (), std::greater<> ()); 84 | 85 | long ans = - (int) xs.size (); 86 | for (auto i = 0; i < t.size (); ++i) ans += t[i] - b[i] + 1; 87 | 88 | return ans; 89 | } 90 | --------------------------------------------------------------------------------