Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1715. Count Apples and Oranges - Leetcode Solution

Problem Description

The "Count Apples and Oranges" problem involves determining how many apples and oranges fall on a house, given the positions of the house, the apple and orange trees, and the distances each fruit falls from its tree.

  • You are given two integers s and t, which represent the starting and ending points of a house on a number line.
  • The position of the apple tree is at point a and the orange tree is at point b.
  • You are also given arrays apples and oranges, where each element represents the distance an apple or orange falls from its tree.
  • Fruits can fall to the left or right of the tree (distances can be negative or positive).
  • Your task is to count how many apples and oranges fall on the house (i.e., their landing positions are between s and t, inclusive).

The problem requires you to output two numbers: the count of apples and the count of oranges that land on the house.

Thought Process

At first glance, it might seem necessary to check every possible combination of fruits and positions, but the problem is more straightforward. For each fruit, we simply calculate where it lands by adding its distance to its tree's position. Then, we check if that landing position falls within the house's boundaries.

  • For apples, add each element in apples to a to get the landing position.
  • For oranges, add each element in oranges to b to get the landing position.
  • Count how many landing positions fall within [s, t].

This is a simple iteration and counting problem, but it's important to keep track of the correct tree and fruit relationships, and to use the correct boundaries for the house.

Solution Approach

Let's break down the solution step by step:

  1. Iterate Through Apples:
    • For each distance in the apples array, calculate the landing position by adding the distance to the apple tree's position a.
    • If the landing position is within the house's boundaries (s to t, inclusive), increment the apple count.
  2. Iterate Through Oranges:
    • For each distance in the oranges array, calculate the landing position by adding the distance to the orange tree's position b.
    • If the landing position is within the house's boundaries, increment the orange count.
  3. Output the Results:
    • Print or return the two counts: the number of apples and oranges that land on the house.

This approach uses simple iteration and conditional logic, making it efficient and easy to understand. No advanced data structures or algorithms are required.

Example Walkthrough

Let's consider an example:

  • s = 7, t = 11 (house is from position 7 to 11)
  • a = 5 (apple tree at position 5)
  • b = 15 (orange tree at position 15)
  • apples = [-2, 2, 1]
  • oranges = [5, -6]

Step-by-step:

  1. Calculate apple landings:
    • -2 + 5 = 3 (not in [7, 11])
    • 2 + 5 = 7 (in [7, 11])
    • 1 + 5 = 6 (not in [7, 11])

    Apples on house: 1

  2. Calculate orange landings:
    • 5 + 15 = 20 (not in [7, 11])
    • -6 + 15 = 9 (in [7, 11])

    Oranges on house: 1

Final Output:
1
1

Time and Space Complexity

Brute-Force Approach:
The brute-force method would still involve checking each fruit individually, so it's effectively the same as the optimal approach for this problem.

  • Time Complexity: O(m + n), where m is the number of apples and n is the number of oranges. Each fruit is checked once.
  • Space Complexity: O(1) extra space, since we only need counters for the apples and oranges that land on the house.

No additional data structures are necessary, and the algorithm is very efficient.

Summary

The "Count Apples and Oranges" problem is a straightforward counting problem. The key insight is to calculate the landing position of each fruit and check if it falls within the house's boundaries. By iterating through each array and using simple arithmetic and conditionals, we efficiently solve the problem in linear time. The solution is elegant in its simplicity and demonstrates the power of clear logic and careful attention to problem constraints.

Code Implementation

def countApplesAndOranges(s, t, a, b, apples, oranges):
    apple_count = 0
    orange_count = 0

    for d in apples:
        if s <= a + d <= t:
            apple_count += 1

    for d in oranges:
        if s <= b + d <= t:
            orange_count += 1

    print(apple_count)
    print(orange_count)
      
#include <iostream>
#include <vector>
using namespace std;

void countApplesAndOranges(int s, int t, int a, int b, vector<int>& apples, vector<int>& oranges) {
    int apple_count = 0;
    int orange_count = 0;
    for (int d : apples) {
        if (a + d >= s && a + d <= t) {
            apple_count++;
        }
    }
    for (int d : oranges) {
        if (b + d >= s && b + d <= t) {
            orange_count++;
        }
    }
    cout << apple_count << endl;
    cout << orange_count << endl;
}
      
public static void countApplesAndOranges(int s, int t, int a, int b, int[] apples, int[] oranges) {
    int appleCount = 0;
    int orangeCount = 0;
    for (int d : apples) {
        if (a + d >= s && a + d <= t) {
            appleCount++;
        }
    }
    for (int d : oranges) {
        if (b + d >= s && b + d <= t) {
            orangeCount++;
        }
    }
    System.out.println(appleCount);
    System.out.println(orangeCount);
}
      
function countApplesAndOranges(s, t, a, b, apples, oranges) {
    let appleCount = 0;
    let orangeCount = 0;
    for (let d of apples) {
        if (a + d >= s && a + d <= t) {
            appleCount++;
        }
    }
    for (let d of oranges) {
        if (b + d >= s && b + d <= t) {
            orangeCount++;
        }
    }
    console.log(appleCount);
    console.log(orangeCount);
}