Skip to content

Commit 3fa6804

Browse files
committed
feat: solve No.1508,2053
1 parent eb4927a commit 3fa6804

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 1508. Range Sum of Sorted Subarray Sums
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Two Pointers, Binary Search, Sorting.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given the array `nums` consisting of `n` positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of `n * (n + 1) / 2` numbers.
10+
11+
**Return the sum of the numbers from index **`left`** to index **`right` (**indexed from 1**)**, inclusive, in the new array. **Since the answer can be a huge number return it modulo `109 + 7`.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: nums = [1,2,3,4], n = 4, left = 1, right = 5
18+
Output: 13
19+
Explanation: All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13.
20+
```
21+
22+
Example 2:
23+
24+
```
25+
Input: nums = [1,2,3,4], n = 4, left = 3, right = 4
26+
Output: 6
27+
Explanation: The given array is the same as example 1. We have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 3 to ri = 4 is 3 + 3 = 6.
28+
```
29+
30+
Example 3:
31+
32+
```
33+
Input: nums = [1,2,3,4], n = 4, left = 1, right = 10
34+
Output: 50
35+
```
36+
37+
 
38+
**Constraints:**
39+
40+
41+
42+
- `n == nums.length`
43+
44+
- `1 <= nums.length <= 1000`
45+
46+
- `1 <= nums[i] <= 100`
47+
48+
- `1 <= left <= right <= n * (n + 1) / 2`
49+
50+
51+
52+
## Solution
53+
54+
```javascript
55+
/**
56+
* @param {number[]} nums
57+
* @param {number} n
58+
* @param {number} left
59+
* @param {number} right
60+
* @return {number}
61+
*/
62+
var rangeSum = function(nums, n, left, right) {
63+
var queue = new MinPriorityQueue();
64+
for (var i = 0; i < nums.length; i++) {
65+
queue.enqueue(i, nums[i]);
66+
}
67+
var res = 0;
68+
var mod = Math.pow(10, 9) + 7;
69+
for (var j = 0; j < right; j++) {
70+
var { element, priority } = queue.dequeue();
71+
if (element < nums.length - 1) {
72+
queue.enqueue(element + 1, priority + nums[element + 1]);
73+
}
74+
if (j >= left - 1) {
75+
res += priority;
76+
res %= mod;
77+
}
78+
}
79+
return res;
80+
};
81+
```
82+
83+
**Explain:**
84+
85+
nope.
86+
87+
**Complexity:**
88+
89+
* Time complexity : O(n^2 * log(n)).
90+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 2053. Kth Distinct String in an Array
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Hash Table, String, Counting.
5+
- Similar Questions: Count Common Words With One Occurrence.
6+
7+
## Problem
8+
9+
A **distinct string** is a string that is present only **once** in an array.
10+
11+
Given an array of strings `arr`, and an integer `k`, return **the **`kth`** **distinct string** present in **`arr`. If there are **fewer** than `k` distinct strings, return **an **empty string ****`""`.
12+
13+
Note that the strings are considered in the **order in which they appear** in the array.
14+
15+
 
16+
Example 1:
17+
18+
```
19+
Input: arr = ["d","b","c","b","c","a"], k = 2
20+
Output: "a"
21+
Explanation:
22+
The only distinct strings in arr are "d" and "a".
23+
"d" appears 1st, so it is the 1st distinct string.
24+
"a" appears 2nd, so it is the 2nd distinct string.
25+
Since k == 2, "a" is returned.
26+
```
27+
28+
Example 2:
29+
30+
```
31+
Input: arr = ["aaa","aa","a"], k = 1
32+
Output: "aaa"
33+
Explanation:
34+
All strings in arr are distinct, so the 1st string "aaa" is returned.
35+
```
36+
37+
Example 3:
38+
39+
```
40+
Input: arr = ["a","b","a"], k = 3
41+
Output: ""
42+
Explanation:
43+
The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".
44+
```
45+
46+
 
47+
**Constraints:**
48+
49+
50+
51+
- `1 <= k <= arr.length <= 1000`
52+
53+
- `1 <= arr[i].length <= 5`
54+
55+
- `arr[i]` consists of lowercase English letters.
56+
57+
58+
59+
## Solution
60+
61+
```javascript
62+
/**
63+
* @param {string[]} arr
64+
* @param {number} k
65+
* @return {string}
66+
*/
67+
var kthDistinct = function(arr, k) {
68+
var map = {};
69+
for (var i = 0; i < arr.length; i++) {
70+
map[arr[i]] = (map[arr[i]] || 0) + 1;
71+
}
72+
for (var j = 0; j < arr.length; j++) {
73+
if (map[arr[j]] === 1) {
74+
k--;
75+
if (k === 0) return arr[j];
76+
}
77+
}
78+
return '';
79+
};
80+
```
81+
82+
**Explain:**
83+
84+
nope.
85+
86+
**Complexity:**
87+
88+
* Time complexity : O(n).
89+
* Space complexity : O(n).

0 commit comments

Comments
 (0)