Skip to content

Commit 0cead3d

Browse files
authored
feat: add solutions to lc problems: No.2944,2969 (#2136)
* No.2944.Minimum Number of Coins for Fruits * No.2969.Minimum Number of Coins for Fruits II
1 parent 7249834 commit 0cead3d

File tree

14 files changed

+1668
-101
lines changed

14 files changed

+1668
-101
lines changed

solution/2900-2999/2944.Minimum Number of Coins for Fruits/README.md

+344-13
Large diffs are not rendered by default.

solution/2900-2999/2944.Minimum Number of Coins for Fruits/README_EN.md

+349-18
Large diffs are not rendered by default.

solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ class Solution {
22
public:
33
int minimumCoins(vector<int>& prices) {
44
int n = prices.size();
5-
int f[n + 1];
6-
memset(f, 0x3f, sizeof(f));
7-
function<int(int)> dfs = [&](int i) {
8-
if (i > n) {
9-
return 0;
5+
deque<int> q;
6+
for (int i = n; i; --i) {
7+
while (q.size() && q.front() > i * 2 + 1) {
8+
q.pop_front();
109
}
11-
if (f[i] == 0x3f3f3f3f) {
12-
for (int j = i + 1; j <= i * 2 + 1; ++j) {
13-
f[i] = min(f[i], prices[i - 1] + dfs(j));
14-
}
10+
if (i <= (n - 1) / 2) {
11+
prices[i - 1] += prices[q.front() - 1];
1512
}
16-
return f[i];
17-
};
18-
return dfs(1);
13+
while (q.size() && prices[q.back() - 1] >= prices[i - 1]) {
14+
q.pop_back();
15+
}
16+
q.push_back(i);
17+
}
18+
return prices[0];
1919
}
2020
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,75 @@
11
func minimumCoins(prices []int) int {
22
n := len(prices)
3-
f := make([]int, n)
4-
var dfs func(int) int
5-
dfs = func(i int) int {
6-
if i > n {
7-
return 0
3+
q := Deque{}
4+
for i := n; i > 0; i-- {
5+
for q.Size() > 0 && q.Front() > i*2+1 {
6+
q.PopFront()
87
}
9-
if f[i] == 0 {
10-
f[i] = 1 << 30
11-
for j := i + 1; j <= i*2+1; j++ {
12-
f[i] = min(f[i], dfs(j)+prices[j-1])
13-
}
8+
if i <= (n-1)/2 {
9+
prices[i-1] += prices[q.Front()-1]
1410
}
15-
return f[i]
11+
for q.Size() > 0 && prices[q.Back()-1] >= prices[i-1] {
12+
q.PopBack()
13+
}
14+
q.PushBack(i)
15+
}
16+
return prices[0]
17+
}
18+
19+
// template
20+
type Deque struct{ l, r []int }
21+
22+
func (q Deque) Empty() bool {
23+
return len(q.l) == 0 && len(q.r) == 0
24+
}
25+
26+
func (q Deque) Size() int {
27+
return len(q.l) + len(q.r)
28+
}
29+
30+
func (q *Deque) PushFront(v int) {
31+
q.l = append(q.l, v)
32+
}
33+
34+
func (q *Deque) PushBack(v int) {
35+
q.r = append(q.r, v)
36+
}
37+
38+
func (q *Deque) PopFront() (v int) {
39+
if len(q.l) > 0 {
40+
q.l, v = q.l[:len(q.l)-1], q.l[len(q.l)-1]
41+
} else {
42+
v, q.r = q.r[0], q.r[1:]
43+
}
44+
return
45+
}
46+
47+
func (q *Deque) PopBack() (v int) {
48+
if len(q.r) > 0 {
49+
q.r, v = q.r[:len(q.r)-1], q.r[len(q.r)-1]
50+
} else {
51+
v, q.l = q.l[0], q.l[1:]
52+
}
53+
return
54+
}
55+
56+
func (q Deque) Front() int {
57+
if len(q.l) > 0 {
58+
return q.l[len(q.l)-1]
59+
}
60+
return q.r[0]
61+
}
62+
63+
func (q Deque) Back() int {
64+
if len(q.r) > 0 {
65+
return q.r[len(q.r)-1]
66+
}
67+
return q.l[0]
68+
}
69+
70+
func (q Deque) Get(i int) int {
71+
if i < len(q.l) {
72+
return q.l[len(q.l)-1-i]
1673
}
17-
return dfs(1)
74+
return q.r[i-len(q.l)]
1875
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
class Solution {
2-
private int[] prices;
3-
private int[] f;
4-
private int n;
5-
62
public int minimumCoins(int[] prices) {
7-
n = prices.length;
8-
f = new int[n + 1];
9-
this.prices = prices;
10-
return dfs(1);
11-
}
12-
13-
private int dfs(int i) {
14-
if (i > n) {
15-
return 0;
16-
}
17-
if (f[i] == 0) {
18-
f[i] = 1 << 30;
19-
for (int j = i + 1; j <= i * 2 + 1; ++j) {
20-
f[i] = Math.min(f[i], prices[i - 1] + dfs(j));
3+
int n = prices.length;
4+
Deque<Integer> q = new ArrayDeque<>();
5+
for (int i = n; i > 0; --i) {
6+
while (!q.isEmpty() && q.peek() > i * 2 + 1) {
7+
q.poll();
8+
}
9+
if (i <= (n - 1) / 2) {
10+
prices[i - 1] += prices[q.peek() - 1];
11+
}
12+
while (!q.isEmpty() && prices[q.peekLast() - 1] >= prices[i - 1]) {
13+
q.pollLast();
2114
}
15+
q.offer(i);
2216
}
23-
return f[i];
17+
return prices[0];
2418
}
2519
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
class Solution:
22
def minimumCoins(self, prices: List[int]) -> int:
3-
@cache
4-
def dfs(i: int) -> int:
5-
if i > len(prices):
6-
return 0
7-
return prices[i - 1] + min(dfs(j) for j in range(i + 1, i * 2 + 2))
8-
9-
return dfs(1)
3+
n = len(prices)
4+
q = deque()
5+
for i in range(n, 0, -1):
6+
while q and q[0] > i * 2 + 1:
7+
q.popleft()
8+
if i <= (n - 1) // 2:
9+
prices[i - 1] += prices[q[0] - 1]
10+
while q and prices[q[-1] - 1] >= prices[i - 1]:
11+
q.pop()
12+
q.append(i)
13+
return prices[0]
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,113 @@
11
function minimumCoins(prices: number[]): number {
22
const n = prices.length;
3-
const f: number[] = Array(n + 1).fill(0);
4-
const dfs = (i: number): number => {
5-
if (i > n) {
6-
return 0;
7-
}
8-
if (f[i] === 0) {
9-
f[i] = 1 << 30;
10-
for (let j = i + 1; j <= i * 2 + 1; ++j) {
11-
f[i] = Math.min(f[i], prices[i - 1] + dfs(j));
12-
}
13-
}
14-
return f[i];
15-
};
16-
return dfs(1);
3+
const q = new Deque<number>();
4+
for (let i = n; i; --i) {
5+
while (q.getSize() && q.frontValue()! > i * 2 + 1) {
6+
q.popFront();
7+
}
8+
if (i <= (n - 1) >> 1) {
9+
prices[i - 1] += prices[q.frontValue()! - 1];
10+
}
11+
while (q.getSize() && prices[q.backValue()! - 1] >= prices[i - 1]) {
12+
q.popBack();
13+
}
14+
q.pushBack(i);
15+
}
16+
return prices[0];
17+
}
18+
19+
class Node<T> {
20+
value: T;
21+
next: Node<T> | null;
22+
prev: Node<T> | null;
23+
24+
constructor(value: T) {
25+
this.value = value;
26+
this.next = null;
27+
this.prev = null;
28+
}
29+
}
30+
31+
class Deque<T> {
32+
private front: Node<T> | null;
33+
private back: Node<T> | null;
34+
private size: number;
35+
36+
constructor() {
37+
this.front = null;
38+
this.back = null;
39+
this.size = 0;
40+
}
41+
42+
pushFront(val: T): void {
43+
const newNode = new Node(val);
44+
if (this.isEmpty()) {
45+
this.front = newNode;
46+
this.back = newNode;
47+
} else {
48+
newNode.next = this.front;
49+
this.front!.prev = newNode;
50+
this.front = newNode;
51+
}
52+
this.size++;
53+
}
54+
55+
pushBack(val: T): void {
56+
const newNode = new Node(val);
57+
if (this.isEmpty()) {
58+
this.front = newNode;
59+
this.back = newNode;
60+
} else {
61+
newNode.prev = this.back;
62+
this.back!.next = newNode;
63+
this.back = newNode;
64+
}
65+
this.size++;
66+
}
67+
68+
popFront(): T | undefined {
69+
if (this.isEmpty()) {
70+
return undefined;
71+
}
72+
const value = this.front!.value;
73+
this.front = this.front!.next;
74+
if (this.front !== null) {
75+
this.front.prev = null;
76+
} else {
77+
this.back = null;
78+
}
79+
this.size--;
80+
return value;
81+
}
82+
83+
popBack(): T | undefined {
84+
if (this.isEmpty()) {
85+
return undefined;
86+
}
87+
const value = this.back!.value;
88+
this.back = this.back!.prev;
89+
if (this.back !== null) {
90+
this.back.next = null;
91+
} else {
92+
this.front = null;
93+
}
94+
this.size--;
95+
return value;
96+
}
97+
98+
frontValue(): T | undefined {
99+
return this.front?.value;
100+
}
101+
102+
backValue(): T | undefined {
103+
return this.back?.value;
104+
}
105+
106+
getSize(): number {
107+
return this.size;
108+
}
109+
110+
isEmpty(): boolean {
111+
return this.size === 0;
112+
}
17113
}

0 commit comments

Comments
 (0)