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

Champernowne Digits

Find digits at given positions of the string 123456789101112... made by writing out the counting numbers.

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

  • Count digits used by numbers of each length
  • Locate a number and digit from a position
  • Use 64-bit arithmetic safely

โจทย์

ดูฉบับแปล

Write the positive integers one after another: 123456789101112131415... Position 1 holds 1, position 10 holds the 1 of 10, position 11 the 0 of 10. For each requested position, find the digit there. The positions go far past what you could build as a string: skip whole blocks of numbers with the same number of digits (9 one-digit numbers, 90 two-digit numbers, 900 three-digit numbers, ...).

Implement champernowne_digits(positions).

Input

positions: Positions in the digit string, counting from 1.

Output

Return the digit at each position, in the same order as the input. An empty input returns an empty list.

เงื่อนไข

0 ≤ positions.length ≤ 100; 1 ≤ position ≤ 10^15

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
positions = [1, 9, 10, 11]

ผลลัพธ์ที่คาดหวัง:
expected = [1, 9, 1, 0]
อินพุต:
std::vector<long long> positions = {1, 9, 10, 11};

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {1, 9, 1, 0};
อินพุต:
long[] positions = new long[] {1, 9, 10, 11};

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {1, 9, 1, 0};

คำอธิบาย

The string starts 1 2 3 4 5 6 7 8 9 1 0 1 1, so the digits are [1, 9, 1, 0].

ตัวอย่างที่ 2
อินพุต:
positions = [15, 190]

ผลลัพธ์ที่คาดหวัง:
expected = [2, 1]
อินพุต:
std::vector<long long> positions = {15, 190};

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {2, 1};
อินพุต:
long[] positions = new long[] {15, 190};

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {2, 1};

คำอธิบาย

Position 15 is the 2 of 12. Positions 10 to 189 hold 10 to 99, so 190 is the first digit of 100: [2, 1].

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

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

คำใบ้ 1

Numbers with d digits take 9 × 10^(d − 1) × d positions in total.

คำใบ้ 2

Subtract whole blocks until the position falls inside one.

คำใบ้ 3

Within the block, (position − 1) / d tells you which number and (position − 1) % d which digit.

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

O(m × log P) time for m positions, O(m) extra space

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

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

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

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

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

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

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