Skip to content

Commit d88b70a

Browse files
committed
feat: solve No.706,2038,1512
1 parent 00323ba commit d88b70a

File tree

3 files changed

+301
-0
lines changed

3 files changed

+301
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 1512. Number of Good Pairs
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Hash Table, Math, Counting.
5+
- Similar Questions: Number of Pairs of Interchangeable Rectangles, Substrings That Begin and End With the Same Letter.
6+
7+
## Problem
8+
9+
Given an array of integers `nums`, return **the number of **good pairs****.
10+
11+
A pair `(i, j)` is called **good** if `nums[i] == nums[j]` and `i` < `j`.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: nums = [1,2,3,1,1,3]
18+
Output: 4
19+
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
20+
```
21+
22+
Example 2:
23+
24+
```
25+
Input: nums = [1,1,1,1]
26+
Output: 6
27+
Explanation: Each pair in the array are good.
28+
```
29+
30+
Example 3:
31+
32+
```
33+
Input: nums = [1,2,3]
34+
Output: 0
35+
```
36+
37+
 
38+
**Constraints:**
39+
40+
41+
42+
- `1 <= nums.length <= 100`
43+
44+
- `1 <= nums[i] <= 100`
45+
46+
47+
48+
## Solution
49+
50+
```javascript
51+
/**
52+
* @param {number[]} nums
53+
* @return {number}
54+
*/
55+
var numIdenticalPairs = function(nums) {
56+
var map = Array(100).fill(0);
57+
for (var i = 0; i < nums.length; i++) {
58+
map[nums[i] - 1]++;
59+
}
60+
var res = 0;
61+
for (var j = 0; j < map.length; j++) {
62+
res += helper(map[j] - 1);
63+
}
64+
return res;
65+
};
66+
67+
var helper = function(num) {
68+
return num * (1 + num) / 2;
69+
};
70+
```
71+
72+
**Explain:**
73+
74+
nope.
75+
76+
**Complexity:**
77+
78+
* Time complexity : O(n).
79+
* Space complexity : O(n).
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# 2038. Remove Colored Pieces if Both Neighbors are the Same Color
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Math, String, Greedy, Game Theory.
5+
- Similar Questions: Longest Subarray With Maximum Bitwise AND.
6+
7+
## Problem
8+
9+
There are `n` pieces arranged in a line, and each piece is colored either by `'A'` or by `'B'`. You are given a string `colors` of length `n` where `colors[i]` is the color of the `ith` piece.
10+
11+
Alice and Bob are playing a game where they take **alternating turns** removing pieces from the line. In this game, Alice moves** first**.
12+
13+
14+
15+
- Alice is only allowed to remove a piece colored `'A'` if **both its neighbors** are also colored `'A'`. She is **not allowed** to remove pieces that are colored `'B'`.
16+
17+
- Bob is only allowed to remove a piece colored `'B'` if **both its neighbors** are also colored `'B'`. He is **not allowed** to remove pieces that are colored `'A'`.
18+
19+
- Alice and Bob **cannot** remove pieces from the edge of the line.
20+
21+
- If a player cannot make a move on their turn, that player **loses** and the other player **wins**.
22+
23+
24+
Assuming Alice and Bob play optimally, return `true`** if Alice wins, or return **`false`** if Bob wins**.
25+
26+
 
27+
Example 1:
28+
29+
```
30+
Input: colors = "AAABABB"
31+
Output: true
32+
Explanation:
33+
AAABABB -> AABABB
34+
Alice moves first.
35+
She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
36+
37+
Now it's Bob's turn.
38+
Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
39+
Thus, Alice wins, so return true.
40+
```
41+
42+
Example 2:
43+
44+
```
45+
Input: colors = "AA"
46+
Output: false
47+
Explanation:
48+
Alice has her turn first.
49+
There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
50+
Thus, Bob wins, so return false.
51+
```
52+
53+
Example 3:
54+
55+
```
56+
Input: colors = "ABBBBBBBAAA"
57+
Output: false
58+
Explanation:
59+
ABBBBBBBAAA -> ABBBBBBBAA
60+
Alice moves first.
61+
Her only option is to remove the second to last 'A' from the right.
62+
63+
ABBBBBBBAA -> ABBBBBBAA
64+
Next is Bob's turn.
65+
He has many options for which 'B' piece to remove. He can pick any.
66+
67+
On Alice's second turn, she has no more pieces that she can remove.
68+
Thus, Bob wins, so return false.
69+
```
70+
71+
 
72+
**Constraints:**
73+
74+
75+
76+
- `1 <= colors.length <= 105`
77+
78+
- `colors` consists of only the letters `'A'` and `'B'`
79+
80+
81+
82+
## Solution
83+
84+
```javascript
85+
/**
86+
* @param {string} colors
87+
* @return {boolean}
88+
*/
89+
var winnerOfGame = function(colors) {
90+
var i = 0;
91+
var a = 0;
92+
var b = 0;
93+
while (i < colors.length) {
94+
var j = i;
95+
while (colors[j] === colors[i]) j++;
96+
if (j - i >= 3) {
97+
if (colors[i] === 'A') {
98+
a += j - i - 2;
99+
} else {
100+
b += j - i - 2;
101+
}
102+
}
103+
i = j;
104+
}
105+
return a > b;
106+
};
107+
```
108+
109+
**Explain:**
110+
111+
nope.
112+
113+
**Complexity:**
114+
115+
* Time complexity : O(n).
116+
* Space complexity : O(1).

701-800/706. Design HashMap.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# 706. Design HashMap
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Hash Table, Linked List, Design, Hash Function.
5+
- Similar Questions: Design HashSet, Design Skiplist.
6+
7+
## Problem
8+
9+
Design a HashMap without using any built-in hash table libraries.
10+
11+
Implement the `MyHashMap` class:
12+
13+
14+
15+
- `MyHashMap()` initializes the object with an empty map.
16+
17+
- `void put(int key, int value)` inserts a `(key, value)` pair into the HashMap. If the `key` already exists in the map, update the corresponding `value`.
18+
19+
- `int get(int key)` returns the `value` to which the specified `key` is mapped, or `-1` if this map contains no mapping for the `key`.
20+
21+
- `void remove(key)` removes the `key` and its corresponding `value` if the map contains the mapping for the `key`.
22+
23+
24+
 
25+
Example 1:
26+
27+
```
28+
Input
29+
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
30+
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
31+
Output
32+
[null, null, null, 1, -1, null, 1, null, -1]
33+
34+
Explanation
35+
MyHashMap myHashMap = new MyHashMap();
36+
myHashMap.put(1, 1); // The map is now [[1,1]]
37+
myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
38+
myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]]
39+
myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
40+
myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
41+
myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]]
42+
myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
43+
myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]]
44+
```
45+
46+
 
47+
**Constraints:**
48+
49+
50+
51+
- `0 <= key, value <= 106`
52+
53+
- At most `104` calls will be made to `put`, `get`, and `remove`.
54+
55+
56+
57+
## Solution
58+
59+
```javascript
60+
61+
var MyHashMap = function() {
62+
this.map = [];
63+
};
64+
65+
/**
66+
* @param {number} key
67+
* @param {number} value
68+
* @return {void}
69+
*/
70+
MyHashMap.prototype.put = function(key, value) {
71+
this.map[key] = value;
72+
};
73+
74+
/**
75+
* @param {number} key
76+
* @return {number}
77+
*/
78+
MyHashMap.prototype.get = function(key) {
79+
return this.map[key] ?? -1;
80+
};
81+
82+
/**
83+
* @param {number} key
84+
* @return {void}
85+
*/
86+
MyHashMap.prototype.remove = function(key) {
87+
this.map[key] = undefined;
88+
};
89+
90+
/**
91+
* Your MyHashMap object will be instantiated and called as such:
92+
* var obj = new MyHashMap()
93+
* obj.put(key,value)
94+
* var param_2 = obj.get(key)
95+
* obj.remove(key)
96+
*/
97+
```
98+
99+
**Explain:**
100+
101+
nope.
102+
103+
**Complexity:**
104+
105+
* Time complexity : O(1).
106+
* Space complexity : O(1).

0 commit comments

Comments
 (0)