-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0670-maximum-swap.rb
55 lines (44 loc) · 1.08 KB
/
0670-maximum-swap.rb
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
# frozen_string_literal: true
# 670. Maximum Swap
# Medium
# https://leetcode.com/problems/maximum-swap
=begin
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.
Example 1:
* Input: num = 2736
* Output: 7236
* Explanation: Swap the number 2 and the number 7.
Example 2:
* Input: num = 9973
* Output: 9973
* Explanation: No swap.
Constraints:
* 0 <= num <= 108
=end
# @param {Integer} num
# @return {Integer}
def maximum_swap(num)
digits = num.digits.reverse
last = Array.new(10, -1)
digits.each_with_index { |digit, i| last[digit] = i }
digits.each_with_index do |digit, i|
(9).downto(digit + 1) do |d|
if last[d] > i
digits[i], digits[last[d]] = digits[last[d]], digits[i]
return digits.join.to_i
end
end
end
num
end
# **************** #
# TEST #
# **************** #
require "test/unit"
class Test_maximum_swap < Test::Unit::TestCase
def test_
assert_equal 7236, maximum_swap(2736)
assert_equal 9973, maximum_swap(9973)
end
end