Revenue Milestones

	public static int[] getMilestoneDays(int[] revenues, int[] milestones) {
		int[] output = new int[milestones.length];
		Arrays.fill(output, -1);
		int sum = 0;
		for (int i = 0; i < revenues.length; i++) {
			revenues[i] += sum;
			sum = revenues[i];
		}
		for (int i = 0; i < milestones.length; i++) {
			int searchFor = milestones[i];
			int low = 0;
			int high = revenues.length - 1;
			int mid = 0;

			while (low <= high) {
				mid = (high + low) / 2;

				if (searchFor == revenues[mid]) {
					output[i] = mid;
					break;
				} else if (searchFor < revenues[mid] && (mid - 1 < revenues.length && searchFor > revenues[mid - 1])) {
					output[i] = mid - 1;
					break;
				} else if (searchFor < revenues[mid])
					high = mid - 1;
				else if (searchFor > revenues[mid] && (mid + 1 < revenues.length && searchFor < revenues[mid + 1])) {
					output[i] = mid + 1;
					break;
				} else if (searchFor > revenues[mid])
					low = mid + 1;

			}
		}
		for (int i = 0; i < output.length; i++)
			output[i]++;
		return output;
	}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s