본문 바로가기

분류 전체보기139

qsort This contents is referenced from here : (https://en.cppreference.com/w/c/algorithm/qsort) There is default qsort() function in c and cpp basic library. If you don't have good function for sorting, qsort is great alternatives. qsort - Sorts elements of given array point in ascending order void qsort(void *ptr, size_t count, size_t size, int (*comp)(const void *, const void *) ); parameters ptr - .. 2023. 1. 2.
[LeetCode] 1614. Maximum Nesting Depth of the Parenthesesm, C Solution - Simple Count of Max Depth Time Complexity O(n) Space Complexity O(n) int maxDpeth(char* s); int main() { char string[] = "(1)+((2))+(((3)))"; int RetVal; RetVal = maxDpeth(string); return 0; } int maxDpeth(char* s) { int Depth = 0; int MaxDepth = 0; int NumOfChar = 0; while (s[NumOfChar] != '\0') { if (s[NumOfChar] == '(') { ++Depth; } else if (s[NumOfChar] == ')') { --Depth; } else {.. 2023. 1. 1.
[LeetCode] 2283. Check if Number Has Equal Digit Count and Digit Value, C Solution - Use Hash Table Time Complexity O(n) Space Complexity O(n) #include bool digitCount(char* pnum); int main() { char string[] = "030"; bool RetVal; RetVal = digitCount(string); return 0; } bool digitCount(char* pnum) { bool bRetVal = true; const int ASCIINumberInit = 48; int i = 0; int NumOfChar = 0; int arrHashTable[10] = { 0 }; while (pnum[NumOfChar] != '\0') { arrHashTable[pnum[NumOfC.. 2023. 1. 1.
[LeetCode] 888. Fair Candy Swap, C Solution Get Total Sum of alice and bob candis number Calculate how many candies should be exchanged, to get equal num each other. But this method Runtime too much, only Beats 7.14% in Leet Code. Topics (presented in LeetCode) Hash Table Binary Search Sorting int* fairCandySwap( int* aliceSizes, int aliceSizesSize, int* bobSizes, int bobSizesSize, int* returnSize) { int i = 0; int aliceCandyNum .. 2022. 12. 31.
[LeetCode] 1941. Check if All Characters Have Equal Number of Occurrences, C Solution - Use Hash Map - Compare Counting Number of each Character (except 0 occurence character) #define NumOfLowerCaseEnglishLetter 26 #define ASCII_INIT_NUM_IDX 97 bool areOccurrencesEqual(char* s) { long int NumOfChar = 0; int RefNum = 0; int arr[NumOfLowerCaseEnglishLetter] = { 0 }; // consists of lowercase English letters bool RetVal = true; while (s[NumOfChar] != '\0') { ++arr[s[NumOfCha.. 2022. 12. 31.
[LeetCode] 383. Ransom Note, C Solution 1 using Hash Map Intuition Declare Hash Map represents charactoer 'a' to 'z' (size 26) Increase Hash Map Index which is in Magazine Note Decrease Hash Map Index which is in ransom Note If one of value in Hash Map under 0, Return false (it means character of Magazine Note doesn't represent ransom Note) When you use index, use should consider ASCII number of 'a' at inital index (97) bool .. 2022. 12. 30.