Skip to content

Commit acca1aa

Browse files
committed
do some easy letcode
1 parent ddd2c84 commit acca1aa

7 files changed

+446
-40
lines changed

.idea/workspace.xml

Lines changed: 131 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

easy/874walking_robot_simulation.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands:
3+
4+
-2: turn left 90 degrees
5+
-1: turn right 90 degrees
6+
1 <= x <= 9: move forward x units
7+
Some of the grid squares are obstacles.
8+
9+
The i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1])
10+
11+
If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)
12+
13+
Return the square of the maximum Euclidean distance that the robot will be from the origin.
14+
15+
16+
17+
Example 1:
18+
19+
Input: commands = [4,-1,3], obstacles = []
20+
Output: 25
21+
Explanation: robot will go to (3, 4)
22+
Example 2:
23+
24+
Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
25+
Output: 65
26+
Explanation: robot will be stuck at (1, 4) before turning left and going to (1, 8)
27+
28+
29+
solution:
30+
start at position x, y = 0, 0 and direction dx, dy = 0, 1
31+
loop over commands
32+
if command == -2, (dx, dy) -> (-dy, dx) ccw rotate 90 degree
33+
if command == -1, (dx, dy) -> (dy, -dx) cw rotate 90 degree
34+
if command 1-9, repeat given steps of x += dx, y += dy if not in obstacle, otherwise stop
35+
"""
36+
37+
38+
class Solution:
39+
def robotSim(self, commands, obstacles) -> int:
40+
x, y, dx, dy = 0, 0, 0, 1 # position & direction
41+
obstacles = set(map(tuple, obstacles))
42+
dis2 = 0
43+
44+
for command in commands:
45+
if command == -2: dx, dy = -dy, dx # ccw (left)
46+
if command == -1: dx, dy = dy, -dx # cw (right)
47+
if 1 <= command <= 9:
48+
for _ in range(command):
49+
if (x + dx, y + dy) in obstacles: break
50+
x += dx
51+
y += dy
52+
dis2 = max(dis2, x ** 2 + y ** 2)
53+
return dis2
54+
55+
56+
if __name__ == '__main__':
57+
s = Solution()
58+
commands = [4, -1, 4, -2, 4]
59+
obstacles = [[2, 4]]
60+
res = s.robotSim(commands, obstacles)
61+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.
3+
4+
Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
5+
6+
Now we view the projection of these cubes onto the xy, yz, and zx planes.
7+
8+
A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane.
9+
10+
Here, we are viewing the "shadow" when looking at the cubes from the top, the front, and the side.
11+
12+
Return the total area of all three projections.
13+
14+
Input: [[1,0],[0,2]]
15+
Output: 8
16+
Example 4:
17+
18+
Input: [[1,1,1],[1,0,1],[1,1,1]]
19+
Output: 14
20+
Example 5:
21+
22+
Input: [[2,2,2],[2,1,2],[2,2,2]]
23+
Output: 21
24+
"""
25+
26+
27+
class Solution:
28+
def projectionArea(self, grid) -> int:
29+
ans = 0
30+
for cubes in grid:
31+
ans += len(cubes) - cubes.count(0) # 'projection onto xy plane'
32+
ans += max(cubes) # 'projection onto yz plane'
33+
zipped = zip(*list(grid))
34+
ys = list(zipped)
35+
for tup in ys:
36+
ans += max(tup) # 'projection onto xz plane'
37+
return ans
38+
39+
40+
if __name__ == '__main__':
41+
s = Solution()
42+
a = [[2, 2, 2], [2, 1, 2], [2, 2, 2]]
43+
res = s.projectionArea(a)
44+
print(res)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
We are given two sentences A and B. (A sentence is a string of space separated words. Each word consists only of lowercase letters.)
3+
4+
A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
5+
6+
Return a list of all uncommon words.
7+
8+
You may return the list in any order.
9+
10+
11+
12+
Example 1:
13+
14+
Input: A = "this apple is sweet", B = "this apple is sour"
15+
Output: ["sweet","sour"]
16+
Example 2:
17+
18+
Input: A = "apple apple", B = "banana"
19+
Output: ["banana"]
20+
21+
使用集合解决
22+
^对称差集
23+
A, B = A.split(), B.split()
24+
return (set(A) ^ set(B)) - set(a for a in A if A.count(a) > 1) - set(b for b in B if B.count(b) > 1)
25+
"""
26+
27+
28+
class Solution:
29+
def uncommonFromSentences(self, A: str, B: str):
30+
a = {}
31+
allstr = A + ' ' + B
32+
aslist = allstr.split(' ')
33+
for el in aslist:
34+
if a.__contains__(el):
35+
a[el] += 1
36+
continue
37+
a[el] = 1
38+
39+
reslist = [k for k, v in a.items() if v == 1]
40+
return reslist
41+
if __name__ == '__main__':
42+
s = Solution()
43+
A = "apple apple"
44+
B = "banana"
45+
res = s.uncommonFromSentences(A, B)
46+
print(res)

easy/888fair_candy_swap.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has.
3+
4+
Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.)
5+
6+
Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the size of the candy bar that Bob must exchange.
7+
8+
If there are multiple answers, you may return any one of them. It is guaranteed an answer exists.
9+
10+
11+
12+
Example 1:
13+
14+
Input: A = [1,1], B = [2,2]
15+
Output: [1,2]
16+
Example 2:
17+
18+
Input: A = [1,2], B = [2,3]
19+
Output: [1,2]
20+
Example 3:
21+
22+
Input: A = [2], B = [1,3]
23+
Output: [2,3]
24+
Example 4:
25+
26+
Input: A = [1,2,5], B = [2,4]
27+
Output: [5,4]
28+
29+
after swap:
30+
sum1 + temp - one == sum2 - temp + one
31+
so :
32+
temp = (sum2+2*one-sum1 )//2
33+
牛P
34+
35+
36+
"""
37+
38+
39+
class Solution:
40+
def fairCandySwap(self, A, B):
41+
sum1, sum2 = sum(A), sum(B)
42+
B = set(B)
43+
for one in A:
44+
temp = (sum2 + 2 * one - sum1) // 2
45+
if temp in B:
46+
return [one, temp]
47+
48+
49+
if __name__ == '__main__':
50+
s = Solution()
51+
A = [2]
52+
B = [1, 3]
53+
res = s.fairCandySwap(A, B)
54+
print(res)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
You are given an array A of strings.
3+
4+
A move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S.
5+
6+
Two strings S and T are special-equivalent if after any number of moves onto S, S == T.
7+
8+
For example, S = "zzxy" and T = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz" that swap S[0] and S[2], then S[1] and S[3].
9+
10+
Now, a group of special-equivalent strings from A is a non-empty subset of A such that:
11+
12+
Every pair of strings in the group are special equivalent, and;
13+
The group is the largest size possible (ie., there isn't a string S not in the group such that S is special equivalent to every string in the group)
14+
Return the number of groups of special-equivalent strings from A.
15+
16+
17+
Example 1:
18+
19+
Input: ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
20+
Output: 3
21+
Explanation:
22+
One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings are all pairwise special equivalent to these.
23+
24+
The other two groups are ["xyzz", "zzxy"] and ["zzyx"]. Note that in particular, "zzxy" is not special equivalent to "zzyx".
25+
Example 2:
26+
27+
Input: ["abc","acb","bac","bca","cab","cba"]
28+
Output: 3
29+
30+
one line solution
31+
from collections import Counter
32+
33+
class Solution:
34+
def numSpecialEquivGroups(self, A: List[str]) -> int:
35+
return len({tuple(tuple(sorted(Counter(S[j::2]).items())) for j in range(2)) for S in A})
36+
"""
37+
38+
from collections import Counter
39+
40+
41+
class Solution:
42+
def numSpecialEquivGroups(self, A) -> int:
43+
s = set()
44+
for S in A:
45+
evens = tuple(sorted(Counter(S[0::2]).items()))
46+
odds = tuple(sorted(Counter(S[1::2]).items()))
47+
s.add((evens, odds))
48+
return len(s)
49+
50+
51+
if __name__ == '__main__':
52+
s = Solution()
53+
a = ["abcd", "cdab", "cbad", "xyzz", "zzxy", "zzyx"]
54+
res = s.numSpecialEquivGroups(a)
55+
print(res)

easy/896monotonic_array.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
An array is monotonic if it is either monotone increasing or monotone decreasing.
3+
4+
An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
5+
6+
Return true if and only if the given array A is monotonic.
7+
8+
9+
10+
Example 1:
11+
12+
Input: [1,2,2,3]
13+
Output: true
14+
Example 2:
15+
16+
Input: [6,5,4,4]
17+
Output: true
18+
Example 3:
19+
20+
Input: [1,3,2]
21+
Output: false
22+
Example 4:
23+
24+
Input: [1,2,4,5]
25+
Output: true
26+
Example 5:
27+
28+
Input: [1,1,1]
29+
Output: true
30+
31+
正面排序
32+
然后反面排序
33+
牛P
34+
35+
"""
36+
37+
38+
class Solution:
39+
def isMonotonic(self, A) -> bool:
40+
if sorted(A) == A:
41+
return True
42+
43+
A.reverse()
44+
45+
if sorted(A) == A:
46+
return True
47+
48+
return False
49+
50+
51+
if __name__ == '__main__':
52+
s = Solution()
53+
inputa = [6, 5, 4, 4]
54+
res = s.isMonotonic(inputa)
55+
print(res)

0 commit comments

Comments
 (0)