Skip to content

Commit 2974106

Browse files
committed
feat: add solutions to lc problem: No.1982
No.1982.Find Array Given Subset Sums
1 parent ec2c823 commit 2974106

File tree

9 files changed

+571
-4
lines changed

9 files changed

+571
-4
lines changed

solution/1000-1099/1096.Brace Expansion II/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
<ul>
1414
<li>如果只给出单一的元素&nbsp;<code>x</code>,那么表达式表示的字符串就只有&nbsp;<code>"x"</code>。<code>R(x) = {x}</code>
15-
1615
<ul>
1716
<li>例如,表达式 <code>"a"</code> 表示字符串 <code>"a"</code>。</li>
1817
<li>而表达式 <code>"w"</code> 就表示字符串 <code>"w"</code>。</li>

solution/1900-1999/1966.Binary Searchable Numbers in an Unsorted Array/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func(sequence, target)
8181
1. 这个元素大于它的左边所有元素,否则,如果左边存在比当前元素大的元素,那么就会被移除,导致无法找到当前元素;
8282
2. 这个元素小于它的右边所有元素,否则,如果右边存在比当前元素小的元素,那么就会被移除,导致无法找到当前元素。
8383

84-
我们创建一个数组 $ok$,其中 $ok[i]$ 表示 $nums[i]$ 是否是可被二分搜索的。初始时$ok[i]$ 都为 $1$。
84+
我们创建一个数组 $ok$,其中 $ok[i]$ 表示 $nums[i]$ 是否是可被二分搜索的。初始时 $ok[i]$ 都为 $1$。
8585

8686
我们先从左到右遍历数组,维护前缀最大值 $mx$,如果当前元素 $x$ 比 $mx$ 小,那么 $x$ 就不是可被二分搜索的,我们将 $ok[i]$ 置为 $0$,否则,我们将 $mx$ 更新为 $x$。
8787

solution/1900-1999/1976.Number of Ways to Arrive at Destination/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
时间复杂度 $O(n^2)$。
6262

63-
> 注意:最短路的长度可能会 超过 32 位整数的范围。
63+
> 注意:最短路的长度可能会超过 32 位整数的范围。
6464
6565
<!-- tabs:start -->
6666

solution/1900-1999/1982.Find Array Given Subset Sums/README.md

+193-1
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,207 @@
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```python
73-
73+
from sortedcontainers import SortedList
74+
75+
76+
class Solution:
77+
def recoverArray(self, n: int, sums: List[int]) -> List[int]:
78+
m = -min(sums)
79+
sl = SortedList(x + m for x in sums)
80+
sl.remove(0)
81+
ans = [sl[0]]
82+
for i in range(1, n):
83+
for j in range(1 << i):
84+
if j >> (i - 1) & 1:
85+
s = sum(ans[k] for k in range(i) if j >> k & 1)
86+
sl.remove(s)
87+
ans.append(sl[0])
88+
for i in range(1 << n):
89+
s = sum(ans[j] for j in range(n) if i >> j & 1)
90+
if s == m:
91+
for j in range(n):
92+
if i >> j & 1:
93+
ans[j] *= -1
94+
break
95+
return ans
7496
```
7597

7698
### **Java**
7799

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

