diff --git a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README.md b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README.md index 066d2ac7eb63a..c9284f8c15b7b 100644 --- a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README.md +++ b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README.md @@ -100,6 +100,18 @@ +**方法一:排序** + +题目实际上要我们找出数组中最长的连续递增子序列的长度,然后再加上 $1$。 + +我们定义一个函数 $f(nums)$,表示数组 $nums$ 中最长的连续递增子序列的长度。 + +对于数组 $nums$,我们先对其进行排序,然后遍历数组,如果当前元素 $nums[i]$ 等于前一个元素 $nums[i - 1]$ 加 $1$,则说明当前元素可以加入到连续递增子序列中,否则,说明当前元素不能加入到连续递增子序列中,我们需要重新开始计算连续递增子序列的长度。最后,我们返回连续递增子序列的长度加 $1$。 + +我们在求出 $hBars$ 和 $vBars$ 中最长的连续递增子序列的长度之后,我们取两者中的最小值作为正方形的边长,然后再求出正方形的面积即可。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $hBars$ 或 $vBars$ 的长度。 + ### **Python3** @@ -107,7 +119,22 @@ ```python - +class Solution: + def maximizeSquareHoleArea( + self, n: int, m: int, hBars: List[int], vBars: List[int] + ) -> int: + def f(nums: List[int]) -> int: + nums.sort() + ans = cnt = 1 + for i in range(1, len(nums)): + if nums[i] == nums[i - 1] + 1: + cnt += 1 + ans = max(ans, cnt) + else: + cnt = 1 + return ans + 1 + + return min(f(hBars), f(vBars)) ** 2 ``` ### **Java** @@ -115,19 +142,119 @@ ```java - +class Solution { + public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) { + int x = Math.min(f(hBars), f(vBars)); + return x * x; + } + + private int f(int[] nums) { + Arrays.sort(nums); + int ans = 1, cnt = 1; + for (int i = 1; i < nums.length; ++i) { + if (nums[i] == nums[i - 1] + 1) { + ans = Math.max(ans, ++cnt); + } else { + cnt = 1; + } + } + return ans + 1; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int maximizeSquareHoleArea(int n, int m, vector& hBars, vector& vBars) { + auto f = [](vector& nums) { + int ans = 1, cnt = 1; + sort(nums.begin(), nums.end()); + for (int i = 1; i < nums.size(); ++i) { + if (nums[i] == nums[i - 1] + 1) { + ans = max(ans, ++cnt); + } else { + cnt = 1; + } + } + return ans + 1; + }; + int x = min(f(hBars), f(vBars)); + return x * x; + } +}; ``` ### **Go** ```go +func maximizeSquareHoleArea(n int, m int, hBars []int, vBars []int) int { + f := func(nums []int) int { + sort.Ints(nums) + ans, cnt := 1, 1 + for i, x := range nums[1:] { + if x == nums[i]+1 { + cnt++ + ans = max(ans, cnt) + } else { + cnt = 1 + } + } + return ans + 1 + } + x := min(f(hBars), f(vBars)) + return x * x +} +``` + +### **TypeScript** + +```ts +function maximizeSquareHoleArea(n: number, m: number, hBars: number[], vBars: number[]): number { + const f = (nums: number[]): number => { + nums.sort((a, b) => a - b); + let [ans, cnt] = [1, 1]; + for (let i = 1; i < nums.length; ++i) { + if (nums[i] === nums[i - 1] + 1) { + ans = Math.max(ans, ++cnt); + } else { + cnt = 1; + } + } + return ans + 1; + }; + return Math.min(f(hBars), f(vBars)) ** 2; +} +``` +### **Rust** + +```rust +impl Solution { + pub fn maximize_square_hole_area(n: i32, m: i32, h_bars: Vec, v_bars: Vec) -> i32 { + let f = |nums: &mut Vec| -> i32 { + let mut ans = 1; + let mut cnt = 1; + nums.sort(); + for i in 1..nums.len() { + if nums[i] == nums[i - 1] + 1 { + cnt += 1; + ans = ans.max(cnt); + } else { + cnt = 1; + } + } + ans + 1 + }; + + let mut h_bars = h_bars; + let mut v_bars = v_bars; + let x = f(&mut h_bars).min(f(&mut v_bars)); + x * x + } +} ``` ### **...** diff --git a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README_EN.md b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README_EN.md index ff5497fa03e2c..1b3f70e9abb11 100644 --- a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README_EN.md +++ b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README_EN.md @@ -93,30 +93,157 @@ Hence, the answer is 9. ## Solutions +**Solution 1: Sorting** + +The problem essentially asks us to find the length of the longest consecutive increasing subsequence in the array, and then add 1 to it. + +We define a function $f(nums)$, which represents the length of the longest consecutive increasing subsequence in the array $nums$. + +For the array $nums$, we first sort it, then traverse the array. If the current element $nums[i]$ equals the previous element $nums[i - 1]$ plus 1, it means that the current element can be added to the consecutive increasing subsequence. Otherwise, it means that the current element cannot be added to the consecutive increasing subsequence, and we need to start calculating the length of the consecutive increasing subsequence again. Finally, we return the length of the consecutive increasing subsequence plus 1. + +After finding the length of the longest consecutive increasing subsequence in $hBars$ and $vBars$, we take the minimum of the two as the side length of the square, and then calculate the area of the square. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $hBars$ or $vBars$. + ### **Python3** ```python - +class Solution: + def maximizeSquareHoleArea( + self, n: int, m: int, hBars: List[int], vBars: List[int] + ) -> int: + def f(nums: List[int]) -> int: + nums.sort() + ans = cnt = 1 + for i in range(1, len(nums)): + if nums[i] == nums[i - 1] + 1: + cnt += 1 + ans = max(ans, cnt) + else: + cnt = 1 + return ans + 1 + + return min(f(hBars), f(vBars)) ** 2 ``` ### **Java** ```java - +class Solution { + public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) { + int x = Math.min(f(hBars), f(vBars)); + return x * x; + } + + private int f(int[] nums) { + Arrays.sort(nums); + int ans = 1, cnt = 1; + for (int i = 1; i < nums.length; ++i) { + if (nums[i] == nums[i - 1] + 1) { + ans = Math.max(ans, ++cnt); + } else { + cnt = 1; + } + } + return ans + 1; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int maximizeSquareHoleArea(int n, int m, vector& hBars, vector& vBars) { + auto f = [](vector& nums) { + int ans = 1, cnt = 1; + sort(nums.begin(), nums.end()); + for (int i = 1; i < nums.size(); ++i) { + if (nums[i] == nums[i - 1] + 1) { + ans = max(ans, ++cnt); + } else { + cnt = 1; + } + } + return ans + 1; + }; + int x = min(f(hBars), f(vBars)); + return x * x; + } +}; ``` ### **Go** ```go +func maximizeSquareHoleArea(n int, m int, hBars []int, vBars []int) int { + f := func(nums []int) int { + sort.Ints(nums) + ans, cnt := 1, 1 + for i, x := range nums[1:] { + if x == nums[i]+1 { + cnt++ + ans = max(ans, cnt) + } else { + cnt = 1 + } + } + return ans + 1 + } + x := min(f(hBars), f(vBars)) + return x * x +} +``` + +### **TypeScript** + +```ts +function maximizeSquareHoleArea(n: number, m: number, hBars: number[], vBars: number[]): number { + const f = (nums: number[]): number => { + nums.sort((a, b) => a - b); + let [ans, cnt] = [1, 1]; + for (let i = 1; i < nums.length; ++i) { + if (nums[i] === nums[i - 1] + 1) { + ans = Math.max(ans, ++cnt); + } else { + cnt = 1; + } + } + return ans + 1; + }; + return Math.min(f(hBars), f(vBars)) ** 2; +} +``` +### **Rust** + +```rust +impl Solution { + pub fn maximize_square_hole_area(n: i32, m: i32, h_bars: Vec, v_bars: Vec) -> i32 { + let f = |nums: &mut Vec| -> i32 { + let mut ans = 1; + let mut cnt = 1; + nums.sort(); + for i in 1..nums.len() { + if nums[i] == nums[i - 1] + 1 { + cnt += 1; + ans = ans.max(cnt); + } else { + cnt = 1; + } + } + ans + 1 + }; + + let mut h_bars = h_bars; + let mut v_bars = v_bars; + let x = f(&mut h_bars).min(f(&mut v_bars)); + x * x + } +} ``` ### **...** diff --git a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.cpp b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.cpp new file mode 100644 index 0000000000000..32c9b5097a36c --- /dev/null +++ b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int maximizeSquareHoleArea(int n, int m, vector& hBars, vector& vBars) { + auto f = [](vector& nums) { + int ans = 1, cnt = 1; + sort(nums.begin(), nums.end()); + for (int i = 1; i < nums.size(); ++i) { + if (nums[i] == nums[i - 1] + 1) { + ans = max(ans, ++cnt); + } else { + cnt = 1; + } + } + return ans + 1; + }; + int x = min(f(hBars), f(vBars)); + return x * x; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.go b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.go new file mode 100644 index 0000000000000..cc82e4dd3fca8 --- /dev/null +++ b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.go @@ -0,0 +1,17 @@ +func maximizeSquareHoleArea(n int, m int, hBars []int, vBars []int) int { + f := func(nums []int) int { + sort.Ints(nums) + ans, cnt := 1, 1 + for i, x := range nums[1:] { + if x == nums[i]+1 { + cnt++ + ans = max(ans, cnt) + } else { + cnt = 1 + } + } + return ans + 1 + } + x := min(f(hBars), f(vBars)) + return x * x +} \ No newline at end of file diff --git a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.java b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.java new file mode 100644 index 0000000000000..9c0aa4783f080 --- /dev/null +++ b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) { + int x = Math.min(f(hBars), f(vBars)); + return x * x; + } + + private int f(int[] nums) { + Arrays.sort(nums); + int ans = 1, cnt = 1; + for (int i = 1; i < nums.length; ++i) { + if (nums[i] == nums[i - 1] + 1) { + ans = Math.max(ans, ++cnt); + } else { + cnt = 1; + } + } + return ans + 1; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.py b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.py new file mode 100644 index 0000000000000..efc3bcb6d0176 --- /dev/null +++ b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def maximizeSquareHoleArea( + self, n: int, m: int, hBars: List[int], vBars: List[int] + ) -> int: + def f(nums: List[int]) -> int: + nums.sort() + ans = cnt = 1 + for i in range(1, len(nums)): + if nums[i] == nums[i - 1] + 1: + cnt += 1 + ans = max(ans, cnt) + else: + cnt = 1 + return ans + 1 + + return min(f(hBars), f(vBars)) ** 2 diff --git a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.rs b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.rs new file mode 100644 index 0000000000000..d1590c8f4405a --- /dev/null +++ b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.rs @@ -0,0 +1,23 @@ +impl Solution { + pub fn maximize_square_hole_area(n: i32, m: i32, h_bars: Vec, v_bars: Vec) -> i32 { + let f = |nums: &mut Vec| -> i32 { + let mut ans = 1; + let mut cnt = 1; + nums.sort(); + for i in 1..nums.len() { + if nums[i] == nums[i - 1] + 1 { + cnt += 1; + ans = ans.max(cnt); + } else { + cnt = 1; + } + } + ans + 1 + }; + + let mut h_bars = h_bars; + let mut v_bars = v_bars; + let x = f(&mut h_bars).min(f(&mut v_bars)); + x * x + } +} diff --git a/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.ts b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.ts new file mode 100644 index 0000000000000..c02fe8e2a52df --- /dev/null +++ b/solution/2900-2999/2943.Maximize Area of Square Hole in Grid/Solution.ts @@ -0,0 +1,15 @@ +function maximizeSquareHoleArea(n: number, m: number, hBars: number[], vBars: number[]): number { + const f = (nums: number[]): number => { + nums.sort((a, b) => a - b); + let [ans, cnt] = [1, 1]; + for (let i = 1; i < nums.length; ++i) { + if (nums[i] === nums[i - 1] + 1) { + ans = Math.max(ans, ++cnt); + } else { + cnt = 1; + } + } + return ans + 1; + }; + return Math.min(f(hBars), f(vBars)) ** 2; +} diff --git a/solution/2900-2999/2944.Minimum Number of Coins for Fruits/README.md b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/README.md index 4acde6721855a..4ccb527e6383d 100644 --- a/solution/2900-2999/2944.Minimum Number of Coins for Fruits/README.md +++ b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/README.md @@ -61,6 +61,19 @@ +**方法一:记忆化搜索** + +我们定义一个函数 $dfs(i)$,表示从第 $i$ 个水果开始购买所有水果所需要的最少金币数。那么答案就是 $dfs(1)$。 + +函数 $dfs(i)$ 的执行逻辑如下: + +- 如果 $i \gt n$,说明已经购买完所有水果,返回 $0$。 +- 否则,我们可以购买水果 $i$,然后在接下来的 $i + 1$ 到 $2i + 1$ 个水果中选择一个水果 $j$ 开始购买,那么 $dfs(i) = prices[i - 1] + \min_{i + 1 \le j \le 2i + 1} dfs(j)$。 + +为了避免重复计算,我们使用记忆化搜索的方法,将已经计算过的结果保存起来,下次遇到相同的情况时,直接返回结果即可。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $prices$ 的长度。 + ### **Python3** @@ -68,7 +81,15 @@ ```python - +class Solution: + def minimumCoins(self, prices: List[int]) -> int: + @cache + def dfs(i: int) -> int: + if i > len(prices): + return 0 + return prices[i - 1] + min(dfs(j) for j in range(i + 1, i * 2 + 2)) + + return dfs(1) ``` ### **Java** @@ -76,19 +97,101 @@ ```java - +class Solution { + private int[] prices; + private int[] f; + private int n; + + public int minimumCoins(int[] prices) { + n = prices.length; + f = new int[n + 1]; + this.prices = prices; + return dfs(1); + } + + private int dfs(int i) { + if (i > n) { + return 0; + } + if (f[i] == 0) { + f[i] = 1 << 30; + for (int j = i + 1; j <= i * 2 + 1; ++j) { + f[i] = Math.min(f[i], prices[i - 1] + dfs(j)); + } + } + return f[i]; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minimumCoins(vector& prices) { + int n = prices.size(); + int f[n + 1]; + memset(f, 0x3f, sizeof(f)); + function dfs = [&](int i) { + if (i > n) { + return 0; + } + if (f[i] == 0x3f3f3f3f) { + for (int j = i + 1; j <= i * 2 + 1; ++j) { + f[i] = min(f[i], prices[i - 1] + dfs(j)); + } + } + return f[i]; + }; + return dfs(1); + } +}; ``` ### **Go** ```go +func minimumCoins(prices []int) int { + n := len(prices) + f := make([]int, n) + var dfs func(int) int + dfs = func(i int) int { + if i > n { + return 0 + } + if f[i] == 0 { + f[i] = 1 << 30 + for j := i + 1; j <= i*2+1; j++ { + f[i] = min(f[i], dfs(j)+prices[j-1]) + } + } + return f[i] + } + return dfs(1) +} +``` +### **TypeScript** + +```ts +function minimumCoins(prices: number[]): number { + const n = prices.length; + const f: number[] = Array(n + 1).fill(0); + const dfs = (i: number): number => { + if (i > n) { + return 0; + } + if (f[i] === 0) { + f[i] = 1 << 30; + for (let j = i + 1; j <= i * 2 + 1; ++j) { + f[i] = Math.min(f[i], prices[i - 1] + dfs(j)); + } + } + return f[i]; + }; + return dfs(1); +} ``` ### **...** diff --git a/solution/2900-2999/2944.Minimum Number of Coins for Fruits/README_EN.md b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/README_EN.md index 933b56f23b559..c8f60af5ad6f6 100644 --- a/solution/2900-2999/2944.Minimum Number of Coins for Fruits/README_EN.md +++ b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/README_EN.md @@ -55,30 +55,133 @@ It can be proven that 2 is the minimum number of coins needed to acquire all the ## Solutions +**Solution 1: Memoization Search** + +We define a function $dfs(i)$, which represents the minimum number of coins needed to buy all fruits starting from the $i$th fruit. The answer is $dfs(1)$. + +The execution logic of the function $dfs(i)$ is as follows: + +- If $i > n$, it means that all fruits have been bought, so return $0$. +- Otherwise, we can buy the $i$th fruit, and then choose a fruit $j$ from the next $i + 1$ to $2i + 1$ fruits to start buying. So, $dfs(i) = prices[i - 1] + \min_{i + 1 \le j \le 2i + 1} dfs(j)$. + +To avoid repeated calculations, we use the method of memoization search, saving the results that have been calculated. When we encounter the same situation next time, we can directly return the result. + +The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $prices$. + ### **Python3** ```python - +class Solution: + def minimumCoins(self, prices: List[int]) -> int: + @cache + def dfs(i: int) -> int: + if i > len(prices): + return 0 + return prices[i - 1] + min(dfs(j) for j in range(i + 1, i * 2 + 2)) + + return dfs(1) ``` ### **Java** ```java - +class Solution { + private int[] prices; + private int[] f; + private int n; + + public int minimumCoins(int[] prices) { + n = prices.length; + f = new int[n + 1]; + this.prices = prices; + return dfs(1); + } + + private int dfs(int i) { + if (i > n) { + return 0; + } + if (f[i] == 0) { + f[i] = 1 << 30; + for (int j = i + 1; j <= i * 2 + 1; ++j) { + f[i] = Math.min(f[i], prices[i - 1] + dfs(j)); + } + } + return f[i]; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minimumCoins(vector& prices) { + int n = prices.size(); + int f[n + 1]; + memset(f, 0x3f, sizeof(f)); + function dfs = [&](int i) { + if (i > n) { + return 0; + } + if (f[i] == 0x3f3f3f3f) { + for (int j = i + 1; j <= i * 2 + 1; ++j) { + f[i] = min(f[i], prices[i - 1] + dfs(j)); + } + } + return f[i]; + }; + return dfs(1); + } +}; ``` ### **Go** ```go +func minimumCoins(prices []int) int { + n := len(prices) + f := make([]int, n) + var dfs func(int) int + dfs = func(i int) int { + if i > n { + return 0 + } + if f[i] == 0 { + f[i] = 1 << 30 + for j := i + 1; j <= i*2+1; j++ { + f[i] = min(f[i], dfs(j)+prices[j-1]) + } + } + return f[i] + } + return dfs(1) +} +``` +### **TypeScript** + +```ts +function minimumCoins(prices: number[]): number { + const n = prices.length; + const f: number[] = Array(n + 1).fill(0); + const dfs = (i: number): number => { + if (i > n) { + return 0; + } + if (f[i] === 0) { + f[i] = 1 << 30; + for (let j = i + 1; j <= i * 2 + 1; ++j) { + f[i] = Math.min(f[i], prices[i - 1] + dfs(j)); + } + } + return f[i]; + }; + return dfs(1); +} ``` ### **...** diff --git a/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.cpp b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.cpp new file mode 100644 index 0000000000000..83a2ea6450e14 --- /dev/null +++ b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minimumCoins(vector& prices) { + int n = prices.size(); + int f[n + 1]; + memset(f, 0x3f, sizeof(f)); + function dfs = [&](int i) { + if (i > n) { + return 0; + } + if (f[i] == 0x3f3f3f3f) { + for (int j = i + 1; j <= i * 2 + 1; ++j) { + f[i] = min(f[i], prices[i - 1] + dfs(j)); + } + } + return f[i]; + }; + return dfs(1); + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.go b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.go new file mode 100644 index 0000000000000..7e2cf5e06d533 --- /dev/null +++ b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.go @@ -0,0 +1,18 @@ +func minimumCoins(prices []int) int { + n := len(prices) + f := make([]int, n) + var dfs func(int) int + dfs = func(i int) int { + if i > n { + return 0 + } + if f[i] == 0 { + f[i] = 1 << 30 + for j := i + 1; j <= i*2+1; j++ { + f[i] = min(f[i], dfs(j)+prices[j-1]) + } + } + return f[i] + } + return dfs(1) +} \ No newline at end of file diff --git a/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.java b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.java new file mode 100644 index 0000000000000..e71ba839d824c --- /dev/null +++ b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.java @@ -0,0 +1,25 @@ +class Solution { + private int[] prices; + private int[] f; + private int n; + + public int minimumCoins(int[] prices) { + n = prices.length; + f = new int[n + 1]; + this.prices = prices; + return dfs(1); + } + + private int dfs(int i) { + if (i > n) { + return 0; + } + if (f[i] == 0) { + f[i] = 1 << 30; + for (int j = i + 1; j <= i * 2 + 1; ++j) { + f[i] = Math.min(f[i], prices[i - 1] + dfs(j)); + } + } + return f[i]; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.py b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.py new file mode 100644 index 0000000000000..7cf6af5344848 --- /dev/null +++ b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def minimumCoins(self, prices: List[int]) -> int: + @cache + def dfs(i: int) -> int: + if i > len(prices): + return 0 + return prices[i - 1] + min(dfs(j) for j in range(i + 1, i * 2 + 2)) + + return dfs(1) diff --git a/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.ts b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.ts new file mode 100644 index 0000000000000..ade4da0041df4 --- /dev/null +++ b/solution/2900-2999/2944.Minimum Number of Coins for Fruits/Solution.ts @@ -0,0 +1,17 @@ +function minimumCoins(prices: number[]): number { + const n = prices.length; + const f: number[] = Array(n + 1).fill(0); + const dfs = (i: number): number => { + if (i > n) { + return 0; + } + if (f[i] === 0) { + f[i] = 1 << 30; + for (let j = i + 1; j <= i * 2 + 1; ++j) { + f[i] = Math.min(f[i], prices[i - 1] + dfs(j)); + } + } + return f[i]; + }; + return dfs(1); +}