Skip to content

Commit 8144034

Browse files
committed
feat: add solutions to lc problems
- No.0300.Longest Increasing Subsequence - No.0343.Integer Break - No.0384.Shuffle an Array - No.0583.Delete Operation for Two Strings - No.0658.Find K Closest Elements - No.1143.Longest Common Subsequence
1 parent daa00f5 commit 8144034

File tree

22 files changed

+718
-21
lines changed

22 files changed

+718
-21
lines changed

solution/0300-0399/0300.Longest Increasing Subsequence/README.md

+25-6
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,28 @@
5858

5959
定义 `dp[i]` 为以 `nums[i]` 结尾的最长子序列的长度,`dp[i]` 初始化为 1(`i∈[0, n)`)。即题目求的是 `dp[i]``i ∈[0, n-1]`)的最大值。
6060

61-
状态转移方程为:`dp[i] = max(dp[j]) + 1`,其中 `0≤j<i``nums[j]<nums[i]`
61+
状态转移方程为:`dp[i] = max(dp[j]) + 1`,其中 `0≤j<i``nums[j] < nums[i]`
6262

63-
时间复杂度 O(n²)
63+
时间复杂度:$O(n^{2})$
6464

6565
**方法二:贪心 + 二分查找**
6666

67-
维护一个数组 `d[i]`,表示长度为 i 的最长上升子序列末尾元素的最小值,初始值 `d[1]=nums[0]`
67+
维护一个数组 `d[i]`,表示长度为 i 的最长上升子序列末尾元素的最小值,初始值 `d[1] = nums[0]`
6868

6969
直观上,`d[i]` 是单调递增数组。
7070

7171
证明:假设存在 `d[j] ≥ d[i]`,且 `j < i`,我们考虑从长度为 i 的最长上升子序列的末尾删除 `i - j` 个元素,那么这个序列长度变为 j,且第 j 个元素 `d[j]` 必然小于 `d[i]`,由于前面假设 `d[j] ≥ d[i]`,产生了矛盾,因此数组 d 是单调递增数组。
7272

7373
算法思路:
7474

75-
设当前求出的最长上升子序列的长度为 size,初始 `size=1`,从前往后遍历数组 nums,在遍历到 `nums[i]` 时:
75+
设当前求出的最长上升子序列的长度为 size,初始 `size = 1`,从前往后遍历数组 nums,在遍历到 `nums[i]` 时:
7676

7777
-`nums[i] > d[size]`,则直接将 `nums[i]` 加入到数组 d 的末尾,并且更新 size 自增;
7878
- 否则,在数组 d 中二分查找(前面证明 d 是一个单调递增数组),找到第一个大于等于 nums[i] 的位置 idx,更新 `d[idx] = nums[i]`
7979

8080
最终返回 size。
8181

82-
时间复杂度 O(nlogn)。
82+
时间复杂度:$O(nlogn)$
8383

8484
**方法三:树状数组**
8585

@@ -92,7 +92,7 @@
9292

9393
本题我们使用树状数组 `tree[x]` 来维护以 x 结尾的最长上升子序列的长度。
9494

95-
时间复杂度 O(nlogn)。
95+
时间复杂度:$O(nlogn)$
9696

9797
```python
9898
def update(x, val):
@@ -573,6 +573,25 @@ func lengthOfLIS(nums []int) int {
573573
}
574574
```
575575

576+
### **Rust**
577+
578+
```rust
579+
impl Solution {
580+
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
581+
let n = nums.len();
582+
let mut dp = vec![1; n];
583+
for i in 1..n {
584+
for j in 0..i {
585+
if nums[i] > nums[j] {
586+
dp[i] = dp[i].max(dp[j] + 1);
587+
}
588+
}
589+
}
590+
*dp.iter().max().unwrap()
591+
}
592+
}
593+
```
594+
576595
### **...**
577596

578597
```

solution/0300-0399/0300.Longest Increasing Subsequence/README_EN.md

+19
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,25 @@ func lengthOfLIS(nums []int) int {
506506
}
507507
```
508508

509+
### **Rust**
510+
511+
```rust
512+
impl Solution {
513+
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
514+
let n = nums.len();
515+
let mut dp = vec![1; n];
516+
for i in 1..n {
517+
for j in 0..i {
518+
if nums[i] > nums[j] {
519+
dp[i] = dp[i].max(dp[j] + 1);
520+
}
521+
}
522+
}
523+
*dp.iter().max().unwrap()
524+
}
525+
}
526+
```
527+
509528
### **...**
510529

511530
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let mut dp = vec![1; n];
5+
for i in 1..n {
6+
for j in 0..i {
7+
if nums[i] > nums[j] {
8+
dp[i] = dp[i].max(dp[j] + 1);
9+
}
10+
}
11+
}
12+
*dp.iter().max().unwrap()
13+
}
14+
}

solution/0300-0399/0343.Integer Break/README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41-
动态规划
41+
**方法一:动态规划**
4242

4343
设 dp[i] 表示正整数 n 能获得的最大乘积,初始化 `dp[1] = 1`
4444

