Skip to content

Instantly share code, notes, and snippets.

View rajeakshay's full-sized avatar

Akshay Raje rajeakshay

  • Bloomberg LP
  • New York, NY
  • 10:17 (UTC -04:00)
View GitHub Profile
@rajeakshay
rajeakshay / main.hs
Created March 31, 2016 05:01
Introduction to Haskell - Notes from the 1 hour talk by Greg Hale (@imalsogreg) at Northeastern University.
module Main where
-- This is how you write a comment in Haskell
import Prelude hiding (length) -- Hide length function to define our own later
import Data.List ()
import Data.Char -- required for toUpper
-- Defining your own data types
data MyBool = MyTrue
@rajeakshay
rajeakshay / ReverseLinkedListII.java
Last active September 18, 2016 12:09
LeetCode 92. Reverse Linked List II (Problem: https://leetcode.com/problems/reverse-linked-list-ii) - Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.
/**
* LeetCode 92. Reverse Linked List II (Problem: https://leetcode.com/problems/reverse-linked-list-ii)
* Reverse a linked list from position m to n. Do it in-place and in one-pass.
* For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL.
* Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.
*/
/**
* Definition for singly-linked list.
@rajeakshay
rajeakshay / AddTwoNumbers.java
Last active September 18, 2016 12:12
LeetCode 2. Add Two Numbers (Problem: https://leetcode.com/problems/add-two-numbers) - You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0…
/**
* LeetCode 2. Add Two Numbers (Problem Link: https://leetcode.com/problems/add-two-numbers)
* You are given two linked lists representing two non-negative numbers.
* The digits are stored in reverse order and each of their nodes contain a single digit.
* Add the two numbers and return it as a linked list.
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
*/
/**
* Definition for singly-linked list.
@rajeakshay
rajeakshay / CountingBits.java
Last active September 18, 2016 12:10
LeetCode 338. Counting Bits - Counting number of set bits in all numbers from 0 to num (Problem: https://leetcode.com/problems/counting-bits) - Given a non negative integer number num. For every number i in the range 0 ≤ i ≤ num, calculate and store the number of 1's in the binary representation of i and return results as an array. Example: For …
/**
* LeetCode 338. Counting Bits
* Counting number of set bits in all numbers from 0 to num (Problem Link: https://leetcode.com/problems/counting-bits)
* Given a non negative integer number num. For every number i in the range 0 ≤ i ≤ num, calculate and store the number of 1's
* in the binary representation of i and return results as an array.
* Example: For num = 5, program should return [0,1,1,2,1,2].
*/
public class CountingBits {
@rajeakshay
rajeakshay / ReverseWordsInAString.java
Last active September 18, 2016 12:08
LeetCode 151. Reverse Words in a String - (Problem: https://leetcode.com/problems/reverse-words-in-a-string) - Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". NOTES: A sequence of non-space characters constitutes a word. Remove all leading and trailing spaces. Reduce mul…
/**
* LeetCode 151. Reverse Words in a String (Problem Link: https://leetcode.com/problems/reverse-words-in-a-string)
* Given an input string, reverse the string word by word.
* For example, given s = "the sky is blue", return "blue is sky the".
* NOTES:
* 1. A sequence of non-space characters constitutes a word.
* 2. Remove all leading and trailing spaces.
* 3. Reduce multiple spaces between words into a single space.
*
*/
@rajeakshay
rajeakshay / DivideTwoIntegers.java
Last active September 18, 2016 12:13
LeetCode 29. Divide Two Integers - (Problem: https://leetcode.com/problems/divide-two-integers) - Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.
/**
* LeetCode 29. Divide Two Integers (Problem Link: https://leetcode.com/problems/divide-two-integers)
* Divide two integers without using multiplication, division and mod operator.
* If it is overflow, return MAX_INT. (Integer.MAX_VALUE in Java)
*/
public class DivideTwoIntegers {
public int divide(int dividend, int divisor) {
// Arrr...gh! Divide by zero
if(divisor == 0) return Integer.MAX_VALUE;
@rajeakshay
rajeakshay / BillPughSingleton.java
Created August 8, 2016 23:06
Two primary approaches of implementing a Singleton class in Java.
/**
*
* Thread-safe singleton with lazy initialization. (Bill Pugh's approach)
*
* NOTES -
* When BillPughSingleton class is loaded, SingletonHelper class is not loaded into memory. Only when someone calls
* getInstance() method, does the SingletonHelper class gets loaded and creates a BillPughSingleton instance.
*
* The advantage comes from the lazy initialization. BillPughSingleton instance, which might be potentially consuming
* very high memory, will only be created when getInstance is called the first time.
@rajeakshay
rajeakshay / DecodeString.java
Last active October 9, 2018 02:13
LeetCode 394. Decode String - (Problem Link - https://leetcode.com/contest/3/problems/decode-string/) Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assu…
/**
* LeetCode 394. Decode String - (Problem Link - https://leetcode.com/contest/3/problems/decode-string/)
*
* Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string
* inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
* You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
* Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat
* numbers, k. For example, there won't be input like 3a or 2[4].
*
* Examples:
@rajeakshay
rajeakshay / NumberOfIslands.java
Last active September 18, 2016 12:14
LeetCode 200. Number of Islands - (Problem Link: https://leetcode.com/problems/number-of-islands/) Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded b…
/**
*
LeetCode 200. Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110
11010
11000
@rajeakshay
rajeakshay / SurroundedRegions.java
Last active September 18, 2016 12:14
LeetCode 130. Surrounded Regions - (Problem Link - https://leetcode.com/problems/surrounded-regions/) Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, [X X X X, X O O X, X X O X, X O X X] After running your fu…
/**
LeetCode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
For example,
X X X X
X O O X
X X O X
X O X X