From 083904357c6eaeda1856ea78cdd95fcba3a9308a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 30 Mar 2025 17:33:38 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3502 No.3502.Minimum Cost to Reach Every Position --- .../README.md | 71 +++++++++++++++++-- .../README_EN.md | 71 +++++++++++++++++-- .../Solution.cpp | 13 ++++ .../Solution.go | 10 +++ .../Solution.java | 12 ++++ .../Solution.py | 9 +++ .../Solution.ts | 10 +++ 7 files changed, 188 insertions(+), 8 deletions(-) create mode 100644 solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.cpp create mode 100644 solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.go create mode 100644 solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.java create mode 100644 solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.py create mode 100644 solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.ts diff --git a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README.md b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README.md index b6b204180c877..606f8926fa060 100644 --- a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README.md +++ b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README.md @@ -77,32 +77,95 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3502.Mi -### 方法一 +### 方法一:脑筋急转弯 + +根据题目描述,每个位置 $i$ 的最小费用,就是从 $0$ 到 $i$ 的最小费用。我们可以用一个变量 $\textit{mi}$ 来记录从 $0$ 到 $i$ 的最小费用。 + +我们从 $0$ 开始遍历每个位置 $i$,每次更新 $\textit{mi}$ 为 $\text{min}(\textit{mi}, \text{cost}[i])$,然后将 $\textit{mi}$ 赋值给答案数组的第 $i$ 个位置。 + +最后返回答案数组即可。 + +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{cost}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def minCosts(self, cost: List[int]) -> List[int]: + n = len(cost) + ans = [0] * n + mi = cost[0] + for i, c in enumerate(cost): + mi = min(mi, c) + ans[i] = mi + return ans ``` #### Java ```java - +class Solution { + public int[] minCosts(int[] cost) { + int n = cost.length; + int[] ans = new int[n]; + int mi = cost[0]; + for (int i = 0; i < n; ++i) { + mi = Math.min(mi, cost[i]); + ans[i] = mi; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector minCosts(vector& cost) { + int n = cost.size(); + vector ans(n); + int mi = cost[0]; + for (int i = 0; i < n; ++i) { + mi = min(mi, cost[i]); + ans[i] = mi; + } + return ans; + } +}; ``` #### Go ```go +func minCosts(cost []int) []int { + n := len(cost) + ans := make([]int, n) + mi := cost[0] + for i, c := range cost { + mi = min(mi, c) + ans[i] = mi + } + return ans +} +``` +#### TypeScript + +```ts +function minCosts(cost: number[]): number[] { + const n = cost.length; + const ans: number[] = Array(n).fill(0); + let mi = cost[0]; + for (let i = 0; i < n; ++i) { + mi = Math.min(mi, cost[i]); + ans[i] = mi; + } + return ans; +} ``` diff --git a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README_EN.md b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README_EN.md index 19f256f355564..225ae995772ba 100644 --- a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README_EN.md +++ b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README_EN.md @@ -75,32 +75,95 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3502.Mi -### Solution 1 +### Solution 1: Brain Teaser + +According to the problem description, the minimum cost for each position $i$ is the minimum cost from $0$ to $i$. We can use a variable $\textit{mi}$ to record the minimum cost from $0$ to $i$. + +Starting from $0$, we iterate through each position $i$, updating $\textit{mi}$ as $\text{min}(\textit{mi}, \text{cost}[i])$ at each step, and assign $\textit{mi}$ to the $i$-th position of the answer array. + +Finally, return the answer array. + +Time complexity is $O(n)$, where $n$ is the length of the array $\textit{cost}$. Ignoring the space used by the answer array, the space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def minCosts(self, cost: List[int]) -> List[int]: + n = len(cost) + ans = [0] * n + mi = cost[0] + for i, c in enumerate(cost): + mi = min(mi, c) + ans[i] = mi + return ans ``` #### Java ```java - +class Solution { + public int[] minCosts(int[] cost) { + int n = cost.length; + int[] ans = new int[n]; + int mi = cost[0]; + for (int i = 0; i < n; ++i) { + mi = Math.min(mi, cost[i]); + ans[i] = mi; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector minCosts(vector& cost) { + int n = cost.size(); + vector ans(n); + int mi = cost[0]; + for (int i = 0; i < n; ++i) { + mi = min(mi, cost[i]); + ans[i] = mi; + } + return ans; + } +}; ``` #### Go ```go +func minCosts(cost []int) []int { + n := len(cost) + ans := make([]int, n) + mi := cost[0] + for i, c := range cost { + mi = min(mi, c) + ans[i] = mi + } + return ans +} +``` +#### TypeScript + +```ts +function minCosts(cost: number[]): number[] { + const n = cost.length; + const ans: number[] = Array(n).fill(0); + let mi = cost[0]; + for (let i = 0; i < n; ++i) { + mi = Math.min(mi, cost[i]); + ans[i] = mi; + } + return ans; +} ``` diff --git a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.cpp b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.cpp new file mode 100644 index 0000000000000..82ea42a622f06 --- /dev/null +++ b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + vector minCosts(vector& cost) { + int n = cost.size(); + vector ans(n); + int mi = cost[0]; + for (int i = 0; i < n; ++i) { + mi = min(mi, cost[i]); + ans[i] = mi; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.go b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.go new file mode 100644 index 0000000000000..41f6666202fb9 --- /dev/null +++ b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.go @@ -0,0 +1,10 @@ +func minCosts(cost []int) []int { + n := len(cost) + ans := make([]int, n) + mi := cost[0] + for i, c := range cost { + mi = min(mi, c) + ans[i] = mi + } + return ans +} \ No newline at end of file diff --git a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.java b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.java new file mode 100644 index 0000000000000..6e505a56618a9 --- /dev/null +++ b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.java @@ -0,0 +1,12 @@ +class Solution { + public int[] minCosts(int[] cost) { + int n = cost.length; + int[] ans = new int[n]; + int mi = cost[0]; + for (int i = 0; i < n; ++i) { + mi = Math.min(mi, cost[i]); + ans[i] = mi; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.py b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.py new file mode 100644 index 0000000000000..6b999ef40eb82 --- /dev/null +++ b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def minCosts(self, cost: List[int]) -> List[int]: + n = len(cost) + ans = [0] * n + mi = cost[0] + for i, c in enumerate(cost): + mi = min(mi, c) + ans[i] = mi + return ans diff --git a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.ts b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.ts new file mode 100644 index 0000000000000..d02a7ec9bc822 --- /dev/null +++ b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/Solution.ts @@ -0,0 +1,10 @@ +function minCosts(cost: number[]): number[] { + const n = cost.length; + const ans: number[] = Array(n).fill(0); + let mi = cost[0]; + for (let i = 0; i < n; ++i) { + mi = Math.min(mi, cost[i]); + ans[i] = mi; + } + return ans; +}