본문 바로가기

코딩 문제31

LeetCode 62. Unique Path Level은 Medium으로 책정되었지만 좀 더 쉬운 난이도의 문제이다. 데이터 구조 Queue를 사용하고 Dynamic Programming 개념을 차용하면 풀 수 있다. class Solution { public: int uniquePaths(int m, int n) { int arr[100][100] = { 0 }; int AlreadyQueue[100][100] = { 0 }; queue grid; pair SearchGrid; grid.push(make_pair(0, 0)); arr[0][0] = 1; while (!grid.empty()) { SearchGrid = grid.front(); grid.pop(); // Go Down if (SearchGrid.first < (m - 1)){ ar.. 2023. 7. 27.
LeetCode 1768 Merge String Alternately 간단한 아이디어로 풀 수 있는 쉬운 문제이다. String Class 메서드 사용에 익숙해야 한다. 훨씬 간단하게도 짤수 있는데! class Solution { public: string mergeAlternately(string word1, string word2) { unsigned int minLength = 0; unsigned int Lengthword1 = word1.length(); unsigned int Lengthword2 = word2.length(); string MergedString; string SortedMergedString; if (Lengthword1 > Lengthword2) { for (unsigned int i = 0; i < (Lengthword1 - Lengthw.. 2023. 7. 17.
[LeetCode] 771, Jewels and Stones Skill Use hash table to decrease time complexity to under log(n^2) Others Use string struct member Use ASCII Number of english letter int gstHashTable[60] = { 0 }; class Solution { public: int numJewelsInStones(string jewels, string stones) { memset(&gstHashTable, 0, sizeof(int) * 60); int RetVal = 0; for (int i = 0; i != stones.size(); ++i) { gstHashTable[stones[i] - 65]++; } for (int i = 0; i .. 2023. 2. 14.
[LeetCode] 1720 Decode XORred Array Solution Use principle : A ^ B = C -> A ^ C = B class Solution { public: vector decode(vector& encoded, int first) { vector res; res.push_back(first); for (int i = 0; i != encoded.size(); ++i) { res.push_back(res.back() ^ encoded[i]); } return res; } }; 2023. 1. 29.
[LeetCode] 728. Self Dividing Numbers, C++ Solution - Calculate digit, Check integer is divable by all digits class Solution { public: bool IdSelfDividingNumber(int n) { int k = 10; int a = n; for (int i = 1; i != 5; ++i) { int digit = a % k; if (digit == 0) { return false; } else if (n % digit != 0) { return false; } else { // Do Nothing } a = a / k; if (a == 0) { return true; } } return true; } vector selfDividingNumbers(int left, int .. 2023. 1. 26.
[LeetCode] 2032. Two Out of Three, C++ Solution - Use Three array hash maps - Check if element is in at least two array by sum of three array value is over 1 Time Complexity : O(n) Space Complexity : O(n) class Solution { public: int arrHashMap1[100] = { 0 }; int arrHashMap2[100] = { 0 }; int arrHashMap3[100] = { 0 }; vector twoOutOfThree(vector& nums1, vector& nums2, vector& nums3) { vector res; memset(arrHashMap1, 0, sizeof(int) * .. 2023. 1. 25.