Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. หากำไรสูงสุดซื้อขายหุ้นสูงสุด 2 ครั้งใน Python (O(n) Time O(1) Space)

    ปัญหา: กำหนด Stock Prices หา Max Profit จาก ≤ 2 Transactions (Buy-Sell Pairs) **Key Insight:** สำหรับ k=2 ใช้ 4 Variables ติดตาม State ได้ใน O(1) Space: `first_buy`: Max Profit หลัง Buy ครั้งที่ 1 (Negative = Cost) `first_sell`: Max Profit หลัง Sell ครั้งที่ 1 `second_buy`: Max Profit หลัง Buy ครั

  2. หา Cost ต่ำสุดไปถึง Index สุดท้าย ก้าวได้สูงสุด k Steps (DP + Min Heap) ใน Python

    ปัญหา: Array `nums[i]` = Cost ของการยืนที่ Index i เริ่ม Index 0 จบ Index n-1 แต่ละ Step ก้าวได้ 1..k Steps หา Min Total Cost อัลกอริทึม: DP + Monotonic Min-Heap (Sliding Window Minimum) DP[i] = Min Cost ถึง Index i = nums[i] + min(DP[i-k], ..., DP[i-1]) ใช้ Min-Heap เก็บ `(dp_value, index)` สำหรับ

  3. หากำไรสูงสุดจากการซื้อขายหุ้นสูงสุด k ครั้งใน Python (DP + Memoization)

    ปัญหา: กำหนด Stock Prices `prices[i]` และ Max Transactions `k` หา Max Profit จาก ≤ k Buy-Sell Pairs (ต้อง Buy ก่อน Sell, Sell ก่อน Buy ถัดไป) อัลกอริทึม: DP Top-Down with Memoization State: `dp(i, k, holding)` = Max Profit จาก Day i ถึง สิ้นสุด กับ k Transactions เหลือ และ Holding Stock หรือไม่ `i

  4. โปรแกรมคำนวณนิพจน์คณิตศาสตร์โดยไม่ใช้ฟังก์ชันสำเร็จรูปใน Python

    ให้สตริงนิพจน์คณิตศาสตร์ประกอบด้วย +, -, *, / (หารจำนวนเต็ม) คำนวณผลลัพธ์โดยไม่ใช้ eval หรือฟังก์ชันสำเร็จรูป ตัวอย่าง: 2+3*5/7 → 4 (เพราะ 2 + ((3*5)/7) = 2 + 2 = 4) อัลกอริธึม Recursive Descent Parser กลับสตริงเพื่อประมวลผลจากซ้ายไปขวา get_value(): อ่านตัวเลข (รองรับเครื่องหมายลบ) get_term(): จัดก

  5. หา Cost ต่ำสุดไปถึงจุดสิ้นสุดด้วย Bus Routes (Dijkstra + State) ใน Python

    ปัญหา: กำหนด Bus Routes `[[src, dest, bus_id], ...]` เริ่มที่ Location 0 ไป Location สูงสุด (Max Location) Cost = จำนวน Bus ที่ขึ้น (ขึ้น Bus เดียวต่อเนื่อง = 1 Cost, เปลี่ยน Bus = +1 Cost) Model: **Shortest Path on State Graph** State = `(location, current_bus)` Edge = Ride Bus หรือ Switch Bus อั

  6. นับวิธีเลือก Empty Cells N ช่องใน N×N Matrix (ทุก Row/Col ต้องมีอย่างน้อย 1) ใน Python

    ปัญหา: N×N Binary Matrix (0=Empty, 1=Blocked) เลือก N Empty Cells ทำให้ทุก Row และทุก Column มี Chosen Cell อย่างน้อย 1 ตัว หาจำนวนวิธี Mod 10⁹+7 Observation: เลือก N Cells, N Rows, N Cols → ต้องเลือก **พอดี 1 Cell ต่อ Row และ 1 Cell ต่อ Column** = **Permutation Matrix** บน Empty Cells อัลกอริทึม:

  7. หาความยาว Longest Circular Increasing Subsequence ใน Python

    ปัญหา: กำหนด Array `nums` หา Longest Increasing Subsequence (LIS) ที่ **Wrap Around** ได้ (Circular) คือ Subsequence สิ้นสุดแล้วต่อหน้าสุดได้ เทคนิค: **Duplicate Array** → `nums + nums` แล้วหา LIS ใน Window ขนาด n เริ่มจากทุก Index อัลกอริทึม: LIS + Sliding Window + Binary Search สร้าง `a = nums

  8. ตรวจสอบจุดอยู่ใน Polygon หรือบนขอบ (Point in Polygon) ใน Python

    ปัญหา: กำหนด Polygon = List of Points `[(x1,y1), (x2,y2), ..., (xn,yn)]` และ Point `pt = (x,y)` หาว่า pt อยู่ใน Polygon หรือบนขอบ (Boundary) ใช้ Ray Casting Algorithm (Even-Odd Rule): ยิง Ray แนวนอนไปทางขวา นับจำนวนครั้งที่ตัด Edge ของ Polygon → คี่ = ใน, คู่ = นอก อัลกอริทึม Loop ทุก Edge `(x0,y

  9. นับ Subsets ที่มีผลรวมเท่ากับ k ใน Python (Subset Sum Count)

    ปัญหา: กำหนด Array `nums` และ Target `k` นับจำนวน Subsets ที่ Sum = k (Mod 10⁹+7) Subset = เลือก Elements ใดๆ (Order ไม่สำคัญ, ไม่ซ้ำ Element เดียวกัน) อัลกอริทึม: 0/1 Knapsack DP (Counting Ways) DP[i][j] = จำนวนวิธีทำ Sum = j ด้วย Elements แรก i ตัว Optimization: 1D Array `dp[j]` Iterate j จาก k ล

  10. หา Populaton สูงสุดของ Cities ใน Tree (No Adjacent Cities) ใน Python

    ปัญหา: Country = Tree (N Nodes, N-1 Edges) Node = Town, Edge = Road Bidirectional Population[i] = ประชากร Town i Upgrade Towns → Cities ภายใต้เงื่อนไข: No two Cities adjacent (Independent Set) Every Town ต้องติด City (Vertex Cover) เงื่อนไข 1+2 → Cities = Max Independent Set ที่เป็น Vertex Cover ด

  11. ตรวจสอบว่า String ทำเป็น Palindrome ได้หรือไม่หลังลบสูงสุด k ตัวอักษรใน Python

    ปัญหา: กำหนด String s และ k หาว่าสามารถลบ ≤ k ตัวอักษร ได้ Palindrome หรือไม่ Observation: ตัวอักษรที่ต้องลบขั้นต่ำ = |s| - LPS(s) โดย LPS = Longest Palindromic Subsequence = LCS(s, reverse(s)) อัลกอริทึม: LCS (Longest Common Subsequence) คำนวณ LCS ระหว่าง s และ reverse(s) → ได้ LPS Length Min De

  12. นับจำนวนวิธีตัด Matrix เป็น k ชิ้น ที่แต่ละชิ้นมี 1 อย่างน้อย 1 ใน Python

    ปัญหา: กำหนด Binary Matrix และ k ต้องตัด Matrix เป็น k ชิ้น โดย: เลือกทิศทาง: Vertical หรือ Horizontal เลือก Index ตัด (Vertical: ตัดแนวตั้ง, Horizontal: ตัดแนวนอน) Vertical Cut: ทิ้งซ้าย, ตัดขวาต่อ | Horizontal Cut: ทิ้งบน, ตัดล่างต่อ แต่ละชิ้นต้องมี 1 อย่างน้อย 1 ตัว หาจำนวนวิธี Mod 10⁹+7 อัลกอ

  13. ตรวจสอบ Cycle ใน Matrix ด้วย DFS (Same Value Adjacent Cells) ใน Python

    ปัญหา: กำหนด 2D Matrix หาว่ามี Cycle ของค่าเดียวกันหรือไม่ โดยเคลื่อนที่ได้ 4 ทิศทาง (บน ล่าง ซ้าย ขวา) ไม่สามารถกลับไป Cell ที่เพิ่งมาจากได้ทันที (No Immediate Backtrack) อัลกอริทึม: DFS + Cycle Detection สร้าง Visited Matrix (R×C) เริ่มต้น False สำหรับแต่ละ Cell ที่ยังไม่ Visited: รัน DFS จ

  14. โปรแกรมนับจำนวน Request ที่ประมวลผลได้ภายใต้เงื่อนไข Rate Limiting ใน Python

    ปัญหา: กำหนด Request Logs = `[[uid, time_sec], ...]` ต้องประมวลผล Request ตามเงื่อนไข Rate Limiting: u: Max Requests ต่อ User ในช่วง 60 วินาที (Sliding Window) g: Max Requests ทั้งระบบ (Global) ในช่วง 60 วินาที Request ที่มีเวลาเท่ากัน: เรียงตาม UID น้อยไปมาก (Priority ให้ UID ต่ำ) Request ที่เกิน

  15. การสร้าง Series จาก NumPy Array พร้อมกำหนด Index เองใน Pandas Python

    Pandas Series สร้างจาก NumPy Array ได้ และสามารถกำหนด Custom Index แทน Default Integer Index ตัวอย่างโค้ด import pandas as pd import numpy as np # NumPy Array ของ Strings my_data = np.array([ab, bc, cd, de, ef, fg, gh, hi]) # Custom Index (ไม่ต่อเนื่อง, ไม่เรียง) my_index = [3, 5, 7, 9, 11, 23,

  16. การแสดง Scatter Plot ด้วย Seaborn ใน Python

    Scatter Plot แสดงความสัมพันธ์ระหว่าง 2 ตัวแปรตัวเลข (Numerical Variables) แต่ละจุด = 1 Observation Seaborn มี jointplot() (Scatter + Marginal Distributions), scatterplot() (Basic), relplot() (FacetGrid) ตัวอย่างโค้ด (jointplot) import seaborn as sb from matplotlib import pyplot as plt df = sb.lo

  17. การสร้าง Series จาก Scalar/Constant Value ใน Pandas Python

    สามารถสร้าง Series จากค่า Scalar (ค่าคงที่) ได้ ค่าดังกล่าวจะถูก Broadcast (กระจาย) ไปทุก Element ตาม Index ที่กำหนด ตัวอย่าง 1: Scalar Value พร้อม Custom Index import pandas as pd my_index = [ab, mn, gh, kl] my_series = pd.Series(7, index=my_index) print(Series from scalar value with custom ind

  18. การดึงหลาย Element จาก Series ด้วย Custom Index ใน Pandas Python

    สามารถเข้าถึงหลาย Element พร้อมกันได้โดยส่ง List ของ Labels เข้าไปใน Bracket `[[...]]` (Double Brackets) ตัวอย่างโค้ด import pandas as pd my_data = [34, 56, 78, 90, 123, 45] my_index = [ab, mn, gh, kl, wq, az] my_series = pd.Series(my_data, index=my_index) print(The series contains following ele

  19. เกิดอะไรขึ้นเมื่อ Index ที่ระบุไม่มีใน Series Pandas Python

    เมื่อใช้ Custom Index ใน Series การเข้าถึงด้วย Label ที่ไม่มีอยู่จะ Raise KeyError ตัวอย่างโค้ด import pandas as pd my_data = [34, 56, 78, 90, 123, 45] my_index = [ab, mn, gh, kl, wq, az] my_series = pd.Series(my_data, index=my_index) print(The series contains following elements) print(my_series

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:196/451  20-คอมพิวเตอร์/Page Goto:1 190 191 192 193 194 195 196 197 198 199 200 201 202