-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #523 from vipul-2003/vipul2
added file reshape the matrix in leetcode folder
- Loading branch information
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* @file "Hotel Prices.cpp" | ||
* @author Vipul Kumar Singh | ||
* @brief (https://www.hackerrank.com/challenges/hotel-prices/problem) | ||
* @version 0.1 | ||
* @date 2021-10-02 | ||
* | ||
* @copyright Copyright (c) 2021 | ||
* | ||
*/ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
class HotelRoom { | ||
|
||
private: | ||
int bedrooms_; | ||
int bathrooms_; | ||
|
||
|
||
public: | ||
HotelRoom(int bedrooms, int bathrooms) | ||
{ | ||
bedrooms_ = (bedrooms); | ||
bathrooms_ = (bathrooms) ; | ||
|
||
} | ||
|
||
int get_price() | ||
{ | ||
int room_price = 50*bedrooms_ + 100*bathrooms_ ; | ||
|
||
return room_price; | ||
} | ||
|
||
}; | ||
|
||
class HotelApartment : public HotelRoom { | ||
public: | ||
HotelApartment(int bedrooms, int bathrooms) | ||
: HotelRoom(bedrooms + 2, bathrooms) {} | ||
|
||
int get_price() { | ||
int hotel_price = ( HotelRoom::get_price()); | ||
return hotel_price ; | ||
} | ||
}; | ||
|
||
int main() { | ||
int n; | ||
cin >> n; | ||
vector<HotelRoom*> rooms; | ||
for (int i = 0; i < n; ++i) { | ||
string room_type; | ||
int bedrooms; | ||
int bathrooms; | ||
cin >> room_type >> bedrooms >> bathrooms; | ||
if (room_type == "standard") { | ||
rooms.push_back(new HotelRoom(bedrooms, bathrooms)); | ||
} else { | ||
rooms.push_back(new HotelApartment(bedrooms, bathrooms)); | ||
} | ||
} | ||
|
||
int total_profit = 0; | ||
for (auto room : rooms) { | ||
total_profit += room->get_price(); | ||
} | ||
cout << total_profit << endl; | ||
|
||
for (auto room : rooms) { | ||
delete room; | ||
} | ||
rooms.clear(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @file "reshape the matrix.cpp" | ||
* @author Vipul Kumar Singh | ||
* @brief (https://leetcode.com/problems/reshape-the-matrix/) | ||
* @version 0.1 | ||
* @copyright Copyright (c) 2021 | ||
*/ | ||
class Solution | ||
{ | ||
public: | ||
vector<vector<int>> matrixReshape(vector<vector<int>> &mat, int r, int c) // creation of vector type function using 2d matrix | ||
{ | ||
int row = mat.size(), row1 = 0; | ||
int column = mat[0].size(), column1 = 0; | ||
|
||
/* | ||
Here , | ||
the variable row is assigned the row size of the mat matrix | ||
the variable column is assigned the column size of the mat matrix | ||
*/ | ||
|
||
if (row * column != r * c) // edge case for better performance and to avoid errors if the size of two (2D matrix) are not same | ||
return mat; | ||
|
||
vector<vector<int>> v(r, vector<int>(c)); // creation of 2d matrix | ||
|
||
// traversing the matrix and perform the some specified actions | ||
for (int i = 0; i < r; i++) | ||
{ | ||
for (int j = 0; j < c; j++) | ||
{ | ||
v[i][j] = mat[row1][column1]; | ||
++column1; | ||
|
||
if (row1 < row && column1 == column) | ||
{ | ||
++row1; | ||
column1 = 0; | ||
} | ||
} | ||
} | ||
|
||
return v; | ||
} | ||
}; | ||
|
||
/* | ||
Constraints: | ||
m == mat.length | ||
n == mat[i].length | ||
1 <= m, n <= 100 | ||
-1000 <= mat[i][j] <= 1000s | ||
1 <= r, c <= 300 | ||
Some Test case : | ||
1 . Input: mat = [[1,2],[3,4]], r = 2, c = 4 | ||
Output: [[1,2],[3,4]] | ||
2. Input: mat = [[1,2],[3,4]], r = 1, c = 4 | ||
Output: [[1,2,3,4]] | ||
*/ |