diff --git a/Jongeun/Day28/213_HouseRobberII.cpp b/Jongeun/Day28/213_HouseRobberII.cpp new file mode 100644 index 0000000..bc06d40 --- /dev/null +++ b/Jongeun/Day28/213_HouseRobberII.cpp @@ -0,0 +1,34 @@ +class Solution +{ +public: + int rob(vector &nums) + { + + if (nums.size() == 1) + { + return nums[0]; + } + + int first = 0, second1 = 0; + + for (int i = 0; i < nums.size() - 1; i++) + { + + int tempt = first; + first = second1; + second1 = max(tempt + nums[i], second1); + } + + first = 0; + int second2 = 0; + + for (int i = 1; i < nums.size(); i++) + { + int tempt = first; + first = second2; + second2 = max(tempt + nums[i], second2); + } + + return max(second1, second2); + } +}; diff --git a/Jongeun/Day28/295_FindMedianfromDataStream.cpp b/Jongeun/Day28/295_FindMedianfromDataStream.cpp new file mode 100644 index 0000000..53480c2 --- /dev/null +++ b/Jongeun/Day28/295_FindMedianfromDataStream.cpp @@ -0,0 +1,73 @@ +class MedianFinder +{ +public: + MedianFinder() + { + } + + void addNum(int num) + { + if (total == 0) + { + maxHeap.push(num); + total++; + } + else + { + if (total % 2 == 0) + { + if (maxHeap.top() < num) + { + minHeap.push(num); + int temp = minHeap.top(); + minHeap.pop(); + maxHeap.push(temp); + } + else + { + maxHeap.push(num); + } + } + else + { + if (maxHeap.top() < num) + { + minHeap.push(num); + } + else + { + maxHeap.push(num); + int temp = maxHeap.top(); + maxHeap.pop(); + minHeap.push(temp); + } + } + + total++; + } + } + + double findMedian() + { + if (total % 2 == 0) + { + return ((double)maxHeap.top() + minHeap.top()) / 2; + } + else + { + return maxHeap.top(); + } + } + +private: + int total = 0; + priority_queue maxHeap; + priority_queue, std::greater> minHeap; +}; + +/** + * Your MedianFinder object will be instantiated and called as such: + * MedianFinder* obj = new MedianFinder(); + * obj->addNum(num); + * double param_2 = obj->findMedian(); + */ diff --git a/Jongeun/Day28/994_RottingOranges.cpp b/Jongeun/Day28/994_RottingOranges.cpp new file mode 100644 index 0000000..4de92e6 --- /dev/null +++ b/Jongeun/Day28/994_RottingOranges.cpp @@ -0,0 +1,58 @@ +class Solution +{ +public: + int orangesRotting(vector> &grid) + { + int m = grid.size(); + int n = grid[0].size(); + queue>> q; + + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (grid[i][j] == 2) + { + q.push({0, {i, j}}); + } + } + } + + int totalTime = 0; + while (!q.empty()) + { + auto cell = q.front(); + q.pop(); + + int i = cell.second.first; + int j = cell.second.second; + int time = cell.first; + + if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0 || grid[i][j] == 3) + { + totalTime = max(totalTime, time - 1); + continue; + } + + grid[i][j] = 3; // mark it as already visited + + q.push({time + 1, {i + 1, j}}); + q.push({time + 1, {i - 1, j}}); + q.push({time + 1, {i, j + 1}}); + q.push({time + 1, {i, j - 1}}); + } + + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (grid[i][j] == 1) + { + return -1; + } + } + } + + return totalTime; + } +};