mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<p>Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print "ERROR".</p>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong> Input</strong>: 0.625
|
||||
|
||||
<strong> Output</strong>: "0.101"
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given an integer <code>n</code> representing the number of nodes in a <strong>perfect binary tree</strong> consisting of nodes numbered from <code>1</code> to <code>n</code>. The root of the tree is node <code>1</code> and each node <code>i</code> in the tree has two children where the left child is the node <code>2 * i</code> and the right child is <code>2 * i + 1</code>.</p>
|
||||
|
||||
<p>Each node in the tree also has a <strong>cost</strong> represented by a given <strong>0-indexed</strong> integer array <code>cost</code> of size <code>n</code> where <code>cost[i]</code> is the cost of node <code>i + 1</code>. You are allowed to <strong>increment</strong> the cost of <strong>any</strong> node by <code>1</code> <strong>any</strong> number of times.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of increments you need to make the cost of paths from the root to each <strong>leaf</strong> node equal</em>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>perfect binary tree </strong>is a tree where each node, except the leaf nodes, has exactly 2 children.</li>
|
||||
<li>The <strong>cost of a path</strong> is the sum of costs of nodes in the path.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/04/04/binaryytreeedrawio-4.png" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, cost = [1,5,2,2,3,3,1]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> We can do the following increments:
|
||||
- Increase the cost of node 4 one time.
|
||||
- Increase the cost of node 3 three times.
|
||||
- Increase the cost of node 7 two times.
|
||||
Each path from the root to a leaf will have a total cost of 9.
|
||||
The total increments we did is 1 + 3 + 2 = 6.
|
||||
It can be shown that this is the minimum answer we can achieve.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/04/04/binaryytreee2drawio.png" style="width: 205px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, cost = [5,3,3]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The two paths already have equal total costs, so no increments are needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>n + 1</code> is a power of <code>2</code></li>
|
||||
<li><code>cost.length == n</code></li>
|
||||
<li><code>1 <= cost[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>Given an array <code>arr</code> and a chunk size <code>size</code>, return a <strong>chunked</strong> array. A <strong>chunked</strong> array contains the original elements in <code>arr</code>, but consists of subarrays each of length <code>size</code>. The length of the last subarray may be less than <code>size</code> if <code>arr.length</code> is not evenly divisible by <code>size</code>.</p>
|
||||
|
||||
<p>You may assume the array is the output of <code>JSON.parse</code>. In other words, it is valid JSON.</p>
|
||||
|
||||
<p>Please solve it without using lodash's <code>_.chunk</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3,4,5], size = 1
|
||||
<strong>Output:</strong> [[1],[2],[3],[4],[5]]
|
||||
<strong>Explanation:</strong> The arr has been split into subarrays each with 1 element.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,9,6,3,2], size = 3
|
||||
<strong>Output:</strong> [[1,9,6],[3,2]]
|
||||
<strong>Explanation:</strong> The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [8,5,3,2,6], size = 6
|
||||
<strong>Output:</strong> [[8,5,3,2,6]]
|
||||
<strong>Explanation:</strong> Size is greater than arr.length thus all elements are in the first subarray.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [], size = 1
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> There are no elements to be chunked so an empty array is returned.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr is a valid JSON array</code></li>
|
||||
<li><code>2 <= JSON.stringify(arr).length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= size <= arr.length + 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
Write a function <code>createHelloWorld</code>. It should return a new function that always returns <code>"Hello World"</code>.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> args = []
|
||||
<strong>Output:</strong> "Hello World"
|
||||
<strong>Explanation:</strong>
|
||||
const f = createHelloWorld();
|
||||
f(); // "Hello World"
|
||||
|
||||
The function returned by createHelloWorld should always return "Hello World".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> args = [{},null,42]
|
||||
<strong>Output:</strong> "Hello World"
|
||||
<strong>Explanation:</strong>
|
||||
const f = createHelloWorld();
|
||||
f({}, null, 42); // "Hello World"
|
||||
|
||||
Any arguments could be passed to the function but it should still always return "Hello World".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= args.length <= 10</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>Given a function <code>fn</code>, return a new function that is identical to the original function except that it ensures <code>fn</code> is called at most once.</p>
|
||||
|
||||
<ul>
|
||||
<li>The first time the returned function is called, it should return the same result as <code>fn</code>.</li>
|
||||
<li>Every subsequent time it is called, it should return <code>undefined</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (a,b,c) => (a + b + c), calls = [[1,2,3],[2,3,6]]
|
||||
<strong>Output:</strong> [{"calls":1,"value":6}]
|
||||
<strong>Explanation:</strong>
|
||||
const onceFn = once(fn);
|
||||
onceFn(1, 2, 3); // 6
|
||||
onceFn(2, 3, 6); // undefined, fn was not called
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (a,b,c) => (a * b * c), calls = [[5,7,4],[2,3,6],[4,6,8]]
|
||||
<strong>Output:</strong> [{"calls":1,"value":140}]
|
||||
<strong>Explanation:</strong>
|
||||
const onceFn = once(fn);
|
||||
onceFn(5, 7, 4); // 140
|
||||
onceFn(2, 3, 6); // undefined, fn was not called
|
||||
onceFn(4, 6, 8); // undefined, fn was not called
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= calls.length <= 10</code></li>
|
||||
<li><code>1 <= calls[i].length <= 100</code></li>
|
||||
<li><code>2 <= JSON.stringify(calls).length <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,125 @@
|
||||
<p>Write a function that converts an array of objects <code>arr</code> into a matrix <code>m</code>.</p>
|
||||
|
||||
<p><code>arr</code> is an array of objects or arrays. Each item in the array can be deeply nested with child arrays and child objects. It can also contain numbers, strings, booleans, and null values.</p>
|
||||
|
||||
<p>The first row <code>m</code> should be the column names. If there is no nesting, the column names are the unique keys within the objects. If there is nesting, the column names are the respective paths in the object separated by <code>"."</code>.</p>
|
||||
|
||||
<p>Each of the remaining rows corresponds to an object in <code>arr</code>. Each value in the matrix corresponds to a value in an object. If a given object doesn't contain a value for a given column, the cell should contain an empty string <code>""</code>.</p>
|
||||
|
||||
<p>The colums in the matrix should be in <strong>lexographically ascending</strong> order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
arr = [
|
||||
{"b": 1, "a": 2},
|
||||
{"b": 3, "a": 4}
|
||||
]
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
["a", "b"],
|
||||
[2, 1],
|
||||
[4, 3]
|
||||
]
|
||||
|
||||
<strong>Explanation:</strong>
|
||||
There are two unique column names in the two objects: "a" and "b".
|
||||
"a" corresponds with [2, 4].
|
||||
"b" coresponds with [1, 3].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
arr = [
|
||||
{"a": 1, "b": 2},
|
||||
{"c": 3, "d": 4},
|
||||
{}
|
||||
]
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
["a", "b", "c", "d"],
|
||||
[1, 2, "", ""],
|
||||
["", "", 3, 4],
|
||||
["", "", "", ""]
|
||||
]
|
||||
|
||||
<strong>Explanation:</strong>
|
||||
There are 4 unique column names: "a", "b", "c", "d".
|
||||
The first object has values associated with "a" and "b".
|
||||
The second object has values associated with "c" and "d".
|
||||
The third object has no keys, so it is just a row of empty strings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
arr = [
|
||||
{"a": {"b": 1, "c": 2}},
|
||||
{"a": {"b": 3, "d": 4}}
|
||||
]
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
["a.b", "a.c", "a.d"],
|
||||
[1, 2, ""],
|
||||
[3, "", 4]
|
||||
]
|
||||
|
||||
<strong>Explanation:</strong>
|
||||
In this example, the objects are nested. The keys represent the full path to each value separated by periods.
|
||||
There are three paths: "a.b", "a.c", "a.d".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
arr = [
|
||||
[{"a": null}],
|
||||
[{"b": true}],
|
||||
[{"c": "x"}]
|
||||
]
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
["0.a", "0.b", "0.c"],
|
||||
[null, "", ""],
|
||||
["", true, ""],
|
||||
["", "", "x"]
|
||||
]
|
||||
|
||||
<strong>Explanation:</strong>
|
||||
Arrays are also considered objects with their keys being their indices.
|
||||
Each array has one element so the keys are "0.a", "0.b", and "0.c".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
arr = [
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
]
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[]
|
||||
]
|
||||
|
||||
<strong>Explanation:</strong>
|
||||
There are no keys so every row is an empty array.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 1000</code></li>
|
||||
<li><code>unique keys <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>The <strong>distinct difference</strong> array of <code>nums</code> is an array <code>diff</code> of length <code>n</code> such that <code>diff[i]</code> is equal to the number of distinct elements in the suffix <code>nums[i + 1, ..., n - 1]</code> <strong>subtracted from</strong> the number of distinct elements in the prefix <code>nums[0, ..., i]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>distinct difference</strong> array of </em><code>nums</code>.</p>
|
||||
|
||||
<p>Note that <code>nums[i, ..., j]</code> denotes the subarray of <code>nums</code> starting at index <code>i</code> and ending at index <code>j</code> inclusive. Particularly, if <code>i > j</code> then <code>nums[i, ..., j]</code> denotes an empty subarray.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> [-3,-1,1,3,5]
|
||||
<strong>Explanation:</strong> For index i = 0, there is 1 element in the prefix and 4 distinct elements in the suffix. Thus, diff[0] = 1 - 4 = -3.
|
||||
For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
|
||||
For index i = 2, there are 3 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 3 - 2 = 1.
|
||||
For index i = 3, there are 4 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 4 - 1 = 3.
|
||||
For index i = 4, there are 5 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 5 - 0 = 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,3,4,2]
|
||||
<strong>Output:</strong> [-2,-1,0,2,3]
|
||||
<strong>Explanation:</strong> For index i = 0, there is 1 element in the prefix and 3 distinct elements in the suffix. Thus, diff[0] = 1 - 3 = -2.
|
||||
For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
|
||||
For index i = 2, there are 2 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 2 - 2 = 0.
|
||||
For index i = 3, there are 3 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 3 - 1 = 2.
|
||||
For index i = 4, there are 3 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 3 - 0 = 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
|
||||
|
||||
<p>The rules of the game are as follows:</p>
|
||||
|
||||
<p><code>1<sup>st</sup></code> friend receives the ball.</p>
|
||||
|
||||
<ul>
|
||||
<li>After that, <code>1<sup>st</sup></code> friend passes it to the friend who is <code>k</code> steps away from them in the <strong>clockwise</strong> direction.</li>
|
||||
<li>After that, the friend who receives the ball should pass it to the friend who is <code>2 * k</code> steps away from them in the <strong>clockwise</strong> direction.</li>
|
||||
<li>After that, the friend who receives the ball should pass it to the friend who is <code>3 * k</code> steps away from them in the <strong>clockwise</strong> direction, and so on and so forth.</li>
|
||||
</ul>
|
||||
|
||||
<p>In other words, on the <code>i<sup>th</sup></code> turn, the friend holding the ball should pass it to the friend who is <code>i * k</code> steps away from them in the <strong>clockwise</strong> direction.</p>
|
||||
|
||||
<p>The game is finished when some friend receives the ball for the second time.</p>
|
||||
|
||||
<p>The <strong>losers</strong> of the game are friends who did not receive the ball in the entire game.</p>
|
||||
|
||||
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the array answer, which contains the losers of the game in the <strong>ascending</strong> order</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, k = 2
|
||||
<strong>Output:</strong> [4,5]
|
||||
<strong>Explanation:</strong> The game goes as follows:
|
||||
1) Start at 1<sup>st</sup> friend and pass the ball to the friend who is 2 steps away from them - 3<sup>rd</sup> friend.
|
||||
2) 3<sup>rd</sup> friend passes the ball to the friend who is 4 steps away from them - 2<sup>nd</sup> friend.
|
||||
3) 2<sup>nd</sup> friend passes the ball to the friend who is 6 steps away from them - 3<sup>rd</sup> friend.
|
||||
4) The game ends as 3<sup>rd</sup> friend receives the ball for the second time.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, k = 4
|
||||
<strong>Output:</strong> [2,3,4]
|
||||
<strong>Explanation:</strong> The game goes as follows:
|
||||
1) Start at the 1<sup>st</sup> friend and pass the ball to the friend who is 4 steps away from them - 1<sup>st</sup> friend.
|
||||
2) The game ends as 1<sup>st</sup> friend receives the ball for the second time.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= n <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>. In an operation, you can choose an element and multiply it by <code>2</code>.</p>
|
||||
|
||||
<p>Return <em>the maximum possible value of </em><code>nums[0] | nums[1] | ... | nums[n - 1]</code> <em>that can be obtained after applying the operation on nums at most </em><code>k</code><em> times</em>.</p>
|
||||
|
||||
<p>Note that <code>a | b</code> denotes the <strong>bitwise or</strong> between two integers <code>a</code> and <code>b</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [12,9], k = 1
|
||||
<strong>Output:</strong> 30
|
||||
<strong>Explanation:</strong> If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [8,1,2], k = 2
|
||||
<strong>Output:</strong> 35
|
||||
<strong>Explanation:</strong> If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 15</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>There is a <strong>0-indexed</strong> array <code>nums</code> of length <code>n</code>. Initially, all elements are <strong>uncolored </strong>(has a value of <code>0</code>).</p>
|
||||
|
||||
<p>You are given a 2D integer array <code>queries</code> where <code>queries[i] = [index<sub>i</sub>, color<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>For each query, you color the index <code>index<sub>i</sub></code> with the color <code>color<sub>i</sub></code> in the array <code>nums</code>.</p>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of the same length as </em><code>queries</code><em> where </em><code>answer[i]</code><em> is the number of adjacent elements with the same color <strong>after</strong> the </em><code>i<sup>th</sup></code><em> query</em>.</p>
|
||||
|
||||
<p>More formally, <code>answer[i]</code> is the number of indices <code>j</code>, such that <code>0 <= j < n - 1</code> and <code>nums[j] == nums[j + 1]</code> and <code>nums[j] != 0</code> after the <code>i<sup>th</sup></code> query.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
|
||||
<strong>Output:</strong> [0,1,1,0,2]
|
||||
<strong>Explanation:</strong> Initially array nums = [0,0,0,0], where 0 denotes uncolored elements of the array.
|
||||
- After the 1<sup>st</sup> query nums = [2,0,0,0]. The count of adjacent elements with the same color is 0.
|
||||
- After the 2<sup>nd</sup> query nums = [2,2,0,0]. The count of adjacent elements with the same color is 1.
|
||||
- After the 3<sup>rd</sup> query nums = [2,2,0,1]. The count of adjacent elements with the same color is 1.
|
||||
- After the 4<sup>th</sup> query nums = [2,1,0,1]. The count of adjacent elements with the same color is 0.
|
||||
- After the 5<sup>th</sup> query nums = [2,1,1,1]. The count of adjacent elements with the same color is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, queries = [[0,100000]]
|
||||
<strong>Output:</strong> [0]
|
||||
<strong>Explanation:</strong> Initially array nums = [0], where 0 denotes uncolored elements of the array.
|
||||
- After the 1<sup>st</sup> query nums = [100000]. The count of adjacent elements with the same color is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>0 <= index<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>1 <= color<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>A <strong>0-indexed</strong> array <code>derived</code> with length <code>n</code> is derived by computing the <strong>bitwise XOR</strong> (⊕) of adjacent values in a <strong>binary array</strong> <code>original</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>Specifically, for each index <code>i</code> in the range <code>[0, n - 1]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>i = n - 1</code>, then <code>derived[i] = original[i] ⊕ original[0]</code>.</li>
|
||||
<li>Otherwise, <code>derived[i] = original[i] ⊕ original[i + 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given an array <code>derived</code>, your task is to determine whether there exists a <strong>valid binary array</strong> <code>original</code> that could have formed <code>derived</code>.</p>
|
||||
|
||||
<p>Return <em><strong>true</strong> if such an array exists or <strong>false</strong> otherwise.</em></p>
|
||||
|
||||
<ul>
|
||||
<li>A binary array is an array containing only <strong>0's</strong> and <strong>1's</strong></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> derived = [1,1,0]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> A valid original array that gives derived is [0,1,0].
|
||||
derived[0] = original[0] ⊕ original[1] = 0 ⊕ 1 = 1
|
||||
derived[1] = original[1] ⊕ original[2] = 1 ⊕ 0 = 1
|
||||
derived[2] = original[2] ⊕ original[0] = 0 ⊕ 0 = 0
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> derived = [1,1]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> A valid original array that gives derived is [0,1].
|
||||
derived[0] = original[0] ⊕ original[1] = 1
|
||||
derived[1] = original[1] ⊕ original[0] = 1
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> derived = [1,0]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> There is no valid original array that gives derived.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == derived.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li>The values in <code>derived</code> are either <strong>0's</strong> or <strong>1's</strong></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>nums</code>. Initially, your score is <code>0</code>. Perform the following operations until the matrix becomes empty:</p>
|
||||
|
||||
<ol>
|
||||
<li>From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter which number is chosen.</li>
|
||||
<li>Identify the highest number amongst all those removed in step 1. Add that number to your <strong>score</strong>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the final <strong>score</strong>.</em></p>
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We remove 1 and add it to the answer. We return 1.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 300</code></li>
|
||||
<li><code>1 <= nums[i].length <= 500</code></li>
|
||||
<li><code>0 <= nums[i][j] <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> matrix <code>grid</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>You can start at <strong>any</strong> cell in the first column of the matrix, and traverse the grid in the following way:</p>
|
||||
|
||||
<ul>
|
||||
<li>From a cell <code>(row, col)</code>, you can move to any of the cells: <code>(row - 1, col + 1)</code>, <code>(row, col + 1)</code> and <code>(row + 1, col + 1)</code> such that the value of the cell you move to, should be <strong>strictly</strong> bigger than the value of the current cell.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of <strong>moves</strong> that you can perform.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/04/11/yetgriddrawio-10.png" style="width: 201px; height: 201px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can start at the cell (0, 0) and make the following moves:
|
||||
- (0, 0) -> (0, 1).
|
||||
- (0, 1) -> (1, 2).
|
||||
- (1, 2) -> (2, 3).
|
||||
It can be shown that it is the maximum number of moves that can be made.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/04/12/yetgrid4drawio.png" />
|
||||
<strong>Input:</strong> grid = [[3,2,4],[2,1,9],[1,1,7]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Starting from any cell in the first column we cannot perform any moves.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>2 <= m, n <= 1000</code></li>
|
||||
<li><code>4 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= grid[i][j] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> vertices, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting vertices <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>complete connected components</strong> of the graph</em>.</p>
|
||||
|
||||
<p>A <strong>connected component</strong> is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.</p>
|
||||
|
||||
<p>A connected component is said to be <b>complete</b> if there exists an edge between every pair of its vertices.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-31-23.png" style="width: 671px; height: 270px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,2],[3,4]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> From the picture above, one can see that all of the components of this graph are complete.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-32-00.png" style="width: 671px; height: 270px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The component containing vertices 0, 1, and 2 is complete since there is an edge between every pair of two vertices. On the other hand, the component containing vertices 3, 4, and 5 is not complete since there is no edge between vertices 4 and 5. Thus, the number of complete components in this graph is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>0 <= edges.length <= n * (n - 1) / 2</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>There are no repeated edges.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>The first ten characters consist of the phone number of passengers.</li>
|
||||
<li>The next character denotes the gender of the person.</li>
|
||||
<li>The following two characters are used to indicate the age of the person.</li>
|
||||
<li>The last two characters determine the seat allotted to that person.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> None of the passengers are older than 60.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= details.length <= 100</code></li>
|
||||
<li><code>details[i].length == 15</code></li>
|
||||
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
|
||||
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
|
||||
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
|
||||
</ul>
|
48
leetcode-cn/problem (English)/节流(English) [throttle].html
Normal file
48
leetcode-cn/problem (English)/节流(English) [throttle].html
Normal file
@@ -0,0 +1,48 @@
|
||||
<p>Given a function <code>fn</code> and a time in milliseconds <code>t</code>, return a <strong>throttled</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>throttled</strong> function is first called without delay and then, for a time interval of <code>t</code> milliseconds, can't be executed but should store the latest function arguments provided to call <code>fn</code> with them after the end of the delay.</p>
|
||||
|
||||
<p>For instance, <code>t = 50ms</code>, and the function was called at <code>30ms</code>, <code>40ms</code>, and <code>60ms</code>. The first function call would block calling functions for the following <code>t</code> milliseconds. The second function call would save arguments, and the third call arguments should overwrite currently stored arguments from the second call because the second and third calls are called before <code>80ms</code>. Once the delay has passed, the throttled function should be called with the latest arguments provided during the delay period, and it should also create another delay period of <code>80ms + t</code>.</p>
|
||||
|
||||
<p><img alt="Throttle Diagram" src="https://assets.leetcode.com/uploads/2023/04/08/screen-shot-2023-04-08-at-120313-pm.png" style="width: 1156px; height: 372px;" />The above diagram shows how throttle will transform events. Each rectangle represents 100ms and the throttle time is 400ms. Each color represents a different set of inputs.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> t = 100, calls = [{"t":20,"inputs":[1]}]
|
||||
<strong>Output:</strong> [{"t":20,"inputs":[1]}]
|
||||
<strong>Explanation:</strong> The 1st call is always called without delay
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> t = 50, calls = [{"t":50,"inputs":[1]},{"t":75,"inputs":[2]}]
|
||||
<strong>Output:</strong> [{"t":50,"inputs":[1]},{"t":100,"inputs":[2]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st is called a function with arguments (1) without delay.
|
||||
The 2nd is called at 75ms, within the delay period because 50ms + 50ms = 100ms, so the next call can be reached at 100ms. Therefore, we save arguments from the 2nd call to use them at the callback of the 1st call.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> t = 70, calls = [{"t":50,"inputs":[1]},{"t":75,"inputs":[2]},{"t":90,"inputs":[8]},{"t": 140, "inputs":[5,7]},{"t": 300, "inputs": [9,4]}]
|
||||
<strong>Output:</strong> [{"t":50,"inputs":[1]},{"t":120,"inputs":[8]},{"t":190,"inputs":[5,7]},{"t":300,"inputs":[9,4]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st is called a function with arguments (1) without delay.
|
||||
The 2nd is called at 75ms within the delay period because 50ms + 70ms = 120ms, so it should only save arguments.
|
||||
The 3rd is also called within the delay period, and because we need just the latest function arguments, we overwrite previous ones. After the delay period, we do a callback at 120ms with saved arguments. That callback makes another delay period of 120ms + 70ms = 190ms so that the next function can be called at 190ms.
|
||||
The 4th is called at 140ms in the delay period, so it should be called as a callback at 190ms. That will create another delay period of 190ms + 70ms = 260ms.
|
||||
The 5th is called at 300ms, but it is after 260ms, so it should be called immediately and should create another delay period of 300ms + 70ms = 370ms.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= t <= 1000</code></li>
|
||||
<li><code>1 <= calls.length <= 10</code></li>
|
||||
<li><code>0 <= calls[i].t <= 1000</code></li>
|
||||
<li><code>0 <= calls[i].inputs[i], calls[i].inputs.length <= 10</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> representing the strength of some heroes. The<b> power</b> of a group of heroes is defined as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, ... ,<code>i<sub>k</sub></code> be the indices of the heroes in a group. Then, the power of this group is <code>max(nums[i<sub>0</sub>], nums[i<sub>1</sub>], ... ,nums[i<sub>k</sub>])<sup>2</sup> * min(nums[i<sub>0</sub>], nums[i<sub>1</sub>], ... ,nums[i<sub>k</sub>])</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of the <strong>power</strong> of all <strong>non-empty</strong> groups of heroes possible.</em> Since the sum could be very large, return it <strong>modulo</strong> <code>10<sup>9 </sup>+ 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,4]
|
||||
<strong>Output:</strong> 141
|
||||
<strong>Explanation:</strong>
|
||||
1<sup>st</sup> group: [2] has power = 2<sup>2</sup> * 2 = 8.
|
||||
2<sup>nd</sup> group: [1] has power = 1<sup>2</sup> * 1 = 1.
|
||||
3<sup>rd</sup> group: [4] has power = 4<sup>2</sup> * 4 = 64.
|
||||
4<sup>th</sup> group: [2,1] has power = 2<sup>2</sup> * 1 = 4.
|
||||
5<sup>th</sup> group: [2,4] has power = 4<sup>2</sup> * 2 = 32.
|
||||
6<sup>th</sup> group: [1,4] has power = 4<sup>2</sup> * 1 = 16.
|
||||
7<sup>th</sup> group: [2,1,4] has power = 4<sup>2</sup> * 1 = 16.
|
||||
The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> A total of 7 groups are possible, and the power of each group will be 1. Therefore, the sum of the powers of all groups is 7.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>Write a function <code>createCounter</code>. It should accept an initial integer <code>init</code>. It should return an object with three functions.</p>
|
||||
|
||||
<p>The three functions are:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>increment()</code> increases the current value by 1 and then returns it.</li>
|
||||
<li><code>decrement()</code> reduces the current value by 1 and then returns it.</li>
|
||||
<li><code>reset()</code> sets the current value to <code>init</code> and then returns it.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> init = 5, calls = ["increment","reset","decrement"]
|
||||
<strong>Output:</strong> [6,5,4]
|
||||
<strong>Explanation:</strong>
|
||||
const counter = createCounter(5);
|
||||
counter.increment(); // 6
|
||||
counter.reset(); // 5
|
||||
counter.decrement(); // 4
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> init = 0, calls = ["increment","increment","decrement","reset","reset"]
|
||||
<strong>Output:</strong> [1,2,1,0,0]
|
||||
<strong>Explanation:</strong>
|
||||
const counter = createCounter(0);
|
||||
counter.increment(); // 1
|
||||
counter.increment(); // 2
|
||||
counter.decrement(); // 1
|
||||
counter.reset(); // 0
|
||||
counter.reset(); // 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-1000 <= init <= 1000</code></li>
|
||||
<li><code>total calls not to exceed 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,71 @@
|
||||
<p>Design a data structure that keeps track of the values in it and answers some queries regarding their frequencies.</p>
|
||||
|
||||
<p>Implement the <code>FrequencyTracker</code> class.</p>
|
||||
|
||||
<ul>
|
||||
<li><code>FrequencyTracker()</code>: Initializes the <code>FrequencyTracker</code> object with an empty array initially.</li>
|
||||
<li><code>void add(int number)</code>: Adds <code>number</code> to the data structure.</li>
|
||||
<li><code>void deleteOne(int number)</code>: Deletes <strong>one</strong> occurence of <code>number</code> from the data structure. The data structure <strong>may not contain</strong> <code>number</code>, and in this case nothing is deleted.</li>
|
||||
<li><code>bool hasFrequency(int frequency)</code>: Returns <code>true</code> if there is a number in the data structure that occurs <code>frequency</code> number of times, otherwise, it returns <code>false</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["FrequencyTracker", "add", "add", "hasFrequency"]
|
||||
[[], [3], [3], [2]]
|
||||
<strong>Output</strong>
|
||||
[null, null, null, true]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
FrequencyTracker frequencyTracker = new FrequencyTracker();
|
||||
frequencyTracker.add(3); // The data structure now contains [3]
|
||||
frequencyTracker.add(3); // The data structure now contains [3, 3]
|
||||
frequencyTracker.hasFrequency(2); // Returns true, because 3 occurs twice
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["FrequencyTracker", "add", "deleteOne", "hasFrequency"]
|
||||
[[], [1], [1], [1]]
|
||||
<strong>Output</strong>
|
||||
[null, null, null, false]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
FrequencyTracker frequencyTracker = new FrequencyTracker();
|
||||
frequencyTracker.add(1); // The data structure now contains [1]
|
||||
frequencyTracker.deleteOne(1); // The data structure becomes empty []
|
||||
frequencyTracker.hasFrequency(1); // Returns false, because the data structure is empty
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["FrequencyTracker", "hasFrequency", "add", "hasFrequency"]
|
||||
[[], [2], [3], [1]]
|
||||
<strong>Output</strong>
|
||||
[null, false, null, true]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
FrequencyTracker frequencyTracker = new FrequencyTracker();
|
||||
frequencyTracker.hasFrequency(2); // Returns false, because the data structure is empty
|
||||
frequencyTracker.add(3); // The data structure now contains [3]
|
||||
frequencyTracker.hasFrequency(1); // Returns true, because 3 occurs once
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= number <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= frequency <= 10<sup>5</sup></code></li>
|
||||
<li>At most, <code>2 * 10<sup>5</sup></code> calls will be made to <code>add</code>, <code>deleteOne</code>, and <code>hasFrequency</code> in <strong>total</strong>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user