Skip to content

Commit 38c51ac

Browse files
committed
feat: solve No.2610,478
1 parent 29b3616 commit 38c51ac

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 2610. Convert an Array Into a 2D Array With Conditions
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Hash Table.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given an integer array `nums`. You need to create a 2D array from `nums` satisfying the following conditions:
10+
11+
12+
13+
- The 2D array should contain **only** the elements of the array `nums`.
14+
15+
- Each row in the 2D array contains **distinct** integers.
16+
17+
- The number of rows in the 2D array should be **minimal**.
18+
19+
20+
Return **the resulting array**. If there are multiple answers, return any of them.
21+
22+
**Note** that the 2D array can have a different number of elements on each row.
23+
24+
 
25+
Example 1:
26+
27+
```
28+
Input: nums = [1,3,4,1,2,3,1]
29+
Output: [[1,3,4,2],[1,3],[1]]
30+
Explanation: We can create a 2D array that contains the following rows:
31+
- 1,3,4,2
32+
- 1,3
33+
- 1
34+
All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer.
35+
It can be shown that we cannot have less than 3 rows in a valid array.
36+
```
37+
38+
Example 2:
39+
40+
```
41+
Input: nums = [1,2,3,4]
42+
Output: [[4,3,2,1]]
43+
Explanation: All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.
44+
```
45+
46+
 
47+
**Constraints:**
48+
49+
50+
51+
- `1 <= nums.length <= 200`
52+
53+
- `1 <= nums[i] <= nums.length`
54+
55+
56+
57+
## Solution
58+
59+
```javascript
60+
/**
61+
* @param {number[]} nums
62+
* @return {number[][]}
63+
*/
64+
var findMatrix = function(nums) {
65+
var numMap = Array(nums.length + 1).fill(0);
66+
var res = [];
67+
for (var i = 0; i < nums.length; i++) {
68+
var num = nums[i];
69+
var row = numMap[nums[i]];
70+
if (!res[row]) {
71+
res.push([]);
72+
}
73+
res[row].push(num);
74+
numMap[num]++;
75+
}
76+
return res;
77+
};
78+
```
79+
80+
**Explain:**
81+
82+
nope.
83+
84+
**Complexity:**
85+
86+
* Time complexity : O(n).
87+
* Space complexity : O(n).
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 478. Generate Random Point in a Circle
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Math, Geometry, Rejection Sampling, Randomized.
5+
- Similar Questions: Random Point in Non-overlapping Rectangles.
6+
7+
## Problem
8+
9+
Given the radius and the position of the center of a circle, implement the function `randPoint` which generates a uniform random point inside the circle.
10+
11+
Implement the `Solution` class:
12+
13+
14+
15+
- `Solution(double radius, double x_center, double y_center)` initializes the object with the radius of the circle `radius` and the position of the center `(x_center, y_center)`.
16+
17+
- `randPoint()` returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array `[x, y]`.
18+
19+
20+
 
21+
Example 1:
22+
23+
```
24+
Input
25+
["Solution", "randPoint", "randPoint", "randPoint"]
26+
[[1.0, 0.0, 0.0], [], [], []]
27+
Output
28+
[null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]]
29+
30+
Explanation
31+
Solution solution = new Solution(1.0, 0.0, 0.0);
32+
solution.randPoint(); // return [-0.02493, -0.38077]
33+
solution.randPoint(); // return [0.82314, 0.38945]
34+
solution.randPoint(); // return [0.36572, 0.17248]
35+
```
36+
37+
 
38+
**Constraints:**
39+
40+
41+
42+
- `0 < radius <= 108`
43+
44+
- `-107 <= x_center, y_center <= 107`
45+
46+
- At most `3 * 104` calls will be made to `randPoint`.
47+
48+
49+
50+
## Solution
51+
52+
```javascript
53+
/**
54+
* @param {number} radius
55+
* @param {number} x_center
56+
* @param {number} y_center
57+
*/
58+
var Solution = function(radius, x_center, y_center) {
59+
this.radius = radius;
60+
this.centerX = x_center;
61+
this.centerY = y_center;
62+
};
63+
64+
/**
65+
* @return {number[]}
66+
*/
67+
Solution.prototype.randPoint = function() {
68+
var radius = Math.sqrt(Math.random()) * this.radius;
69+
var rand = Math.random();
70+
var degree = Math.PI / 2 * (rand === 1 ? 0 : rand);
71+
var x = Math.cos(degree) * radius;
72+
var y = Math.sin(degree) * radius;
73+
return [
74+
this.centerX + (Math.random() > 0.5 ? 1 : -1) * x,
75+
this.centerY + (Math.random() > 0.5 ? 1 : -1) * y,
76+
];
77+
};
78+
79+
/**
80+
* Your Solution object will be instantiated and called as such:
81+
* var obj = new Solution(radius, x_center, y_center)
82+
* var param_1 = obj.randPoint()
83+
*/
84+
```
85+
86+
**Explain:**
87+
88+
nope.
89+
90+
**Complexity:**
91+
92+
* Time complexity : O(n).
93+
* Space complexity : O(1).

0 commit comments

Comments
 (0)