LeetCode 45. Jump Game II

Question

Given an array of non-negative integers nums, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

You can assume that you can always reach the last index.

Read more

LeetCode 55. Jump Game

Question

You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.

Read more

LeetCode 123. Best Time to Buy and Sell Stock III

Question

You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete at most two transactions.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Read more

LeetCode 134. Gas Station

Question

There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.

Given two integer arrays gas and cost, return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique

Read more

OA Count Analogous Arrays

Question

An array is said to be analogous to the secret array if all of the following conditions are true:
• The length of the array is equal to the length of the secret array.
• Each integer in the array lies in the interval [lowerBound, upperBound].
• The difference between each pair of consecutive integers of the array must be equal to the difference between the respective pair of consecutive integers in the secret array. In other words, let the secret array be [s[0], s[1],…, s[n-1]] and let the analogous array be [a[0], a[1],…, a[n-1]], then (a[i-1] - a[i]) must be equal to (s[i-1] - s[i]) for each i from 1 to n -1.

Given the value of integers lowerBound and upperBound, inclusive, and the array of differences between each pair of consecutive integers of the secret array, find the number of arrays that are analogous to the secret array. If there is no array analogous to the secret array, return 0.

For example:
consecutiveDifference = [-2, -1, -2, 5]
lowerBound = 3
upperBound = 10

Note that none of the values is out of the bound. All possible analogous arrays are:
[3, 5, 6, 8, 3]
[4, 6, 7, 9, 4]
[5, 7, 8, 10, 5]

Tha answer is 3.

Source: https://leetcode.com/discuss/interview-question/1332322/amazon-online-assessment-july-2021-secret-array

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static int countAnalogousArrays(int[] consecutiveDifference, int lowerBound, int upperBound) {
// parameter validation
if (consecutiveDifference == null || consecutiveDifference.length < 1 || lowerBound > upperBound) {
return 0;
}
int delta = 0, maxDelta = 0, minDelta = 0;
for (int i = 0; i < consecutiveDifference.length; i++) {
delta += consecutiveDifference[i];
maxDelta = Math.max(maxDelta, delta);
minDelta = Math.min(minDelta, delta);
}
int maxDiff = maxDelta - minDelta, boundGap = upperBound - lowerBound;
// max difference exceeds bound gap
if (maxDiff > boundGap) {
return 0;
}
return boundGap - maxDiff;
}