Leetcode-561. Array Partition I && 62. Unique Paths && 63. Unique Paths II

Source: Internet
Author: User

561, Topic:

Given an array of 2n integers, your task are to group integers into n pairs of integers, say (A1, B1), (A2, B2), ..., (A, BN) which makes sum of min (AI, bi) for all I from 1 to n as large as possible.

Example 1:
Input: [1,4,3,2]

output:4
explanation:n is 2, and the maximum sum of pairs is 4.
Note:
n was a positive integer, which is in the range of [1, 10000].
The integers in the array of would is in the range of [-10000, 10000].

In order to find a maximum minimum value problem, after reading the title, it is easy to see that the final problem can be turned into sorting the array, and then each sum, because in order to make the last and the largest, we need to let the maximum and the second large value in a number of pairs, and so on, and so on, the only way to "waste the Code in:

    public int arraypairsum (int[] nums) {
        arrays.sort (nums);
        int sum = 0;
        for (int i=0; i<nums.length; i+=2) {
            sum = nums[i];
        }
        return sum;
    }

62, Topic:

A robot is located at the Top-left corner of a m x N grid (marked ' Start ' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the "grid" (marked ' Finish ' in the diagram below).

How many possible unique paths are there?

The point is to find the number of different paths between two points, because the robot can only go down and right forward, so we can calculate according to the column, can be summed up each column of each point of the different path number is the previous column of the corresponding point path number plus the sum of the number of points in this column path As shown below:
1,1,1,1
1,2,3,4
1,3,6,10
1,4,10,20 ...
It is easy to draw the following code, using an array of length m to record the number of paths for each column element, and iterate:

    public int uniquepaths (int m, int n) {
        int[] row = new Int[m];
        for (int i=0; i<m; i++)
            row[i] = 1;
        for (int i=1; i<n; i++) {for
            (int j=1; j<m; J + +) {
                row[j] = row[j-1] + row[j];
            }
        return row[m-1];
    }

In addition, the path number of the last element in each column can be obtained by mathematical induction, and the code is in the following way:

    public int uniquePaths1 (int m, int n) {

        long result = 1;
        for (int i=0;i<math.min (m-1,n-1), i++) Result
            = result* (m+n-2-i)/(i+1);
        return (int) result;

    }

63, Topic:

Follow up for ' Unique Paths ': Now consider if some obstacles are to the added

. How many the unique paths would there be?

An obstacle and empty are marked as 1 and 0 respectively in the grid.

For example, There are one obstacle in the
middle of a 3x3 grid as illustrated below.

[
  [0,0,0], [
  0,1,0],
  [0,0,0]] The total number of a
unique paths is 2.

Note:m and n'll are at most 100.

The subject on the basis of 62 added an obstacle, that is, 1 of the elements are blocked, unable to pass, using the same ideas as above, but for each element to judge, if "1" then the element of the path number 0 can be. Code in:

 public int uniquepathswithobstacles (int[][] obstaclegrid) {int width = obstaclegrid[
        0].length;
        Int[] dp = new Int[width];
        Dp[0] = 1;
                    For (int[] row:obstaclegrid) {for (int j = 0; J < width; j + +) {if (row[j] = 1)
                DP[J] = 0;
            else if (J > 0) dp[j] + = dp[j-1];
    } return dp[width-1]; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.