190.Largest Perimeter Triangle

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。


Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths.

If it is impossible to form any triangle of non-zero area, return 0.

Example 1:

Input: [2,1,2]
Output: 5
Example 2:

Input: [1,2,1]
Output: 0
Example 3:

Input: [3,2,3,4]
Output: 10
Example 4:

Input: [3,6,2,3]
Output: 8


3 <= A.length <= 10000
1 <= A[i] <= 10^6




(2).如果连续三个数字(a>b>c),不能满足三角形特性(a - b > c),那么再往c的后一个更不可能找到了。
解决方案和上面一个,先排序,对于任意相邻的两个数字 a 和 b,找到所有可以满足a - b > c 的 c 的个数,最后累加这些个数即可;


class Solution(object):
    def largestPerimeter(self, A):
        :type A: List[int]
        :rtype: int
        (2).如果连续三个数字(a>b>c),不能满足三角形特性(a - b > c),那么再往c的后一个更不可能找到了。
        for i in range(0, len(A) - 2):
            if A[i] - A[i+1] < A[i + 2]:
                return A[i] + A[i+1] + A[i + 2]
        return 0

Triangle Cuts


DescriptionnnnnA computer science professor was watching his young daughter use scissors to cut large triangular pieces of paper that were green on one side and white on the other. She always kept the white side of the paper up, so the green side was never visible, and she always followed the same general procedure. Starting with one large white triangle, she would always make exactly three straight cuts, each of which separated one piece of paper into two, and end up with exactly four smaller white triangles. Then she would put the small triangles aside and start over with a new large triangle. nnThe first three figures show some of the possibilities. All angles are in degrees. In Figures 1 and 2, no cut goes all the way from one vertex of the original large triangle to the opposite side. In fact, these two figures (including any rotations thereof) show the only ways that she could produce four triangles without making such a cut. The pattern in Figure 4 can't happen, because her cuts always separate the current piece of paper into two pieces; after she cuts off the rightmost triangle in Figure 4, none of the remaining cuts will separate the remaining triangle into two pieces. nnThe professor imagined a room full of children producing piles of triangles using different methods, and wondered if he could identify his daughter's work. Given the exact size and shape of four small triangles, could he tell if she could have produced them from one large triangle? After a moment's thought he realized that he could simplify the problem and consider only the shapes of the triangles, noting that if the shapes were compatible, there would always be some appropriate sizes for the small triangles. This led to the following problem for you to solve: given the angles of a large triangle and four smaller ones, is it possible that the small triangles could have been obtained from the large one using the procedure his daughter was following? nInputnnThe input contains from 1 to 30 datasets followed by a line containing only "0 0 180". Each dataset contains 15 positive integers separated by single blanks on one line. Each integer is less than 180. Each group of three integers are the vertex angles for one triangle, expressed in degrees. Assume that none of the triangles is flipped over so the green side of the paper is up, and the vertex angles are listed in clockwise order around each individual triangle. nnThe first four datasets of the sample input below correspond to the figures above. nOutputnnThe output contains one line for each data set. If the child's cutting play, as described above, started with a triangle with the first three angles, and could have ended up with triangles matching the last four triangles in the dataset, then the line contains "yes". Otherwise the line contains "no".nSample Inputnn60 70 50 30 100 50 75 70 35 75 60 45 45 65 70n40 75 65 60 40 80 20 120 40 45 85 50 25 55 100n60 60 60 30 60 90 30 60 90 90 60 30 90 60 30n30 60 90 30 120 30 30 120 30 30 120 30 30 120 30n60 70 50 30 100 50 75 70 35 75 60 45 70 65 45n0 0 180nSample Outputnnyesnyesnyesnnonno 问答

Triangle War


Triangle War is a two-player game played on the following triangular grid:nnnTwo players, A and B, take turns filling in any dotted line connecting two dots, with A starting first. Once a line is filled, it cannot be filled again. If the line filled by a player completes one or more triangles, she owns the completed triangles and she is awarded another turn (i.e. the opponent skips a turn). The game ends after all dotted lines are filled in, and the player with the most triangles wins the game. The difference in the number of triangles owned by the two players is not important.nnFor example, if A fills in the line between 2 and 5 in the partial game on the left below:nnnnThen, she owns the triangle labelled A and takes another turn to fill in the line between 3 and 5. B can now own 3 triangles (if he wishes) by filling in the line between 2 and 3, then the one between 5 and 6, and finally the one between 6 and 9. B would then make one more move before it is A's turn again.nnIn this problem, you are given a number of moves that have already been made. From the partial game, you should determine which player will win assuming that each player plays a perfect game from that point on. That is, assume that each player always chooses the play that leads to the best possible outcome for himself/herself.nnnThis problem contains multiple test cases!nnThe first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.nnThe output format consists of N output blocks. There is a blank line between output blocks.nnnInputnnYou will be given a number of games in the input. The first line of input is a positive integer 6 <= m <= 18 indicating the number of games to follow. Each game starts with an integer indicating the number of moves that have been made in the game. The next m lines indicate the moves made by the two players in order, each of the form i j (with i < j) indicating that the line between i and j is filled in that move. You may assume that all given moves are legal.nnnOutput nnFor each game, print the game number and the result on one line as shown below. If A wins, print the sentence ``A wins.'' If B wins, print ``B wins.''nnnSample Input nn1nn4n6n2 4n4 5n5 9n3 6n2 5n3 5n7n2 4n4 5n5 9n3 6n2 5n3 5n7 8n6n1 2n2 3n1 3n2 4n2 5n4 5n10n1 2n2 5n3 6n5 8n4 7n6 10n2 4n4 5n4 8n7 8nnnSample OutputnnGame 1: B wins.nGame 2: A wins.nGame 3: A wins.nGame 4: B wins. 问答