-
Counting Valleys해커랭크(HackerRank) 2018. 8. 19. 17:17반응형
1. 문제
2. 알고리즘
키워드 - 구현
3. 코드
1234567891011121314151617181920212223242526272829303132333435363738#include <bits/stdc++.h>using namespace std;int countingValleys(int n, string s) {// Complete this function// 초기 위치는 0int position = 0;// 깊이int depth = 0;for(int i =0; i<n; i++) {if(s[i] == 'D') {position = position - 1;//cout << "D " << position << endl;} else {position = position + 1;//cout << "U " << position << endl;}if(0 == position && s[i] == 'U') {depth++;}}return depth;}int main() {int n;cin >> n;string s;cin >> s;int result = countingValleys(n, s);cout << result << endl;return 0;}cs 반응형'해커랭크(HackerRank)' 카테고리의 다른 글
Reverse a linked list (0) 2018.08.19 Compare two linked lists (0) 2018.08.19 Insert a node at a specific position in a linked list (0) 2018.08.19 CamelCase (0) 2018.08.19 Interview Preparation KitArraysArrays: Left Rotation (0) 2018.07.04