{"id":22846574,"url":"https://github.com/jsonkao/competitive-coding","last_synced_at":"2025-03-31T05:41:25.871Z","repository":{"id":114470306,"uuid":"162903118","full_name":"jsonkao/competitive-coding","owner":"jsonkao","description":"Resources for competitive computing, interviews, etc.","archived":false,"fork":false,"pushed_at":"2019-01-25T23:15:27.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-06T10:15:27.363Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsonkao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-23T15:38:44.000Z","updated_at":"2019-01-25T23:15:28.000Z","dependencies_parsed_at":"2023-06-08T07:00:43.468Z","dependency_job_id":null,"html_url":"https://github.com/jsonkao/competitive-coding","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonkao%2Fcompetitive-coding","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonkao%2Fcompetitive-coding/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonkao%2Fcompetitive-coding/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonkao%2Fcompetitive-coding/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsonkao","download_url":"https://codeload.github.com/jsonkao/competitive-coding/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246423494,"owners_count":20774796,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-13T03:29:38.907Z","updated_at":"2025-03-31T05:41:25.851Z","avatar_url":"https://github.com/jsonkao.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"### Table of Contents\n\n| [Strategy](#strategy) |\n|-----------------|\n\n| [Problem Patterns](#problem-patterns) |\n|-----------------|\n| [Arrays](#arrays) |\n\n| [Data Structures](#data-structures) |\n|-----------------|\n| [Using Arrays](#using-arrays) |\n| [Heaps](#heaps) |\n| [Graphs](#graphs) |\n\n| [Algorithms](#algorithms) |\n|-----------------|\n| [Sorting](#sorting) |\n| [Rubin-Karp substring search](#rubin-karp-substring-search) |\n\n# Strategy\n\n1. Binary search for a doable problem. Don't start from the easiest problems.\n2. [Recipe](http://mdotsabouri.blogspot.com/2014/10/my-recipe-to-improve-your-programming.html)\n\n\n# Problem Patterns\n\n## Arrays\n\nWhen doing greedy algorithms on arrays with dynamic programming, you typically store the best possible answer up to index `i` in another array's index `i`.\n\n# Data Structures\n\n## Using Arrays\n\n_For storing ordered collections_\n\nFor LinkedLists, Stacks, Queues, use native array methods:\n- Add to the end of an Array: `let newLength = fruits.push('Orange')`\n- Add to the front of an Array: `fruits.unshift('Apple')`\n- Remove from the end of an Array: `let last = fruits.pop(); // remove Orange (from the end)`\n- Remove from the front of an Array `let first = fruits.shift(); // remove Apple from the front`\n\nMore array methods:\n- Remove n items by index position: `let removedItem(s) = fruits.splice(pos, n); // this is how to remove an item`\n\n### Sets\n\n`new Set([ ... ])` stores unique values.\n\n## Graphs\n\nA graph is a set of vertices and a set of edges. Implementations are:\n1. each Vertex stores an adjacency list\n2. a map of { Vertex: adjacency list }\n\n### Topological sort\n\nSome ordering of vertices in a graph where if there is a path v_i to v_j, v_i appears before v_j in the ordering.\n\n1. We store an indegree on each vertex, which represents how many vertices point to it.\n2. We maintain a queue of vertices that have indegree = 0.\n3. For every vertex in the queue, decrement its neighbors' indegrees. Add neighbors\n\n```\ntopsort() throws CycleFoundException {\n  Queue\u003cVertex\u003e q = new Queue\u003cVertex\u003e( );\n  int counter = 0;\n\n  for each Vertex v\n    if( v.indegree == 0 )\n      q.enqueue( v );\n\n  while( !q.isEmpty( ) ) {\n\n    Vertex v = q.dequeue( ); \n    v.topNum = ++counter; // Assign next number\n\n    for each Vertex w adjacent to v\n      if( --w.indegree == 0 ) \n        q.enqueue( w );\n  }\n  if ( counter != NUM_VERTICES )\n    throw new CycleFoundException( );\n}\n```\n\n### Djiktra's algorithm\n\n```\nStarting at a vertex s,\nfor each Vertex v\n\tv.dist = Infinity\n\tv.known = false\ns.dist = 0\nwhile (there are unknown vertices) {\n\tVertex v = the unknown vertex with the smallest distance\n\tv.known = true\n\tfor each adjacent Vertex w to v:\n\t\tif (!w.known) {\n\t\t\td = distance from w to v\n\t\t\tif (v.dist + d \u003c w.dist)\n\t\t\t\tw.dist = v.dist + d\n\t\t}\n}\n```\n\n## Heaps\n\nHeaps are complete binary trees where the parent must always be (isMax ? \u003e : \u003c) than its children.\n- **insert:** Add to end of binary tree, then percolate up\n- **remove:** Put last element in root's spot, then percolate down. If max heap, switch with larger child. If min heap, switch with smaller child.\n\n---\n\n# Algorithms\n\n## Sorting\n\n**HeapSort:** Add elements of array into a max heap is `O(N)`. Swap last element of heap with deleteMax(), then last element down to a valid index (before deleteMax’s spot). We are adding the maxes from right to left, so we get an ascending array `O(NlogN)`. \n\n**Insertion sort:** For p from 1 to N-1, make sure array from to 0 to p is sorted. Basically depends on the p-1 case. So basically bubbling the newly absorbed element leftwards.\n\n**Merge sort:** sort left, sort right, merge the two. Merging has three counters: left, right, current. Inc them as appropriate while comparing left and right to determine which one goes into current. It's a good idea to Insertion sort arrays with N ≤ 20.\n\n**Quicksort:** pivot = median(first, middle, last element). partition with counters i from 0 and j from last - 1 (backwards). Move i forward until element ≥ pivot. Move j back until element ≤ pivot. Switch i and j. Return quicksort(left) + pivot + quicksort(right).\n\n## Rubin-Karp: search substring of length `s` in string of length `b`\n\nHashes windows of length `s`. Exploits the fact that `two strings equal --\u003e hashes are equal`. Strongly reduces string matching.\n\nProblems come from collisions, which can be reduced with a rolling hash function, which computes a hash based on the hash of a previous value in constant time.\n\nOur hash function treats a string in some base \u003e= the number of possible letters, say 128.\n```js\n// String: 'doe '\nlet code = c =\u003e c.charCodeAt(0);\n\nhash('doe') = code('d') * 128^2 + code('o') * 128 + code('e') * 1\nhash('oe ') = (hash('doe') - code('d') * 128^2) * 128 + code(' ')\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonkao%2Fcompetitive-coding","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsonkao%2Fcompetitive-coding","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonkao%2Fcompetitive-coding/lists"}