알고리즘/leetcode26 953. Verifying an Alien Dictionary class Solution { public: bool isAlienSorted(vector& words, string order) { int mapping[26]; for (int i = 0; i < 26; ++i) mapping[order[i] - 'a'] = i; for (auto& w : words) { for (auto& c : w) { c = mapping[c - 'a']; } } return is_sorted(words.begin(), words.end()); } }; 특수한 order를 일반적인 order로(integer order) 변환 2022. 9. 15. 1588. Sum of All Odd Length Subarrays class Solution { public: int sumOddLengthSubarrays(vector& arr) { int n = arr.size(); int ans = 0; for (int i = 0; i < n; ++i) { int k = (i + 1) * (n - i); // total_left * total_right k = ((i+2)/2)*((n-i+1)/2) + ((i+1)/2)*((n-i)/2); //cout 2022. 9. 13. 496. Next Greater Element I class Solution { public: vector nextGreaterElement(vector& nums1, vector& nums2) { unordered_map greater; stack left; for (auto n : nums2) { while (!left.empty() && left.top() < n) { greater[left.top()] = n; left.pop(); } left.push(n); } vector ans(nums1.size()); for (int i = 0; i < nums1.size(); ++i) { ans[i] = greater.count(nums1[i]) ? greater[nums1[i]] : -1; } return ans; } }; 2022. 9. 13. 57. Insert Interval class Solution { public: vector insert(vector& vals, vector& val) { int n = vals.size(); int i = 0; vector ans; while (i < n && vals[i][1] < val[0]) { ans.push_back(vals[i]); ++i; } while (i < n && vals[i][0] 2022. 9. 1. 이전 1 2 3 4 ··· 7 다음