Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions submissions/Vaishnavi_Mishra/Question1/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*Problem 402: Remove K Digits
*Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.
*/

#include <string>
#include <vector>
using namespace std;

class Solution {
public:
string removeKdigits(string num, int k) {
if (num.length() == k)
return "0";

string ans;
vector<char> stack;

for (int i = 0; i < num.length(); ++i) {
while (k > 0 && !stack.empty() && stack.back() > num[i]) {
stack.pop_back();
--k;
}
stack.push_back(num[i]);
}

while (k-- > 0)
stack.pop_back();

for (const char c : stack) {
if (c == '0' && ans.empty())
continue;
ans += c;
}

return ans.empty() ? "0" : ans;
}
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions submissions/Vaishnavi_Mishra/Question2/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*Problem 901: Online Stock Span
*Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day.
*The span of the stock's price in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to the price of that day.
*/

#include <stack>
using namespace std;

class StockSpanner {
public:
int next(int price) {
int span = 1;
while (!stack.empty() && stack.top().first <= price)
span += stack.top().second, stack.pop();
stack.emplace(price, span);
return span;
}

private:
stack<pair<int, int>> stack; // (price, span)
};