Don't have an account? Register
Already have an account? Login
Thank you for completing this questionnaire. Your results have been saved.
This questionnaire contains 3 problems.
Thank you for your responses. Your questionnaire has been successfully completed and submitted.
Question:
How is the diamond problem solved in C++?
Using virtual inheritance.
Return the minimum number of arrivals to be discarded so that every w-day window contains at most m occurrences of each type.
w
m
You are given two integers w and m, and an integer array arrivals, where arrivals[i] is the type of item arriving on day i (days are 1-indexed).
arrivals
arrivals[i]
i
Items are managed according to the following rules:
[max(1, i - w + 1), i]
Example 1:
Input: arrivals = [1,2,1,3,1], w = 4, m = 2
Output: 0
Explanation:
[1, 2, 1]
[1, 2, 1, 3]
[2, 1, 3, 1]
There are no discarded items, so return 0.
Example 2:
Input: arrivals = [1,2,3,3,3,4], w = 3, m = 2
Output: 1
[1, 2]
[1, 2, 3]
[2, 3, 3]
[3, 3, 3]
[3, 4]
Item 3 on day 5 is discarded, and this is the minimum number of arrivals to discard, so return 1.
Constraints:
1 <= arrivals.length <= 105
1 <= arrivals[i] <= 105
1 <= w <= arrivals.length
1 <= m <= w
First step is to translate the obtuse problem definition into something clear and crisp.
Requirement: find No of items to be discarded
Givens:
C++:
class Logger{ public: enum class LogLevel { INFO, DEBUG, ERROR }; Logger(LogLevel level) : currentLevel(level) {} void log(const std::string& message, LogLevel level) { if (level >= currentLevel) { if (level == LogLevel::INFO) { std::cout << "INFO: "; } else if (level == LogLevel::DEBUG) { std::cout << "DEBUG: "; } else if (level == LogLevel::ERROR) { std::cout << "ERROR: "; } std::cout << message << std::endl; } } private: LogLevel currentLevel; }; // TYPE: Basic Sliding Window // Whole difficulty is translating the incredibly obtuse description. class Solution { public: int minArrivalsToDiscard(vector<int>& arrivals, int w, int m) { Logger logger(Logger::LogLevel::DEBUG); map<int,int> counter; int discartionCount = 0; unordered_set<int> discardedIndices; for (int t = 0; t < arrivals.size(); ++t) { //logger.log("Day: " + to_string(t), Logger::LogLevel::DEBUG); //for (const pair<int, int>& element : counter){ // int key = element.first; // int value = element.second; // logger.log("Key: " + to_string(key), Logger::LogLevel::DEBUG); // logger.log("Value: " + to_string(value), Logger::LogLevel::DEBUG); //} // Sliding counter maintenance. int r = t; int l = max(0, t - w + 1); ++counter[arrivals[t]]; //logger.log("Added " + to_string(arrivals[t]) + " to counter.", Logger::LogLevel::DEBUG); if ((r - l + 1) == w && t >= w && !discardedIndices.count(l-1)>0) { // Tricky -- no point in removing already discarded items. if (counter[arrivals[l-1]] == 1){ counter.erase(arrivals[l-1]); } else { --counter[arrivals[l-1]]; } } // Main constraint processing. int no = counter[arrivals[t]]; if (no > m) { discartionCount += 1; discardedIndices.insert(t); int val = arrivals[t]; counter[val]-=1; if (counter[val]==0){ counter.erase(val); // Tricky -- need to delete from counter as well discards. }; } //logger.log("Discartion count: " + to_string(discartionCount), Logger::LogLevel::DEBUG); } return discartionCount; } };
Instantiate a 2D pixellatedImage variable.
pixellatedImage
Language is C++ 11
int imagePixels[9][4];
int height = 9;
int width = 4;
int[height][width] imagePixel;
int<int> imagePixel;