Skip to content

Instantly share code, notes, and snippets.

View hsaputra's full-sized avatar

Henry Saputra hsaputra

View GitHub Profile
class Solution {
public int minPathSum(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
final int rows = grid.length;
final int cols = grid[0].length;
int[][] visited = new int[rows][cols];
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
if (candidates == null || candidates.length == 0) {
return new LinkedList<List<Integer>>();
}
List<List<Integer>> results = new LinkedList<>();
List<Integer> prefix = new LinkedList<>();
recurseSum(results, prefix, 0, candidates, target);
@hsaputra
hsaputra / gist:a591b7347043d3ea1a51278600d367dc
Created October 27, 2018 05:26
Leetcode 109 Convert Sorted List to Binary Search Tree
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
@hsaputra
hsaputra / gist:c8e142b5cacb4efb5bd1b34cc7acc50d
Created November 1, 2018 18:45
Convert Binary Search Tree to Sorted Doubly Linked List
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node() {}
public Node(int _val,Node _left,Node _right) {
@hsaputra
hsaputra / gist:d2cc3a1fbe8cbb106b0d98679272d8c3
Last active November 10, 2018 17:41
33. Search in Rotated Sorted Array
class Solution {
// [4,5,6,7,0,1,2,3]
// [6,7,0,1,2,3,4,5]
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
final int numsLen = nums.length;
class Solution {
public int leastInterval(char[] tasks, int n) {
if (n < 0 ) {
return 0;
}
// Group tasks
int[] count = new int[26];
for (char cur : tasks) {
int pos = cur - 'A';
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] results = new int[nums.length];
int[] left = new int[nums.length];
Arrays.fill(left, 1);
// [1, 2, 3, 4]
// [1, 1, 2, 6]
for (int i = 1; i < nums.length; i++) {
class Solution {
public int trap(int[] height) {
int count = 0;
int left = 0;
int right = height.length - 1;
int maxLeft = 0;
int maxRight = 0;
while (left < right) {
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class Solution {