[Community Puzzle] Rectangle Partition

time out for clojure, but easily pass with same algorithm in python…
So I think the reason is the speed of the language LOL

1 Like

Finally passed in clojure with your hints, thanks soooo much !!! It is really fun!
And I think it should be put in medium, not easy…

5 Likes

To master a language, one should understand the strength and weakness of the language in use, and knows what design and approach can leverage the strength and counter the weakness.

It is interesting to know how clojure performs in this puzzle.

Also share my result using java. It can pass all tests without any tuning of the algorithm. A direct nested loop works - may be because its List or Collections library is well optimized for this style of operations, or it has a very good cache structure that significantly enhances performance.

@nabil.alibou x = [] creates an empty list. You could either:

  • declare x = [None]*count_x to create a list having the proper size;
  • or declare x = [] and use x.append(int(i)) in the loop to add successive inputs at the end of the list;
  • or simply write x = list(map(int, input().split())) or equivalently x = [int(i) for i in input().split()] to create and fill the list at the same time.
1 Like

I am a beginner, I used a simple way to solve the problem. if you guys have any solutions ,please let me know them.it’s my pleasure. Thanks in advance!! :sweat_smile::sweat_smile:

import java.util.;
import java.io.
;
import java.math.*;

// I seperate w and h into single segments
//example : w = 10,on w we have 2 and 5
// these segments are 2 5 10 5 8 3(arrX)
//like that , with h = 5 , we have 2 5 3 (arrY)
// the result will appear when we compare arrX and arrY
class Solution {

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int w = in.nextInt();
    int h = in.nextInt();
    int countX = in.nextInt();
    int countY = in.nextInt();
    // initializing two arrays to store value
    int[] arrX = new int[100000]; int nX = 0;
    int[] arrY = new int[100000];int nY = 0;
    
    for (int i = 0; i < countX; i++) {
        int x = in.nextInt();
        arrX[nX++] = x;
    }

    for (int i = 0; i < countY; i++) {
        int y = in.nextInt();
        arrY[nY++] = y;
    }
   // adding w and h into these arrays, they are also segments in these arrays
    arrX[nX++] = w;
    arrY[nY++] = h;
    
    for (int i = countX; i >= 1; i--) {
        for (int j = i - 1; j >= 0; j--) {
            arrX[nX++] = arrX[i] - arrX[j];
        }
    }

    for (int i = countY; i >= 1; i--) {
        for (int j = i - 1; j >= 0; j--) {
            arrY[nY++] = arrY[i] - arrY[j];
        }
    }
    //compare 2 arrays and output the result
  int result = 0 ;
    for(int i = 0 ; i < nX ; i++){
        for(int j = 0 ; j < nY ; j++){
            if(arrX[i] == arrY[j]) result ++ ;
        }
    }
    System.out.println(result);
}

}
;

This was a great puzzle! I started by ‘drawing’ the whole rectangle with the sub-rectangles, passing only the first 3 tests. After thinking about how to do it more efficient, I realized I didn’t need any of that. Like most of the time, I was complicating myself. The lesson learned was: keep things simple!

2 Likes

1

This no longer looks like an easy puzzle. :thinking:

Рекурсия наше усё …

4 Likes

It is harder than some other “easy” ones but as reflected by peer coders, it seems to be a good challenge to encourage them think and try out different algorithms to enhance performance, and finally helped enhancing programming skill. Review the many tips in this thread for assistance if you may need it.

2 Likes

I invite everybody who is struggling to pass the high density tests to really spend time understanding what is the quickest way to compare values in a loop. I am quite new to Python and only used very common functions but it worked!

I had an interesting case where I passed all the tests, but failed on submission item #3. I was using C++; changing some assignment items to do bounds checking (e.g. using vec.at(x) rather than vec[x] showed problems in the given test items.

(And yes, I was accessing memory out of bounds – the height can be bigger than the width. I was a little surprised it didn’t fail more issues).

Fixing the bounds issue solved the problem, although I also avoided it entirely moving to a map – I just find the syntax around maps for C++ (e.g. incrementing a value in one) so tedious I had avoided it in my first pass.

1 Like

Greate puzzle. With this hint and a variation that does not uses list i was able to pass every test, thanks.

1 Like
501 501 500 500
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

Contributed by @Niako. This is a “Super Hi-density” testcase. This test might be too harsh for some already submitted solutions. Challenge yourself to improve with this optional test.

2 Likes

Thank you. I did this with Groovy. And got 42042751 with green light :slight_smile:

42042751 is indeed the right answer to that case (just confirming as it wasn’t mentioned).

I also did this “Super Hi-density” with Python3. I timed my solver in GC’s own IDE and got solution in 0.0527 seconds.

Hm. I think this puzzle should be listed as Medium difficulty. I may of course have overlooked something, but my first, straightforward approach took too long, and I needed to apply some brain and experience to find a faster solution. And still get only 77% on the new solution, presumably because it is a bit more complex (harder to debug).

Update: I did solve it, though. And there were even simpler and faster solutions than mine. But I still think it should perhaps be Medium. I struggled more with it (and ended up with longer code) than with “The Resistance”, which is supposed to be Very hard.

4 Likes

I don’t understand … I pass all test excepted the additional validator 3 …
More information about this validator ? :frowning:

I agree that it should be listed under Medium difficulty. It took me a while to come up with a solution for the first testcases, but than I got stuck on testcase 7, 8 and 9. I had to rethink my solution and worry about performance. Good practice though and at the end solved it, but it was difficult.

Hi, I’m having problem solving hi density.
What I did was getting all the possible edges of each height and width and store it in array

Sample 1:
widthLength = [2,3,5,8,5,10]
heightLength = [3, 2, 5]
and compare those two array. (2x2, 3x3, 5x5,5x5) = 4.

My problem I think is that my algorithm for storing the lengths is not optimize. Can you suggest an optimize way to do that?

Really unfair :smiley:, I coded the same code from C++ to Python 3 and I passed Hi Density. That’s why most people here said they solved it using Python.

1 Like