Skip to content

Commit bff2d2c

Browse files
committed
feat: add 1422,1637
1 parent d486d52 commit bff2d2c

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 1422. Maximum Score After Splitting a String
2+
3+
- Difficulty: Easy.
4+
- Related Topics: String.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Given a string `s` of zeros and ones, **return the maximum score after splitting the string into two **non-empty** substrings** (i.e. **left** substring and **right** substring).
10+
11+
The score after splitting a string is the number of **zeros** in the **left** substring plus the number of **ones** in the **right** substring.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: s = "011101"
18+
Output: 5
19+
Explanation:
20+
All possible ways of splitting s into two non-empty substrings are:
21+
left = "0" and right = "11101", score = 1 + 4 = 5
22+
left = "01" and right = "1101", score = 1 + 3 = 4
23+
left = "011" and right = "101", score = 1 + 2 = 3
24+
left = "0111" and right = "01", score = 1 + 1 = 2
25+
left = "01110" and right = "1", score = 2 + 1 = 3
26+
```
27+
28+
Example 2:
29+
30+
```
31+
Input: s = "00111"
32+
Output: 5
33+
Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5
34+
```
35+
36+
Example 3:
37+
38+
```
39+
Input: s = "1111"
40+
Output: 3
41+
```
42+
43+
 
44+
**Constraints:**
45+
46+
47+
48+
- `2 <= s.length <= 500`
49+
50+
- The string `s` consists of characters `'0'` and `'1'` only.
51+
52+
53+
54+
## Solution
55+
56+
```javascript
57+
/**
58+
* @param {string} s
59+
* @return {number}
60+
*/
61+
var maxScore = function(s) {
62+
var numOfOnes = 0;
63+
var numOfZeros = 0;
64+
var max = 0;
65+
for (var i = 0; i < s.length; i++) {
66+
s[i] === '1' && numOfOnes++;
67+
}
68+
for (var j = 0; j < s.length - 1; j++) {
69+
s[j] === '0' && numOfZeros++;
70+
s[j] === '1' && numOfOnes--;
71+
max = Math.max(numOfOnes + numOfZeros, max);
72+
}
73+
return max;
74+
};
75+
```
76+
77+
**Explain:**
78+
79+
nope.
80+
81+
**Complexity:**
82+
83+
* Time complexity : O(n).
84+
* Space complexity : O(0).
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 1637. Widest Vertical Area Between Two Points Containing No Points
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Sorting.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Given `n` `points` on a 2D plane where `points[i] = [xi, yi]`, Return** the **widest vertical area** between two points such that no points are inside the area.**
10+
11+
A **vertical area** is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The **widest vertical area** is the one with the maximum width.
12+
13+
Note that points **on the edge** of a vertical area **are not** considered included in the area.
14+
15+
 
16+
Example 1:
17+
18+
![](https://assets.leetcode.com/uploads/2020/09/19/points3.png)
19+
20+
```
21+
Input: points = [[8,7],[9,9],[7,4],[9,7]]
22+
Output: 1
23+
Explanation: Both the red and the blue area are optimal.
24+
```
25+
26+
Example 2:
27+
28+
```
29+
Input: points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
30+
Output: 3
31+
```
32+
33+
 
34+
**Constraints:**
35+
36+
37+
38+
- `n == points.length`
39+
40+
- `2 <= n <= 105`
41+
42+
- `points[i].length == 2`
43+
44+
- `0 <= xi, yi <= 109`
45+
46+
47+
48+
## Solution
49+
50+
```javascript
51+
/**
52+
* @param {number[][]} points
53+
* @return {number}
54+
*/
55+
var maxWidthOfVerticalArea = function(points) {
56+
var maxGap = 0;
57+
points.sort((a, b) => a[0] - b[0]);
58+
for (var i = 1; i < points.length; i++) {
59+
maxGap = Math.max(maxGap, points[i][0] - points[i - 1][0]);
60+
}
61+
return maxGap;
62+
};
63+
```
64+
65+
**Explain:**
66+
67+
nope.
68+
69+
**Complexity:**
70+
71+
* Time complexity : O(n).
72+
* Space complexity : O(1).

0 commit comments

Comments
 (0)