Skip to content

Commit d611090

Browse files
authored
Merge branch 'master' into add/next_permutation
2 parents 4072c96 + 799bd88 commit d611090

6 files changed

+121
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Find Subarrays With Equal Sum
3+
https://leetcode.com/problems/find-subarrays-with-equal-sum/description/
4+
5+
Given a 0-indexed integer array nums, determine whether there exist two subarrays of length 2 with equal sum. Note that the two subarrays must begin at different indices.
6+
7+
Return true if these subarrays exist, and false otherwise.
8+
9+
A subarray is a contiguous non-empty sequence of elements within an array.
10+
11+
12+
13+
Example 1:
14+
15+
Input: nums = [4,2,4]
16+
Output: true
17+
Explanation: The subarrays with elements [4,2] and [2,4] have the same sum of 6.
18+
19+
20+
Example 2:
21+
22+
Input: nums = [1,2,3,4,5]
23+
Output: false
24+
Explanation: No two subarrays of size 2 have the same sum.
25+
26+
Example 3:
27+
28+
Input: nums = [0,0,0]
29+
Output: true
30+
Explanation: The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0.
31+
Note that even though the subarrays have the same content, the two subarrays are considered different because they are in different positions in the original array.
32+
*/
33+
34+
/**
35+
* @param {number[]} nums
36+
* @return {boolean}
37+
*/
38+
var findSubarrays = function (nums) {
39+
const sumsSeen = new Set();
40+
41+
for (let i = 0; i < nums.length - 1; i++) {
42+
if (sumsSeen.has(nums[i] + nums[i + 1])) {
43+
return true;
44+
}
45+
sumsSeen.add(nums[i] + nums[i + 1]);
46+
}
47+
48+
return false;
49+
};
50+
51+
module.exports.findSubarrays = findSubarrays;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
You are given a binary string s. In one second, all occurrences of "01" are simultaneously replaced with "10". This process repeats until no occurrences of "01" exist.
3+
4+
Return the number of seconds needed to complete this process.
5+
6+
7+
8+
Example 1:
9+
10+
Input: s = "0110101"
11+
Output: 4
12+
Explanation:
13+
After one second, s becomes "1011010".
14+
After another second, s becomes "1101100".
15+
After the third second, s becomes "1110100".
16+
After the fourth second, s becomes "1111000".
17+
No occurrence of "01" exists any longer, and the process needed 4 seconds to complete,
18+
so we return 4.
19+
Example 2:
20+
21+
Input: s = "11100"
22+
Output: 0
23+
Explanation:
24+
No occurrence of "01" exists in s, and the processes needed 0 seconds to complete,
25+
so we return 0.
26+
27+
28+
Constraints:
29+
30+
1 <= s.length <= 1000
31+
s[i] is either '0' or '1'.
32+
*/
33+
34+
35+
/**
36+
* @param {string} s
37+
* @return {number}
38+
*/
39+
var secondsToRemoveOccurrences = function(s) {
40+
let result = 0;
41+
while (true) {
42+
const replaced = s.replaceAll('01', '10');
43+
if (s === replaced) return result;
44+
s = replaced;
45+
result += 1;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const assert = require('assert');
2+
const findSubarrays = require('../../LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum').findSubarrays;
3+
4+
var test = function () {
5+
assert.equal(findSubarrays([4,2,4]), true);
6+
assert.equal(findSubarrays([1,2,3,4,5]), false);
7+
assert.equal(findSubarrays([0,0,0]), true);
8+
}
9+
10+
module.exports.test = test;

LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const assert = require('assert');
22
const maxSum = require('../../LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum').maxSum;
33

44
function test() {
5-
assert.equal(maxSum([[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]));
6-
assert.equal(maxSum([[1,2,3],[4,5,6],[7,8,9]]));
5+
assert.equal(maxSum([[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]), 30);
6+
assert.equal(maxSum([[1,2,3],[4,5,6],[7,8,9]]), 35);
77
}
88

99
module.exports.test = test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const assert = require('assert');
2+
const secondsToRemoveOccurrences = require('../../LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String').secondsToRemoveOccurrences;
3+
4+
function test() {
5+
assert.equal(secondsToRemoveOccurrences("0110101"), 4);
6+
assert.equal(secondsToRemoveOccurrences("11100"), 0)
7+
};
8+
9+
module.exports.test = test

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
6363
| [Lowest Common Ancestor of a Binary Tree ](/LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree.js) | Medium | https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ |
6464
| [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ |
6565
| [Next Permutation](/LeetcodeProblems/Algorithms/Next_Permutation.js) | Medium | https://leetcode.com/problems/next-permutation/ |
66+
| [Time Needed to Rearrange a Binary String](/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js)| Medium | https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ |
67+
| [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ |
6668
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
6769
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
6870
| [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |

0 commit comments

Comments
 (0)