Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 66418a9

Browse files
authored
feat: add solutions to lc problems: No.2221,2233 (doocs#3971)
1 parent 2872f88 commit 66418a9

File tree

31 files changed

+285
-183
lines changed

31 files changed

+285
-183
lines changed
29.6 KB
Loading

solution/2200-2299/2221.Find Triangular Sum of an Array/README.md

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ tags:
6969

7070
<!-- solution:start -->
7171

72-
### 方法一
72+
### 方法一:模拟
73+
74+
我们可以直接模拟题目描述的操作,对数组 $\textit{nums}$ 进行 $n - 1$ 轮操作,每轮操作都按照题目描述的规则更新数组 $\textit{nums}$。最后返回数组 $\textit{nums}$ 中剩下的唯一元素即可。
75+
76+
时间复杂度 $O(n^2)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
7377

7478
<!-- tabs:start -->
7579

@@ -78,10 +82,9 @@ tags:
7882
```python
7983
class Solution:
8084
def triangularSum(self, nums: List[int]) -> int:
81-
n = len(nums)
82-
for i in range(n, 0, -1):
83-
for j in range(i - 1):
84-
nums[j] = (nums[j] + nums[j + 1]) % 10
85+
for k in range(len(nums) - 1, 0, -1):
86+
for i in range(k):
87+
nums[i] = (nums[i] + nums[i + 1]) % 10
8588
return nums[0]
8689
```
8790

@@ -90,10 +93,9 @@ class Solution:
9093
```java
9194
class Solution {
9295
public int triangularSum(int[] nums) {
93-
int n = nums.length;
94-
for (int i = n; i >= 0; --i) {
95-
for (int j = 0; j < i - 1; ++j) {
96-
nums[j] = (nums[j] + nums[j + 1]) % 10;
96+
for (int k = nums.length - 1; k > 0; --k) {
97+
for (int i = 0; i < k; ++i) {
98+
nums[i] = (nums[i] + nums[i + 1]) % 10;
9799
}
98100
}
99101
return nums[0];
@@ -107,10 +109,11 @@ class Solution {
107109
class Solution {
108110
public:
109111
int triangularSum(vector<int>& nums) {
110-
int n = nums.size();
111-
for (int i = n; i >= 0; --i)
112-
for (int j = 0; j < i - 1; ++j)
113-
nums[j] = (nums[j] + nums[j + 1]) % 10;
112+
for (int k = nums.size() - 1; k; --k) {
113+
for (int i = 0; i < k; ++i) {
114+
nums[i] = (nums[i] + nums[i + 1]) % 10;
115+
}
116+
}
114117
return nums[0];
115118
}
116119
};
@@ -120,16 +123,28 @@ public:
120123
121124
```go
122125
func triangularSum(nums []int) int {
123-
n := len(nums)
124-
for i := n; i >= 0; i-- {
125-
for j := 0; j < i-1; j++ {
126-
nums[j] = (nums[j] + nums[j+1]) % 10
126+
for k := len(nums) - 1; k > 0; k-- {
127+
for i := 0; i < k; i++ {
128+
nums[i] = (nums[i] + nums[i+1]) % 10
127129
}
128130
}
129131
return nums[0]
130132
}
131133
```
132134

135+
#### TypeScript
136+
137+
```ts
138+
function triangularSum(nums: number[]): number {
139+
for (let k = nums.length - 1; k; --k) {
140+
for (let i = 0; i < k; ++i) {
141+
nums[i] = (nums[i] + nums[i + 1]) % 10;
142+
}
143+
}
144+
return nums[0];
145+
}
146+
```
147+
133148
<!-- tabs:end -->
134149

135150
<!-- solution:end -->

solution/2200-2299/2221.Find Triangular Sum of an Array/README_EN.md

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ Since there is only one element in nums, the triangular sum is the value of that
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Simulation
69+
70+
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}$.
71+
72+
The time complexity is $O(n^2)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
6973

7074
<!-- tabs:start -->
7175

@@ -74,10 +78,9 @@ Since there is only one element in nums, the triangular sum is the value of that
7478
```python
7579
class Solution:
7680
def triangularSum(self, nums: List[int]) -> int:
77-
n = len(nums)
78-
for i in range(n, 0, -1):
79-
for j in range(i - 1):
80-
nums[j] = (nums[j] + nums[j + 1]) % 10
81+
for k in range(len(nums) - 1, 0, -1):
82+
for i in range(k):
83+
nums[i] = (nums[i] + nums[i + 1]) % 10
8184
return nums[0]
8285
```
8386

@@ -86,10 +89,9 @@ class Solution:
8689
```java
8790
class Solution {
8891
public int triangularSum(int[] nums) {
89-
int n = nums.length;
90-
for (int i = n; i >= 0; --i) {
91-
for (int j = 0; j < i - 1; ++j) {
92-
nums[j] = (nums[j] + nums[j + 1]) % 10;
92+
for (int k = nums.length - 1; k > 0; --k) {
93+
for (int i = 0; i < k; ++i) {
94+
nums[i] = (nums[i] + nums[i + 1]) % 10;
9395
}
9496
}
9597
return nums[0];
@@ -103,10 +105,11 @@ class Solution {
103105
class Solution {
104106
public:
105107
int triangularSum(vector<int>& nums) {
106-
int n = nums.size();
107-
for (int i = n; i >= 0; --i)
108-
for (int j = 0; j < i - 1; ++j)
109-
nums[j] = (nums[j] + nums[j + 1]) % 10;
108+
for (int k = nums.size() - 1; k; --k) {
109+
for (int i = 0; i < k; ++i) {
110+
nums[i] = (nums[i] + nums[i + 1]) % 10;
111+
}
112+
}
110113
return nums[0];
111114
}
112115
};
@@ -116,16 +119,28 @@ public:
116119
117120
```go
118121
func triangularSum(nums []int) int {
119-
n := len(nums)
120-
for i := n; i >= 0; i-- {
121-
for j := 0; j < i-1; j++ {
122-
nums[j] = (nums[j] + nums[j+1]) % 10
122+
for k := len(nums) - 1; k > 0; k-- {
123+
for i := 0; i < k; i++ {
124+
nums[i] = (nums[i] + nums[i+1]) % 10
123125
}
124126
}
125127
return nums[0]
126128
}
127129
```
128130

131+
#### TypeScript
132+
133+
```ts
134+
function triangularSum(nums: number[]): number {
135+
for (let k = nums.length - 1; k; --k) {
136+
for (let i = 0; i < k; ++i) {
137+
nums[i] = (nums[i] + nums[i + 1]) % 10;
138+
}
139+
}
140+
return nums[0];
141+
}
142+
```
143+
129144
<!-- tabs:end -->
130145

131146
<!-- solution:end -->
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class Solution {
22
public:
33
int triangularSum(vector<int>& nums) {
4-
int n = nums.size();
5-
for (int i = n; i >= 0; --i)
6-
for (int j = 0; j < i - 1; ++j)
7-
nums[j] = (nums[j] + nums[j + 1]) % 10;
4+
for (int k = nums.size() - 1; k; --k) {
5+
for (int i = 0; i < k; ++i) {
6+
nums[i] = (nums[i] + nums[i + 1]) % 10;
7+
}
8+
}
89
return nums[0];
910
}
10-
};
11+
};
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
func triangularSum(nums []int) int {
2-
n := len(nums)
3-
for i := n; i >= 0; i-- {
4-
for j := 0; j < i-1; j++ {
5-
nums[j] = (nums[j] + nums[j+1]) % 10
2+
for k := len(nums) - 1; k > 0; k-- {
3+
for i := 0; i < k; i++ {
4+
nums[i] = (nums[i] + nums[i+1]) % 10
65
}
76
}
87
return nums[0]
9-
}
8+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Solution {
22
public int triangularSum(int[] nums) {
3-
int n = nums.length;
4-
for (int i = n; i >= 0; --i) {
5-
for (int j = 0; j < i - 1; ++j) {
6-
nums[j] = (nums[j] + nums[j + 1]) % 10;
3+
for (int k = nums.length - 1; k > 0; --k) {
4+
for (int i = 0; i < k; ++i) {
5+
nums[i] = (nums[i] + nums[i + 1]) % 10;
76
}
87
}
98
return nums[0];
109
}
11-
}
10+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution:
22
def triangularSum(self, nums: List[int]) -> int:
3-
n = len(nums)
4-
for i in range(n, 0, -1):
5-
for j in range(i - 1):
6-
nums[j] = (nums[j] + nums[j + 1]) % 10
3+
for k in range(len(nums) - 1, 0, -1):
4+
for i in range(k):
5+
nums[i] = (nums[i] + nums[i + 1]) % 10
76
return nums[0]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function triangularSum(nums: number[]): number {
2+
for (let k = nums.length - 1; k; --k) {
3+
for (let i = 0; i < k; ++i) {
4+
nums[i] = (nums[i] + nums[i + 1]) % 10;
5+
}
6+
}
7+
return nums[0];
8+
}

solution/2200-2299/2233.Maximum Product After K Increments/README.md

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ tags:
6363

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

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

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

7070
<!-- tabs:start -->
7171

@@ -76,31 +76,27 @@ class Solution:
7676
def maximumProduct(self, nums: List[int], k: int) -> int:
7777
heapify(nums)
7878
for _ in range(k):
79-
heappush(nums, heappop(nums) + 1)
80-
ans = 1
79+
heapreplace(nums, nums[0] + 1)
8180
mod = 10**9 + 7
82-
for v in nums:
83-
ans = (ans * v) % mod
84-
return ans
81+
return reduce(lambda x, y: x * y % mod, nums)
8582
```
8683

8784
#### Java
8885

8986
```java
9087
class Solution {
91-
private static final int MOD = (int) 1e9 + 7;
92-
9388
public int maximumProduct(int[] nums, int k) {
94-
PriorityQueue<Integer> q = new PriorityQueue<>();
95-
for (int v : nums) {
96-
q.offer(v);
89+
PriorityQueue<Integer> pq = new PriorityQueue<>();
90+
for (int x : nums) {
91+
pq.offer(x);
9792
}
9893
while (k-- > 0) {
99-
q.offer(q.poll() + 1);
94+
pq.offer(pq.poll() + 1);
10095
}
96+
final int mod = (int) 1e9 + 7;
10197
long ans = 1;
102-
while (!q.isEmpty()) {
103-
ans = (ans * q.poll()) % MOD;
98+
for (int x : pq) {
99+
ans = (ans * x) % mod;
104100
}
105101
return (int) ans;
106102
}
@@ -113,16 +109,22 @@ class Solution {
113109
class Solution {
114110
public:
115111
int maximumProduct(vector<int>& nums, int k) {
116-
int mod = 1e9 + 7;
117-
make_heap(nums.begin(), nums.end(), greater<int>());
118-
while (k--) {
119-
pop_heap(nums.begin(), nums.end(), greater<int>());
120-
++nums.back();
121-
push_heap(nums.begin(), nums.end(), greater<int>());
112+
priority_queue<int, vector<int>, greater<int>> pq;
113+
for (int x : nums) {
114+
pq.push(x);
115+
}
116+
while (k-- > 0) {
117+
int smallest = pq.top();
118+
pq.pop();
119+
pq.push(smallest + 1);
122120
}
121+
const int mod = 1e9 + 7;
123122
long long ans = 1;
124-
for (int v : nums) ans = (ans * v) % mod;
125-
return ans;
123+
while (!pq.empty()) {
124+
ans = (ans * pq.top()) % mod;
125+
pq.pop();
126+
}
127+
return static_cast<int>(ans);
126128
}
127129
};
128130
```
@@ -137,8 +139,8 @@ func maximumProduct(nums []int, k int) int {
137139
heap.Fix(&h, 0)
138140
}
139141
ans := 1
140-
for _, v := range nums {
141-
ans = (ans * v) % (1e9 + 7)
142+
for _, x := range nums {
143+
ans = (ans * x) % (1e9 + 7)
142144
}
143145
return ans
144146
}
@@ -149,6 +151,25 @@ func (hp) Push(any) {}
149151
func (hp) Pop() (_ any) { return }
150152
```
151153

154+
#### TypeScript
155+
156+
```ts
157+
function maximumProduct(nums: number[], k: number): number {
158+
const pq = new MinPriorityQueue();
159+
nums.forEach(x => pq.enqueue(x));
160+
while (k--) {
161+
const x = pq.dequeue().element;
162+
pq.enqueue(x + 1);
163+
}
164+
let ans = 1;
165+
const mod = 10 ** 9 + 7;
166+
while (!pq.isEmpty()) {
167+
ans = (ans * pq.dequeue().element) % mod;
168+
}
169+
return ans;
170+
}
171+
```
172+
152173
#### JavaScript
153174

154175
```js
@@ -158,18 +179,16 @@ func (hp) Pop() (_ any) { return }
158179
* @return {number}
159180
*/
160181
var maximumProduct = function (nums, k) {
161-
const n = nums.length;
162-
let pq = new MinPriorityQueue();
163-
for (let i = 0; i < n; i++) {
164-
pq.enqueue(nums[i]);
165-
}
166-
for (let i = 0; i < k; i++) {
167-
pq.enqueue(pq.dequeue().element + 1);
182+
const pq = new MinPriorityQueue();
183+
nums.forEach(x => pq.enqueue(x));
184+
while (k--) {
185+
const x = pq.dequeue().element;
186+
pq.enqueue(x + 1);
168187
}
169188
let ans = 1;
170-
const limit = 10 ** 9 + 7;
171-
for (let i = 0; i < n; i++) {
172-
ans = (ans * pq.dequeue().element) % limit;
189+
const mod = 10 ** 9 + 7;
190+
while (!pq.isEmpty()) {
191+
ans = (ans * pq.dequeue().element) % mod;
173192
}
174193
return ans;
175194
};

0 commit comments

Comments
 (0)