Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/g0801_0900/s0858_mirror_reflection/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g0801_0900.s0858_mirror_reflection;

// #Medium #Math #Geometry

public class Solution {
public int mirrorReflection(int p, int q) {
while (p % 2 == 0 && q % 2 == 0) {
p /= 2;
q /= 2;
}
if (p % 2 == 0) {
return 2;
} else if (q % 2 == 0) {
return 0;
} else {
return 1;
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/g0801_0900/s0858_mirror_reflection/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
858\. Mirror Reflection

Medium

There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered `0`, `1`, and `2`.

The square room has walls of length `p` and a laser ray from the southwest corner first meets the east wall at a distance `q` from the <code>0<sup>th</sup></code> receptor.

Given the two integers `p` and `q`, return _the number of the receptor that the ray meets first_.

The test cases are guaranteed so that the ray will meet a receptor eventually.

**Example 1:**

![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/18/reflection.png)

**Input:** p = 2, q = 1

**Output:** 2

**Explanation:** The ray meets receptor 2 the first time it gets reflected back to the left wall.

**Example 2:**

**Input:** p = 3, q = 1

**Output:** 1

**Constraints:**

* `1 <= q <= p <= 1000`
41 changes: 41 additions & 0 deletions src/main/java/g0801_0900/s0859_buddy_strings/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g0801_0900.s0859_buddy_strings;

// #Easy #String #Hash_Table

public class Solution {
public boolean buddyStrings(String s, String goal) {
int diffCount = 0;
int first = -1;
int second = -1;

int[] sCounts = new int[26];
if (s.equals(goal)) {
for (int i = 0; i < s.length(); i++) {
sCounts[s.charAt(i) - 'a']++;
if (sCounts[s.charAt(i) - 'a'] > 1) {
return true;
}
}
}

for (int i = 0; i < s.length(); i++) {
char curr = s.charAt(i);
sCounts[curr - 'a']++;
if (curr != goal.charAt(i)) {
diffCount++;
if (first == -1) {
first = i;
} else {
second = i;
char[] ss = s.toCharArray();
char temp = ss[first];
ss[first] = ss[second];
ss[second] = temp;
return String.valueOf(ss).equals(goal);
}
}
}

return false;
}
}
38 changes: 38 additions & 0 deletions src/main/java/g0801_0900/s0859_buddy_strings/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
859\. Buddy Strings

Easy

Given two strings `s` and `goal`, return `true` _if you can swap two letters in_ `s` _so the result is equal to_ `goal`_, otherwise, return_ `false`_._

Swapping letters is defined as taking two indices `i` and `j` (0-indexed) such that `i != j` and swapping the characters at `s[i]` and `s[j]`.

* For example, swapping at indices `0` and `2` in `"abcd"` results in `"cbad"`.

**Example 1:**

**Input:** s = "ab", goal = "ba"

**Output:** true

**Explanation:** You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.

**Example 2:**

**Input:** s = "ab", goal = "ab"

**Output:** false

**Explanation:** The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.

**Example 3:**

**Input:** s = "aa", goal = "aa"

**Output:** true

**Explanation:** You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.

**Constraints:**

* <code>1 <= s.length, goal.length <= 2 * 10<sup>4</sup></code>
* `s` and `goal` consist of lowercase letters.
31 changes: 31 additions & 0 deletions src/main/java/g0801_0900/s0860_lemonade_change/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g0801_0900.s0860_lemonade_change;

// #Easy #Array #Greedy

public class Solution {
public boolean lemonadeChange(int[] bills) {
int countFive = 0;
int countTen = 0;
for (int bill : bills) {
if (bill == 5) {
countFive++;
} else if (bill == 10) {
if (countFive == 0) {
return false;
}
countFive--;
countTen++;
} else if (bill == 20) {
if (countFive > 0 && countTen > 0) {
countFive--;
countTen--;
} else if (countFive >= 3) {
countFive -= 3;
} else {
return false;
}
}
}
return true;
}
}
46 changes: 46 additions & 0 deletions src/main/java/g0801_0900/s0860_lemonade_change/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
860\. Lemonade Change

Easy

At a lemonade stand, each lemonade costs `$5`. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a `$5`, `$10`, or `$20` bill. You must provide the correct change to each customer so that the net transaction is that the customer pays `$5`.

Note that you do not have any change in hand at first.

Given an integer array `bills` where `bills[i]` is the bill the <code>i<sup>th</sup></code> customer pays, return `true` _if you can provide every customer with the correct change, or_ `false` _otherwise_.

**Example 1:**

**Input:** bills = [5,5,5,10,20]

**Output:** true

**Explanation:**

From the first 3 customers, we collect three $5 bills in order.

From the fourth customer, we collect a $10 bill and give back a $5.

From the fifth customer, we give a $10 bill and a $5 bill.

Since all customers got correct change, we output true.

**Example 2:**

**Input:** bills = [5,5,10,10,20]

**Output:** false

**Explanation:**

From the first two customers in order, we collect two $5 bills.

For the next two customers in order, we collect a $10 bill and give back a $5 bill.

For the last customer, we can not give the change of $15 back because we only have two $10 bills.

Since not every customer received the correct change, the answer is false.

**Constraints:**

* <code>1 <= bills.length <= 10<sup>5</sup></code>
* `bills[i]` is either `5`, `10`, or `20`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g0801_0900.s0861_score_after_flipping_matrix;

// #Medium #Array #Greedy #Matrix #Bit_Manipulation

public class Solution {
public int matrixScore(int[][] a) {
int m = a.length;
int n = a[0].length;
for (int i = 0; i < m; i++) {
if (a[i][0] == 0) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 0) {
a[i][j] = 1;
} else {
a[i][j] = 0;
}
}
}
}
for (int j = 1; j < n; j++) {
int ones = 0;
for (int[] ints : a) {
if (ints[j] == 1) {
ones++;
}
}
if (ones <= m / 2) {
for (int i = 0; i < m; i++) {
if (a[i][j] == 1) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}
}
int result = 0;
for (int[] ints : a) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
sb.append(ints[j]);
}
result += Integer.parseInt(sb.toString(), 2);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
861\. Score After Flipping Matrix

Medium

You are given an `m x n` binary matrix `grid`.

A **move** consists of choosing any row or column and toggling each value in that row or column (i.e., changing all `0`'s to `1`'s, and all `1`'s to `0`'s).

Every row of the matrix is interpreted as a binary number, and the **score** of the matrix is the sum of these numbers.

Return _the highest possible **score** after making any number of **moves** (including zero moves)_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/07/23/lc-toogle1.jpg)

**Input:** grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]

