모든 챌린지
보통 0명 해결 정답률 0%

Zigzag Wave Pattern

Draw a zigzag line of stars that bounces between the top and bottom rows of a grid.

배우게 될 내용

  • Turn a repeating motion into a formula with mod
  • Fill a grid column by column
  • Trim trailing spaces and keep empty rows

문제

번역본 보기

Picture a grid with height rows and width columns, both numbered from 0. A line of stars starts in the top-left cell and moves one column right at every step, going one row down each time until it reaches the bottom row and then one row up each time until it reaches the top row, again and again. Exactly: let P = 2(height − 1) and p = c mod P. Column c has its only star in row p when p < height, and in row P − p otherwise. All other cells are spaces.

Implement zigzag_wave_pattern(width, height).

Input

width: The number of columns.

height: The number of rows.

Output

Return height strings from top to bottom with trailing spaces removed. A row that ends up with no star is an empty string.

제약 조건

1 ≤ width ≤ 80, 2 ≤ height ≤ 20

예시

예시 1
입력:
width = 9
height = 3

예상 출력:
expected = ["*   *   *", " * * * *", "  *   *"]
입력:
long long width = 9;
long long height = 3;

예상 출력:
std::vector<std::string> expected = {"*   *   *", " * * * *", "  *   *"};
입력:
long width = 9;
long height = 3;

예상 출력:
string[] expected = new string[] {"*   *   *", " * * * *", "  *   *"};

설명

P = 4, so columns 0 to 8 use rows 0, 1, 2, 1, 0, 1, 2, 1, 0: ["* * *", " * * * *", " * *"].

예시 2
입력:
width = 5
height = 2

예상 출력:
expected = ["* * *", " * *"]
입력:
long long width = 5;
long long height = 2;

예상 출력:
std::vector<std::string> expected = {"* * *", " * *"};
입력:
long width = 5;
long height = 2;

예상 출력:
string[] expected = new string[] {"* * *", " * *"};

설명

With two rows the star alternates top and bottom: ["* * *", " * *"].

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

The movement repeats every 2(height − 1) columns.

힌트 2

Inside one period the row goes down from 0 to height − 1 and back up.

힌트 3

It can be easier to fill a 2D grid of spaces first and then turn each row into a string.

예상 복잡도

O(width * height) time, O(width * height) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.