Coder Social home page Coder Social logo

leetcode-ruby's Introduction

Table of Contents

leetcode-ruby

What are those

These are my leetcode solutions along with some other stuff:

  • Each id.problem_title.rb is a leetcode solution in ruby.
  • other contains problems which are not on leetcode.
  • data-structures contains the general data structures I use to solve problems. They are used throughout my solutions.

What you get

  • Solutions have problem description with it so that you don’t need to visit leetcode. You can also use this repo with leetcode-cli. If you are using Emacs, you can use my plugin lain-leetcode.el.
  • Well documented code, see 4.median-of-two-sorted-arrays.rb for an example.
  • Some problems are difficult to understand if I just shove the optimal solution in your face, therefore I write my thought process and code evolving process, see 10.regular-expression-matching.rb for an example.
  • Well written code, maybe? Coming from FP and OOP background, I tend to write concise function and class with clean API.

Ruby

Ruby, as Matz said, really is a programming language that makes my happy (with leetcode problems of course, being a dynamically typed language after all). But Ruby is not the language to solve intense problems on codeforces or spoj, you’ll get TLE most of the time. However I like to prototype using Ruby for its easy-to-write nature, then rewrite in C/C++.

Ruby arrays are dynamic, it’s kind of like C++’s std::vector or Java’s ArrayList. Its shift (delete at beginning), unshift (insert at beginning), push (insert at end) and pop (delete at end) are amortized O(1) ops. With those amortized O(1) ops it can simulate stack and queue:

Stack

stack = []
stack << 2    # push 2 => stack = [2]
stack << 3    # push 3 => stack = [2, 3]
stack.pop     # pop  3 => stack = [2]

Queue

queue = []
queue << 2    # push 2 => queue = [2]
queue << 3    # push 3 => queue = [2, 3]
queue.shift   # pop  2 => queue = [3]

Map

Maps in ruby are hashes:

map = {}           # empty map
map = Hash.new(0)  # or a map with a default value of 0
map[:a] = 1        # map = {a:1}
map[:b] = 2        # map = {a:1,b:2}

Set

set = Set.new      # empty set
set << 1           # set = #{1}
set << 2           # set = #{1,2}
set << 1           # set = #{1,2}

Other Data Structures

Other data structures such as Priority Queue, Heap, BST, Ruby doesn’t have them build-in, so I implement them when I need to, see data-structures directory:

  • uniou-find.rb Union Find (weighed quick-union with path compression).
  • heap.rb Min Heap, Max Heap and Priority Queue.
  • fenwick-tree.rb 1d Fenwick Tree with range sum query.
  • segment-tree.rb 1d Segment Tree with various query examples.
  • sparse-table.rb 1d Sparse Table with range minimum/maximum query.
  • treap.rb Treap (rotation based and merge/split based).

Memes

./imgs/meme0.jpg

./imgs/meme1.jpg

./imgs/meme2.jpg

Other Useful Stuff

Algorithm

System design

Other

  • TODO Knight Tour
  • Fair Work Load (Binary Search)
  • TODO HashMap With Expiration Time

leetcode-ruby's People

Contributors

acemerlin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.