Mots-clés » D.P.

The Guava Chronicles

As a child, I ate guava like it was going out of style, as we Trinis would say. We had a guava tree that never failed to bear fruit (actually, when I left Trinidad the tree was still standing and healthy). 465 mots de plus

Bogotá

Leetcode: House Robber II

Leetcode: House Robber I很类似,但是需要遍历两遍,第一遍加入第一个元素,因此最后一个元素不能用,第二遍不要第一个元素,因此可以使用最后一个元素。很巧妙。

public class Solution {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }      
        //special case of length <= 3, return the max of the 3 elements
        if (nums.length <= 3) {
            return Math.max(nums[0], nums.length == 1 ? 93 mots de plus
Leetcode

Edit Distance - leetcode

Question:
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) 167 mots de plus

DP

Climbing Stairs - leetcode

Question:
You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. 78 mots de plus

DP

Minimum Path Sum - leetcode

Question:
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. 126 mots de plus

Array