Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Binary Tree Zigzag Level Order Traversal: Level Order สลับทิศทาง (Zigzag)

    โจทย์ กำหนด Binary Tree ทำ Zigzag Level Order Traversal: Level 0 (root): ซ้าย → ขวา Level 1: ขวา → ซ้าย Level 2: ซ้าย → ขวา สลับกันไปเรื่อยๆ ตัวอย่าง: 3 / \ 9 20 / \ 15 7 Output: [[3], [20,9], [15,7]] อัลกอริทึม 1: BFS + Deque (Double-ended Queue) ใช้ deque สำหรับแต่ละ level

  2. Basic Calculator II: เครื่องคิดเลขพื้นฐาน (+, -, *, /) ใน Python

    โจทย์ Implement Basic Calculator evaluate string expression ที่มี: non-negative integers, +, -, *, /, และ spaces การหารเป็น integer division (truncate toward zero) ตัวอย่าง: 3+2*2 → 7, 3/2 → 1, 3+5 / 2 → 5 อัลกอริทึม: Stack + Single Pass (Operator Precedence) จัดการ precedence: * และ / ก่อน + แ

  3. Shuffle an Array: สุ่มลำดับ Array (Fisher-Yates Shuffle)

    โจทย์ ออกแบบ Class รองรับ: reset(): คืน array ต้นฉบับ shuffle(): คืน array ที่สุ่มลำดับ (Uniform random permutation) อัลกอริทึม: Fisher-Yates Shuffle (Knuth Shuffle) - O(n) สุ่มแบบ unbiased ทุก permutation โอกาสเท่ากัน: for i from n-1 down to 1: j = random integer in [0, i] swap(arr[i],

  4. Construct Binary Search Tree from Preorder Traversal: สร้าง BST จาก Preorder

    โจทย์ กำหนด Preorder traversal ของ BST สร้าง BST คืน root BST property: Left subtree < Node < Right subtree Preorder: Root → Left → Right ตัวอย่าง: [8,5,1,7,10,12] → BST: 8 / \ 5 10 / \ \ 1 7 12 อัลกอริทึม: Stack-based (Iterative) - O(n) ใช้ Stack เก็บ path จาก root

  5. Capacity To Ship Packages Within D Days: หาความจุเรือขั้นต่ำส่งของภายใน D วัน

    โจทย์ มี packages น้ำหนัก weights[i] ส่งภายใน D วัน ทุกวันบรรทุกตามลำดับ (contiguous) ไม่เกินความจุ capacity หา Minimum Capacity ที่ส่งครบภายใน D วัน ตัวอย่าง: weights=[3,2,2,4,1,4], D=3 → Capacity=6 (Day1: 3+2, Day2: 2+4, Day3: 1+4) อัลกอริทึม: Binary Search on Answer + Greedy Check Search Space:

  6. Grumpy Bookstore Owner: หาลูกค้าที่พอใจสูงสุดด้วยเทคนิคไม่ขี้อ้อน X นาที

    โจทย์ ร้านหนังสือเปิด n นาที นาที i มีลูกค้า customers[i] คน เจ้าร้านขี้อ้อน (grumpy) นาที i: grumpy[i]=1 → ลูกค้านั้นไม่พอใจ เจ้าร้านไม่ขี้อ้อน: grumpy[i]=0 → ลูกค้าพอใจ เจ้าร้านมีเทคนิค ไม่ขี้อ้อน ใช้ได้ 1 ครั้ง ต่อเนื่อง X นาที หา Maximum satisfied customers ตัวอย่าง: customers=[1,0,1,2,1,1,7,5

  7. Insufficient Nodes in Root to Leaf Paths: ลบโหนดที่ Path Sum น้อยกว่า Limit

    โจทย์ กำหนด Binary Tree และ limit โหนดเป็น Insufficient ถ้า ทุก root-to-leaf path ที่ผ่านโหนดนั้น มี sum < limit ลบโหนด insufficient ทั้งหมดพร้อมกัน คืน root ของ tree ที่เหลือ ตัวอย่าง: limit=1 Original: After: 1 1 / \ / \ 2 3 2

  8. Smallest Subsequence of Distinct Characters: หา Subsequence เล็กที่สุด Lexicographically

    โจทย์ กำหนด string text หา Lexicographically Smallest Subsequence ที่มี ตัวอักษร distinct ครบทุกตัว ที่ปรากฏใน text อย่างละ 1 ครั้ง ตัวอย่าง: cdadabcc → adbc Distinct chars: a,b,c,d → ต้องมีทั้ง 4 ตัว ลำดับ lexicographically smallest อัลกอริทึม: Monotonic Stack + Greedy คล้าย Remove Duplicate Lette

  9. Path With Maximum Minimum Value: หาเส้นทางที่ค่า Min สูงสุด (Maximin Path)

    โจทย์ กำหนด Matrix A (R×C) หา path จาก [0,0] ไป [R-1,C-1] (4 ทิศทาง) ที่ Maximum Minimum Value (ค่า minimum บน path สูงสุดเท่าที่จะเป็น) ตัวอย่าง: 5 4 5 1 2 6 7 4 6 Path: 5→4→5→6→6 (min=4) → Output: 4 อัลกอริทึม: Modified Dijkstra / BFS with Max-Heap (Priority Queue) State: (min_value_on_path, ro

  10. Filling Bookcase Shelves: จัดหนังสือบนชั้นให้ความสูงรวมต่ำสุด

    โจทย์ มีหนังสือลำดับ books[i] = [thickness, height] วางบนชั้นกว้าง shelf_width ต้องวางตามลำดับ เลือกหนังสือต่อเนื่องๆ วางบนชั้นเดียวกัน (ผลรวม thickness ≤ shelf_width) ความสูงชั้น = max height ของหนังสือบนชั้นนั้น หา Minimum Total Height ของตู้หนังสือทั้งหมด ตัวอย่าง: books=[[1,1],[2,3],[2,3],[1,1],

  11. Delete Nodes And Return Forest: ลบโหนดและคืน Forest ใน Binary Tree

    โจทย์ กำหนด Binary Tree root และ array to_delete ลบโหนดที่ value อยู่ใน to_delete ทั้งหมดพร้อมกัน คืน roots ของ forest ที่เหลือ ตัวอย่าง: Tree [1,2,3,4,5,6,7], to_delete=[3,5] → Forest roots: [1,6,7] Original: After delete 3,5: 1 1 / \ / 2 3

  12. Maximum Nesting Depth of Two Valid Parentheses Strings: แบ่ง Parentheses 2 ส่วนลด Depth

    โจทย์ กำหนด Valid Parentheses String (VPS) seq แบ่งเป็น 2 subsequence A และ B (disjoint, union = seq) ทั้งคู่ต้องเป็น VPS ให้ max(depth(A), depth(B)) น้อยที่สุด คืน array ans โดย ans[i]=0 ถ้า seq[i] 属于 A, 1 ถ้า 属于 B ตัวอย่าง: ()(())() → [1,1,1,0,1,0,1,1] ข้อสังเกตสำคัญ Depth ของ VPS = จำนวน unmatch

  13. Minimum Cost Tree From Leaf Values: ต้นทุนต่ำสุดของต้นไม้จาก Leaf Values

    โจทย์ กำหนด array arr ของจำนวนเต็มบวก สร้าง Binary Tree ที่: โหนดแต่ละตัวมีลูก 0 หรือ 2 ตัว (Full Binary Tree) Leaf nodes (inorder) = arr ตามลำดับ Non-leaf node value = ผลคูณของ max leaf ใน left subtree × max leaf ใน right subtree หา Minimum Sum ของ non-leaf nodes ทั้งหมด ตัวอย่าง: arr = [6,2,4] →

  14. Course Schedule: ตรวจสอบว่าเรียนวิชาครบทุกวิชาได้หรือไม่ (Cycle Detection)

    โจทย์ มี numCourses วิชา และ prerequisites [[course, pre], ...] ตรวจสอบว่า สามารถเรียนจบทุกวิชาได้หรือไม่ (คือ graph ไม่มี cycle) ตัวอย่าง: 2, [[1,0]] → True (เรียน 0 แล้ว 1) อัลกอริทึม: Cycle Detection ใน Directed Graph (DFS + 3 Colors) White (0): ยังไม่เยี่ยม Gray (1): กำลังเยี่ยม (อยู่ใน recurs

  15. Course Schedule II: หาลำดับเรียนวิชา (Topological Sort)

    โจทย์ มี n วิชา (0..n-1) และ prerequisites [[course, pre], ...] หา ลำดับเรียน ที่สำเร็จทุกวิชา (Topological Order) ถ้าไม่ได้ (มี cycle) → [] ตัวอย่าง: n=2, [[1,0]] → [0,1] (เรียน 0 ก่อน 1) อัลกอริทึม: Topological Sort (Kahns Algorithm / BFS) สร้าง adjacency list และ indegree array Queue ใส่โหนดที่

  16. Flatten Nested List Iterator: Iterator แปลง Nested List เป็น Flat

    โจทย์ กำหนด Nested List ของจำนวนเต็ม (แต่ละ element เป็น int หรือ list) สร้าง Iterator ที่ flatten ทุกอย่าง ตัวอย่าง: [[1,1],2,[1,1]] → Iterator ให้ 1, 1, 2, 1, 1 วิธีแก้: Pre-flatten ใน Constructor (Eager) ใช้ DFS recursive แปลง nested list ทั้งหมดเป็น flat list ตอน init แล้ว iterator แค่เดิน inde

  17. Insert Delete GetRandom O(1): Data Structure รองรับ Insert, Delete, GetRandom ใน O(1)

    โจทย์ ออกแบบ Data Structure รองรับ operations ทั้ง 3 ใน Average O(1): insert(val): เพิ่ม val ถ้ายังไม่มี → True, ถ้ามีแล้ว → False remove(val): ลบ val ถ้ามี → True, ถ้าไม่มี → False getRandom(): คืนค่า random element จากชุดปัจจุบัน (ทุก element โอกาสเท่ากัน) วิธีแก้: Array + Hash Map (Index Mappi

  18. Broken Calculator: เครื่องคิดเลขเสีย (Double/Decrement) หา operations น้อยที่สุด

    โจทย์ เครื่องคิดเลขเสีย มี 2 ปุ่ม: Double: คูณ 2 Decrement: ลด 1 เริ่มที่ X ต้องการไป Y หา operations น้อยที่สุด ตัวอย่าง: X=5, Y=8 → 2 ops: 5→4 (dec), 4→8 (double) ข้อสังเกตสำคัญ: ย้อนกลับ (Reverse Thinking) แทนที่จะคิด X→Y (branch factor 2) ให้คิด Y→X (branch factor 1 หรือ 2): ถ้า Y เป็นเลขคู่

  19. The Earliest Moment When Everyone Become Friends: Union-Find หาเวลาทุกคนเพื่อนกัน

    โจทย์ มี N คน (id 0..N-1) และ logs [timestamp, id_A, id_B] แสดงเวลาที่ A และ B กลายเป็นเพื่อน มิติ acquainted แบบ transitive (เพื่อนของเพื่อน = เพื่อน) หา timestamp แรก ที่ทุกคน acquainted กันหมด (connected component = 1) ถ้าไม่มี → -1 ตัวอย่าง: N=6, logs=[[20190101,0,1], [20190104,3,4], [20190107,

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:137/451  20-คอมพิวเตอร์/Page Goto:1 131 132 133 134 135 136 137 138 139 140 141 142 143