**Output:** 39

**Explanation:** 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39

**Example 2:**

**Input:** grid = [[0]]

**Output:** 1

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 20`
* `grid[i][j]` is either `0` or `1`.
18 changes: 18 additions & 0 deletions src/test/java/g0801_0900/s0858_mirror_reflection/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0801_0900.s0858_mirror_reflection;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void mirrorReflection() {
assertThat(new Solution().mirrorReflection(2, 1), equalTo(2));
}

@Test
void mirrorReflection2() {
assertThat(new Solution().mirrorReflection(3, 1), equalTo(1));
}
}
23 changes: 23 additions & 0 deletions src/test/java/g0801_0900/s0859_buddy_strings/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g0801_0900.s0859_buddy_strings;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void buddyStrings() {
assertThat(new Solution().buddyStrings("ab", "ba"), equalTo(true));
}

@Test
void buddyStrings2() {
assertThat(new Solution().buddyStrings("ab", "ab"), equalTo(false));
}

@Test
void buddyStrings3() {
assertThat(new Solution().buddyStrings("aa", "aa"), equalTo(true));
}
}
18 changes: 18 additions & 0 deletions src/test/java/g0801_0900/s0860_lemonade_change/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0801_0900.s0860_lemonade_change;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void lemonadeChange() {
assertThat(new Solution().lemonadeChange(new int[] {5, 5, 5, 10, 20}), equalTo(true));
}

@Test
void lemonadeChange2() {
assertThat(new Solution().lemonadeChange(new int[] {5, 5, 10, 10, 20}), equalTo(false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g0801_0900.s0861_score_after_flipping_matrix;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void matrixScore() {
assertThat(
new Solution().matrixScore(new int[][] {{0, 0, 1, 1}, {1, 0, 1, 0}, {1, 1, 0, 0}}),
equalTo(39));
}

@Test
void matrixScore2() {
assertThat(new Solution().matrixScore(new int[][] {{0}}), equalTo(1));
}
}