[Community Puzzle] Rectangle Partition

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.

3 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

Continuing the discussion from [Community Puzzle] Rectangle Partition:

What did you finally do to solve this. After reading the comments made, i realise i have the same code but it doesn’t work except the 3 first tests.

here is my code :

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

/**

  • Auto-generated code below aims at helping you parse

  • the standard input according to the problem statement.
    **/
    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();
     
     List<Integer> listeX = new ArrayList<Integer>();
     List<Integer> listeY = new ArrayList<Integer>();
    
     
     for (int i = 0; i < countX; i++) {
         int x = in.nextInt();
         listeX.add(x);
     }
         
     for (int i = 0; i < countY; i++) {
         int y = in.nextInt();
         listeY.add(y);
      }
      
     listeX.add(w);
     listeY.add(h);
    
    
     for(int i = 1; i <= countX ; i++){
         for(int j = 0; j < i; j++){
                 listeX.add(listeX.get(i) - listeX.get(j));
         }
     }
    
     
     for(int i = 1; i <= countY; i++){
         for(int j = 0; j < i; j++){
                 listeY.add(listeY.get(i) - listeY.get(j));
         }
     }
     
     
    
     int nbCarres = 0;
     
     for(int i = 0; i < listeX.size(); i++){
         for(int j = 0; j < listeY.size(); j++){
             if(listeX.get(i) == listeY.get(j)){
                 nbCarres = nbCarres +1;
             }
         }
     }
    
     // Write an action using System.out.println()
     // To debug: System.err.println("Debug messages...");
    
     System.out.println(nbCarres);
    

    }
    }

Your solution is way too slow, you have too many loops to count the squares.
Hints for faster solutions have been given earlier in this thread, you should try and read it.

The expected complexity is O(n^2) where n = max(number of x-axis measurements, number of y-axis measurements). However, the test cases don’t cover the extreme case where there are 500 measurements along each axis, allowing O(n^4) solutions to pass.

Hello guys,
I have nearly completed this puzzles, but i can’t find out why i cant’ pass 3 tests out of 9 (Bigger-2, Hi-density 1, Imbalance). It makes me crazy !:exploding_head: Here is my Javascript code :

var inputs = readline().split(" ");

const w = parseInt(inputs[0]);
const h = parseInt(inputs[1]);
const countX = parseInt(inputs[2]);
const countY = parseInt(inputs[3]);
const wSlices = [...readline().split(" "), w].map(el => +el);
const hSlices = [...readline().split(" "), h].map(el => +el);
let nombreDeCarres = 0;

let wCombination = [...wSlices];
let hCombination = [...hSlices];

calculateCombination(countX, wSlices, wCombination);
calculateCombination(countX, hSlices, hCombination);

function calculateCombination(count, slices, resultArray) {
  for (let i = 0; i < count; i++) {
    const nb = slices.shift();
    slices.forEach(el => {
      resultArray.push(el - nb);
    });
  }
}

for(let i=0;i<wCombination.length;i++){
    for(let j=0;j<hCombination.length;j++){
        if(hCombination[j]===wCombination[i])nombreDeCarres++
    }
}

console.log(nombreDeCarres);

If anyone could give me some help, i would appreciate, it’s so frustrating ^^

Thanks !

This is a very challenging problem. I agree it should be in medium difficulty. I made a first solution very quickly but then I realize it’s so slow for the Hi-density 1 and Hi-density 2 test cases. After that I tried to optimize it but I continue to time out. I’m using c++ to solve it. The algorithm I use it’s very simple store in a vector all the possible heights and all the possible widths for the sub-rectantes and then compare the 2 vectors and count how many are equals.I also has an extra vector to save the count of each possible width or height value because there can be repeated. I’m still thinking how can I improve the performance…

1 Like

Read some earlier posts in this thread. There are some tips answering this exact question.

Finally I could solve it with mapping. It was really hard for me. I’m learning C++ and it was the first problem I solved with that language. I don’t know what data structures are more efficiently for each kind of problem in that language.