ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Checkerboard Pattern

Draw a checkerboard of # and . cells with the given number of rows and columns.

สิ่งที่คุณจะได้เรียนรู้

  • Use r + c to decide between two alternating values
  • Build a rectangular grid row by row
  • Handle a grid with zero rows

โจทย์

ดูฉบับแปล

A checkerboard alternates two colors in both directions. Here a dark cell is # and a light cell is .. Number rows and columns from 0; the cell in row r and column c is # when r + c is even and . when it is odd, so the top-left cell is always #.

Implement checkerboard_pattern(rows, cols).

Input

rows: The number of rows.

cols: The number of columns.

Output

Return rows strings from top to bottom, each exactly cols characters long with no spaces. When rows is 0, return an empty list.

เงื่อนไข

0 ≤ rows ≤ 60, 1 ≤ cols ≤ 60

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
rows = 3
cols = 4

ผลลัพธ์ที่คาดหวัง:
expected = ["#.#.", ".#.#", "#.#."]
อินพุต:
long long rows = 3;
long long cols = 4;

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"#.#.", ".#.#", "#.#."};
อินพุต:
long rows = 3;
long cols = 4;

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"#.#.", ".#.#", "#.#."};

คำอธิบาย

Row 0 starts with #, row 1 with ., row 2 with # again: ["#.#.", ".#.#", "#.#."].

ตัวอย่างที่ 2
อินพุต:
rows = 2
cols = 1

ผลลัพธ์ที่คาดหวัง:
expected = ["#", "."]
อินพุต:
long long rows = 2;
long long cols = 1;

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"#", "."};
อินพุต:
long rows = 2;
long cols = 1;

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"#", "."};

คำอธิบาย

One column: cell (0, 0) has an even sum and cell (1, 0) an odd one: ["#", "."].

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Neighbouring cells differ by one in either r or c, so r + c changes parity.

คำใบ้ 2

Decide each cell from (r + c) mod 2.

คำใบ้ 3

Every row has exactly cols characters.

ความซับซ้อนที่คาดหวัง

O(rows * cols) time, O(rows * cols) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก