[LeetCode]514. Freedom Trail
給一個環和一個key,判斷環經多少步可以得到key,
Input: ring = "godding", key = "gd" Output: 4 Explanation:
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
public class Solution { public int findRotateSteps(String ring, String key) { int n = ring.length(), m = key.length(); int[][] dp = new int[m + 1][n]; int[][] clock = preproc(ring, 1), anti = preproc(ring, -1); for (int i = m - 1; i >= 0; --i) { int idx = key.charAt(i) - 'a'; for (int j = 0; j < n; ++j) { // fill dp[i][j] int p = clock[j][idx]; int q = anti[j][idx]; // 順時針從i ~ p走過的路徑是i與p之間包含12點鐘位置的那一段!!實際轉一下圓環就知道 dp[i][j] = Math.min(dp[i + 1][p] + (j + n - p) % n, dp[i + 1][q] + (q + n - j) % n); } } return dp[0][0] + m; } // ans[i]表示以i位置為視角,正/反向離char[i]最近的各字母index是多少 int[][] preproc(String r, int inc) { int n = r.length(); int[][] ans = new int[n][26]; int[] map = new int[26]; // 要遍歷兩個長度才能保證前面的index知道後面的值是什麼 for (int i = 0, j = 0; j < n * 2 - 1; ++j) { map[r.charAt(i) - 'a'] = i; System.arraycopy(map, 0, ans[i], 0, 26); i = (i + inc + n) % n; } return ans; } }
相關推薦
LeetCode 514. Freedom Trail
一、題目描述 In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ri
[LeetCode]514. Freedom Trail
給一個環和一個key,判斷環經多少步可以得到key, Input: ring = "godding", key = "gd" Output: 4 Explanation: For the first key character 'g', since it is a
Leetcode 514. Freedom Trail
In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring”, and use the dial
514 Freedom Trail 自由之路
ring leetcode 參考 return des abs problem nbsp logs 詳見:https://leetcode.com/problems/freedom-trail/description/ C++: class Solution { publ
演算法分析與設計課程05——514. Freedom Trail(Hard)
給定一個字串ring=”godding”和key=”gd”,ring按照下面方式放在圓盤上,對於key[0]即’g’字元,和12點鐘方向【即圓盤最上面】的字元相匹配,所以不用轉圓盤,按下圓中按鈕,記錄步驟1次,對於key[1]=’d’,12點鐘方向顯然不是’
514. Freedom Trail Hard
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring", and use
[LeetCode]Freedom Trail
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring", and use
動態規劃——Freedom Trail
題目:https://leetcode.com/problems/freedom-trail/ 額。。。不解釋大意了,題目我也不想寫過程了有點繁瑣,直接給出程式碼: 1 public int findRotateSteps(String ring, String key) { 2
Leetcode 514.自由之路
如果 color from 取值 http 字母 視頻遊戲 anti enter 自由之路 視頻遊戲"輻射4"中,任務"通向自由"要求玩家到達名為"Freedom Trail Ring"的金屬表盤,並使用表盤拼寫特定關鍵詞才能開門。 給定一個字符串 ring,表示刻在外環
[LeetCode]160.Intersection of Two Linked Lists
col style return tro nod sts diff original you Intersection of Two Linked Lists Write a program to find the node at which the intersectio
[LeetCode] Reshape the Matrix 矩陣重塑
ren ati num 我們 資料 call posit tar led In MATLAB, there is a very useful function called ‘reshape‘, which can reshape a matrix into a ne
leetcode題解 || Roman to Integer問題
leet only top xiv har convert 擁有 pro think problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within
100. Same Tree(LeetCode)
write end nodes ons code logs 相等 same tree treenode Given two binary trees, write a function to check if they are equal or not. Two binar
LeetCode Length of Last Word
archive n-1 art fine ive lan ets hello style 1. 題目Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘
leetcode 209. Minimum Size Subarray Sum
urn https scrip subarray () ins -s mar color https://leetcode.com/problems/minimum-size-subarray-sum/#/description 題目很簡單。題意就是求字符串中長度最小的一
[LeetCode][Java] Subsets
sort arrays ++ lee 題意 integer sel duplicate ati 題目: Given a set of distinct integers, nums, return all possible subsets. Note: Ele
Leetcode:search_insert_position
size search ++ ins urn leet 位置 art -s 一、 題目 給定一個數組和要插入數的大小。求插入的位置。 二、 分析 太水,直接掃描。過……. class Solution { public: int search
(LeetCode)兩個鏈表的第一個公共節點
struct link note lee mono lengthb borde san length LeetCode上面的題目例如以下: Write a program to find the node at which the intersection of t
【LeetCode】091. Decode Ways
rom size etc oss following nbsp pan ron ann 題目: A message containing letters from A-Z is being encoded to numbers using the following map
[LeetCode] Word Abbreviation 單詞縮寫
any array number example note long win min which Given an array of n distinct non-empty strings, you need to generate minimal possible