Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitee2dd5c

Browse files
authored
Added tasks 2724-2729
1 parentbfda616 commitee2dd5c

File tree

15 files changed

+567
-0
lines changed

15 files changed

+567
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2724\. Sort By
2+
3+
Easy
4+
5+
Given an array`arr` and a function`fn`, return a sorted array`sortedArr`. You can assume`fn` only returns numbers and those numbers determine the sort order of`sortedArr`.`sortedArray` must be sorted in**ascending order** by`fn` output.
6+
7+
You may assume that`fn` will never duplicate numbers for a given array.
8+
9+
**Example 1:**
10+
11+
**Input:** arr =[5, 4, 1, 2, 3], fn = (x) => x
12+
13+
**Output:**[1, 2, 3, 4, 5]
14+
15+
**Explanation:** fn simply returns the number passed to it so the array is sorted in ascending order.
16+
17+
**Example 2:**
18+
19+
**Input:** arr =[{"x": 1}, {"x": 0}, {"x": -1}], fn = (d) => d.x
20+
21+
**Output:**[{"x": -1}, {"x": 0}, {"x": 1}]
22+
23+
**Explanation:** fn returns the value for the "x" key. So the array is sorted based on that value.
24+
25+
**Example 3:**
26+
27+
**Input:** arr =[[3, 4],[5, 2],[10, 1]], fn = (x) => x[1]
28+
29+
**Output:**[[10, 1],[5, 2],[3, 4]]
30+
31+
**Explanation:** arr is sorted in ascending order by number at index=1.
32+
33+
**Constraints:**
34+
35+
*`arr is a valid JSON array`
36+
*`fn is a function that returns a number`
37+
* <code>1 <= arr.length <= 5 * 10<sup>5</sup></code>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #Easy #2023_09_19_Time_124_ms_(86.52%)_Space_57_MB_(53.08%)
2+
3+
typeJSONValue=null|boolean|number|string|JSONValue[]|{[key:string]:JSONValue}
4+
typeFn=(value:JSONValue)=>number
5+
6+
constsortBy=(arr:JSONValue[],fn:Fn):JSONValue[]=>[...arr].sort((a,b)=>fn(a)>fn(b) ?1 :-1)
7+
8+
export{sortBy}
9+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
2725\. Interval Cancellation
2+
3+
Easy
4+
5+
Given a function`fn`, an array of arguments`args`, and an interval time`t`, return a cancel function`cancelFn`.
6+
7+
The function`fn` should be called with`args` immediately and then called again every`t` milliseconds until`cancelFn` is called at`cancelT` ms.
8+
9+
**Example 1:**
10+
11+
**Input:** fn = (x) => x * 2, args =[4], t = 20, cancelT = 110
12+
13+
**Output:**
14+
15+
[
16+
{"time": 0, "returned": 8},
17+
{"time": 20, "returned": 8},
18+
{"time": 40, "returned": 8},
19+
{"time": 60, "returned": 8},
20+
{"time": 80, "returned": 8},
21+
{"time": 100, "returned": 8}
22+
]
23+
24+
**Explanation:**
25+
26+
const cancel = cancellable(x => x * 2, [4], 20);
27+
setTimeout(cancel, 110);
28+
29+
Every 20ms, fn(4) is called. Until t=110ms, then it is cancelled.
30+
31+
1st fn call is at 0ms. fn(4) returns 8.
32+
33+
2nd fn call is at 20ms. fn(4) returns 8.
34+
35+
3rd fn call is at 40ms. fn(4) returns 8.
36+
37+
4th fn call is at 60ms. fn(4) returns 8.
38+
39+
5th fn call is at 80ms. fn(4) returns 8.
40+
41+
6th fn call is at 100ms. fn(4) returns 8.
42+
43+
Cancelled at 110ms
44+
45+
**Example 2:**
46+
47+
**Input:** fn = (x1, x2) => (x1 * x2), args =[2, 5], t = 25, cancelT = 140
48+
49+
**Output:**
50+
51+
[
52+
{"time": 0, "returned": 10},
53+
{"time": 25, "returned": 10},
54+
{"time": 50, "returned": 10},
55+
{"time": 75, "returned": 10},
56+
{"time": 100, "returned": 10},
57+
{"time": 125, "returned": 10}
58+
]
59+
60+
**Explanation:**
61+
62+
const cancel = cancellable((x1, x2) => (x1 * x2), [2, 5], 25);
63+
setTimeout(cancel, 140);
64+
65+
Every 25ms, fn(2, 5) is called. Until t=140ms, then it is cancelled.
66+
67+
1st fn call is at 0ms
68+
69+
2nd fn call is at 25ms
70+
71+
3rd fn call is at 50ms
72+
73+
4th fn call is at 75ms
74+
75+
5th fn call is at 100ms
76+
77+
6th fn call is at 125ms
78+
79+
Cancelled at 140ms
80+
81+
**Example 3:**
82+
83+
**Input:** fn = (x1, x2, x3) => (x1 + x2 + x3), args =[5, 1, 3], t = 50, cancelT = 180
84+
85+
**Output:**
86+
87+
[
88+
{"time": 0, "returned": 9},
89+
{"time": 50, "returned": 9},
90+
{"time": 100, "returned": 9},
91+
{"time": 150, "returned": 9}
92+
]
93+
94+
**Explanation:**
95+
96+
const cancel = cancellable((x1, x2, x3) => (x1 + x2 + x3), [5, 1, 3], 50);
97+
setTimeout(cancel, 180);
98+
99+
Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled.
100+
101+
1st fn call is at 0ms
102+
103+
2nd fn call is at 50ms
104+
105+
3rd fn call is at 100ms
106+
107+
4th fn call is at 150ms
108+
109+
Cancelled at 180ms
110+
111+
**Constraints:**
112+
113+
*`fn is a function`
114+
*`args is a valid JSON array`
115+
*`1 <= args.length <= 10`
116+
*`20 <= t <= 1000`
117+
*`10 <= cancelT <= 1000`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// #Easy #2023_09_19_Time_51_ms_(98.87%)_Space_43.3_MB_(30.92%)
2+
3+
functioncancellable(fn:Function,args:any[],t:number):Function{
4+
fn(...args)
5+
consttimer=setInterval(()=>{
6+
fn(...args)
7+
},t)
8+
9+
return()=>clearTimeout(timer)
10+
}
11+
12+
/*
13+
* const result = []
14+
*
15+
* const fn = (x) => x * 2
16+
* const args = [4], t = 20, cancelT = 110
17+
*
18+
* const start = performance.now()
19+
*
20+
* const log = (...argsArr) => {
21+
* const diff = Math.floor(performance.now() - start)
22+
* result.push({"time": diff, "returned": fn(...argsArr)})
23+
* }
24+
*
25+
* const cancel = cancellable(log, args, t);
26+
*
27+
* setTimeout(() => {
28+
* cancel()
29+
* }, cancelT)
30+
*
31+
* setTimeout(() => {
32+
* console.log(result) // [
33+
* // {"time":0,"returned":8},
34+
* // {"time":20,"returned":8},
35+
* // {"time":40,"returned":8},
36+
* // {"time":60,"returned":8},
37+
* // {"time":80,"returned":8},
38+
* // {"time":100,"returned":8}
39+
* // ]
40+
* }, cancelT + t + 15)
41+
*/
42+
43+
export{cancellable}
44+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2726\. Calculator with Method Chaining
2+
3+
Easy
4+
5+
Design a`Calculator` class. The class should provide the mathematical operations of addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining. The`Calculator` class constructor should accept a number which serves as the initial value of`result`.
6+
7+
Your`Calculator` class should have the following methods:
8+
9+
*`add` - This method adds the given number`value` to the`result` and returns the updated`Calculator`.
10+
*`subtract` - This method subtracts the given number`value` from the`result` and returns the updated`Calculator`.
11+
*`multiply` - This method multiplies the`result` by the given number`value` and returns the updated`Calculator`.
12+
*`divide` - This method divides the`result` by the given number`value` and returns the updated`Calculator`. If the passed value is`0`, an error`"Division by zero is not allowed"` should be thrown.
13+
*`power` - This method raises the`result` to the power of the given number`value` and returns the updated`Calculator`.
14+
*`getResult` - This method returns the`result`.
15+
16+
Solutions within <code>10<sup>-5</sup></code> of the actual result are considered correct.
17+
18+
**Example 1:**
19+
20+
**Input:** actions =["Calculator", "add", "subtract", "getResult"], values =[10, 5, 7]
21+
22+
**Output:** 8
23+
24+
**Explanation:** new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
25+
26+
**Example 2:**
27+
28+
**Input:** actions =["Calculator", "multiply", "power", "getResult"], values =[2, 5, 2]
29+
30+
**Output:** 100
31+
32+
**Explanation:** new Calculator(2).multiply(5).power(2).getResult() // (2\* 5) ^ 2 = 100
33+
34+
**Example 3:**
35+
36+
**Input:** actions =["Calculator", "divide", "getResult"], values =[20, 0]
37+
38+
**Output:** "Division by zero is not allowed"
39+
40+
**Explanation:** new Calculator(20).divide(0).getResult() // 20 / 0 The error should be thrown because we cannot divide by zero.
41+
42+
**Constraints:**
43+
44+
* <code>2 <= actions.length <= 2 * 10<sup>4</sup></code>
45+
* <code>1 <= values.length <= 2 * 10<sup>4</sup> - 1</code>
46+
*`actions[i] is one of "Calculator", "add", "subtract", "multiply", "divide", "power", and "getResult"`
47+
*`Last action is always "getResult"`
48+
*`values is a JSON array of numbers`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// #Easy #2023_09_19_Time_39_ms_(99.67%)_Space_42.1_MB_(95.49%)
2+
3+
classCalculator{
4+
init:number
5+
6+
constructor(value:number){
7+
this.init=value
8+
}
9+
10+
add(value:number):Calculator{//NOSONAR
11+
this.init+=value
12+
returnthis
13+
}
14+
15+
subtract(value:number):Calculator{//NOSONAR
16+
this.init-=value
17+
returnthis
18+
}
19+
20+
multiply(value:number):Calculator{//NOSONAR
21+
this.init*=value
22+
returnthis
23+
}
24+
25+
divide(value:number):Calculator{//NOSONAR
26+
if(value===0)throwError('Division by zero is not allowed')
27+
this.init/=value
28+
returnthis
29+
}
30+
31+
power(value:number):Calculator{//NOSONAR
32+
this.init=this.init**value
33+
returnthis
34+
}
35+
36+
getResult():number{
37+
returnthis.init
38+
}
39+
}
40+
41+
export{Calculator}
42+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2727\. Is Object Empty
2+
3+
Easy
4+
5+
Given an object or an array, return if it is empty.
6+
7+
* An empty object contains no key-value pairs.
8+
* An empty array contains no elements.
9+
10+
You may assume the object or array is the output of`JSON.parse`.
11+
12+
**Example 1:**
13+
14+
**Input:** obj = {"x": 5, "y": 42}
15+
16+
**Output:** false
17+
18+
**Explanation:** The object has 2 key-value pairs so it is not empty.
19+
20+
**Example 2:**
21+
22+
**Input:** obj = {}
23+
24+
**Output:** true
25+
26+
**Explanation:** The object doesn't have any key-value pairs so it is empty.
27+
28+
**Example 3:**
29+
30+
**Input:** obj =[null, false, 0]
31+
32+
**Output:** false
33+
34+
**Explanation:** The array has 3 elements so it is not empty.
35+
36+
**Constraints:**
37+
38+
* <code>2 <= JSON.stringify(obj).length <= 10<sup>5</sup></code>
39+
40+
**Can you solve it in O(1) time?**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// #Easy #2023_09_19_Time_52_ms_(79.26%)_Space_44.6_MB_(77.05%)
2+
3+
functionisEmpty(obj:Record<string,any>|any[]):boolean{
4+
returnObject.keys(obj).length===0
5+
}
6+
7+
export{isEmpty}
8+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
packageg2701_2800.s2729_check_if_the_number_is_fascinating;
2+
3+
// #Easy #Hash_Table #Math #2023_09_19_Time_1_ms_(96.07%)_Space_40.3_MB_(46.53%)
4+
5+
importjava.util.HashSet;
6+
importjava.util.Set;
7+
8+
publicclassSolution {
9+
publicbooleanisFascinating(intn) {
10+
Set<Integer>set =newHashSet<>();
11+
returnadd(set,n) &&add(set,2 *n) &&add(set,3 *n);
12+
}
13+
14+
privatebooleanadd(Set<Integer>set,intcur) {
15+
while (cur >0) {
16+
intdigit =cur %10;
17+
if (digit ==0 ||set.contains(digit)) {
18+
returnfalse;
19+
}
20+
set.add(digit);
21+
cur /=10;
22+
}
23+
returntrue;
24+
}
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp