Skip to content

Commit a00c9f1

Browse files
committed
feat: add No.1700,1790
1 parent 1d40db5 commit a00c9f1

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# 1700. Number of Students Unable to Eat Lunch
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Stack, Queue, Simulation.
5+
- Similar Questions: Time Needed to Buy Tickets.
6+
7+
## Problem
8+
9+
The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers `0` and `1` respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.
10+
11+
The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a **stack**. At each step:
12+
13+
14+
15+
- If the student at the front of the queue **prefers** the sandwich on the top of the stack, they will **take it** and leave the queue.
16+
17+
- Otherwise, they will **leave it** and go to the queue's end.
18+
19+
20+
This continues until none of the queue students want to take the top sandwich and are thus unable to eat.
21+
22+
You are given two integer arrays `students` and `sandwiches` where `sandwiches[i]` is the type of the `i​​​​​​th` sandwich in the stack (`i = 0` is the top of the stack) and `students[j]` is the preference of the `j​​​​​​th` student in the initial queue (`j = 0` is the front of the queue). Return **the number of students that are unable to eat.**
23+
24+
 
25+
Example 1:
26+
27+
```
28+
Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
29+
Output: 0
30+
Explanation:
31+
- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
32+
- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
33+
- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
34+
- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
35+
- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
36+
- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
37+
- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
38+
- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
39+
Hence all students are able to eat.
40+
```
41+
42+
Example 2:
43+
44+
```
45+
Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
46+
Output: 3
47+
```
48+
49+
 
50+
**Constraints:**
51+
52+
53+
54+
- `1 <= students.length, sandwiches.length <= 100`
55+
56+
- `students.length == sandwiches.length`
57+
58+
- `sandwiches[i]` is `0` or `1`.
59+
60+
- `students[i]` is `0` or `1`.
61+
62+
63+
64+
## Solution
65+
66+
```javascript
67+
/**
68+
* @param {number[]} students
69+
* @param {number[]} sandwiches
70+
* @return {number}
71+
*/
72+
var countStudents = function(students, sandwiches) {
73+
var numOfOnes = 0;
74+
var numOfZeros = 0;
75+
for (var i = 0; i < students.length; i++) {
76+
if (students[i] === 1) numOfOnes++;
77+
else numOfZeros++;
78+
}
79+
for (var j = 0; j < sandwiches.length; j++) {
80+
if (sandwiches[j] === 1) {
81+
if (numOfOnes > 0) numOfOnes--;
82+
else break;
83+
} else {
84+
if (numOfZeros > 0) numOfZeros--;
85+
else break;
86+
}
87+
}
88+
return numOfOnes + numOfZeros;
89+
};
90+
```
91+
92+
**Explain:**
93+
94+
nope.
95+
96+
**Complexity:**
97+
98+
* Time complexity : O(n).
99+
* Space complexity : O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 1790. Check if One String Swap Can Make Strings Equal
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Hash Table, String, Counting.
5+
- Similar Questions: Buddy Strings, Make Number of Distinct Characters Equal.
6+
7+
## Problem
8+
9+
You are given two strings `s1` and `s2` of equal length. A **string swap** is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.
10+
11+
Return `true` **if it is possible to make both strings equal by performing **at most one string swap **on **exactly one** of the strings. **Otherwise, return `false`.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: s1 = "bank", s2 = "kanb"
18+
Output: true
19+
Explanation: For example, swap the first character with the last character of s2 to make "bank".
20+
```
21+
22+
Example 2:
23+
24+
```
25+
Input: s1 = "attack", s2 = "defend"
26+
Output: false
27+
Explanation: It is impossible to make them equal with one string swap.
28+
```
29+
30+
Example 3:
31+
32+
```
33+
Input: s1 = "kelb", s2 = "kelb"
34+
Output: true
35+
Explanation: The two strings are already equal, so no string swap operation is required.
36+
```
37+
38+
 
39+
**Constraints:**
40+
41+
42+
43+
- `1 <= s1.length, s2.length <= 100`
44+
45+
- `s1.length == s2.length`
46+
47+
- `s1` and `s2` consist of only lowercase English letters.
48+
49+
50+
51+
## Solution
52+
53+
```javascript
54+
/**
55+
* @param {string} s1
56+
* @param {string} s2
57+
* @return {boolean}
58+
*/
59+
var areAlmostEqual = function(s1, s2) {
60+
var diffs = [];
61+
for (var i = 0; i < s1.length; i++) {
62+
if (s1[i] !== s2[i]) diffs.push(i);
63+
}
64+
return diffs.length === 0 || (
65+
diffs.length === 2 &&
66+
s1[diffs[0]] === s2[diffs[1]] &&
67+
s1[diffs[1]] === s2[diffs[0]]
68+
);
69+
};
70+
```
71+
72+
**Explain:**
73+
74+
nope.
75+
76+
**Complexity:**
77+
78+
* Time complexity : O(n).
79+
* Space complexity : O(n).

0 commit comments

Comments
 (0)