Skip to content

Commit

Permalink
Merge pull request #184 from Oreniscool/master
Browse files Browse the repository at this point in the history
added two new problems from leetcode
  • Loading branch information
JenilGajjar20 authored Oct 17, 2024
2 parents b2f7bb5 + d1b82f1 commit bf2e4ab
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Cpp/2938-separate-white-black-balls/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Separate black and white balls

There are n balls on a table, each ball has a color black or white.

You are given a 0-indexed binary string s of length n, where 1 and 0 represent black and white balls, respectively.

In each step, you can choose two adjacent balls and swap them.

Return the minimum number of steps to group all the black balls to the right and all the white balls to the left.

## Example 1:

Input: s = "101"
Output: 1
Explanation: We can group all the black balls to the right in the following way:

- Swap s[0] and s[1], s = "011".
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.

## Example 2:

Input: s = "100"
Output: 2
Explanation: We can group all the black balls to the right in the following way:

- Swap s[0] and s[1], s = "010".
- Swap s[1] and s[2], s = "001".
It can be proven that the minimum number of steps needed is 2.

## Example 3:

Input: s = "0111"
Output: 0
Explanation: All the black balls are already grouped to the right.

Constraints:

1 <= n == s.length <= 105
s[i] is either '0' or '1'.
26 changes: 26 additions & 0 deletions Cpp/2938-separate-white-black-balls/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
long long minimumSteps(string s) {
//Initialise both steps and ones
long long steps=0;
long long ones=0;
//For every character,
for(char c: s) {
//If the character is 1, count it in the ones variable
if(c=='1') {
ones+=1;
} else if(c=='0'&& ones>0) { //else if it a 0 after 1, add it to steps
steps+=ones;
}
}
//Return steps
return steps;
}
};

static const auto init = [](){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}();
19 changes: 19 additions & 0 deletions Cpp/632.smallest-range-covering-elements-from-k-lists/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Smallest range covering elements from k lists

You have k lists of sorted integers in non-decreasing order. Find the smallest range that includes at least one number from each of the k lists.

We define the range [a, b] is smaller than range [c, d] if b - a < d - c or a < c if b - a == d - c.

## Example 1:

Input: nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
Output: [20,24]
Explanation:
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].

## Example 2:

Input: nums = [[1,2,3],[1,2,3],[1,2,3]]
Output: [1,1]
34 changes: 34 additions & 0 deletions Cpp/632.smallest-range-covering-elements-from-k-lists/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
vector<int> smallestRange(vector<vector<int>>& nums) {
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> minHeap;
int maxEle=INT_MIN;
for(int i=0; i<nums.size(); i++) {
minHeap.push({nums[i][0],i,0});
maxEle=max(maxEle,nums[i][0]);
}
int low=0, high=INT_MAX;

while(true) {
auto current = minHeap.top();
minHeap.pop();
int minEle = current[0], arr_idx=current[1], ele_idx=current[2];
if(maxEle-minEle<high-low) {
high=maxEle;
low=minEle;
}
if(ele_idx+1==nums[arr_idx].size()) break;
int nextEle=nums[arr_idx][ele_idx+1];
minHeap.push({nextEle,arr_idx,ele_idx+1});
maxEle=max(maxEle,nextEle);
}
return {low,high};
}
};

static const auto init = [](){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return 0;
}();

0 comments on commit bf2e4ab

Please sign in to comment.