tinycodinglm / cp2 /cp-0019.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
4.52 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space com...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity.

User 2026-11-07T16:20:03.537Z

Quickest Profit Target

Topic: Two pointers and sliding window | Difficulty: hard

Problem

A shop records daily profit changes a[1..n], which may be negative. Find the length of the shortest non-empty contiguous block of days whose total is at least K. If no block reaches K, print -1.

Input

The first line contains n and K. The second line contains a[1..n].

Output

Print the minimum block length, or -1 if none exists.

Constraints

  • 1 <= n <= 2*10^5
  • 1 <= K <= 10^14
  • |a[i]| <= 10^9

Examples

Example 1

Input:

markdown
3 3
2 -1 2

Output:

markdown
3

Note: Only the whole array reaches 3.

Example 2

Input:

markdown
1 5
1

Output:

markdown
-1

Example 3

Input:

markdown
6 6
1 -3 5 -2 2 1

Output:

markdown
4

Note: Only days 3..6 (5, -2, 2, 1) reach 6; the shorter block 5, -2, 2 gives 5.

Solve this in Ruby, Java 17, JavaScript (Node.js), one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-07T16:20:05.511Z

Approach. Work on prefix sums P and keep a deque of indices with strictly increasing P. For each new index i, pop from the front while P[i]-P[front] >= K recording i-front, then pop from the back while P[back] >= P[i], and push i.

Correctness. A front index that reaches K with i can never give a shorter block with a later end, so it is removed; a back index with P not smaller than P[i] is dominated by i (later start, no larger prefix) and is removed.

Complexity. Time: O(n). Space: O(n).

Ruby

ruby
n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
p = [0]
a.each { |x| p << p[-1] + x }
dq = []
head = 0
best = nil
(0..n).each do |i|
  while head < dq.size && p[i] - p[dq[head]] >= k
    len = i - dq[head]
    best = len if best.nil? || len < best
    head += 1
  end
  dq.pop while dq.size > head && p[dq[-1]] >= p[i]
  dq << i
end
puts best.nil? ? -1 : best

Java 17

java
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        StringTokenizer st = new StringTokenizer(new String(System.in.readAllBytes()));
        int n = Integer.parseInt(st.nextToken());
        long k = Long.parseLong(st.nextToken());
        long[] p = new long[n + 1];
        for (int i = 1; i <= n; i++) p[i] = p[i - 1] + Long.parseLong(st.nextToken());
        int[] dq = new int[n + 1];
        int head = 0, tail = 0;
        int best = Integer.MAX_VALUE;
        for (int i = 0; i <= n; i++) {
            while (head < tail && p[i] - p[dq[head]] >= k) {
                best = Math.min(best, i - dq[head]);
                head++;
            }
            while (tail > head && p[dq[tail - 1]] >= p[i]) tail--;
            dq[tail++] = i;
        }
        System.out.println(best == Integer.MAX_VALUE ? -1 : best);
    }
}

JavaScript (Node.js)

javascript
const t = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter(x => x.length).map(Number);
const n = t[0], k = t[1];
const p = new Float64Array(n + 1);
for (let i = 1; i <= n; i++) p[i] = p[i - 1] + t[1 + i];
const dq = new Int32Array(n + 1);
let head = 0, tail = 0, best = Infinity;
for (let i = 0; i <= n; i++) {
  while (head < tail && p[i] - p[dq[head]] >= k) {
    best = Math.min(best, i - dq[head]);
    head++;
  }
  while (tail > head && p[dq[tail - 1]] >= p[i]) tail--;
  dq[tail++] = i;
}
console.log(best === Infinity ? '-1' : String(best));