Practice #2
Coddyの「スタック - データ構造シリーズ #1」コースのレッスン 10/13。
次のチャレンジは、スタックを使用するように設計されています。
Stackデータ構造はすでに用意されています。ぜひ活用してください!
チャレンジ
簡単整数配列を受け取り、各要素の(左側にある)最近接のより小さい要素 (nearest smaller element) を含む配列を返す関数 nse を作成してください。要素の左側により小さい要素が存在しない場合は、-1 を返してください。
この問題を解決するために、提供されている Stack を使用してください!
例:
入力: [4, 5, 2, 10, 8]
期待される出力: [-1, 4, -1, 2, 2]
自分で試してみよう
#include <stdio.h>
#include <stdlib.h>
#include "nse.h"
int main() {
int* a = (int*)malloc(sizeof(int) * 1000);
int aSize = 0;
int x;
while (scanf("%d", &x) == 1) {
a[aSize++] = x;
}
int returnSize;
int* result = nse(a, aSize, &returnSize);
for (int i = 0; i < returnSize; i++) {
printf("%d\n", result[i]);
}
free(a);
return 0;
}