From 327487d3611f8496e816d6b960f12bc194c71b51 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 17 Dec 2023 08:47:36 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.0746,0747 * No.0746.Min Cost Climbing Stairs * No.0747.Largest Number At Least Twice of Others --- .../0746.Min Cost Climbing Stairs/README.md | 155 ++++++++++------- .../README_EN.md | 156 +++++++++++------- .../Solution.cpp | 22 +-- .../0746.Min Cost Climbing Stairs/Solution.go | 8 +- .../Solution.java | 20 +-- .../0746.Min Cost Climbing Stairs/Solution.py | 12 +- .../0746.Min Cost Climbing Stairs/Solution.rs | 11 ++ .../0746.Min Cost Climbing Stairs/Solution.ts | 9 +- .../README.md | 120 ++++++++------ .../README_EN.md | 120 ++++++++------ .../Solution.cpp | 32 ++-- .../Solution.go | 20 +-- .../Solution.java | 31 ++-- .../Solution.js | 19 +-- .../Solution.py | 15 +- .../Solution.ts | 14 ++ 16 files changed, 446 insertions(+), 318 deletions(-) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.rs create mode 100644 solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.ts diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md index a177bcb33bdb1..d200638b31aa2 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md @@ -54,19 +54,19 @@ **方法一:动态规划** -定义 `dp[i]` 表示到达第 `i` 个台阶的最小花费。可以得到状态转移方程: +我们定义 $f[i]$ 表示到达第 $i$ 个阶梯所需要的最小花费,初始时 $f[0] = f[1] = 0$,答案即为 $f[n]$。 + +当 $i \ge 2$ 时,我们可以从第 $i - 1$ 个阶梯使用 $1$ 步直接到达第 $i$ 个阶梯,或者从第 $i - 2$ 个阶梯使用 $2$ 步到达第 $i$ 个阶梯,因此我们有状态转移方程: $$ -dp[i] = \min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) +f[i] = \min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]) $$ -最终结果为 `dp[n]`。其中 $n$ 表示 `cost` 数组的长度。 - -时间复杂度 $O(n)$,空间复杂度 $O(n)$。 +最终的答案即为 $f[n]$。 -由于 `dp[i]` 只跟 `dp[i-1]` 和 `dp[i-2]` 有关,因此我们还可以对空间进行优化,只用两个变量 `a`, `b` 来记录。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `cost` 的长度。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。 +我们注意到,状态转移方程中的 $f[i]$ 只和 $f[i - 1]$ 与 $f[i - 2]$ 有关,因此我们可以使用两个变量 $f$ 和 $g$ 交替地记录 $f[i - 2]$ 和 $f[i - 1]$ 的值,这样空间复杂度可以优化到 $O(1)$。 @@ -78,19 +78,19 @@ $$ class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: n = len(cost) - dp = [0] * (n + 1) + f = [0] * (n + 1) for i in range(2, n + 1): - dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) - return dp[-1] + f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]) + return f[n] ``` ```python class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: - a = b = 0 - for i in range(1, len(cost)): - a, b = b, min(a + cost[i - 1], b + cost[i]) - return b + f = g = 0 + for i in range(2, len(cost) + 1): + f, g = g, min(f + cost[i - 2], g + cost[i - 1]) + return g ``` ### **Java** @@ -101,11 +101,11 @@ class Solution: class Solution { public int minCostClimbingStairs(int[] cost) { int n = cost.length; - int[] dp = new int[n + 1]; + int[] f = new int[n + 1]; for (int i = 2; i <= n; ++i) { - dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); + f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); } - return dp[n]; + return f[n]; } } ``` @@ -113,38 +113,14 @@ class Solution { ```java class Solution { public int minCostClimbingStairs(int[] cost) { - int a = 0, b = 0; - for (int i = 1; i < cost.length; ++i) { - int c = Math.min(a + cost[i - 1], b + cost[i]); - a = b; - b = c; + int f = 0, g = 0; + for (int i = 2; i <= cost.length; ++i) { + int gg = Math.min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; } - return b; - } -} -``` - -### **TypeScript** - -```ts -function minCostClimbingStairs(cost: number[]): number { - const n = cost.length; - const dp = new Array(n + 1).fill(0); - for (let i = 2; i <= n; ++i) { - dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); - } - return dp[n]; -} -``` - -```ts -function minCostClimbingStairs(cost: number[]): number { - let a = 0, - b = 0; - for (let i = 1; i < cost.length; ++i) { - [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + return g; } - return b; } ``` @@ -155,11 +131,11 @@ class Solution { public: int minCostClimbingStairs(vector& cost) { int n = cost.size(); - vector dp(n + 1); + vector f(n + 1); for (int i = 2; i <= n; ++i) { - dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); + f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); } - return dp[n]; + return f[n]; } }; ``` @@ -168,13 +144,13 @@ public: class Solution { public: int minCostClimbingStairs(vector& cost) { - int a = 0, b = 0; - for (int i = 1; i < cost.size(); ++i) { - int c = min(a + cost[i - 1], b + cost[i]); - a = b; - b = c; + int f = 0, g = 0; + for (int i = 2; i <= cost.size(); ++i) { + int gg = min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; } - return b; + return g; } }; ``` @@ -184,21 +160,74 @@ public: ```go func minCostClimbingStairs(cost []int) int { n := len(cost) - dp := make([]int, n+1) + f := make([]int, n+1) for i := 2; i <= n; i++ { - dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]) + f[i] = min(f[i-1]+cost[i-1], f[i-2]+cost[i-2]) } - return dp[n] + return f[n] } ``` ```go func minCostClimbingStairs(cost []int) int { - a, b := 0, 0 - for i := 1; i < len(cost); i++ { - a, b = b, min(a+cost[i-1], b+cost[i]) + var f, g int + for i := 2; i <= n; i++ { + f, g = g, min(f+cost[i-2], g+cost[i-1]) } - return b + return g +} +``` + +### **TypeScript** + +```ts +function minCostClimbingStairs(cost: number[]): number { + const n = cost.length; + const f: number[] = Array(n + 1).fill(0); + for (let i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); + } + return f[n]; +} +``` + +```ts +function minCostClimbingStairs(cost: number[]): number { + let a = 0, + b = 0; + for (let i = 1; i < cost.length; ++i) { + [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + } + return b; +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let n = cost.len(); + let mut f = vec![0; n + 1]; + for i in 2..=n { + f[i] = std::cmp::min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); + } + f[n] + } +} +``` + +```rust +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let (mut f, mut g) = (0, 0); + for i in 2..=cost.len() { + let gg = std::cmp::min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; + } + g + } } ``` diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md index 4212825f450fd..583c3645935ba 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md @@ -46,6 +46,22 @@ The total cost is 6. ## Solutions +**Solution 1: Dynamic Programming** + +We define $f[i]$ as the minimum cost required to reach the $i$th step, initially $f[0] = f[1] = 0$. The answer is $f[n]$. + +When $i \ge 2$, we can directly reach the $i$th step from the $(i - 1)$th step using $1$ step, or reach the $i$th step from the $(i - 2)$th step using $2$ steps. Therefore, we have the state transition equation: + +$$ +f[i] = \min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]) +$$ + +The final answer is $f[n]$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the `cost` array. + +We notice that $f[i]$ in the state transition equation is only related to $f[i - 1]$ and $f[i - 2]$. Therefore, we can use two variables $f$ and $g$ to alternately record the values of $f[i - 2]$ and $f[i - 1]$, which optimizes the space complexity to $O(1)$. + ### **Python3** @@ -54,19 +70,19 @@ The total cost is 6. class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: n = len(cost) - dp = [0] * (n + 1) + f = [0] * (n + 1) for i in range(2, n + 1): - dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) - return dp[-1] + f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]) + return f[n] ``` ```python class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: - a = b = 0 - for i in range(1, len(cost)): - a, b = b, min(a + cost[i - 1], b + cost[i]) - return b + f = g = 0 + for i in range(2, len(cost) + 1): + f, g = g, min(f + cost[i - 2], g + cost[i - 1]) + return g ``` ### **Java** @@ -75,11 +91,11 @@ class Solution: class Solution { public int minCostClimbingStairs(int[] cost) { int n = cost.length; - int[] dp = new int[n + 1]; + int[] f = new int[n + 1]; for (int i = 2; i <= n; ++i) { - dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); + f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); } - return dp[n]; + return f[n]; } } ``` @@ -87,38 +103,14 @@ class Solution { ```java class Solution { public int minCostClimbingStairs(int[] cost) { - int a = 0, b = 0; - for (int i = 1; i < cost.length; ++i) { - int c = Math.min(a + cost[i - 1], b + cost[i]); - a = b; - b = c; + int f = 0, g = 0; + for (int i = 2; i <= cost.length; ++i) { + int gg = Math.min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; } - return b; - } -} -``` - -### **TypeScript** - -```ts -function minCostClimbingStairs(cost: number[]): number { - const n = cost.length; - const dp = new Array(n + 1).fill(0); - for (let i = 2; i <= n; ++i) { - dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); + return g; } - return dp[n]; -} -``` - -```ts -function minCostClimbingStairs(cost: number[]): number { - let a = 0, - b = 0; - for (let i = 1; i < cost.length; ++i) { - [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; - } - return b; } ``` @@ -129,11 +121,11 @@ class Solution { public: int minCostClimbingStairs(vector& cost) { int n = cost.size(); - vector dp(n + 1); + vector f(n + 1); for (int i = 2; i <= n; ++i) { - dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); + f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); } - return dp[n]; + return f[n]; } }; ``` @@ -142,13 +134,13 @@ public: class Solution { public: int minCostClimbingStairs(vector& cost) { - int a = 0, b = 0; - for (int i = 1; i < cost.size(); ++i) { - int c = min(a + cost[i - 1], b + cost[i]); - a = b; - b = c; + int f = 0, g = 0; + for (int i = 2; i <= cost.size(); ++i) { + int gg = min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; } - return b; + return g; } }; ``` @@ -158,21 +150,73 @@ public: ```go func minCostClimbingStairs(cost []int) int { n := len(cost) - dp := make([]int, n+1) + f := make([]int, n+1) for i := 2; i <= n; i++ { - dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]) + f[i] = min(f[i-1]+cost[i-1], f[i-2]+cost[i-2]) } - return dp[n] + return f[n] } ``` ```go func minCostClimbingStairs(cost []int) int { - a, b := 0, 0 - for i := 1; i < len(cost); i++ { - a, b = b, min(a+cost[i-1], b+cost[i]) + var f, g int + for i := 2; i <= n; i++ { + f, g = g, min(f+cost[i-2], g+cost[i-1]) } - return b + return g +} +``` + +### **TypeScript** + +```ts +function minCostClimbingStairs(cost: number[]): number { + const n = cost.length; + const f: number[] = Array(n + 1).fill(0); + for (let i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); + } + return f[n]; +} +``` + +```ts +function minCostClimbingStairs(cost: number[]): number { + let [f, g] = [0, 0]; + for (let i = 2; i <= cost.length; ++i) { + [f, g] = [g, Math.min(f + cost[i - 2], g + cost[i - 1])]; + } + return g; +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let n = cost.len(); + let mut f = vec![0; n + 1]; + for i in 2..=n { + f[i] = std::cmp::min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); + } + f[n] + } +} +``` + +```rust +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let (mut f, mut g) = (0, 0); + for i in 2..=cost.len() { + let gg = std::cmp::min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; + } + g + } } ``` diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp index 4908ed9e240d4..3e83bd9c0896d 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp @@ -1,12 +1,12 @@ -class Solution { -public: - int minCostClimbingStairs(vector& cost) { - int a = 0, b = 0; - for (int i = 1; i < cost.size(); ++i) { - int c = min(a + cost[i - 1], b + cost[i]); - a = b; - b = c; - } - return b; - } +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + int f = 0, g = 0; + for (int i = 2; i <= cost.size(); ++i) { + int gg = min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; + } + return g; + } }; \ No newline at end of file diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.go b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.go index 5eeec3b9006e3..24764a4c60f57 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.go +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.go @@ -1,7 +1,7 @@ func minCostClimbingStairs(cost []int) int { - a, b := 0, 0 - for i := 1; i < len(cost); i++ { - a, b = b, min(a+cost[i-1], b+cost[i]) + var f, g int + for i := 2; i <= n; i++ { + f, g = g, min(f+cost[i-2], g+cost[i-1]) } - return b + return g } \ No newline at end of file diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.java b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.java index 85369621c102e..78e778bcf49d3 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.java +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.java @@ -1,11 +1,11 @@ -class Solution { - public int minCostClimbingStairs(int[] cost) { - int a = 0, b = 0; - for (int i = 1; i < cost.length; ++i) { - int c = Math.min(a + cost[i - 1], b + cost[i]); - a = b; - b = c; - } - return b; - } +class Solution { + public int minCostClimbingStairs(int[] cost) { + int f = 0, g = 0; + for (int i = 2; i <= cost.length; ++i) { + int gg = Math.min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; + } + return g; + } } \ No newline at end of file diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.py b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.py index 5166afc4fd42e..ae51a443d9f8e 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.py +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.py @@ -1,6 +1,6 @@ -class Solution: - def minCostClimbingStairs(self, cost: List[int]) -> int: - a = b = 0 - for i in range(1, len(cost)): - a, b = b, min(a + cost[i - 1], b + cost[i]) - return b +class Solution: + def minCostClimbingStairs(self, cost: List[int]) -> int: + f = g = 0 + for i in range(2, len(cost) + 1): + f, g = g, min(f + cost[i - 2], g + cost[i - 1]) + return g diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.rs b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.rs new file mode 100644 index 0000000000000..5d62ffd744c5c --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.rs @@ -0,0 +1,11 @@ +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let (mut f, mut g) = (0, 0); + for i in 2..=cost.len() { + let gg = std::cmp::min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; + } + g + } +} diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.ts b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.ts index 2b133cba36647..dc38fa0f3da7f 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.ts +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.ts @@ -1,8 +1,7 @@ function minCostClimbingStairs(cost: number[]): number { - let a = 0, - b = 0; - for (let i = 1; i < cost.length; ++i) { - [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + let [f, g] = [0, 0]; + for (let i = 2; i <= cost.length; ++i) { + [f, g] = [g, Math.min(f + cost[i - 2], g + cost[i - 1])]; } - return b; + return g; } diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/README.md b/solution/0700-0799/0747.Largest Number At Least Twice of Others/README.md index 6be746334bf5a..aeb3d49369c01 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/README.md +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/README.md @@ -42,7 +42,13 @@ -遍历数组找到最大值和次大值,最后判断是否满足条件即可。 +**方法一:遍历** + +我们可以遍历数组 $nums$,找到数组中的最大值 $x$ 和第二大的值 $y$,如果 $x \ge 2y$,则返回 $x$ 的下标,否则返回 $-1$。 + +我们也可以先找到数组中的最大值 $x$,同时找到最大值 $x$ 的下标 $k$。然后再遍历一次数组,如果发现 $k$ 以外的元素 $y$ 满足 $x < 2y$,则返回 $-1$。否则遍历结束后返回 $k$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。 @@ -53,15 +59,8 @@ ```python class Solution: def dominantIndex(self, nums: List[int]) -> int: - mx = mid = 0 - ans = -1 - for i, v in enumerate(nums): - if v > mx: - mid, mx = mx, v - ans = i - elif v > mid: - mid = v - return ans if mx >= 2 * mid else -1 + x, y = nlargest(2, nums) + return nums.index(x) if x >= 2 * y else -1 ``` ### **Java** @@ -71,18 +70,19 @@ class Solution: ```java class Solution { public int dominantIndex(int[] nums) { - int mx = 0, mid = 0; - int ans = -1; - for (int i = 0; i < nums.length; ++i) { - if (nums[i] > mx) { - mid = mx; - mx = nums[i]; - ans = i; - } else if (nums[i] > mid) { - mid = nums[i]; + int n = nums.length; + int k = 0; + for (int i = 0; i < n; ++i) { + if (nums[k] < nums[i]) { + k = i; } } - return mx >= mid * 2 ? ans : -1; + for (int i = 0; i < n; ++i) { + if (k != i && nums[k] < nums[i] * 2) { + return -1; + } + } + return k; } } ``` @@ -93,17 +93,19 @@ class Solution { class Solution { public: int dominantIndex(vector& nums) { - int mx = 0, mid = 0; - int ans = 0; - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] > mx) { - mid = mx; - mx = nums[i]; - ans = i; - } else if (nums[i] > mid) - mid = nums[i]; + int n = nums.size(); + int k = 0; + for (int i = 0; i < n; ++i) { + if (nums[k] < nums[i]) { + k = i; + } } - return mx >= mid * 2 ? ans : -1; + for (int i = 0; i < n; ++i) { + if (k != i && nums[k] < nums[i] * 2) { + return -1; + } + } + return k; } }; ``` @@ -112,20 +114,37 @@ public: ```go func dominantIndex(nums []int) int { - mx, mid := 0, 0 - ans := 0 - for i, v := range nums { - if v > mx { - mid, mx = mx, v - ans = i - } else if v > mid { - mid = v + k := 0 + for i, x := range nums { + if nums[k] < x { + k = i } } - if mx >= mid*2 { - return ans + for i, x := range nums { + if k != i && nums[k] < x*2 { + return -1 + } } - return -1 + return k +} +``` + +### **TypeScript** + +```ts +function dominantIndex(nums: number[]): number { + let k = 0; + for (let i = 0; i < nums.length; ++i) { + if (nums[i] > nums[k]) { + k = i; + } + } + for (let i = 0; i < nums.length; ++i) { + if (i !== k && nums[k] < nums[i] * 2) { + return -1; + } + } + return k; } ``` @@ -137,19 +156,18 @@ func dominantIndex(nums []int) int { * @return {number} */ var dominantIndex = function (nums) { - let mx = 0, - mid = 0; - let ans = 0; + let k = 0; + for (let i = 0; i < nums.length; ++i) { + if (nums[i] > nums[k]) { + k = i; + } + } for (let i = 0; i < nums.length; ++i) { - if (nums[i] > mx) { - mid = mx; - mx = nums[i]; - ans = i; - } else if (nums[i] > mid) { - mid = nums[i]; + if (i !== k && nums[k] < nums[i] * 2) { + return -1; } } - return mx >= mid * 2 ? ans : -1; + return k; }; ``` diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/README_EN.md b/solution/0700-0799/0747.Largest Number At Least Twice of Others/README_EN.md index 70756a6655869..098ab39966fff 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/README_EN.md +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/README_EN.md @@ -38,6 +38,14 @@ The index of value 6 is 1, so we return 1. ## Solutions +**Solution 1: Traversal** + +We can traverse the array $nums$ to find the maximum value $x$ and the second largest value $y$ in the array. If $x \ge 2y$, then return the index of $x$, otherwise return $-1$. + +We can also first find the maximum value $x$ in the array and find the index $k$ of the maximum value $x$ at the same time. Then traverse the array again. If we find an element $y$ outside of $k$ that satisfies $x < 2y$, then return $-1$. Otherwise, return $k$ after the traversal ends. + +The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$. + ### **Python3** @@ -45,15 +53,8 @@ The index of value 6 is 1, so we return 1. ```python class Solution: def dominantIndex(self, nums: List[int]) -> int: - mx = mid = 0 - ans = -1 - for i, v in enumerate(nums): - if v > mx: - mid, mx = mx, v - ans = i - elif v > mid: - mid = v - return ans if mx >= 2 * mid else -1 + x, y = nlargest(2, nums) + return nums.index(x) if x >= 2 * y else -1 ``` ### **Java** @@ -61,18 +62,19 @@ class Solution: ```java class Solution { public int dominantIndex(int[] nums) { - int mx = 0, mid = 0; - int ans = -1; - for (int i = 0; i < nums.length; ++i) { - if (nums[i] > mx) { - mid = mx; - mx = nums[i]; - ans = i; - } else if (nums[i] > mid) { - mid = nums[i]; + int n = nums.length; + int k = 0; + for (int i = 0; i < n; ++i) { + if (nums[k] < nums[i]) { + k = i; + } + } + for (int i = 0; i < n; ++i) { + if (k != i && nums[k] < nums[i] * 2) { + return -1; } } - return mx >= mid * 2 ? ans : -1; + return k; } } ``` @@ -83,17 +85,19 @@ class Solution { class Solution { public: int dominantIndex(vector& nums) { - int mx = 0, mid = 0; - int ans = 0; - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] > mx) { - mid = mx; - mx = nums[i]; - ans = i; - } else if (nums[i] > mid) - mid = nums[i]; + int n = nums.size(); + int k = 0; + for (int i = 0; i < n; ++i) { + if (nums[k] < nums[i]) { + k = i; + } + } + for (int i = 0; i < n; ++i) { + if (k != i && nums[k] < nums[i] * 2) { + return -1; + } } - return mx >= mid * 2 ? ans : -1; + return k; } }; ``` @@ -102,20 +106,37 @@ public: ```go func dominantIndex(nums []int) int { - mx, mid := 0, 0 - ans := 0 - for i, v := range nums { - if v > mx { - mid, mx = mx, v - ans = i - } else if v > mid { - mid = v + k := 0 + for i, x := range nums { + if nums[k] < x { + k = i } } - if mx >= mid*2 { - return ans + for i, x := range nums { + if k != i && nums[k] < x*2 { + return -1 + } } - return -1 + return k +} +``` + +### **TypeScript** + +```ts +function dominantIndex(nums: number[]): number { + let k = 0; + for (let i = 0; i < nums.length; ++i) { + if (nums[i] > nums[k]) { + k = i; + } + } + for (let i = 0; i < nums.length; ++i) { + if (i !== k && nums[k] < nums[i] * 2) { + return -1; + } + } + return k; } ``` @@ -127,19 +148,18 @@ func dominantIndex(nums []int) int { * @return {number} */ var dominantIndex = function (nums) { - let mx = 0, - mid = 0; - let ans = 0; + let k = 0; + for (let i = 0; i < nums.length; ++i) { + if (nums[i] > nums[k]) { + k = i; + } + } for (let i = 0; i < nums.length; ++i) { - if (nums[i] > mx) { - mid = mx; - mx = nums[i]; - ans = i; - } else if (nums[i] > mid) { - mid = nums[i]; + if (i !== k && nums[k] < nums[i] * 2) { + return -1; } } - return mx >= mid * 2 ? ans : -1; + return k; }; ``` diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.cpp b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.cpp index 3e00c2f9a9f15..6f58941b1b216 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.cpp +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.cpp @@ -1,16 +1,18 @@ -class Solution { -public: - int dominantIndex(vector& nums) { - int mx = 0, mid = 0; - int ans = 0; - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] > mx) { - mid = mx; - mx = nums[i]; - ans = i; - } else if (nums[i] > mid) - mid = nums[i]; - } - return mx >= mid * 2 ? ans : -1; - } +class Solution { +public: + int dominantIndex(vector& nums) { + int n = nums.size(); + int k = 0; + for (int i = 0; i < n; ++i) { + if (nums[k] < nums[i]) { + k = i; + } + } + for (int i = 0; i < n; ++i) { + if (k != i && nums[k] < nums[i] * 2) { + return -1; + } + } + return k; + } }; \ No newline at end of file diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.go b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.go index 158c453ccf75e..69865c6d7b6d6 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.go +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.go @@ -1,16 +1,14 @@ func dominantIndex(nums []int) int { - mx, mid := 0, 0 - ans := 0 - for i, v := range nums { - if v > mx { - mid, mx = mx, v - ans = i - } else if v > mid { - mid = v + k := 0 + for i, x := range nums { + if nums[k] < x { + k = i } } - if mx >= mid*2 { - return ans + for i, x := range nums { + if k != i && nums[k] < x*2 { + return -1 + } } - return -1 + return k } \ No newline at end of file diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.java b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.java index 5fdb66df1adf7..1ba2fe8e8acbb 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.java +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.java @@ -1,16 +1,17 @@ -class Solution { - public int dominantIndex(int[] nums) { - int mx = Integer.MIN_VALUE, mid = Integer.MIN_VALUE; - int ans = -1; - for (int i = 0; i < nums.length; ++i) { - if (nums[i] > mx) { - mid = mx; - mx = nums[i]; - ans = i; - } else if (nums[i] > mid) { - mid = nums[i]; - } - } - return mx >= mid * 2 ? ans : -1; - } +class Solution { + public int dominantIndex(int[] nums) { + int n = nums.length; + int k = 0; + for (int i = 0; i < n; ++i) { + if (nums[k] < nums[i]) { + k = i; + } + } + for (int i = 0; i < n; ++i) { + if (k != i && nums[k] < nums[i] * 2) { + return -1; + } + } + return k; + } } \ No newline at end of file diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.js b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.js index 81b980dd548bb..78436921e6505 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.js +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.js @@ -3,17 +3,16 @@ * @return {number} */ var dominantIndex = function (nums) { - let mx = 0, - mid = 0; - let ans = 0; + let k = 0; for (let i = 0; i < nums.length; ++i) { - if (nums[i] > mx) { - mid = mx; - mx = nums[i]; - ans = i; - } else if (nums[i] > mid) { - mid = nums[i]; + if (nums[i] > nums[k]) { + k = i; } } - return mx >= mid * 2 ? ans : -1; + for (let i = 0; i < nums.length; ++i) { + if (i !== k && nums[k] < nums[i] * 2) { + return -1; + } + } + return k; }; diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.py b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.py index 3b4e284e4ad0e..3c23b293c01f8 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.py +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.py @@ -1,11 +1,4 @@ -class Solution: - def dominantIndex(self, nums: List[int]) -> int: - mx = mid = 0 - ans = -1 - for i, v in enumerate(nums): - if v > mx: - mid, mx = mx, v - ans = i - elif v > mid: - mid = v - return ans if mx >= 2 * mid else -1 +class Solution: + def dominantIndex(self, nums: List[int]) -> int: + x, y = nlargest(2, nums) + return nums.index(x) if x >= 2 * y else -1 diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.ts b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.ts new file mode 100644 index 0000000000000..22531648076f9 --- /dev/null +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.ts @@ -0,0 +1,14 @@ +function dominantIndex(nums: number[]): number { + let k = 0; + for (let i = 0; i < nums.length; ++i) { + if (nums[i] > nums[k]) { + k = i; + } + } + for (let i = 0; i < nums.length; ++i) { + if (i !== k && nums[k] < nums[i] * 2) { + return -1; + } + } + return k; +}