File tree 12 files changed +281
-0
lines changed
12 files changed +281
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int lastStoneWeight (vector<int >& stones) {
4
+ priority_queue<int > pq;
5
+ for (auto x : stones)
6
+ pq.push (x);
7
+
8
+ while (pq.size () >= 2 ) {
9
+ int a = pq.top (); pq.pop ();
10
+ int b = pq.top (); pq.pop ();
11
+
12
+ if (a == b)
13
+ continue ;
14
+ else
15
+ pq.push (a - b);
16
+ }
17
+
18
+ if (pq.empty ())
19
+ return 0 ;
20
+ else
21
+ return pq.top ();
22
+ }
23
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int lastStoneWeight (int [] stones ) {
3
+ PriorityQueue <Integer > pq = new PriorityQueue <>(31 , Collections .reverseOrder ());
4
+ for (int x : stones )
5
+ pq .add (x );
6
+
7
+ while (pq .size () >= 2 ) {
8
+ int a = pq .poll ();
9
+ int b = pq .poll ();
10
+
11
+ if (a == b )
12
+ continue ;
13
+ else
14
+ pq .add (a - b );
15
+ }
16
+
17
+ if (pq .size () == 0 )
18
+ return 0 ;
19
+ else
20
+ return pq .peek ();
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def lastStoneWeight (self , stones : List [int ]) -> int :
3
+ h = [- x for x in stones ]
4
+ heapq .heapify (h )
5
+
6
+ while len (h ) >= 2 :
7
+ a = heapq .heappop (h )
8
+ b = heapq .heappop (h )
9
+
10
+ heapq .heappush (h , a - b )
11
+
12
+ if len (h ) == 0 :
13
+ return 0
14
+ else :
15
+ return - h [0 ]
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int lastStoneWeightII (vector<int >& stones) {
4
+ // + + - - + - + -
5
+ int sum = 0 ;
6
+ for (int x : stones)
7
+ sum += x;
8
+ int m = sum / 2 ;
9
+ int n = stones.size ();
10
+
11
+ vector<vector<int >> dp (n + 1 , vector<int >(m + 1 , 0 ));
12
+ for (int i = 0 ; i <= n; i++) {
13
+ for (int j = 0 ; j <= m; j++) {
14
+ if (i == 0 and j == 0 )
15
+ dp[i][j] = 1 ;
16
+ else if (i == 0 )
17
+ dp[i][j] = 0 ;
18
+ else if (j == 0 )
19
+ dp[i][j] = 1 ;
20
+ else if (stones[i - 1 ] <= j)
21
+ dp[i][j] = dp[i - 1 ][j - stones[i - 1 ]] || dp[i - 1 ][j];
22
+ else
23
+ dp[i][j] = dp[i - 1 ][j];
24
+ }
25
+ }
26
+
27
+ int ans = 0 ;
28
+ for (int j = m; j >= 0 ; j--) {
29
+ if (dp[n][j]) {
30
+ ans = j;
31
+ break ;
32
+ }
33
+ }
34
+
35
+ return sum - 2 * ans;
36
+ }
37
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int lastStoneWeightII (int [] stones ) {
3
+ // + + - - + - + -
4
+ int sum = 0 ;
5
+ for (int x : stones )
6
+ sum += x ;
7
+ int m = sum / 2 ;
8
+ int n = stones .length ;
9
+
10
+ // vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
11
+ int [][] dp = new int [n + 1 ][m + 1 ];
12
+
13
+ for (int i = 0 ; i <= n ; i ++) {
14
+ for (int j = 0 ; j <= m ; j ++) {
15
+ if (i == 0 && j == 0 )
16
+ dp [i ][j ] = 1 ;
17
+ else if (i == 0 )
18
+ dp [i ][j ] = 0 ;
19
+ else if (j == 0 )
20
+ dp [i ][j ] = 1 ;
21
+ else if (stones [i - 1 ] <= j )
22
+ dp [i ][j ] = dp [i - 1 ][j - stones [i - 1 ]] | dp [i - 1 ][j ];
23
+ else
24
+ dp [i ][j ] = dp [i - 1 ][j ];
25
+ }
26
+ }
27
+
28
+ int ans = 0 ;
29
+ for (int j = m ; j >= 0 ; j --) {
30
+ if (dp [n ][j ] != 0 ) {
31
+ ans = j ;
32
+ break ;
33
+ }
34
+ }
35
+
36
+ return sum - 2 * ans ;
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def lastStoneWeightII (self , stones : List [int ]) -> int :
3
+ sum = 0
4
+ for x in stones :
5
+ sum += x
6
+ m = sum // 2
7
+ n = len (stones )
8
+
9
+ dp = [[0 for _ in range (m + 1 )] for _ in range (n + 1 )]
10
+
11
+ for i in range (0 , n + 1 ):
12
+ for j in range (0 , m + 1 ):
13
+ if i == 0 and j == 0 :
14
+ dp [i ][j ] = 1
15
+ elif i == 0 :
16
+ dp [i ][j ] = 0
17
+ elif j == 0 :
18
+ dp [i ][j ] = 1
19
+ elif stones [i - 1 ] <= j :
20
+ dp [i ][j ] = dp [i - 1 ][j ] | dp [i - 1 ][j - stones [i - 1 ]]
21
+ else :
22
+ dp [i ][j ] = dp [i - 1 ][j ]
23
+
24
+ ans = 0
25
+ for j in range (m , - 1 , - 1 ):
26
+ if dp [n ][j ] != 0 :
27
+ ans = j
28
+ break
29
+
30
+ return sum - 2 * ans ;
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int maximalSquare (vector<vector<char >>& mat) {
4
+ int n = mat.size ();
5
+ int m = mat[0 ].size ();
6
+
7
+ int mx = 0 ;
8
+ vector<vector<int >> dp (n, vector<int >(m, 0 ));
9
+
10
+ for (int i = 0 ; i < n; i++) {
11
+ for (int j = 0 ; j < m; ++j) {
12
+ if (i == 0 || j == 0 )
13
+ dp[i][j] = (mat[i][j] == ' 0' ? 0 : 1 );
14
+ else if (mat[i][j] == ' 1' )
15
+ dp[i][j] = min ({dp[i - 1 ][j], dp[i][j - 1 ], dp[i - 1 ][j - 1 ]}) + 1 ;
16
+ else
17
+ dp[i][j] = 0 ;
18
+
19
+ mx = max (mx, dp[i][j]);
20
+
21
+ }
22
+ }
23
+
24
+ return mx * mx;// return area
25
+ }
26
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int maximalSquare (char [][] mat ) {
3
+ int n = mat .length ;
4
+ int m = mat [0 ].length ;
5
+
6
+ int mx = 0 ;
7
+ // vector<vector<int>> vec(n, vector<int>(m, 0));
8
+ int [][] dp = new int [n ][m ];
9
+
10
+ for (int i = 0 ; i < n ; i ++) {
11
+ for (int j = 0 ; j < m ; ++j ) {
12
+ if (i == 0 || j == 0 )
13
+ dp [i ][j ] = (mat [i ][j ] == '0' ? 0 : 1 );
14
+ else if (mat [i ][j ] == '1' )
15
+ dp [i ][j ] = Math .min (dp [i - 1 ][j ], Math .min (dp [i ][j - 1 ], dp [i - 1 ][j - 1 ])) + 1 ;
16
+ else
17
+ dp [i ][j ] = 0 ;
18
+
19
+ mx = Math .max (mx , dp [i ][j ]);
20
+
21
+ }
22
+ }
23
+
24
+ return mx * mx ;// return area
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maximalSquare (self , mat : List [List [str ]]) -> int :
3
+ n = len (mat )
4
+ m = len (mat [0 ])
5
+
6
+ mx = 0
7
+ dp = [[0 for _ in range (m )] for _ in range (n )]
8
+
9
+ for i in range (n ):
10
+ for j in range (m ):
11
+ if i == 0 or j == 0 :
12
+ dp [i ][j ] = 0 if mat [i ][j ] == "0" else 1
13
+ elif mat [i ][j ] == "1" :
14
+ dp [i ][j ] = min (dp [i - 1 ][j ], dp [i ][j - 1 ], dp [i - 1 ][j - 1 ]) + 1
15
+ else :
16
+ dp [i ][j ] = 0
17
+
18
+ mx = max (mx , dp [i ][j ])
19
+
20
+ return mx * mx
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int numSquares (int n) {
4
+ vector<int > f (n + 1 );
5
+ f[0 ] = 0 ;
6
+ f[1 ] = 1 ;
7
+ for (int i = 2 ; i <= n; i++) {
8
+ f[i] = f[i - 1 ] + 1 ;
9
+ for (int j = 1 ; j * j <= i; j++)
10
+ f[i] = min (f[i], f[i - j * j] + 1 );
11
+ }
12
+
13
+ return f[n];
14
+ }
15
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int numSquares (int n ) {
3
+ // vector<int> f(n + 1);
4
+ int [] f = new int [n + 1 ];
5
+ f [0 ] = 0 ;
6
+ f [1 ] = 1 ;
7
+
8
+ for (int i = 2 ; i <= n ; i ++) {
9
+ f [i ] = f [i - 1 ] + 1 ;
10
+ for (int j = 1 ; j * j <= i ; j ++)
11
+ f [i ] = Math .min (f [i ], f [i - j * j ] + 1 );
12
+ }
13
+
14
+ return f [n ];
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numSquares (self , n : int ) -> int :
3
+ f = [None ] * (n + 1 )
4
+ f [0 ]= 0
5
+ f [1 ]= 1
6
+ for i in range (2 , n + 1 ):
7
+ f [i ] = f [i - 1 ]+ 1
8
+ for j in range (1 , i ):
9
+ if j * j <= i :
10
+ f [i ] = min (f [i ], f [i - j * j ] + 1 )
11
+ else :
12
+ break
13
+ return f [n ]
You can’t perform that action at this time.
0 commit comments