[LeetCode] A Distance Maximizing Problem

Question:

Given an array A of integers, find the maximum of j-i subjected to the constraint of A[i] < A[j].

// O(n)
public int maxDistance(int[] A)
{
    // Assumptions...
    
    int local = Integer.MIN_VALUE;
    int global = Integer.MIN_VALUE;
    
    for (int i = 1 ; i < A.length ; i ++)
    {
        if (A[i] > A[i - 1])
        {
            local += 1;
        }
        else
        {
            local = 0;
        }
        global = Math.max(global, local);
    }
    
    return global;
}
时间: 2024-08-29 11:29:29

[LeetCode] A Distance Maximizing Problem的相关文章

[LeetCode] Edit Distance(很好的DP)

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.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Repla

LeetCode Maximum Distance in Arrays

原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/ 题目: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from two different arrays (each array picks one) and calculate the distanc

[LeetCode#218] The Skyline Problem

Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), w

[LeetCode] Hamming Distance 汉明距离

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note:0 ≤ x, y < 231. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1

[LeetCode] Maximum Distance in Arrays 数组中的最大距离

Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute differ

Leetcode:Edit Distance 字符串编辑距离

原题戳我 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.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c)

Java for LeetCode 218 The Skyline Problem【Comming Soon】

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a pr

Leetcode: Water and Jug Problem &amp;&amp; Summary: GCD求法(辗转相除法 or Euclidean algorithm)

You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs. If z liters of water is measurable, you must

[leetcode]Edit Distance @ Python

原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: 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.) You have the following 3 operations permitted on a w