Skip to content

Commit 5cbddcf

Browse files
committed
feat: add solutions to lc problem: No.1673
No.1673.Find the Most Competitive Subsequence
1 parent ba74d51 commit 5cbddcf

File tree

14 files changed

+277
-95
lines changed

14 files changed

+277
-95
lines changed

.github/workflows/sync.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.

solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060

6161
那么最终答案就是 $n - \max(left[i] + right[i] - 1)$,其中 $1 \leq i \leq n$,并且 $left[i] \gt 1$ 且 $right[i] \gt 1$。
6262

63+
时间复杂度 $O(n^2)$。
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**

solution/1600-1699/1672.Richest Customer Wealth/README.md

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:求和**
57+
58+
遍历 `accounts`,求出每一行的和,然后求出最大值。
59+
60+
时间复杂度 $O(m\times n)$。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
@@ -62,7 +68,7 @@
6268
```python
6369
class Solution:
6470
def maximumWealth(self, accounts: List[List[int]]) -> int:
65-
return max(sum(account) for account in accounts)
71+
return max(sum(v) for v in accounts)
6672
```
6773

6874
### **Java**
@@ -72,15 +78,15 @@ class Solution:
7278
```java
7379
class Solution {
7480
public int maximumWealth(int[][] accounts) {
75-
int res = 0;
76-
for (int[] account : accounts) {
77-
int t = 0;
78-
for (int money : account) {
79-
t += money;
81+
int ans = 0;
82+
for (var e : accounts) {
83+
int s = 0;
84+
for (int v : e) {
85+
s += v;
8086
}
81-
res = Math.max(res, t);
87+
ans = Math.max(ans, s);
8288
}
83-
return res;
89+
return ans;
8490
}
8591
}
8692
```
@@ -91,10 +97,11 @@ class Solution {
9197
class Solution {
9298
public:
9399
int maximumWealth(vector<vector<int>>& accounts) {
94-
int res = 0;
95-
for (auto& account : accounts)
96-
res = max(res, accumulate(account.begin(), account.end(), 0));
97-
return res;
100+
int ans = 0;
101+
for (auto& v : accounts) {
102+
ans = max(ans, accumulate(v.begin(), v.end(), 0));
103+
}
104+
return ans;
98105
}
99106
};
100107
```
@@ -103,17 +110,17 @@ public:
103110
104111
```go
105112
func maximumWealth(accounts [][]int) int {
106-
res := 0
107-
for _, account := range accounts {
108-
t := 0
109-
for _, money := range account {
110-
t += money
111-
}
112-
if t > res {
113-
res = t
114-
}
115-
}
116-
return res
113+
ans := 0
114+
for _, e := range accounts {
115+
s := 0
116+
for _, v := range e {
117+
s += v
118+
}
119+
if ans < s {
120+
ans = s
121+
}
122+
}
123+
return ans
117124
}
118125
```
119126

solution/1600-1699/1672.Richest Customer Wealth/README_EN.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,23 @@ The 2nd customer is the richest with a wealth of 10.</pre>
5757
```python
5858
class Solution:
5959
def maximumWealth(self, accounts: List[List[int]]) -> int:
60-
return max(sum(account) for account in accounts)
60+
return max(sum(v) for v in accounts)
6161
```
6262

6363
### **Java**
6464

6565
```java
6666
class Solution {
6767
public int maximumWealth(int[][] accounts) {
68-
int res = 0;
69-
for (int[] account : accounts) {
70-
int t = 0;
71-
for (int money : account) {
72-
t += money;
68+
int ans = 0;
69+
for (var e : accounts) {
70+
int s = 0;
71+
for (int v : e) {
72+
s += v;
7373
}
74-
res = Math.max(res, t);
74+
ans = Math.max(ans, s);
7575
}
76-
return res;
76+
return ans;
7777
}
7878
}
7979
```
@@ -84,10 +84,11 @@ class Solution {
8484
class Solution {
8585
public:
8686
int maximumWealth(vector<vector<int>>& accounts) {
87-
int res = 0;
88-
for (auto& account : accounts)
89-
res = max(res, accumulate(account.begin(), account.end(), 0));
90-
return res;
87+
int ans = 0;
88+
for (auto& v : accounts) {
89+
ans = max(ans, accumulate(v.begin(), v.end(), 0));
90+
}
91+
return ans;
9192
}
9293
};
9394
```
@@ -96,17 +97,17 @@ public:
9697
9798
```go
9899
func maximumWealth(accounts [][]int) int {
99-
res := 0
100-
for _, account := range accounts {
101-
t := 0
102-
for _, money := range account {
103-
t += money
104-
}
105-
if t > res {
106-
res = t
107-
}
108-
}
109-
return res
100+
ans := 0
101+
for _, e := range accounts {
102+
s := 0
103+
for _, v := range e {
104+
s += v
105+
}
106+
if ans < s {
107+
ans = s
108+
}
109+
}
110+
return ans
110111
}
111112
```
112113

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class Solution {
22
public:
33
int maximumWealth(vector<vector<int>>& accounts) {
4-
int res = 0;
5-
for (auto& account : accounts)
6-
res = max(res, accumulate(account.begin(), account.end(), 0));
7-
return res;
4+
int ans = 0;
5+
for (auto& v : accounts) {
6+
ans = max(ans, accumulate(v.begin(), v.end(), 0));
7+
}
8+
return ans;
89
}
910
};
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
func maximumWealth(accounts [][]int) int {
2-
res := 0
3-
for _, account := range accounts {
4-
t := 0
5-
for _, money := range account {
6-
t += money
2+
ans := 0
3+
for _, e := range accounts {
4+
s := 0
5+
for _, v := range e {
6+
s += v
77
}
8-
if t > res {
9-
res = t
8+
if ans < s {
9+
ans = s
1010
}
1111
}
12-
return res
12+
return ans
1313
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public int maximumWealth(int[][] accounts) {
3-
int res = 0;
4-
for (int[] account : accounts) {
5-
int t = 0;
6-
for (int money : account) {
7-
t += money;
3+
int ans = 0;
4+
for (var e : accounts) {
5+
int s = 0;
6+
for (int v : e) {
7+
s += v;
88
}
9-
res = Math.max(res, t);
9+
ans = Math.max(ans, s);
1010
}
11-
return res;
11+
return ans;
1212
}
1313
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Solution:
22
def maximumWealth(self, accounts: List[List[int]]) -> int:
3-
return max(sum(account) for account in accounts)
3+
return max(sum(v) for v in accounts)

solution/1600-1699/1673.Find the Most Competitive Subsequence/README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,96 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:栈**
47+
48+
我们从左到右遍历数组 `nums`,维护一个栈 `stk`,遍历过程中,如果当前元素 `nums[i]` 小于栈顶元素,且栈中元素个数加上 $n-i$ 大于 $k$,则将栈顶元素出栈,直到不满足上述条件为止。此时如果栈中元素个数小于 $k$,则将当前元素入栈。
49+
50+
遍历结束后,栈中元素即为所求。
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(k)$。其中 $n$ 为数组 `nums` 的长度。
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**
4957

5058
<!-- 这里可写当前语言的特殊实现逻辑 -->
5159

5260
```python
53-
61+
class Solution:
62+
def mostCompetitive(self, nums: List[int], k: int) -> List[int]:
63+
stk = []
64+
n = len(nums)
65+
for i, v in enumerate(nums):
66+
while stk and stk[-1] > v and len(stk) + n - i > k:
67+
stk.pop()
68+
if len(stk) < k:
69+
stk.append(v)
70+
return stk
5471
```
5572

5673
### **Java**
5774

5875
<!-- 这里可写当前语言的特殊实现逻辑 -->
5976

6077
```java
78+
class Solution {
79+
public int[] mostCompetitive(int[] nums, int k) {
80+
Deque<Integer> stk = new ArrayDeque<>();
81+
int n = nums.length;
82+
for (int i = 0; i < nums.length; ++i) {
83+
while (!stk.isEmpty() && stk.peek() > nums[i] && stk.size() + n - i > k) {
84+
stk.pop();
85+
}
86+
if (stk.size() < k) {
87+
stk.push(nums[i]);
88+
}
89+
}
90+
int[] ans = new int[stk.size()];
91+
for (int i = ans.length - 1; i >= 0; --i) {
92+
ans[i] = stk.pop();
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
vector<int> mostCompetitive(vector<int>& nums, int k) {
105+
vector<int> stk;
106+
int n = nums.size();
107+
for (int i = 0; i < n; ++i) {
108+
while (stk.size() && stk.back() > nums[i] && stk.size() + n - i > k) {
109+
stk.pop_back();
110+
}
111+
if (stk.size() < k) {
112+
stk.push_back(nums[i]);
113+
}
114+
}
115+
return stk;
116+
}
117+
};
118+
```
61119
120+
### **Go**
121+
122+
```go
123+
func mostCompetitive(nums []int, k int) []int {
124+
stk := []int{}
125+
n := len(nums)
126+
for i, v := range nums {
127+
for len(stk) > 0 && stk[len(stk)-1] > v && len(stk)+n-i > k {
128+
stk = stk[:len(stk)-1]
129+
}
130+
if len(stk) < k {
131+
stk = append(stk, v)
132+
}
133+
}
134+
return stk
135+
}
62136
```
63137

64138
### **...**

0 commit comments

Comments
 (0)