Skip to content

Instantly share code, notes, and snippets.

View the-fejw's full-sized avatar

FeJW the-fejw

  • Fullstack Engineer
  • CA
View GitHub Profile
@the-fejw
the-fejw / 4.1.2findMaxSubarrsy.java
Last active August 29, 2015 13:55
CLRS 4.1.2-4.1.3 O(nlgn)
public static int[] findMaxSubArray(int[] A, int low, int high) {
int[] result = new int[3];
if (high == low) {
result[0] = low;
result[1] = high;
result[2] = A[low];
} else {
int mid = (low + high) / 2;
int[] leftResult = findMaxSubArray(A, low, mid);
@the-fejw
the-fejw / gist:8781249
Created February 3, 2014 10:00
CLRS 4.1.2 Brute Force
public static int[] bruteForce(int[] A, int low, int high) {
int[] result = new int[3]; //result[0]: max-left, result[1]: max-right, result[2]: max-sum
int sum = 0;
int maxSum = Integer.MIN_VALUE;
for (int i = low; i <= high; i++) {
for (int j = i; j <= high; j++) {
sum += A[j];
if (sum > maxSum) {
maxSum = sum;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head, a ListNode
# @return a ListNode
def deleteDuplicates(self, head):
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param two ListNodes
# @return a ListNode
def mergeTwoLists(self, l1, l2):
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {