File tree 5 files changed +129
-0
lines changed
5 files changed +129
-0
lines changed Original file line number Diff line number Diff line change
1
+ // time: O(n * m), where n is the amount and m is the number of coins
2
+ // space: O(n)
3
+ class Solution {
4
+
5
+ public int coinChange (int [] coins , int amount ) {
6
+ int max = amount + 1 ;
7
+ int [] dp = new int [amount + 1 ];
8
+ Arrays .fill (dp , max );
9
+ dp [0 ] = 0 ;
10
+ for (int i = 1 ; i <= amount ; i ++) {
11
+ for (int j = 0 ; j < coins .length ; j ++) {
12
+ if (coins [j ] <= i ) {
13
+ dp [i ] = Math .min (dp [i ], dp [i - coins [j ]] + 1 );
14
+ }
15
+ }
16
+ }
17
+ if (dp [amount ] > amount ) {
18
+ return -1 ;
19
+ }
20
+ return dp [amount ];
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ // time: O(N)
2
+ // space: O(N)
3
+ class Solution {
4
+
5
+ public int numDecodings (String s ) {
6
+ int [] dp = new int [s .length () + 1 ];
7
+ dp [0 ] = 1 ;
8
+ dp [1 ] = s .charAt (0 ) == '0' ? 0 : 1 ;
9
+
10
+ for (int i = 2 ; i < dp .length ; i ++) {
11
+ if (s .charAt (i - 1 ) != '0' ) {
12
+ dp [i ] = dp [i - 1 ];
13
+ }
14
+
15
+ int twoDigits = Integer .valueOf (s .substring (i - 2 , i ));
16
+ if (twoDigits >= 10 && twoDigits <= 26 ) {
17
+ dp [i ] += dp [i - 2 ];
18
+ }
19
+ }
20
+ return dp [s .length ()];
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ // time: O(N)
2
+ // space: O(1)
3
+ class Solution {
4
+
5
+ public int maxProduct (int [] nums ) {
6
+ double max = Integer .MIN_VALUE ;
7
+ double product = 1 ;
8
+
9
+ for (int num : nums ) {
10
+ product *= num ;
11
+ max = Math .max (product , max );
12
+ if (product == 0 ) {
13
+ product = 1 ;
14
+ }
15
+
16
+ }
17
+
18
+ product = 1 ;
19
+ for (int i = nums .length - 1 ; i >= 0 ; i --) {
20
+ product *= nums [i ];
21
+ max = Math .max (product , max );
22
+ if (product == 0 ) {
23
+ product = 1 ;
24
+ }
25
+ }
26
+
27
+ return (int ) max ;
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ // time: O(N^3) (the worst case)
2
+ // space: O(1)
3
+ class Solution {
4
+
5
+ public int countSubstrings (String s ) {
6
+ int count = 0 ;
7
+ for (int i = 0 ; i < s .length (); i ++) {
8
+ for (int j = i ; j < s .length (); j ++) {
9
+ count += isPalindrome (s , i , j );
10
+ }
11
+ }
12
+ return count ;
13
+ }
14
+
15
+ int isPalindrome (String s , int start , int end ) {
16
+ while (start < end ) {
17
+ if (s .charAt (start ) != s .charAt (end )) {
18
+ return 0 ;
19
+ }
20
+ start ++;
21
+ end --;
22
+ }
23
+ return 1 ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ // time: O(N^2)
2
+ // space: O(N)
3
+ class Solution {
4
+
5
+ public boolean wordBreak (String s , List <String > wordDict ) {
6
+ Set <String > words = new HashSet <>(wordDict );
7
+ Queue <Integer > que = new LinkedList <>();
8
+ boolean [] visited = new boolean [s .length () + 1 ];
9
+ que .add (0 );
10
+
11
+ while (!que .isEmpty ()) {
12
+ int start = que .remove ();
13
+ if (start == s .length ()) {
14
+ return true ;
15
+ }
16
+
17
+ for (int i = start + 1 ; i <= s .length (); i ++) {
18
+ if (visited [i ]) {
19
+ continue ;
20
+ }
21
+
22
+ if (words .contains (s .substring (start , i ))) {
23
+ que .add (i );
24
+ visited [i ] = true ;
25
+ }
26
+ }
27
+ }
28
+
29
+ return false ;
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments