lintcode584- Drop Eggs II- medium

There is a building of n floors. If an egg drops from the k th floor or above, it will break. If it‘s dropped from any floor below, it will not break.

You‘re given m eggs, Find k while minimize the number of drops for the worst case. Return the number of drops in the worst case.

Example

Given m = 2n = 100 return 14
Given m = 2n = 36 return 8

动态规划+递归。用二元数组存储某鸡蛋某层所需的次数。迭代试扔第一个鸡蛋,在某层扔。1.扔碎了即转为鸡蛋少一个,楼层少一层的子问题。2.没扔碎即转化为鸡蛋没有少楼层少为上半层那么多的子问题。

思维方式见:http://datagenetics.com/blog/july22012/index.html的“Look see I can do three”

public class Solution {
    /*
     * @param m: the number of eggs
     * @param n: the number of floors
     * @return: the number of drops in the worst case
     */

    public int dropEggs2(int m, int n) {
        // write your code here
        int[][] dp = new int[m + 1][n + 1];

        for (int i = 1; i <= m; i++){
            dp[i][0] = 0;
            dp[i][1] = 1;
        }

        for (int j = 1; j <= n; j++){
            dp[1][j] = j;
        }

        for (int i = 2; i <= m; i++){
            for (int j = 2; j <= n; j++){
                dp[i][j] = Integer.MAX_VALUE;
                for(int k = 1; k < j; k++){
                    dp[i][j] = Math.min(dp[i][j],
                                        1 + Math.max(dp[i - 1][k - 1], dp[i][j - k]));
                }
            }
        }
        return dp[m][n];
    }
}
时间: 2024-10-24 00:56:59

lintcode584- Drop Eggs II- medium的相关文章

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,

[array] leetcode - 40. Combination Sum II - Medium

leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat

leetCode 213. House Robber II | Medium | Dynamic Programming

213. House Robber II Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are a

[CareerCup] 6.5 Drop Eggs 扔鸡蛋问题

6.5 There is a building of 100 floors. If an egg drops from the Nth floor or above, it will break. If it's dropped from any floor below, it will not break. You're given two eggs. Find N, while minimizing the number of drops for the worst case 这道题说有10

leetcode496 - Next Greater Element I - easy &amp;&amp; leetcode503 - Next Greater Element II - medium

496. Next Greater Element IYou are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.The Next Greater Number o

122. Best Time to Buy and Sell Stock II [medium] (Python)

题目链接 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目原文 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions

lintcode143 - Sort Colors II - medium

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.ExampleGiven colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-p

leetcode253 - Meeting Rooms II - medium

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.Example 1:Input: [[0, 30],[5, 10],[15, 20]]Output: 2Example 2:Input: [[7,10],[2,4]]Output

92. Reverse Linked List II - Medium

Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 只反转从m到n的部分链表 M1: iterative 首先找到prev的位置(开始反转位置的前一个节

445. Add Two Numbers II - Medium

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers