File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed
Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ $ senate = '"RRDDD" ' ;
4+ print_r (predictPartyVictory ($ senate ));
5+ print_r (predictPartyVictory1 ($ senate ));
6+ function predictPartyVictory1 ($ senate )
7+ {
8+ $ n = strlen ($ senate );
9+ $ nextStr = '' ;
10+ $ curStr = $ senate ;
11+ $ radiant = $ dire = 0 ;
12+ while (true ) {
13+ for ($ i = 0 ; $ i < $ n ; $ i ++) {
14+ if ($ curStr [$ i ] == 'R ' ) {
15+ if ($ dire > 0 ) {
16+ $ radiant --;
17+ } else {
18+ $ nextStr .= 'R ' ;
19+ $ dire ++;
20+ }
21+ } else {
22+ if ($ dire > 0 ) {
23+ $ dire --;
24+ } else {
25+ $ nextStr .= 'D ' ;
26+ $ radiant ++;
27+ }
28+ }
29+ }
30+ $ n = strlen ($ nextStr );
31+ if ($ radiant >= $ n ) {
32+ return 'Dire ' ;
33+ }
34+ if ($ dire >= $ n ) {
35+ return 'Radiant ' ;
36+ }
37+ $ curStr = $ nextStr ;
38+ $ nextStr = '' ;
39+ }
40+ }
41+
42+
43+ function predictPartyVictory ($ senate )
44+ {
45+ $ dire = new SplQueue ();
46+ $ radiant = new SplQueue ();
47+ $ len = strlen ($ senate );
48+ for ($ i = 0 ; $ i < $ len ; $ i ++) {
49+ if ($ senate [$ i ] == 'R ' ) {
50+ $ radiant ->enqueue ($ i );
51+ } else {
52+ $ dire ->enqueue ($ i );
53+ }
54+ }
55+ while (!$ radiant ->isEmpty () && !$ dire ->isEmpty ()) {
56+ $ r = $ radiant ->dequeue ();
57+ $ d = $ dire ->dequeue ();
58+ if ($ r > $ d ) {
59+ $ dire ->enqueue ($ d + $ len );
60+ } else {
61+ $ radiant ->enqueue ($ r + $ len );
62+ }
63+ }
64+ return $ dire ->isEmpty () ? "Radiant " : "Dire " ;
65+ }
You can’t perform that action at this time.
0 commit comments