LeetCode 2701: Consecutive Transactions with Increasing Amounts

Database

Problem Description

Explanation:

To solve this problem, we can iterate through the list of transactions and keep track of consecutive transactions with increasing amounts. We will maintain a counter to track the current consecutive count and update the maximum consecutive count whenever we encounter a new increasing transaction. We can then return the maximum consecutive count.

  • Initialize variables maxConsecutive and consecutive to 1.
  • Iterate through the transactions starting from index 1:
    • If the current transaction amount is greater than the previous transaction amount, increment consecutive and update maxConsecutive if consecutive is greater.
    • Otherwise, reset consecutive to 1.
  • Return maxConsecutive as the result. :

Solutions

class Solution {
    public int maxConsecutive(int[] transactions) {
        int maxConsecutive = 1;
        int consecutive = 1;
        
        for (int i = 1; i < transactions.length; i++) {
            if (transactions[i] > transactions[i - 1]) {
                consecutive++;
                maxConsecutive = Math.max(maxConsecutive, consecutive);
            } else {
                consecutive = 1;
            }
        }
        
        return maxConsecutive;
    }
}

Loading editor...