int cmp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int* avoidFlood(int* rains, int rainsSize, int* returnSize) {
int *result = (int *)malloc(sizeof(int) * rainsSize);
int *nextRain = (int *)malloc(sizeof(int) * rainsSize);
int *fullLakes = (int *)malloc(sizeof(int) * rainsSize);
int fullLakesSize = 0, nextRainSize = 0;
memset(result, 0, sizeof(int) * rainsSize);
memset(nextRain, 0, sizeof(int) * rainsSize);
memset(fullLakes, 0, sizeof(int) * rainsSize);
for (int i = 0; i < rainsSize; i++) {
if (rains[i] > 0) {
for (int j = 0; j < fullLakesSize; j++) {
if (fullLakes[j] == rains[i]) {
*returnSize = 0;
return NULL;
}
}
fullLakes[fullLakesSize++] = rains[i];
nextRain[nextRainSize++] = i;
result[i] = -1;
} else {
if (nextRainSize == 0) {
result[i] = 1;
} else {
qsort(nextRain, nextRainSize, sizeof(int), cmp);
int lakeToDry = -1;
for (int j = 0; j < nextRainSize; j++) {
if (rains[nextRain[j]] > 0) {
lakeToDry = rains[nextRain[j]];
break;
}
}
if (lakeToDry == -1) {
result[i] = 1;
} else {
result[i] = lakeToDry;
for (int j = 0; j < fullLakesSize; j++) {
if (fullLakes[j] == lakeToDry) {
fullLakes[j] = fullLakes[--fullLakesSize];
break;
}
}
for (int j = 0; j < nextRainSize; j++) {
if (rains[nextRain[j]] == lakeToDry) {
nextRain[j] = nextRain[--nextRainSize];
break;
}
}
}
}
}
}
*returnSize = rainsSize;
return result;
}
#[1488] Avoid Flood in The City
1 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Wrong Answer
67/82 cases passed (N/A)
Testcase
[1,2,0,2,3,0,1]
Answer
[]
Expected Answer
[-1,-1,2,-1,-1,1,-1]
you should really invest in a formatter
no
it's because I can submit 2000 characters
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
where is the assignment for the exercise ? how are we supposed to know what it should compute ?
sorry, my mistake: https://leetcode.com/problems/avoid-flood-in-the-city/description/
You can save some characters by not casting the result of malloc. In C, pointers are coerced to and from void * except in varargs
what do you mean?
!f
int cmp(const void* a, const void* b) {
return *(int*)a - *(int*)b;
}
int* avoidFlood(int* rains, int rainsSize, int* returnSize) {
int* result = (int*)malloc(sizeof(int) * rainsSize);
int* nextRain = (int*)malloc(sizeof(int) * rainsSize);
int* fullLakes = (int*)malloc(sizeof(int) * rainsSize);
int fullLakesSize = 0, nextRainSize = 0;
memset(result, 0, sizeof(int) * rainsSize);
memset(nextRain, 0, sizeof(int) * rainsSize);
memset(fullLakes, 0, sizeof(int) * rainsSize);
for (int i = 0; i < rainsSize; i++) {
if (rains[i] > 0) {
for (int j = 0; j < fullLakesSize; j++) {
if (fullLakes[j] == rains[i]) {
*returnSize = 0;
return NULL;
}
}
fullLakes[fullLakesSize++] = rains[i];
nextRain[nextRainSize++] = i;
result[i] = -1;
} else {
if (nextRainSize == 0) {
result[i] = 1;
} else {
qsort(nextRain, nextRainSize, sizeof(int), cmp);
int lakeToDry = -1;
for (int j = 0; j < nextRainSize; j++) {
if (rains[nextRain[j]] > 0) {
lakeToDry = rains[nextRain[j]];
break;
}
}
if (lakeToDry == -1) {
result[i] = 1;
} else {
result[i] = lakeToDry;
for (int j = 0; j < fullLakesSize; j++) {
if (fullLakes[j] == lakeToDry) {
fullLakes[j] = fullLakes[--fullLakesSize];
break;
}
}
for (int j = 0; j < nextRainSize; j++) {
if (rains[nextRain[j]] == lakeToDry) {
nextRain[j] = nextRain[--nextRainSize];
break;
}
}
}
}
}
}
*returnSize = rainsSize;
return result;
}
this still does not solve the problem
They mean that instead of int *nextRain = (int *)malloc(, you can just do int *nextRain = malloc(, since malloc() returns a void *, which doesn't need explicit casting
If you still need to figure out the bug, I recommend pressing "Copy Text" on the below code message, so that you can send it yourself here with main() fixed so that it does compile. (If you edit your message after you've sent it, the bot will keep recompiling and running the updated code.)
You can also paste your code into godbolt.org instead, so you don't have to worry about a character limit.
;compile -Wall -Wextra -Werror -Wpedantic -fsanitize=address,undefined -g
#include <stdlib.h>
#include <string.h>
int cmp(const void* a, const void* b) {
return *(int*)a - *(int*)b;
}
int* avoidFlood(int* rains, int rainsSize, int* returnSize) {
int* result = malloc(sizeof(int) * rainsSize);
int* nextRain = malloc(sizeof(int) * rainsSize);
int* fullLakes = malloc(sizeof(int) * rainsSize);
int fullLakesSize = 0, nextRainSize = 0;
memset(result, 0, sizeof(int) * rainsSize);
memset(nextRain, 0, sizeof(int) * rainsSize);
memset(fullLakes, 0, sizeof(int) * rainsSize);
for (int i = 0; i < rainsSize; i++) {
if (rains[i] > 0) {
for (int j = 0; j < fullLakesSize; j++) {
if (fullLakes[j] == rains[i]) {
*returnSize = 0;
return NULL;
}
}
fullLakes[fullLakesSize++] = rains[i];
nextRain[nextRainSize++] = i;
result[i] = -1;
} else {
if (nextRainSize == 0) {
result[i] = 1;
} else {
qsort(nextRain, nextRainSize, sizeof(int), cmp);
int lakeToDry = -1;
for (int j = 0; j < nextRainSize; j++) {
if (rains[nextRain[j]] > 0) {
lakeToDry = rains[nextRain[j]];
break;
}
}
if (lakeToDry == -1) {
result[i] = 1;
} else {
result[i] = lakeToDry;
for (int j = 0; j < fullLakesSize; j++) {
if (fullLakes[j] == lakeToDry) {
fullLakes[j] = fullLakes[--fullLakesSize];
break;
}
}
for (int j = 0; j < nextRainSize; j++) {
if (rains[nextRain[j]] == lakeToDry) {
nextRain[j] = nextRain[--nextRainSize];
break;
}
}
}
}
}
}
*returnSize = rainsSize;
return result;
}
int main() {
avoidFlood();
}
Compiler Output
<source>: In function 'main':
<source>:66:5: error: too few arguments to function 'avoidFlood'
66 | avoidFlood();
| ^~~~~~~~~~
<source>:8:6: note: declared here
8 | int* avoidFlood(int* rains, int rainsSize, int* returnSize) {
| ^~~~~~~~~~
Build failed
mynameistrez | c | x86-64 gcc 13.2 | godbolt.org