Skip to content

Commit f8d0fff

Browse files
committed
add leetcode: 1226 , 53
1 parent 8ec764e commit f8d0fff

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package leetCode.multiThread;
2+
3+
import java.util.concurrent.Semaphore;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* @author : CodeWater
8+
* @create :2022-08-02-15:25
9+
* @Function Description :1226.哲学家进餐
10+
*/
11+
public class _1226PhilosopherDine {
12+
class DiningPhilosophers {
13+
14+
// 1个叉子对应一个ReentrantLock
15+
private final ReentrantLock[] lockList = {new ReentrantLock() , new ReentrantLock(), new ReentrantLock() , new ReentrantLock() , new ReentrantLock() };
16+
17+
// 最多只有4个哲学家持有叉子
18+
private Semaphore eatLimit = new Semaphore(4);
19+
20+
public DiningPhilosophers() {
21+
22+
}
23+
24+
// call the run() method of any runnable to execute its code
25+
public void wantsToEat(int philosopher,
26+
Runnable pickLeftFork,
27+
Runnable pickRightFork,
28+
Runnable eat,
29+
Runnable putLeftFork,
30+
Runnable putRightFork) throws InterruptedException {
31+
// leftFork左边叉子的编号 ; rightFork右边的
32+
int leftFork = ( philosopher + 1 ) % 5 ;
33+
int rightFork = philosopher ;
34+
35+
// 限制人数减1
36+
eatLimit.acquire();
37+
38+
// 拿起左边和右边的叉子
39+
lockList[ leftFork ].lock();
40+
lockList[ rightFork ].lock();
41+
42+
//拿起叉子之后的具体执行
43+
pickLeftFork.run();
44+
pickRightFork.run();
45+
46+
// 吃意大利面的执行
47+
eat.run();
48+
49+
// 放下左边和右边叉子的具体执行
50+
putLeftFork.run();
51+
putRightFork.run();
52+
53+
// 放下左边和右边的叉子
54+
lockList[ leftFork ].unlock();
55+
lockList[ rightFork ].unlock();
56+
57+
// 限制的人数+1
58+
eatLimit.release();
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package leetCode.subject.number51_100;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-08-02-17:45
6+
* @Function Description :53. 最大子数组和
7+
*/
8+
public class _53TheLargestNumber {
9+
//==========================DP最优============================
10+
class Solution {
11+
/**f[i]所有以nums[i]结尾的区间中,最大和是多少。
12+
时间O(n),空间O(1)
13+
*/
14+
public int maxSubArray(int[] nums) {
15+
int res = Integer.MIN_VALUE;
16+
for( int i = 0 , last = 0 ; i < nums.length ; i++ ) {
17+
// last存储上一轮的最大值
18+
last = nums[i] + Math.max( last , 0 );
19+
res = Math.max( last , res );
20+
}
21+
22+
return res;
23+
}
24+
}
25+
26+
//==========================分治============================
27+
class Solution2 {
28+
/**分治(线段树?)
29+
*/
30+
class Node{
31+
// sum整个区间总和,整个区间最大连续字段和s,最大前缀ls,最大后缀rs
32+
int sum , s , ls , rs ;
33+
// 要写构造,才不会报错
34+
Node(){}
35+
Node( int sum , int s , int ls , int rs ){
36+
this.sum = sum;
37+
this.s = s;
38+
this.ls = ls;
39+
this.rs = rs;
40+
}
41+
}
42+
43+
public int maxSubArray(int[] nums) {
44+
int t = Integer.MIN_VALUE;
45+
for( int x : nums ) t = Math.max( t , x );
46+
if( t < 0 ) return t;
47+
48+
// 递归nums从0到length
49+
Node res = build( nums , 0 , nums.length - 1 );
50+
return res.s;
51+
52+
}
53+
54+
public Node build( int[] nums , int l , int r ){
55+
// 递归到最后的叶结点(先允许区间中为空,后面再特判)
56+
if( l == r ){
57+
int v = Math.max( nums[l] , 0 );
58+
return new Node( nums[l] , v , v , v );
59+
}
60+
61+
// 区间终点mid
62+
int mid = l + r >> 1;
63+
Node ll = build( nums , l , mid ) ;
64+
Node rr = build( nums , mid + 1 , r );
65+
66+
// 递归左右区间之后需要把答案合并
67+
Node res = new Node();
68+
res.sum = ll.sum + rr.sum;
69+
res.s = Math.max( Math.max( ll.s , rr.s ) , ll.rs + rr.ls );
70+
res.ls = Math.max( ll.ls , ll.sum + rr.ls );
71+
res.rs = Math.max( rr.rs , rr.sum + ll.rs );
72+
73+
return res;
74+
}
75+
}
76+
77+
78+
}

0 commit comments

Comments
 (0)