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

Number Spiral Lookup

Find a value by row and column in an expanding number spiral.

The infinite spiral starts at the top-left cell. The first four rows and columns are shown below. Continue the same pattern in expanding square layers. Coordinates start at 1.

 1   2   9  10
 4   3   8  11
 5   6   7  12
16  15  14  13

Return one cell value without constructing the entire grid.

Implement number_spiral(row, column).

Input

row: The row counted from the top, starting at 1.

column: The column counted from the left, starting at 1.

Output

Return the number at that position.

เงื่อนไข

1 ≤ row, column ≤ 1,000,000,000. The value fits a signed 64-bit integer.

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
row = 3
column = 2

ผลลัพธ์ที่คาดหวัง:
expected = 6
อินพุต:
long long row = 3;
long long column = 2;

ผลลัพธ์ที่คาดหวัง:
long long expected = 6;
อินพุต:
long row = 3;
long column = 2;

ผลลัพธ์ที่คาดหวัง:
long expected = 6;

คำอธิบาย

In the displayed third row, the second value is 6.

ตัวอย่างที่ 2
อินพุต:
row = 1
column = 4

ผลลัพธ์ที่คาดหวัง:
expected = 10
อินพุต:
long long row = 1;
long long column = 4;

ผลลัพธ์ที่คาดหวัง:
long long expected = 10;
อินพุต:
long row = 1;
long column = 4;

ผลลัพธ์ที่คาดหวัง:
long expected = 10;

คำอธิบาย

The fourth square layer begins at 10 in the top row.

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

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

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

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

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