@@ -77,24 +77,8 @@ public int minimumCost(String target, String[] words, int[] costs) {
77
77
}
78
78
}
79
79
}
80
- Map <Integer , Map <Integer , Integer >> dm = new HashMap <>();
81
- for (int i = 0 ; i < words .length ; i ++) {
82
- String word = words [i ];
83
- if (word .length () >= 320 ) {
84
- List <List <Integer >> q = find (targetPrefix , target , word );
85
- for (List <Integer > pair : q ) {
86
- int b = pair .get (0 );
87
- int e = pair .get (1 );
88
- dm .putIfAbsent (e , new HashMap <>());
89
- Map <Integer , Integer > qm = dm .get (e );
90
- if (qm .containsKey (b )) {
91
- qm .put (b , Math .min (qm .get (b ), costs [i ]));
92
- } else {
93
- qm .put (b , costs [i ]);
94
- }
95
- }
96
- }
97
- }
80
+ Map <Integer , Map <Integer , Integer >> dm =
81
+ getIntegerMapMap (target , words , costs , targetPrefix );
98
82
List <NodeCostPair > d = new ArrayList <>();
99
83
d .add (new NodeCostPair (root , 0 ));
100
84
int [] dp = new int [target .length () + 1 ];
@@ -115,16 +99,7 @@ public int minimumCost(String target, String[] words, int[] costs) {
115
99
q .add (new NodeCostPair (w , cost ));
116
100
}
117
101
}
118
- Map <Integer , Integer > qm = dm .getOrDefault (i + 1 , Collections .emptyMap ());
119
- for (Map .Entry <Integer , Integer > entry : qm .entrySet ()) {
120
- int b = entry .getKey ();
121
- if (dp [b ] >= 0 ) {
122
- t =
123
- t == null
124
- ? dp [b ] + entry .getValue ()
125
- : Math .min (t , dp [b ] + entry .getValue ());
126
- }
127
- }
102
+ t = getInteger (dm , i , dp , t );
128
103
if (t != null ) {
129
104
dp [i + 1 ] = t ;
130
105
q .add (new NodeCostPair (root , t ));
@@ -134,6 +109,40 @@ public int minimumCost(String target, String[] words, int[] costs) {
134
109
return dp [target .length ()];
135
110
}
136
111
112
+ private Integer getInteger (Map <Integer , Map <Integer , Integer >> dm , int i , int [] dp , Integer t ) {
113
+ Map <Integer , Integer > qm = dm .getOrDefault (i + 1 , Collections .emptyMap ());
114
+ for (Map .Entry <Integer , Integer > entry : qm .entrySet ()) {
115
+ int b = entry .getKey ();
116
+ if (dp [b ] >= 0 ) {
117
+ t = t == null ? dp [b ] + entry .getValue () : Math .min (t , dp [b ] + entry .getValue ());
118
+ }
119
+ }
120
+ return t ;
121
+ }
122
+
123
+ private Map <Integer , Map <Integer , Integer >> getIntegerMapMap (
124
+ String target , String [] words , int [] costs , List <Integer > targetPrefix ) {
125
+ Map <Integer , Map <Integer , Integer >> dm = new HashMap <>();
126
+ for (int i = 0 ; i < words .length ; i ++) {
127
+ String word = words [i ];
128
+ if (word .length () >= 320 ) {
129
+ List <List <Integer >> q = find (targetPrefix , target , word );
130
+ for (List <Integer > pair : q ) {
131
+ int b = pair .get (0 );
132
+ int e = pair .get (1 );
133
+ dm .putIfAbsent (e , new HashMap <>());
134
+ Map <Integer , Integer > qm = dm .get (e );
135
+ if (qm .containsKey (b )) {
136
+ qm .put (b , Math .min (qm .get (b ), costs [i ]));
137
+ } else {
138
+ qm .put (b , costs [i ]);
139
+ }
140
+ }
141
+ }
142
+ }
143
+ return dm ;
144
+ }
145
+
137
146
private static class Node {
138
147
Map <Character , Node > children ;
139
148
Integer cost ;
0 commit comments