Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.2221,2233 #3971

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added solution/0500-0599/0554.Brick Wall/images/a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 32 additions & 17 deletions solution/2200-2299/2221.Find Triangular Sum of an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们可以直接模拟题目描述的操作,对数组 $\textit{nums}$ 进行 $n - 1$ 轮操作,每轮操作都按照题目描述的规则更新数组 $\textit{nums}$。最后返回数组 $\textit{nums}$ 中剩下的唯一元素即可。

时间复杂度 $O(n^2)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -78,10 +82,9 @@ tags:
```python
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n, 0, -1):
for j in range(i - 1):
nums[j] = (nums[j] + nums[j + 1]) % 10
for k in range(len(nums) - 1, 0, -1):
for i in range(k):
nums[i] = (nums[i] + nums[i + 1]) % 10
return nums[0]
```

Expand All @@ -90,10 +93,9 @@ class Solution:
```java
class Solution {
public int triangularSum(int[] nums) {
int n = nums.length;
for (int i = n; i >= 0; --i) {
for (int j = 0; j < i - 1; ++j) {
nums[j] = (nums[j] + nums[j + 1]) % 10;
for (int k = nums.length - 1; k > 0; --k) {
for (int i = 0; i < k; ++i) {
nums[i] = (nums[i] + nums[i + 1]) % 10;
}
}
return nums[0];
Expand All @@ -107,10 +109,11 @@ class Solution {
class Solution {
public:
int triangularSum(vector<int>& nums) {
int n = nums.size();
for (int i = n; i >= 0; --i)
for (int j = 0; j < i - 1; ++j)
nums[j] = (nums[j] + nums[j + 1]) % 10;
for (int k = nums.size() - 1; k; --k) {
for (int i = 0; i < k; ++i) {
nums[i] = (nums[i] + nums[i + 1]) % 10;
}
}
return nums[0];
}
};
Expand All @@ -120,16 +123,28 @@ public:

```go
func triangularSum(nums []int) int {
n := len(nums)
for i := n; i >= 0; i-- {
for j := 0; j < i-1; j++ {
nums[j] = (nums[j] + nums[j+1]) % 10
for k := len(nums) - 1; k > 0; k-- {
for i := 0; i < k; i++ {
nums[i] = (nums[i] + nums[i+1]) % 10
}
}
return nums[0]
}
```

#### TypeScript

```ts
function triangularSum(nums: number[]): number {
for (let k = nums.length - 1; k; --k) {
for (let i = 0; i < k; ++i) {
nums[i] = (nums[i] + nums[i + 1]) % 10;
}
}
return nums[0];
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ Since there is only one element in nums, the triangular sum is the value of that

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

We can directly simulate the operations described in the problem. Perform $n - 1$ rounds of operations on the array $\textit{nums}$, updating the array $\textit{nums}$ according to the rules described in the problem for each round. Finally, return the only remaining element in the array $\textit{nums}$.

The time complexity is $O(n^2)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -74,10 +78,9 @@ Since there is only one element in nums, the triangular sum is the value of that
```python
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n, 0, -1):
for j in range(i - 1):
nums[j] = (nums[j] + nums[j + 1]) % 10
for k in range(len(nums) - 1, 0, -1):
for i in range(k):
nums[i] = (nums[i] + nums[i + 1]) % 10
return nums[0]
```

Expand All @@ -86,10 +89,9 @@ class Solution:
```java
class Solution {
public int triangularSum(int[] nums) {
int n = nums.length;
for (int i = n; i >= 0; --i) {
for (int j = 0; j < i - 1; ++j) {
nums[j] = (nums[j] + nums[j + 1]) % 10;
for (int k = nums.length - 1; k > 0; --k) {
for (int i = 0; i < k; ++i) {
nums[i] = (nums[i] + nums[i + 1]) % 10;
}
}
return nums[0];
Expand All @@ -103,10 +105,11 @@ class Solution {
class Solution {
public:
int triangularSum(vector<int>& nums) {
int n = nums.size();
for (int i = n; i >= 0; --i)
for (int j = 0; j < i - 1; ++j)
nums[j] = (nums[j] + nums[j + 1]) % 10;
for (int k = nums.size() - 1; k; --k) {
for (int i = 0; i < k; ++i) {
nums[i] = (nums[i] + nums[i + 1]) % 10;
}
}
return nums[0];
}
};
Expand All @@ -116,16 +119,28 @@ public:

```go
func triangularSum(nums []int) int {
n := len(nums)
for i := n; i >= 0; i-- {
for j := 0; j < i-1; j++ {
nums[j] = (nums[j] + nums[j+1]) % 10
for k := len(nums) - 1; k > 0; k-- {
for i := 0; i < k; i++ {
nums[i] = (nums[i] + nums[i+1]) % 10
}
}
return nums[0]
}
```

#### TypeScript

```ts
function triangularSum(nums: number[]): number {
for (let k = nums.length - 1; k; --k) {
for (let i = 0; i < k; ++i) {
nums[i] = (nums[i] + nums[i + 1]) % 10;
}
}
return nums[0];
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Solution {
public:
int triangularSum(vector<int>& nums) {
int n = nums.size();
for (int i = n; i >= 0; --i)
for (int j = 0; j < i - 1; ++j)
nums[j] = (nums[j] + nums[j + 1]) % 10;
for (int k = nums.size() - 1; k; --k) {
for (int i = 0; i < k; ++i) {
nums[i] = (nums[i] + nums[i + 1]) % 10;
}
}
return nums[0];
}
};
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
func triangularSum(nums []int) int {
n := len(nums)
for i := n; i >= 0; i-- {
for j := 0; j < i-1; j++ {
nums[j] = (nums[j] + nums[j+1]) % 10
for k := len(nums) - 1; k > 0; k-- {
for i := 0; i < k; i++ {
nums[i] = (nums[i] + nums[i+1]) % 10
}
}
return nums[0]
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
class Solution {
public int triangularSum(int[] nums) {
int n = nums.length;
for (int i = n; i >= 0; --i) {
for (int j = 0; j < i - 1; ++j) {
nums[j] = (nums[j] + nums[j + 1]) % 10;
for (int k = nums.length - 1; k > 0; --k) {
for (int i = 0; i < k; ++i) {
nums[i] = (nums[i] + nums[i + 1]) % 10;
}
}
return nums[0];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n, 0, -1):
for j in range(i - 1):
nums[j] = (nums[j] + nums[j + 1]) % 10
for k in range(len(nums) - 1, 0, -1):
for i in range(k):
nums[i] = (nums[i] + nums[i + 1]) % 10
return nums[0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function triangularSum(nums: number[]): number {
for (let k = nums.length - 1; k; --k) {
for (let i = 0; i < k; ++i) {
nums[i] = (nums[i] + nums[i + 1]) % 10;
}
}
return nums[0];
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ tags:

### 方法一:贪心 + 优先队列(小根堆)

每次操作,贪心地选择最小的元素进行加 $1$,共进行 $k$ 次操作。最后累乘所有元素得到结果。注意取模操作
根据题目描述,要使得乘积最大,我们需要尽量增大较小的数,因此我们可以使用小根堆来维护数组 $\textit{nums}$。每次从小根堆中取出最小的数,将其增加 $1$,然后重新放回小根堆中。重复这个过程 $k$ 次后,我们将当前小根堆中的所有数相乘,即可得到答案

时间复杂度 $O(n+klogn)$。其中,$n$ 表示 $nums$ 的长度。建堆的时间复杂度为 $O(n)$,每次弹出最小元素进行加 $1$,再放回堆中,时间复杂度为 $O(logn)$,共进行 $k$ 次操作
时间复杂度 $O(k \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度

<!-- tabs:start -->

Expand All @@ -76,31 +76,27 @@ class Solution:
def maximumProduct(self, nums: List[int], k: int) -> int:
heapify(nums)
for _ in range(k):
heappush(nums, heappop(nums) + 1)
ans = 1
heapreplace(nums, nums[0] + 1)
mod = 10**9 + 7
for v in nums:
ans = (ans * v) % mod
return ans
return reduce(lambda x, y: x * y % mod, nums)
```

#### Java

```java
class Solution {
private static final int MOD = (int) 1e9 + 7;

public int maximumProduct(int[] nums, int k) {
PriorityQueue<Integer> q = new PriorityQueue<>();
for (int v : nums) {
q.offer(v);
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int x : nums) {
pq.offer(x);
}
while (k-- > 0) {
q.offer(q.poll() + 1);
pq.offer(pq.poll() + 1);
}
final int mod = (int) 1e9 + 7;
long ans = 1;
while (!q.isEmpty()) {
ans = (ans * q.poll()) % MOD;
for (int x : pq) {
ans = (ans * x) % mod;
}
return (int) ans;
}
Expand All @@ -113,16 +109,22 @@ class Solution {
class Solution {
public:
int maximumProduct(vector<int>& nums, int k) {
int mod = 1e9 + 7;
make_heap(nums.begin(), nums.end(), greater<int>());
while (k--) {
pop_heap(nums.begin(), nums.end(), greater<int>());
++nums.back();
push_heap(nums.begin(), nums.end(), greater<int>());
priority_queue<int, vector<int>, greater<int>> pq;
for (int x : nums) {
pq.push(x);
}
while (k-- > 0) {
int smallest = pq.top();
pq.pop();
pq.push(smallest + 1);
}
const int mod = 1e9 + 7;
long long ans = 1;
for (int v : nums) ans = (ans * v) % mod;
return ans;
while (!pq.empty()) {
ans = (ans * pq.top()) % mod;
pq.pop();
}
return static_cast<int>(ans);
}
};
```
Expand All @@ -137,8 +139,8 @@ func maximumProduct(nums []int, k int) int {
heap.Fix(&h, 0)
}
ans := 1
for _, v := range nums {
ans = (ans * v) % (1e9 + 7)
for _, x := range nums {
ans = (ans * x) % (1e9 + 7)
}
return ans
}
Expand All @@ -149,6 +151,25 @@ func (hp) Push(any) {}
func (hp) Pop() (_ any) { return }
```

#### TypeScript

```ts
function maximumProduct(nums: number[], k: number): number {
const pq = new MinPriorityQueue();
nums.forEach(x => pq.enqueue(x));
while (k--) {
const x = pq.dequeue().element;
pq.enqueue(x + 1);
}
let ans = 1;
const mod = 10 ** 9 + 7;
while (!pq.isEmpty()) {
ans = (ans * pq.dequeue().element) % mod;
}
return ans;
}
```

#### JavaScript

```js
Expand All @@ -158,18 +179,16 @@ func (hp) Pop() (_ any) { return }
* @return {number}
*/
var maximumProduct = function (nums, k) {
const n = nums.length;
let pq = new MinPriorityQueue();
for (let i = 0; i < n; i++) {
pq.enqueue(nums[i]);
}
for (let i = 0; i < k; i++) {
pq.enqueue(pq.dequeue().element + 1);
const pq = new MinPriorityQueue();
nums.forEach(x => pq.enqueue(x));
while (k--) {
const x = pq.dequeue().element;
pq.enqueue(x + 1);
}
let ans = 1;
const limit = 10 ** 9 + 7;
for (let i = 0; i < n; i++) {
ans = (ans * pq.dequeue().element) % limit;
const mod = 10 ** 9 + 7;
while (!pq.isEmpty()) {
ans = (ans * pq.dequeue().element) % mod;
}
return ans;
};
Expand Down
Loading
Loading