Skip to content

Commit f3d1e0b

Browse files
author
Partho Biswas
committed
1320_Minimum_Distance_to_Type_a_Word_Using_Two_Fingers
1 parent 33a9f1f commit f3d1e0b

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
640640
|45| **[975. Odd Even Jump](https://tinyurl.com/y5ovyklj)** | [Python](https://tinyurl.com/wu6rdaw/975_Odd_Even_Jump.py), [Swift](https://tinyurl.com/wuja3c4/975_Odd_Even_Jump.swift)| [Vid 1](https://tinyurl.com/vxyed3g), **[Art 1](https://tinyurl.com/wbjrnyt)** | Hard | DP using stack. Vary tricky and Interesting problem, loved it |
641641
|46| **[562. Longest Line of Consecutive One in Matrix](https://tinyurl.com/u2fb4a6)** | [Python](https://tinyurl.com/wu6rdaw/562_Longest_Line_of_Consecutive_One_in_Matrix.py), [Swift](https://tinyurl.com/wuja3c4/562_Longest_Line_of_Consecutive_One_in_Matrix.swift)| [Art 1](https://tinyurl.com/vqppbae), [Art 2](https://tinyurl.com/t3xtfq8) | Medium | |
642642
|47| **[552. Student Attendance Record II](https://tinyurl.com/tnxj5oa)** | [Python](https://tinyurl.com/wu6rdaw/552_Student_Attendance_Record_II.py), [Swift](https://tinyurl.com/wuja3c4/552_Student_Attendance_Record_II.swift)| [Art 1](https://tinyurl.com/rdt2cgr), [Art 2](https://tinyurl.com/s8okmdb), [Art 3](https://tinyurl.com/uhggppt) | Hard | **This is a FUCKING difficult DP problem. Don't dare to solve it** |
643-
|48| **[1320. Minimum Distance to Type a Word Using Two Fingers](https://tinyurl.com/ur876a6)** | [Python](https://tinyurl.com/wu6rdaw/1320_Minimum_Distance_to_Type_a_Word_Using_Two_Fingers.py), [Swift](https://tinyurl.com/wuja3c4/1320_Minimum_Distance_to_Type_a_Word_Using_Two_Fingers.swift)| [Vid 1](https://tinyurl.com/u5fcb3z), [Art 2](https://tinyurl.com/s8okmdb), [Art 3](https://tinyurl.com/uhggppt) | Hard | **Important. TODO: Check again** |
643+
|48| **[1320. Minimum Distance to Type a Word Using Two Fingers](https://tinyurl.com/ur876a6)** | [Python](https://tinyurl.com/wu6rdaw/1320_Minimum_Distance_to_Type_a_Word_Using_Two_Fingers.py), [Swift](https://tinyurl.com/wuja3c4/1320_Minimum_Distance_to_Type_a_Word_Using_Two_Fingers.swift)| [Vid 1](https://tinyurl.com/u5fcb3z), [Art 1](https://tinyurl.com/w386adm), [Art 2](https://tinyurl.com/rnkbhp3), [Art 3](https://tinyurl.com/tqwgraz), [Art 4](https://tinyurl.com/v46fuzm) | Hard | **Important. TODO: Check again** |
644644

645645

646646
</p>
@@ -1077,7 +1077,7 @@ Learn the following modules by heart. Just knowing all of the following items wi
10771077
03. [Union Find \[Disjoint Set\] Playlist](https://tinyurl.com/srz7uua)
10781078
04. [Disjoint Sets Data Structure - Weighted Union and Collapsing Find](https://www.youtube.com/watch?v=wU6udHRIkcc)
10791079
05. [Codinginterviewclass.com](https://tinyurl.com/wpgl4nt)
1080-
06. Single/All Source Shortest Path Algorithms: Dijkstra ([1](https://tinyurl.com/r8omw8l), [2](https://tinyurl.com/sscslz7)), Bellman-Ford([1](https://tinyurl.com/tghcsmz), [2](https://tinyurl.com/rdcax5b)), Floyd–Warshall([1](https://tinyurl.com/vsp4ulb), [2](https://tinyurl.com/s5wjb5a))
1080+
06. Single(Di-Bel)/All(Flo) Source Shortest Path Algorithms: Dijkstra ([1](https://tinyurl.com/r8omw8l), [2](https://tinyurl.com/sscslz7)), Bellman-Ford([1](https://tinyurl.com/tghcsmz), [2](https://tinyurl.com/rdcax5b)), Floyd–Warshall([1](https://tinyurl.com/vsp4ulb), [2](https://tinyurl.com/s5wjb5a))
10811081
07. Dijkstra’s Algorithm Vs Bellman-Ford Algorithm Vs Floyd Warshall Algorithm ( [1](https://tinyurl.com/vxwc2je), [2](https://tinyurl.com/s6vff87), [3](https://tinyurl.com/s5o57nz) )
10821082
08. Minimum Spanning Tree Algorithm: Prim's ( [1](https://tinyurl.com/vgbgdl2), [2](https://tinyurl.com/yecuqtty), Kruskal's ([1](https://tinyurl.com/vgbgdl2), [2](https://tinyurl.com/yhxpvpkn) )
10831083
09. Kruskal's algorithm Vs Prim's algorithm ( [1](https://qr.ae/TSEPGc), [2](https://tinyurl.com/yz6399ck), [3](https://tinyurl.com/yz3q7vox) )
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# https://tinyurl.com/u5fcb3z
2+
# Approach 1: Top-Down DFS with memoization, DP
3+
import string
4+
class Solution(object):
5+
def minimumDistance(self, word):
6+
"""
7+
:type word: str
8+
:rtype: int
9+
"""
10+
# Preprocessing
11+
alphabet = string.ascii_uppercase
12+
keyboardDict = {}
13+
for idx, char in enumerate(alphabet):
14+
keyboardDict[char] = (idx // 6, idx % 6)
15+
memo = {}
16+
return self.minimumDistanceHelper(word, keyboardDict, memo, 0, None, None)
17+
18+
19+
def minimumDistanceHelper(self, word, keyboard, memo, nextCharPosition, currentFingure1Char, currentFingure2Char):
20+
if nextCharPosition >= len(word): # we have processed all the character and we have reached the end
21+
return 0
22+
if (nextCharPosition, currentFingure1Char, currentFingure2Char) in memo:
23+
return memo[(nextCharPosition, currentFingure1Char, currentFingure2Char)]
24+
25+
choice1 = self.distance(currentFingure1Char, word[nextCharPosition], keyboard) + self.minimumDistanceHelper(word, keyboard, memo, nextCharPosition + 1, word[nextCharPosition], currentFingure2Char) # we are moving finger 1 here, finger 2 stays still
26+
choice2 = self.distance(currentFingure2Char, word[nextCharPosition], keyboard) + self.minimumDistanceHelper(word, keyboard, memo, nextCharPosition + 1, currentFingure1Char, word[nextCharPosition]) # we are moving finger 2 here, finger 1 stays still
27+
memo[(nextCharPosition, currentFingure1Char, currentFingure2Char)] = min(choice1, choice2)
28+
return memo[(nextCharPosition, currentFingure1Char, currentFingure2Char)]
29+
30+
31+
def distance(self, startChar, destChar, keyboard):
32+
if startChar is None:
33+
return 0
34+
x1, y1 = keyboard[startChar]
35+
x2, y2 = keyboard[destChar]
36+
return abs(x1 - x2) + abs(y1 - y2)

0 commit comments

Comments
 (0)