classSolution { public: intcountHillValley(vector<int>& nums){ int n = nums.size(); vector<int> vec; for (int i = 0; i < n; i ++) { while (i + 1 < n && nums[i] == nums[i + 1]) i ++; vec.push_back(nums[i]); } int res = 0; // for (auto num: vec) cout << num << endl; for (int i = 1; i < vec.size() - 1; i ++) { if (vec[i - 1] > vec[i] && vec[i + 1] > vec[i]) res ++; if (vec[i - 1] < vec[i] && vec[i] > vec[i + 1]) res ++; } return res; } };
classSolution { public: intcountCollisions(string directions){ int ans = 0; bool flag = false; for (int i = 0; i < directions.size(); i ++) { if (directions[i] == 'R' || directions[i] == 'S') flag = true; elseif (flag) ans ++; } flag = false; for (int i = directions.size() - 1; i >= 0; i --) { if (directions[i] == 'L' || directions[i] == 'S') flag = true; elseif (flag) ans ++; } return ans; } };