Skip to content

Commit e45dc3f

Browse files
committed
add: [LC-4] accepted in C++ and JS
1 parent fbf69e3 commit e45dc3f

File tree

7 files changed

+123
-11
lines changed

7 files changed

+123
-11
lines changed

LC-1005/LC-1005.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Solution {
1212
* 4. If there still have K (i.e., K > 0), determine whether the K is odd or even
1313
* - If K is odd, then only change the first element into negative
1414
* - If K is even, then no change
15-
* 5. Sum up the array
15+
* 5. Sum up the array
1616
*/
1717
int largestSumAfterKNegations(vector<int>& A, int K) {
1818
int i = 0, sum = 0;

LC-1528/LC-1528.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#include <string>
2+
#include <vector>
3+
4+
using namespace std;
5+
16
class Solution {
27
public:
38
/**
@@ -6,14 +11,14 @@ class Solution {
611
* 2. Sorting and concatenate to new string
712
*/
813
string restoreString(string s, vector<int>& indices) {
9-
vector<pair<char, int>> pairs;
14+
vector< pair<char, int> > pairs;
1015
for (int i = 0; i < indices.size(); ++i) {
1116
pairs.push_back(make_pair(s[i], indices[i]));
1217
}
13-
18+
1419
// Sorting
1520
sort(pairs.begin(), pairs.end(), compare);
16-
21+
1722
// Concatenation
1823
string result = "";
1924
for (int i = 0; i < pairs.size(); ++i) {

LC-3/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ Given a string, find the length of the longest substring without repeating chara
99

1010
```
1111
Input: "abcabcbb"
12-
Output: 3
13-
Explanation:
14-
The answer is "abc", with the length of 3.
12+
Output: 3
13+
Explanation:
14+
The answer is "abc", with the length of 3.
1515
```
1616

1717
```
1818
Input: "bbbbb"
1919
Output: 1
20-
Explanation:
20+
Explanation:
2121
The answer is "b", with the length of 1.
2222
```
2323

2424
```
2525
Input: "pwwkew"
2626
Output: 3
27-
Explanation:
28-
The answer is "wke", with the length of 3.
27+
Explanation:
28+
The answer is "wke", with the length of 3.
2929
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
3030
```
3131

LC-4/LC-4.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <vector>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
/**
9+
* Concepts: Merging and Sorting
10+
* 1. Merge two arrays into one array
11+
* 2. Sorting
12+
* 3. Return median of merged array
13+
*/
14+
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
15+
int nums2Len = nums2.size();
16+
for (int i = 0; i < nums2Len; ++i) {
17+
nums1.push_back(nums2[i]);
18+
}
19+
20+
sort(nums1.begin(), nums1.end());
21+
22+
int nums1Len = nums1.size();
23+
if (nums1Len % 2 == 0) {
24+
double a = nums1[nums1Len / 2 - 1];
25+
double b = nums1[nums1Len / 2];
26+
return (a + b) / 2;
27+
}
28+
return nums1[nums1Len / 2];
29+
}
30+
};

LC-4/LC-4.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number}
5+
*/
6+
7+
var findMedianSortedArrays = function(nums1, nums2) {
8+
/**
9+
* Concepts: Merging and Sorting
10+
* 1. Merge two arrays into one array
11+
* 2. Sorting
12+
* 3. Return median of merged array
13+
*/
14+
const nums2Len = nums2.length;
15+
nums1 = nums1.concat(nums2);
16+
nums1 = nums1.sort((a, b) => a - b);
17+
18+
const nums1Len = nums1.length;
19+
if (nums1Len % 2 === 0) {
20+
return (nums1[Math.floor(nums1Len / 2) - 1] + nums1[Math.floor(nums1Len / 2)]) / 2;
21+
}
22+
return nums1[Math.floor(nums1Len / 2)];
23+
};

LC-4/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# LC-4 - Median of Two Sorted Arrays
2+
3+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
4+
5+
The overall run time complexity should be `O(log (m+n))`.
6+
7+
---
8+
## Examples
9+
10+
```
11+
Input: nums1 = [1,3], nums2 = [2]
12+
Output: 2.00000
13+
Explanation:
14+
merged array = [1,2,3] and median is 2.
15+
```
16+
17+
```
18+
Input: nums1 = [1,2], nums2 = [3,4]
19+
Output: 2.50000
20+
Explanation:
21+
merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
22+
```
23+
24+
```
25+
Input: nums1 = [0,0], nums2 = [0,0]
26+
Output: 0.00000
27+
```
28+
29+
```
30+
Input: nums1 = [], nums2 = [1]
31+
Output: 1.00000
32+
```
33+
34+
```
35+
Input: nums1 = [2], nums2 = []
36+
Output: 2.00000
37+
```
38+
39+
---
40+
## Notes
41+
42+
- `nums1.length == m`
43+
- `nums2.length == n`
44+
- `0 <= m <= 1000`
45+
- `0 <= n <= 1000`
46+
- `1 <= m + n <= 2000`
47+
- `-106 <= nums1[i], nums2[i] <= 106`
48+
49+
---
50+
## Solutions
51+
52+
1. Merging and Sorting
53+
- Time complexity: $O(n \log(n))$
54+
- Space complexity: $O(1)$

LC-877/LC-877-1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Solution {
1717
vector< vector<int> > DP(n, vector<int>(n, 0));
1818
for (int i = 0; i < n; ++i)
1919
DP[i][i] = piles[i];
20-
20+
2121
for (int d = 1; d < n; ++d) {
2222
for (int i = 0; i < n - d; ++i)
2323
DP[i][i + d] = max(piles[i] - DP[i + 1][i + d], piles[i + d] - DP[i][i + d - 1]);

0 commit comments

Comments
 (0)