80102
```java
103+
class Solution {
104+
public int[] recoverArray(int n, int[] sums) {
105+
int m = 1 << 30;
106+
for (int x : sums) {
107+
m = Math.min(m, x);
108+
}
109+
m = -m;
110+
TreeMap<Integer, Integer> tm = new TreeMap<>();
111+
for (int x : sums) {
112+
tm.merge(x + m, 1, Integer::sum);
113+
}
114+
int[] ans = new int[n];
115+
if (tm.merge(0, -1, Integer::sum) == 0) {
116+
tm.remove(0);
117+
}
118+
ans[0] = tm.firstKey();
119+
for (int i = 1; i < n; ++i) {
120+
for (int j = 0; j < 1 << i; ++j) {
121+
if ((j >> (i - 1) & 1) == 1) {
122+
int s = 0;
123+
for (int k = 0; k < i; ++k) {
124+
if (((j >> k) & 1) == 1) {
125+
s += ans[k];
126+
}
127+
}
128+
if (tm.merge(s, -1, Integer::sum) == 0) {
129+
tm.remove(s);
130+
}
131+
}
132+
}
133+
ans[i] = tm.firstKey();
134+
}
135+
for (int i = 0; i < 1 << n; ++i) {
136+
int s = 0;
137+
for (int j = 0; j < n; ++j) {
138+
if (((i >> j) & 1) == 1) {
139+
s += ans[j];
140+
}
141+
}
142+
if (s == m) {
143+
for (int j = 0; j < n; ++j) {
144+
if (((i >> j) & 1) == 1) {
145+
ans[j] *= -1;
146+
}
147+
}
148+
break;
149+
}
150+
}
151+
return ans;
152+
}
153+
}
154+
```
155+
156+
### **C++**
157+
158+
```cpp
159+
class Solution {
160+
public:
161+
vector<int> recoverArray(int n, vector<int>& sums) {
162+
int m = *min_element(sums.begin(), sums.end());
163+
m = -m;
164+
multiset<int> st;
165+
for (int x : sums) {
166+
st.insert(x + m);
167+
}
168+
st.erase(st.begin());
169+
vector<int> ans;
170+
ans.push_back(*st.begin());
171+
for (int i = 1; i < n; ++i) {
172+
for (int j = 0; j < 1 << i; ++j) {
173+
if (j >> (i - 1) & 1) {
174+
int s = 0;
175+
for (int k = 0; k < i; ++k) {
176+
if (j >> k & 1) {
177+
s += ans[k];
178+
}
179+
}
180+
st.erase(st.find(s));
181+
}
182+
}
183+
ans.push_back(*st.begin());
184+
}
185+
for (int i = 0; i < 1 << n; ++i) {
186+
int s = 0;
187+
for (int j = 0; j < n; ++j) {
188+
if (i >> j & 1) {
189+
s += ans[j];
190+
}
191+
}
192+
if (s == m) {
193+
for (int j = 0; j < n; ++j) {
194+
if (i >> j & 1) {
195+
ans[j] = -ans[j];
196+
}
197+
}
198+
break;
199+
}
200+
}
201+
return ans;
202+
}
203+
};
204+
```
81205
206+
### **Go**
207+
208+
```go
209+
func recoverArray(n int, sums []int) []int {
210+
m := 0
211+
for _, x := range sums {
212+
m = min(m, x)
213+
}
214+
m = -m
215+
rbt := redblacktree.NewWithIntComparator()
216+
merge := func(key int, value int) {
217+
if v, ok := rbt.Get(key); ok {
218+
nxt := v.(int) + value
219+
if nxt == 0 {
220+
rbt.Remove(key)
221+
} else {
222+
rbt.Put(key, nxt)
223+
}
224+
} else {
225+
rbt.Put(key, value)
226+
}
227+
}
228+
for _, x := range sums {
229+
merge(x+m, 1)
230+
}
231+
ans := make([]int, n)
232+
merge(ans[0], -1)
233+
ans[0] = rbt.Left().Key.(int)
234+
for i := 1; i < n; i++ {
235+
for j := 0; j < 1<<i; j++ {
236+
if j>>(i-1)&1 == 1 {
237+
s := 0
238+
for k := 0; k < i; k++ {
239+
if j>>k&1 == 1 {
240+
s += ans[k]
241+
}
242+
}
243+
merge(s, -1)
244+
}
245+
}
246+
ans[i] = rbt.Left().Key.(int)
247+
}
248+
for i := 0; i < 1<<n; i++ {
249+
s := 0
250+
for j := 0; j < n; j++ {
251+
if i>>j&1 == 1 {
252+
s += ans[j]
253+
}
254+
}
255+
if s == m {
256+
for j := 0; j < n; j++ {
257+
if i>>j&1 == 1 {
258+
ans[j] = -ans[j]
259+
}
260+
}
261+
break
262+
}
263+
}
264+
return ans
265+
266+
}
267+
268+
func min(a, b int) int {
269+
if a < b {
270+
return a
271+
}
272+
return b
273+
}
82274
```
83275

84276
### **...**

0 commit comments

Comments
 (0)