Skip to content

Commit

Permalink
tower breakers
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Jan 3, 2025
1 parent a4d8b3a commit fb01b9e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ This repo register my evolution in the Cracking The Coding Interview book as I g

[Combine Arrays](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/squarespace/CombineArrays.java)

[Tower Breakers]()

# AWS Challenges

[Review Score](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/aws/ReviewScore.java)
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/hackerRank/week2/TowerBreakers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package hackerRank.week2;

public class TowerBreakers {

public static int towerBreakers(int n, int m) {

// 2 players
// n = n towers
// m = height
// x = current_height
// y = reduceTo in which ((y>=1) && (y<x)) // evenly divides x
// assumed that player 2 always mirrors player 1's moves (this is not written and was found in the discussions)

// 2 (6)
// p1 6 -> 3
// p2 6 -> 3
// p1 3 -> 1
// p2 3 -> 1
// p1 loses
// return p2

// p1 6 -> 1
// p2 6 -> 1
// p1 loses
// return p2

// 2 (2)
// p1 -> 2 -> 1
// p2 -> 2 -> 1
// p1 loses
// return p2

// 1 (4)
// p1 -> 4 -> 1
// p1 wins


// 2 (1)
// p1 loses
// return 2
// that's the case for m == 1. That means p1 has no moves therefore loses (wtfff)

// also the below is true because towers can directly be reduced to 1.
if (n%2 == 0 || m == 1 ) {
return 2;
} else {
return 1;
}

// If you are here because the description doesn't match the results, you gotta consider that a tower can be reduced directly to 1. example:
// 2 towers of height 7
// p1: 7 -> 1 (x=1 y=6 -> x divides y)
// p2: 7 -> 1 (x=1 y=6 -> x divides y)
// p1 loses
// p2 wins, therefore returns p2

// the description says that y should divide x but in reality x should divide y
// best of luck. brendon!

}

}

0 comments on commit fb01b9e

Please sign in to comment.