Code in cpp only pass all test cases solve and wri...

Creado el: 13 de julio de 2025

Respondido usando O3 por Chat01

Pregunta

20250713_175251.jpg20250713_175251.jpg
20250713_175302.jpg20250713_175302.jpg
20250713_175316.jpg20250713_175316.jpg
20250713_175324.jpg20250713_175324.jpg

Code in cpp only pass all test cases solve and write only in get maximum score function acc to snipet given

Python

python
from PIL import Image import matplotlib.pyplot as plt img = Image.open('/mnt/data/20250713_175251.jpg') plt.imshow(img); plt.axis('off') (-0.5, 4623.5, 3467.5, -0.5)

Python

python
plt.figure(figsize=(6,10)) plt.imshow(img.rotate(90, expand=True)); plt.axis('off') (-0.5, 3467.5, 4623.5, -0.5)

Python

python
plt.figure(figsize=(6,10)) plt.imshow(img.rotate(-90, expand=True)); plt.axis('off') (-0.5, 3467.5, 4623.5, -0.5)

Python

python
# crop central area maybe plt.figure(figsize=(6,8)) plt.imshow(img.rotate(-90, expand=True).crop((0,0,1300,2500))); plt.axis('off') (-0.5, 1299.5, 2499.5, -0.5)

Python

python
img2 = Image.open('/mnt/data/20250713_175302.jpg') plt.figure(figsize=(6,10)) plt.imshow(img2.rotate(-90, expand=True)); plt.axis('off') (-0.5, 3467.5, 4623.5, -0.5)

Python

python
img3 = Image.open('/mnt/data/20250713_175316.jpg') plt.figure(figsize=(6,10)) plt.imshow(img3.rotate(-90, expand=True)); plt.axis('off') (-0.5, 3467.5, 4623.5, -0.5)

Pensamiento

Developing a solution

It looks like the solution might not be publicly available, so I’ll try to develop one myself. The key is understanding the process as picking an index, keeping one side, and discarding the other. This can be thought of like a "partition tree," where the sequence of chosen indices corresponds to a binary representation, and we can compute the optimal score. Maybe a greedy approach can work, where we always pick the largest value at each step. For example, with the array [-5, 4, -10, -1, -5, 8, -3] and k=3, we get a score of 11.

Testing the hypothesis

I'm testing the idea that we can always pick the k largest elements from the array by sorting them in descending order. The basic idea works with examples like the array [4,6,-10,-1,10,-20] where picking the 4 largest numbers gives the correct sum. However, there could be exceptions where the partitioning rules restrict how we pick elements. For example, in [5, -100, 4] with k=2, we can pick 5 and 4 despite the large negative value in-between because we're allowed to discard one side. This seems promising.

Refining the picking strategy

I’m testing a strategy where I pick the leftmost or rightmost desired element and discard the side that doesn't contain the remaining desired picks. This works for most cases, like in arrays with duplicate values or negative values separating high positive values. As long as one partition without other desired elements exists, I can safely discard it. This strategy ensures I can always pick elements in order without leaving any necessary choices behind. I'll keep refining the proof on the feasibility of this method for all possible cases.

Thought for 4m 38s

Respuesta

cpp
long getMaximumScore(vector<int> arr, int k) { /* Any k indices can always be collected: pick the left-most of the remaining wanted indices each time and discard its left partition (which never contains a wanted element). Therefore the best total is simply the sum of the k largest values in the whole array. We only need the k largest values, not their order, so `nth_element` gives an O(n) solution and keeps memory small. */ if (k <= 0) return 0; const int n = arr.size(); k = min(k, n); // safety, though constraints guarantee k ≤ n nth_element(arr.begin(), // after this call arr.begin() + k, // the k-th position arr.end(), greater<int>()); // puts the k largest elements in front long long score = 0; // 64-bit to avoid overflow for (int i = 0; i < k; ++i) score += arr[i]; return static_cast<long>(score); // HackerRank’s `long` is 64-bit }

Compartir este Q&A