Skip to content

Commit 732db44

Browse files
authored
Create Approach.md
1 parent 7390539 commit 732db44

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### Approach
2+
3+
We use two pointers: left and right to simulate buying books from both ends.
4+
5+
- If pairCost is cheaper than buying both ends separately, and we still have k pair discounts left, we use it.
6+
- Otherwise, we buy the cheaper of the two ends individually.
7+
- If only one book remains, we buy it directly.
8+
9+
### Edge Cases
10+
- If k = 0, we never use the pair option.
11+
- If pairCost >= cost[left] + cost[right], it's better to buy individually.
12+
13+
### Tips
14+
- Always compare pairCost with cost[left] + cost[right].
15+
- Use long to avoid overflow for large cost arrays.
16+
17+
### Time Complexity
18+
- **O(n)** – We process each book once.

0 commit comments

Comments
 (0)