{ "cells": [ { "cell_type": "markdown", "source": [ "You are given an integer `num`. You will apply the following steps exactly\n", "**two** times:\n", "\n", " * Pick a digit `x (0 <= x <= 9)`.\n", " * Pick another digit `y (0 <= y <= 9)`. The digit `y` can be equal to `x`.\n", " * Replace all the occurrences of `x` in the decimal representation of `num` by `y`.\n", " * The new integer **cannot** have any leading zeros, also the new integer **cannot** be 0.\n", "\n", "Let `a` and `b` be the results of applying the operations to `num` the first\n", "and second times, respectively.\n", "\n", "Return _the max difference_ between `a` and `b`.\n", "\n", "\n", "\n", "**Example 1:**\n", "\n", "\n", "\n", " Input: num = 555\n", " Output: 888\n", " Explanation: The first time pick x = 5 and y = 9 and store the new integer in a.\n", " The second time pick x = 5 and y = 1 and store the new integer in b.\n", " We have now a = 999 and b = 111 and max difference = 888\n", "\n", "\n", "**Example 2:**\n", "\n", "\n", "\n", " Input: num = 9\n", " Output: 8\n", " Explanation: The first time pick x = 9 and y = 9 and store the new integer in a.\n", " The second time pick x = 9 and y = 1 and store the new integer in b.\n", " We have now a = 9 and b = 1 and max difference = 8\n", "\n", "\n", "**Example 3:**\n", "\n", "\n", "\n", " Input: num = 123456\n", " Output: 820000\n", "\n", "\n", "**Example 4:**\n", "\n", "\n", "\n", " Input: num = 10000\n", " Output: 80000\n", "\n", "\n", "**Example 5:**\n", "\n", "\n", "\n", " Input: num = 9288\n", " Output: 8700\n", "\n", "\n", "\n", "\n", "**Constraints:**\n", "\n", " * `1 <= num <= 10^8`" ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "max_score_1432 (generic function with 1 method)" }, "metadata": {}, "execution_count": 1 } ], "cell_type": "code", "source": [ "# @lc code=start\n", "using LeetCode\n", "\n", "function max_score_1432(card_points::Vector{Int}, k::Int)\n", " len, tt = length(card_points), sum(card_points)\n", " i, j = 1, len - k\n", " minn = sum(card_points[i:j])\n", " tmp = minn\n", " while j < len\n", " tmp -= card_points[i]\n", " i += 1\n", " tmp += card_points[j += 1]\n", " minn = min(tmp, minn)\n", " end\n", " tt - minn\n", "end\n", "# @lc code=end" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.10.1" }, "kernelspec": { "name": "julia-1.10", "display_name": "Julia 1.10.1", "language": "julia" } }, "nbformat": 4 }