File tree Expand file tree Collapse file tree 2 files changed +164
-0
lines changed
algorithms/dynamic_programming
data_structures/queues/queue Expand file tree Collapse file tree 2 files changed +164
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < vector>
3+ #include < climits>
4+ using namespace std ;
5+
6+ // Function to find minimum number of attempts needed
7+ int eggDrop (int eggs, int floors) {
8+ vector<vector<int >> dp (eggs + 1 , vector<int >(floors + 1 , 0 ));
9+
10+ // Base cases
11+ for (int i = 1 ; i <= eggs; i++)
12+ dp[i][0 ] = 0 , dp[i][1 ] = 1 ;
13+ for (int j = 1 ; j <= floors; j++)
14+ dp[1 ][j] = j;
15+
16+ // Fill the rest using DP
17+ for (int e = 2 ; e <= eggs; e++) {
18+ for (int f = 2 ; f <= floors; f++) {
19+ dp[e][f] = INT_MAX;
20+ for (int k = 1 ; k <= f; k++) {
21+ int attempts = 1 + max (dp[e - 1 ][k - 1 ], dp[e][f - k]);
22+ dp[e][f] = min (dp[e][f], attempts);
23+ }
24+ }
25+ }
26+
27+ return dp[eggs][floors];
28+ }
29+
30+ int main () {
31+ int eggs, floors;
32+ cout << " Enter number of eggs: " ;
33+ cin >> eggs;
34+ cout << " Enter number of floors: " ;
35+ cin >> floors;
36+
37+ int minAttempts = eggDrop (eggs, floors);
38+ cout << " Minimum number of attempts needed: " << minAttempts << endl;
39+
40+ return 0 ;
41+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < stdexcept>
3+
4+ template <typename T>
5+ class Deque {
6+ private:
7+ struct Node {
8+ T data;
9+ Node* prev;
10+ Node* next;
11+ };
12+
13+ Node* front;
14+ Node* rear;
15+ int size;
16+
17+ public:
18+ Deque () : front(nullptr ), rear(nullptr ), size(0 ) {}
19+
20+ ~Deque () {
21+ while (size > 0 ) {
22+ deleteFront ();
23+ }
24+ }
25+
26+ void insertFront (T value) {
27+ Node* newNode = new Node{value, nullptr , front};
28+ if (front) {
29+ front->prev = newNode;
30+ }
31+ front = newNode;
32+ if (!rear) {
33+ rear = front;
34+ }
35+ ++size;
36+ }
37+
38+ void insertRear (T value) {
39+ Node* newNode = new Node{value, rear, nullptr };
40+ if (rear) {
41+ rear->next = newNode;
42+ }
43+ rear = newNode;
44+ if (!front) {
45+ front = rear;
46+ }
47+ ++size;
48+ }
49+
50+ void deleteFront () {
51+ if (front) {
52+ Node* temp = front;
53+ front = front->next ;
54+ if (front) {
55+ front->prev = nullptr ;
56+ } else {
57+ rear = nullptr ;
58+ }
59+ delete temp;
60+ --size;
61+ } else {
62+ throw std::out_of_range (" Deque is empty" );
63+ }
64+ }
65+
66+ void deleteRear () {
67+ if (rear) {
68+ Node* temp = rear;
69+ rear = rear->prev ;
70+ if (rear) {
71+ rear->next = nullptr ;
72+ } else {
73+ front = nullptr ;
74+ }
75+ delete temp;
76+ --size;
77+ } else {
78+ throw std::out_of_range (" Deque is empty" );
79+ }
80+ }
81+
82+ T getFront () const {
83+ if (front) {
84+ return front->data ;
85+ } else {
86+ throw std::out_of_range (" Deque is empty" );
87+ }
88+ }
89+
90+ T getRear () const {
91+ if (rear) {
92+ return rear->data ;
93+ } else {
94+ throw std::out_of_range (" Deque is empty" );
95+ }
96+ }
97+
98+ int getSize () const {
99+ return size;
100+ }
101+
102+ bool isEmpty () const {
103+ return size == 0 ;
104+ }
105+ };
106+
107+ int main () {
108+ Deque<int > dq;
109+ dq.insertFront (10 );
110+ dq.insertRear (20 );
111+ dq.insertFront (5 );
112+
113+ std::cout << " Front: " << dq.getFront () << std::endl;
114+ std::cout << " Rear: " << dq.getRear () << std::endl;
115+ std::cout << " Size: " << dq.getSize () << std::endl;
116+
117+ dq.deleteFront ();
118+ dq.deleteRear ();
119+
120+ std::cout << " Size after deletions: " << dq.getSize () << std::endl;
121+
122+ return 0 ;
123+ }
You can’t perform that action at this time.
0 commit comments