728x90
반응형
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 = 0;
int bobCandyNum = 0;
int DivideCandyNum = 0;
int* ExchangeArr = (int*)malloc(sizeof(int) * 2);
for (i = 0; i != aliceSizesSize; ++i)
{
aliceCandyNum += aliceSizes[i];
}
for (i = 0; i != bobSizesSize; ++i)
{
bobCandyNum += bobSizes[i];
}
DivideCandyNum = (aliceCandyNum + bobCandyNum) / 2;
int aliceNeedCandyNum = DivideCandyNum - aliceCandyNum;
int FindAnswer = 0;
if (aliceNeedCandyNum == 0)
{
for (int j = 0; j != aliceSizesSize; ++j)
{
for (int k = 0; k != bobSizesSize; ++k)
{
if (aliceSizes[j] == bobSizes[k])
{
ExchangeArr[0] = aliceSizes[j];
ExchangeArr[1] = bobSizes[k];
FindAnswer = 1;
break;
}
else
{
// Do Nothing
}
}
if (FindAnswer == 1)
{
break;
}
else
{
// Do Nothing
}
}
}
else
{
for (int j = 0; j != aliceSizesSize; ++j)
{
for (int k = 0; k != bobSizesSize; ++k)
{
if ((aliceSizes[j] + aliceNeedCandyNum) == bobSizes[k])
{
ExchangeArr[0] = aliceSizes[j];
ExchangeArr[1] = bobSizes[k];
FindAnswer = 1;
break;
}
else
{
// Do Nothing
}
}
if (FindAnswer == 1)
{
break;
}
else
{
// Do Nothing
}
}
}
*returnSize = 2;
return ExchangeArr;
}
728x90
반응형
'코딩 문제' 카테고리의 다른 글
[LeetCode] 1614. Maximum Nesting Depth of the Parenthesesm, C (0) | 2023.01.01 |
---|---|
[LeetCode] 2283. Check if Number Has Equal Digit Count and Digit Value, C (0) | 2023.01.01 |
[LeetCode] 1941. Check if All Characters Have Equal Number of Occurrences, C (0) | 2022.12.31 |
[LeetCode] 383. Ransom Note, C (0) | 2022.12.30 |
[LeetCode] 876. Middle of the Linked List, C (0) | 2022.12.28 |