diff --git a/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js b/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js new file mode 100644 index 0000000..87e5b89 --- /dev/null +++ b/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js @@ -0,0 +1,47 @@ +/* +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. + +Return the number of seconds needed to complete this process. + + + +Example 1: + +Input: s = "0110101" +Output: 4 +Explanation: +After one second, s becomes "1011010". +After another second, s becomes "1101100". +After the third second, s becomes "1110100". +After the fourth second, s becomes "1111000". +No occurrence of "01" exists any longer, and the process needed 4 seconds to complete, +so we return 4. +Example 2: + +Input: s = "11100" +Output: 0 +Explanation: +No occurrence of "01" exists in s, and the processes needed 0 seconds to complete, +so we return 0. + + +Constraints: + +1 <= s.length <= 1000 +s[i] is either '0' or '1'. +*/ + + +/** + * @param {string} s + * @return {number} + */ + var secondsToRemoveOccurrences = function(s) { + let result = 0; + while (true) { + const replaced = s.replaceAll('01', '10'); + if (s === replaced) return result; + s = replaced; + result += 1; + } +}; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js b/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js index b612df0..066b941 100644 --- a/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js +++ b/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js @@ -2,8 +2,8 @@ const assert = require('assert'); const maxSum = require('../../LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum').maxSum; function test() { - assert.equal(maxSum([[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]])); - assert.equal(maxSum([[1,2,3],[4,5,6],[7,8,9]])); + assert.equal(maxSum([[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]), 30); + assert.equal(maxSum([[1,2,3],[4,5,6],[7,8,9]]), 35); } module.exports.test = test diff --git a/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js b/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js new file mode 100644 index 0000000..c4e2f00 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js @@ -0,0 +1,9 @@ +const assert = require('assert'); +const secondsToRemoveOccurrences = require('../../LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String').secondsToRemoveOccurrences; + +function test() { + assert.equal(secondsToRemoveOccurrences("0110101"), 4); + assert.equal(secondsToRemoveOccurrences("11100"), 0) +}; + +module.exports.test = test diff --git a/README.md b/README.md index 6085d84..673ad78 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ To run a specific problem in your console run `node ` (e.g. | [Construct Binary Tree from Preorder and Inorder Traversal ](/LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js) | Medium | https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ | | [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/ | | [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ | +| [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/ | | [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ | | [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ | | [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/ |