मध्यम 0 हल किए गए 0% स्वीकृति

Grayscale Filter

Turn a grid of packed RGB pixels into grayscale using weighted channel brightness.

आप क्या सीखेंगे

  • Unpack red, green and blue from one integer
  • Round a weighted average using integer arithmetic
  • Pack three channels back into one integer

समस्या

अनुवाद दिखाएँ

An image is a grid of pixels, and each pixel is stored as one integer that packs its color: red × 65536 + green × 256 + blue, with each channel from 0 to 255 (the hex color #FF8000 is the integer 16744448). To make the image gray, give every pixel the same value g in all three channels, where g is the pixel's brightness:

g = (299 × red + 587 × green + 114 × blue) / 1000, rounded to the nearest whole number with halves rounded up.

Using integers only, that is g = (299 × red + 587 × green + 114 × blue + 500) / 1000 with integer division.

Implement grayscale_filter(pixels).

Input

pixels: The image, one list per row. Rows may have different lengths.

Output

Return a grid of the same shape where each pixel is g × 65536 + g × 256 + g. An empty image gives an empty grid.

सीमाएँ

0 ≤ rows ≤ 50, 0 ≤ pixels per row ≤ 50

0 ≤ each pixel ≤ 16,777,215

उदाहरण

उदाहरण 1
इनपुट:
pixels = [
    [16744448, 255]
]

अपेक्षित आउटपुट:
expected = [
    [9934743, 1907997]
]
इनपुट:
std::vector<std::vector<long long>> pixels = {
    {16744448, 255}
};

अपेक्षित आउटपुट:
std::vector<std::vector<long long>> expected = {
    {9934743, 1907997}
};
इनपुट:
long[][] pixels = new long[][] {
    new long[] {16744448, 255}
};

अपेक्षित आउटपुट:
long[][] expected = new long[][] {
    new long[] {9934743, 1907997}
};

व्याख्या

#FF8000 has red 255, green 128, blue 0: (76245 + 75136 + 0 + 500) / 1000 = 151, giving 151 × 65793 = 9934743. Pure blue 255 gives (29070 + 500) / 1000 = 29, so 1907997.

उदाहरण 2
इनपुट:
pixels = [
    [0, 16777215],
    [8421504, 65280]
]

अपेक्षित आउटपुट:
expected = [
    [0, 16777215],
    [8421504, 9868950]
]
इनपुट:
std::vector<std::vector<long long>> pixels = {
    {0, 16777215},
    {8421504, 65280}
};

अपेक्षित आउटपुट:
std::vector<std::vector<long long>> expected = {
    {0, 16777215},
    {8421504, 9868950}
};
इनपुट:
long[][] pixels = new long[][] {
    new long[] {0, 16777215},
    new long[] {8421504, 65280}
};

अपेक्षित आउटपुट:
long[][] expected = new long[][] {
    new long[] {0, 16777215},
    new long[] {8421504, 9868950}
};

व्याख्या

Black stays 0 and white stays 16777215. Gray #808080 keeps 128 (8421504), and pure green gives (149685 + 500) / 1000 = 150, so 9868950.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Red is (pixel / 65536) % 256, green is (pixel / 256) % 256 and blue is pixel % 256; shifts and masks work too.

संकेत 2

Adding 500 before dividing by 1000 rounds halves up.

संकेत 3

Put the same gray value in all three channels when packing it back.

अपेक्षित जटिलता

O(rows × columns) time, O(rows × columns) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।