From 92c01da5298ea6826eed63bbcdaca6e0864bb96d Mon Sep 17 00:00:00 2001 From: dkoval Date: Thu, 17 Oct 2024 17:52:37 +0200 Subject: [PATCH] Brute force solution to "Maximum Swap" problem --- .../leetcode/challenge/MaximumSwap.java | 66 +++++++++++++++++++ .../leetcode/challenge/MaximumSwapTest.kt | 38 +++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/main/java/com/github/dkoval/leetcode/challenge/MaximumSwap.java create mode 100644 src/test/kotlin/com/github/dkoval/leetcode/challenge/MaximumSwapTest.kt diff --git a/src/main/java/com/github/dkoval/leetcode/challenge/MaximumSwap.java b/src/main/java/com/github/dkoval/leetcode/challenge/MaximumSwap.java new file mode 100644 index 00000000..7e266588 --- /dev/null +++ b/src/main/java/com/github/dkoval/leetcode/challenge/MaximumSwap.java @@ -0,0 +1,66 @@ +package com.github.dkoval.leetcode.challenge; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Maximum Swap + *

+ * You are given an integer num. You can swap two digits at most once to get the maximum valued number. + *

+ * Return the maximum valued number you can get. + *

+ * Constraints: + *

+ * 0 <= num <= 10^8 + */ +public interface MaximumSwap { + + int maximumSwap(int num); + + class MaximumSwapRev1 implements MaximumSwap { + + @Override + public int maximumSwap(int num) { + // convert num to a list of digits + List digits = new ArrayList<>(); + int x = num; + while (x > 0) { + digits.add(x % 10); + x /= 10; + } + + int n = digits.size(); + Collections.reverse(digits); + + // brute force + int best = num; + for (int i = 0; i < n - 1; i++) { + for (int j = i + 1; j < n; j++) { + // swap digits at indices i and j + swap(digits, i, j); + best = Math.max(best, asNumber(digits)); + // undo swap before considering a new pair of indices + swap(digits, i, j); + } + } + return best; + } + + private void swap(List digits, int i, int j) { + int tmp = digits.get(i); + digits.set(i, digits.get(j)); + digits.set(j, tmp); + } + + private int asNumber(List digits) { + int x = 0; + for (int d : digits) { + x *= 10; + x += d; + } + return x; + } + } +} diff --git a/src/test/kotlin/com/github/dkoval/leetcode/challenge/MaximumSwapTest.kt b/src/test/kotlin/com/github/dkoval/leetcode/challenge/MaximumSwapTest.kt new file mode 100644 index 00000000..60879582 --- /dev/null +++ b/src/test/kotlin/com/github/dkoval/leetcode/challenge/MaximumSwapTest.kt @@ -0,0 +1,38 @@ +package com.github.dkoval.leetcode.challenge + +import com.github.dkoval.leetcode.challenge.MaximumSwap.MaximumSwapRev1 +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.extension.ExtensionContext +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.ArgumentsProvider +import org.junit.jupiter.params.provider.ArgumentsSource +import java.util.stream.Stream + +internal class MaximumSwapTest { + + class InputArgumentsProvider : ArgumentsProvider { + + override fun provideArguments(context: ExtensionContext): Stream = Stream.of( + Arguments.of(2736, 7236), + Arguments.of(9973, 9973), + Arguments.of(1, 1) + ) + } + + @Nested + inner class MaximumSwapRev1Test { + + @ParameterizedTest + @ArgumentsSource(InputArgumentsProvider::class) + fun `should return the maximum valued number you can get`(num: Int, expected: Int) { + MaximumSwapRev1().test(num, expected) + } + } +} + +private fun MaximumSwap.test(num: Int, expected: Int) { + val actual = maximumSwap(num) + assertEquals(expected, actual) +}