-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClumsyFactorial.java
More file actions
198 lines (186 loc) · 5.52 KB
/
Copy pathClumsyFactorial.java
File metadata and controls
198 lines (186 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Source : https://leetcode.com/problems/clumsy-factorial/
// Author : cornprincess
// Date : 2021-04-01
/*****************************************************************************************************
*
* Normally, the factorial of a positive integer n is the product of all positive integers less than
* or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.
*
* We instead make a clumsy factorial: using the integers in decreasing order, we swap out the
* multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and
* subtract (-) in this order.
*
* For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1. However, these operations are
* still applied using the usual order of operations of arithmetic: we do all multiplication and
* division steps before any addition or subtraction steps, and multiplication and division steps are
* processed left to right.
*
* Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11. This
* guarantees the result is an integer.
*
* Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial
* of N.
*
* Example 1:
*
* Input: 4
* Output: 7
* Explanation: 7 = 4 * 3 / 2 + 1
*
* Example 2:
*
* Input: 10
* Output: 12
* Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
*
* Note:
*
* 1 <= N <= 10000
* -2^31 <= answer <= 2^31 - 1 (The answer is guaranteed to fit within a 32-bit integer.)
*
******************************************************************************************************/
package ClumsyFactorial;
import java.util.*;
public class ClumsyFactorial {
List<Integer> temp = new ArrayList<>();
List<Integer> temp2 = new ArrayList<>();
public int clumsy(int N) {
int bar = N % 4;
int res = 0;
for (int i = N; i > bar; i -= 4) {
calc(i);
}
if (N > 3) {
int last = bar > 0 ? calc(bar) : 0;
res = temp.get(0);
for (int i = 1; i < temp.size(); i++) {
res -= temp.get(i);
}
for (int i : temp2) {
res += i;
}
res -= last;
} else {
res = calc(bar);
}
return res;
}
private int calc(int N) {
int tmp = N;
int mul = 0;
int t = N - 1;
while (t > 0 && mul < 3) {
if (mul % 4 == 0) {
tmp = tmp * t;
} else if (mul % 4 == 1) {
tmp = tmp / t;
} else if (mul % 4 == 2) {
temp.add(tmp);
temp2.add(t);
}
mul++;
t--;
}
return tmp;
}
// stack
// 时间复杂度:O(N)
// 空间复杂度:O(N)
public int clumsy2(int N) {
Deque<Integer> stack = new LinkedList<>();
int mul = 0;
stack.push(N--);
int res = 0;
while (N > 0) {
if (mul % 4 == 0) {
stack.push(stack.pop() * N);
} else if (mul % 4 == 1) {
stack.push(stack.pop() / N);
} else if (mul % 4 == 2) {
stack.push(N);
} else if (mul % 4 == 3) {
stack.push(-N);
}
N--;
mul++;
}
while (!stack.isEmpty()) {
res += stack.pop();
}
return res;
}
// math
public int clumsy3(int N) {
if (N == 1) {
return 1;
}
if (N == 2) {
return 2;
}
if (N == 3) {
return 6;
}
if (N == 4) {
return 7;
}
if (N % 4 == 0) {
return N + 1;
}
if (N % 4 <= 2) {
return N + 2;
}
return N - 1;
}
// stack
// 双栈解法 万金油
// 一个栈用来维护 num, 另一个栈用来维护 ops,其中需要维护运算符的优先级
public int clumsy4(int N) {
Deque<Integer> nums = new LinkedList<>();
Deque<Character> ops = new LinkedList<>();
char[] chars = {'*', '/', '+', '-'};
Map<Character, Integer> priorityMap = new HashMap<Character, Integer>() {{
put('*', 2);
put('/', 2);
put('+', 1);
put('-', 1);
}};
int mul = 0;
// 构造双栈,并且在构造的时候就已经开始优先级高的计算
while (N > 0) {
// core 先加入 num
nums.push(N);
char op = chars[mul % 4];
// core 这是重点,先把栈内高优先级的运算符先计算掉,然后再将当前运算符加入
while (!ops.isEmpty() && priorityMap.get(ops.peek()) >= priorityMap.get(op)) {
calc(nums, ops);
}
// core 后加入op
if (N > 1) {
ops.push(op);
}
N--;
mul++;
}
// 继续进行计算
while (!ops.isEmpty()) {
calc(nums, ops);
}
return nums.peek();
}
private void calc(Deque<Integer> nums, Deque<Character> chars) {
int b = nums.pop();
int a = nums.pop();
int op = chars.pop();
int res = 0;
if (op == '+') {
res= a + b;
} else if (op == '*') {
res= a * b;
} else if (op == '/') {
res= a / b;
} else if (op == '-') {
res= a - b;
}
nums.push(res);
}
}