Skip to content

Commit

Permalink
Added zad6
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelkubek committed Nov 18, 2023
1 parent 703d8f3 commit a21575f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ _WDP\* MIM UW_
- [zad_4](./src/cw6/zad4/zad4.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad. 5.]
- [katastrofy](./src/cw6/zad5/katastrofy.cpp) (C++) :white_check_mark::microscope:
- [zad. 6.]
- [zad_6](./src/cw6/zad6/zad6.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad. 7.]
- [zad_7](./src/cw6/zad7/zad7_skyline.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad. 8.]
Expand Down
50 changes: 50 additions & 0 deletions src/cw6/zad6/zad6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include<stack>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int *widoczne(int t[], int n){
int *wynik = (int*)calloc((size_t)n, sizeof(int));
int powtorki = 0;
stack<int> s;
for(int i = 0; i < n; i++){
while(!s.empty() && s.top() <= t[i]){
if(s.top() == t[i])
powtorki++;
s.pop();
wynik[i]++;
}
if(!s.empty())
wynik[i]++;
s.push(t[i]);
for(int j = 0; j < powtorki; j++) s.push(t[i]);
powtorki = 0;
}

while(!s.empty()) s.pop();
for(int i = n - 1; i >= 0; i--){
while(!s.empty() && s.top() <= t[i]){
if(s.top() == t[i])
powtorki++;
s.pop();
wynik[i]++;
}
if(!s.empty())
wynik[i]++;
s.push(t[i]);
for(int j = 0; j < powtorki; j++) s.push(t[i]);
powtorki = 0;
}
return wynik;
}

int main(){
// int n = 7, t[] = {1, 8, 6, 7, 4, 8, 9};
int n = 5, t[] = {1, 8, 5, 6, 4};
int *wynik = widoczne(t, n);
for(int i = 0; i < n; i++)
printf("%i ", wynik[i]);
printf("\n");
return 0;
}

0 comments on commit a21575f

Please sign in to comment.