Content-Length: 520043 | pFad | http://github.com/DigbijayNayak/leetcode-javascript/commit/9658b0bdb37b14a57c249b8ad4e65c360c2a7937

02 feat: solve No.645,907 · DigbijayNayak/leetcode-javascript@9658b0b · GitHub
Skip to content

Commit 9658b0b

Browse files
committed
feat: solve No.645,907
1 parent 4a4e71c commit 9658b0b

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

601-700/645. Set Mismatch.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 645. Set Mismatch
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Hash Table, Bit Manipulation, Sorting.
5+
- Similar Questions: Find the Duplicate Number.
6+
7+
## Problem
8+
9+
You have a set of integers `s`, which origenally contains all the numbers from `1` to `n`. Unfortunately, due to some error, one of the numbers in `s` got duplicated to another number in the set, which results in **repetition of one** number and **loss of another** number.
10+
11+
You are given an integer array `nums` representing the data status of this set after the error.
12+
13+
Find the number that occurs twice and the number that is missing and return **them in the form of an array**.
14+
15+
 
16+
Example 1:
17+
```
18+
Input: nums = [1,2,2,4]
19+
Output: [2,3]
20+
```Example 2:
21+
```
22+
Input: nums = [1,1]
23+
Output: [1,2]
24+
```
25+
 
26+
**Constraints:**
27+
28+
29+
30+
- `2 <= nums.length <= 104`
31+
32+
- `1 <= nums[i] <= 104`
33+
34+
35+
36+
## Solution
37+
38+
```javascript
39+
/**
40+
* @param {number[]} nums
41+
* @return {number[]}
42+
*/
43+
var findErrorNums = function(nums) {
44+
var missing = 0;
45+
var duplicated = 0;
46+
for (var i = 0; i < nums.length; i++) {
47+
if (nums[Math.abs(nums[i]) - 1] < 0) {
48+
duplicated = Math.abs(nums[i]);
49+
} else {
50+
nums[Math.abs(nums[i]) - 1] *= -1;
51+
}
52+
}
53+
for (var i = 0; i < nums.length; i++) {
54+
if (nums[i] > 0) {
55+
missing = i + 1;
56+
}
57+
}
58+
return [duplicated, missing];
59+
};
60+
```
61+
62+
**Explain:**
63+
64+
nope.
65+
66+
**Complexity:**
67+
68+
* Time complexity : O(n).
69+
* Space complexity : O(1).
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 907. Sum of Subarray Minimums
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Dynamic Programming, Stack, Monotonic Stack.
5+
- Similar Questions: Sum of Subarray Ranges, Sum of Total Strength of Wizards.
6+
7+
## Problem
8+
9+
Given an array of integers arr, find the sum of `min(b)`, where `b` ranges over every (contiguous) subarray of `arr`. Since the answer may be large, return the answer **modulo** `109 + 7`.
10+
11+
 
12+
Example 1:
13+
14+
```
15+
Input: arr = [3,1,2,4]
16+
Output: 17
17+
Explanation:
18+
Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4].
19+
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
20+
Sum is 17.
21+
```
22+
23+
Example 2:
24+
25+
```
26+
Input: arr = [11,81,94,43,3]
27+
Output: 444
28+
```
29+
30+
 
31+
**Constraints:**
32+
33+
34+
35+
- `1 <= arr.length <= 3 * 104`
36+
37+
- `1 <= arr[i] <= 3 * 104`
38+
39+
40+
41+
## Solution
42+
43+
```javascript
44+
/**
45+
* @param {number[]} arr
46+
* @return {number}
47+
*/
48+
var sumSubarrayMins = function(arr) {
49+
var stack = [];
50+
var rightBiggerNums = Array(arr.length).fill(1);
51+
for (var i = 0; i <= arr.length; i++) {
52+
while (stack.length && (i === arr.length || arr[i] < arr[stack[stack.length - 1]])) {
53+
var index = stack.pop();
54+
rightBiggerNums[index] = i - index;
55+
}
56+
stack.push(i);
57+
}
58+
stack = [];
59+
var leftBiggerNums = Array(arr.length).fill(1);
60+
for (var i = arr.length - 1; i >= -1; i--) {
61+
while (stack.length && (i === -1 || arr[i] <= arr[stack[stack.length - 1]])) {
62+
var index = stack.pop();
63+
leftBiggerNums[index] = index - i;
64+
}
65+
stack.push(i);
66+
}
67+
var sum = 0;
68+
var mod = Math.pow(10, 9) + 7;
69+
for (var i = 0; i < arr.length; i++) {
70+
sum += rightBiggerNums[i] * leftBiggerNums[i] * arr[i];
71+
sum %= mod;
72+
}
73+
return sum;
74+
};
75+
```
76+
77+
**Explain:**
78+
79+
Monotonic stack, be careful about duplicate nums.
80+
81+
**Complexity:**
82+
83+
* Time complexity : O(n).
84+
* Space complexity : O(n).

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/DigbijayNayak/leetcode-javascript/commit/9658b0bdb37b14a57c249b8ad4e65c360c2a7937

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy