source code Archives - Page 7 of 10 - Coddicted

Minimum Path Sum

Title: Minimum Path Sum Source: leetcode.com Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Java solution Java /* https://leetcode.com/problems/minimum-path-sum/ */ ...

Merge Intervals

Title: Merge Intervals Source: leetcode.com Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. Java solution Java /* https://leetcode.com/problems/merge-intervals/ */ import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Stack; public class MergeIntervals { public List<Interval> merge(List<Interval> intervals) { this.sort(intervals); Stack<Interval> stack = new Stack<>(); for(Interval interval : intervals) ...

Maximum Subarray

Title: Maximum Subarray Source: leetcode.com Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figured out the O(n) solution, try coding another solution using the divide ...

Kth Smallest Element in a BST

Title: Kth Smallest Element in a BST Source: leetcode.com Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements. Follow up: What if the BST is modified (insert/delete operations) often and you need ...

Implement Stack using Queues

Title: Implement Stack using Queues Source: leetcode.com Implement the following operations of a stack using queues. push(x) — Push element x onto stack. pop() — Removes the element on top of the stack. top() — Get the top element. empty() — Return whether the stack is empty. Notes: You must use only standard operations of ...

Implement Queue using Stacks

Title: Implement Queue using Stacks Source: leetcode.com Implement the following operations of a queue using stacks. push(x) — Push element x to the back of queue. pop() — Removes the element from in front of queue. peek() — Get the front element. empty() — Return whether the queue is empty. Notes: You must use only ...

House Robber

Title: House Robber Source: leetcode.com You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses ...

Gray Code

Title: Gray Code Source: leetcode.com The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. For example, given n = ...

First Bad Version

Title: First Bad Version Source: leetcode.com You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have ...

Course Schedule

Title: Course Schedule Source: leetcode.com There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and ...