Programming/C C++

Counting Valleys

728x90

www.hackerrank.com/challenges/counting-valleys/problem

사용자로부터 U, D (uphills, downhills) 문자열을 입력받고 처음시작을 해수면(0)으로 해서 valley의 수를 구하는 코드를 작성해야 한다.

int countingValleys(int steps, char* path) {
    int valley=0;
    int pre_level = 0,level =0;
    for(int i=0;i<steps;i++){
        if(path[i] == 'U'){
            level +=1;
        }
        else{
            level -=1;
        }
        if(pre_level <0 && level == 0)  valley+=1;
        else {
        pre_level = level;
        }
        
    }
    return valley;
}

valley나 mountain이나 항상 그 끝은 sea level에 닿아야하기 때문에 이를 이용해 코드를 작성했다.

 

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();
char* ltrim(char*);
char* rtrim(char*);

int parse_int(char*);

/*
 * Complete the 'countingValleys' function below.
 *
 * The function is expected to return an INTEGER.
 * The function accepts following parameters:
 *  1. INTEGER steps
 *  2. STRING path
 */

int countingValleys(int steps, char* path) {
    int valley=0;
    int pre_level = 0,level =0;
    for(int i=0;i<steps;i++){
        if(path[i] == 'U'){
            level +=1;
        }
        else{
            level -=1;
        }
        if(pre_level <0 && level == 0)  valley+=1;
        else {
        pre_level = level;
        }
        
    }
    return valley;
}

int main()
{
    FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

    int steps = parse_int(ltrim(rtrim(readline())));

    char* path = readline();

    int result = countingValleys(steps, path);

    fprintf(fptr, "%d\n", result);

    fclose(fptr);

    return 0;
}

char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;

    char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) {
            break;
        }

        data_length += strlen(cursor);

        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
            break;
        }

        alloc_length <<= 1;

        data = realloc(data, alloc_length);

        if (!data) {
            data = '\0';

            break;
        }
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';

        data = realloc(data, data_length);

        if (!data) {
            data = '\0';
        }
    } else {
        data = realloc(data, data_length + 1);

        if (!data) {
            data = '\0';
        } else {
            data[data_length] = '\0';
        }
    }

    return data;
}

char* ltrim(char* str) {
    if (!str) {
        return '\0';
    }

    if (!*str) {
        return str;
    }

    while (*str != '\0' && isspace(*str)) {
        str++;
    }

    return str;
}

char* rtrim(char* str) {
    if (!str) {
        return '\0';
    }

    if (!*str) {
        return str;
    }

    char* end = str + strlen(str) - 1;

    while (end >= str && isspace(*end)) {
        end--;
    }

    *(end + 1) = '\0';

    return str;
}

int parse_int(char* str) {
    char* endptr;
    int value = strtol(str, &endptr, 10);

    if (endptr == str || *endptr != '\0') {
        exit(EXIT_FAILURE);
    }

    return value;
}

△ 전체 코드

SMALL

'Programming > C C++' 카테고리의 다른 글

Eletronics Shop  (0) 2020.11.08
[DS] Get Node Value  (0) 2020.10.04
Drawing Book  (0) 2020.09.27
[DS] Reverse a doubly-linked list  (0) 2020.09.27
[DS] Delete a Node  (0) 2020.09.20