@@ -119,6 +119,32 @@ func max(a, b int) int {
119119
}
120120
```
121121

122+
### **C**
123+
124+
```c
125+
int integerBreak(int n) {
126+
if (n < 4) {
127+
return n - 1;
128+
}
129+
int count = (n - 2) / 3;
130+
return pow(3, count) * (n - count * 3);
131+
}
132+
```
133+
134+
### **Rust**
135+
136+
```rust
137+
impl Solution {
138+
pub fn integer_break(n: i32) -> i32 {
139+
if n < 4 {
140+
return n - 1;
141+
}
142+
let count = (n - 2) / 3;
143+
3i32.pow(count as u32) * (n - count * 3)
144+
}
145+
}
146+
```
147+
122148
### **...**
123149

124150
```

solution/0300-0399/0343.Integer Break/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ func max(a, b int) int {
107107
}
108108
```
109109

110+
### **C**
111+
112+
```c
113+
int integerBreak(int n) {
114+
if (n < 4) {
115+
return n - 1;
116+
}
117+
int count = (n - 2) / 3;
118+
return pow(3, count) * (n - count * 3);
119+
}
120+
```
121+
122+
### **Rust**
123+
124+
```rust
125+
impl Solution {
126+
pub fn integer_break(n: i32) -> i32 {
127+
if n < 4 {
128+
return n - 1;
129+
}
130+
let count = (n - 2) / 3;
131+
3i32.pow(count as u32) * (n - count * 3)
132+
}
133+
}
134+
```
135+
110136
### **...**
111137

112138
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int integerBreak(int n) {
2+
if (n < 4) {
3+
return n - 1;
4+
}
5+
int count = (n - 2) / 3;
6+
return pow(3, count) * (n - count * 3);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl Solution {
2+
pub fn integer_break(n: i32) -> i32 {
3+
if n < 4 {
4+
return n - 1;
5+
}
6+
let count = (n - 2) / 3;
7+
3i32.pow(count as u32) * (n - count * 3)
8+
}
9+
}

solution/0300-0399/0384.Shuffle an Array/README.md

+114
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,120 @@ func (this *Solution) Shuffle() []int {
192192
*/
193193
```
194194

195+
### **JavaScript**
196+
197+
```js
198+
/**
199+
* @param {number[]} nums
200+
*/
201+
const Solution = function (nums) {
202+
this.nums = nums || [];
203+
};
204+
205+
/**
206+
* Resets the array to its original configuration and return it.
207+
* @return {number[]}
208+
*/
209+
Solution.prototype.reset = function () {
210+
return this.nums;
211+
};
212+
213+
/**
214+
* Returns a random shuffling of the array.
215+
* @return {number[]}
216+
*/
217+
Solution.prototype.shuffle = function () {
218+
let a = this.nums.slice();
219+
for (let i = 0; i < a.length; i++) {
220+
let rand = Math.floor(Math.random() * (a.length - i)) + i;
221+
let tmp = a[i];
222+
a[i] = a[rand];
223+
a[rand] = tmp;
224+
}
225+
return a;
226+
};
227+
228+
/**
229+
* Your Solution object will be instantiated and called as such:
230+
* var obj = Object.create(Solution).createNew(nums)
231+
* var param_1 = obj.reset()
232+
* var param_2 = obj.shuffle()
233+
*/
234+
```
235+
236+
### **TypeScript**
237+
238+
```ts
239+
class Solution {
240+
private nums: number[];
241+
242+
constructor(nums: number[]) {
243+
this.nums = nums;
244+
}
245+
246+
reset(): number[] {
247+
return this.nums;
248+
}
249+
250+
shuffle(): number[] {
251+
const n = this.nums.length;
252+
const res = [...this.nums];
253+
for (let i = 0; i < n; i++) {
254+
const j = Math.floor(Math.random() * n);
255+
[res[i], res[j]] = [res[j], res[i]];
256+
}
257+
return res;
258+
}
259+
}
260+
261+
/**
262+
* Your Solution object will be instantiated and called as such:
263+
* var obj = new Solution(nums)
264+
* var param_1 = obj.reset()
265+
* var param_2 = obj.shuffle()
266+
*/
267+
```
268+
269+
### **Rust**
270+
271+
```rust
272+
use rand::Rng;
273+
struct Solution {
274+
nums: Vec<i32>,
275+
}
276+
277+
/**
278+
* `&self` means the method takes an immutable reference.
279+
* If you need a mutable reference, change it to `&mut self` instead.
280+
*/
281+
impl Solution {
282+
fn new(nums: Vec<i32>) -> Self {
283+
Self { nums }
284+
}
285+
286+
fn reset(&self) -> Vec<i32> {
287+
self.nums.clone()
288+
}
289+
290+
fn shuffle(&mut self) -> Vec<i32> {
291+
let n = self.nums.len();
292+
let mut res = self.nums.clone();
293+
for i in 0..n {
294+
let j = rand::thread_rng().gen_range(0, n);
295+
res.swap(i, j);
296+
}
297+
res
298+
}
299+
}
300+
301+
/**
302+
* Your Solution object will be instantiated and called as such:
303+
* let obj = Solution::new(nums);
304+
* let ret_1: Vec<i32> = obj.reset();
305+
* let ret_2: Vec<i32> = obj.shuffle();
306+
*/
307+
```
308+
195309
### **...**
196310

197311
```

0 commit comments

Comments
 (0)