File tree 2 files changed +42
-0
lines changed
2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 514
514
678|[ Valid Parenthesis String] ( ./0678-valid-parenthesis-string.js ) |Medium|
515
515
679|[ 24 Game] ( ./0679-24-game.js ) |Hard|
516
516
680|[ Valid Palindrome II] ( ./0680-valid-palindrome-ii.js ) |Easy|
517
+ 682|[ Baseball Game] ( ./0682-baseball-game.js ) |Easy|
517
518
684|[ Redundant Connection] ( ./0684-redundant-connection.js ) |Medium|
518
519
686|[ Repeated String Match] ( ./0686-repeated-string-match.js ) |Easy|
519
520
693|[ Binary Number with Alternating Bits] ( ./0693-binary-number-with-alternating-bits.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 682. Baseball Game
3
+ * https://leetcode.com/problems/baseball-game/
4
+ * Difficulty: Easy
5
+ *
6
+ * You are keeping the scores for a baseball game with strange rules. At the beginning of the
7
+ * game, you start with an empty record.
8
+ *
9
+ * You are given a list of strings operations, where operations[i] is the ith operation you
10
+ * must apply to the record and is one of the following:
11
+ * - An integer x.
12
+ * - Record a new score of x.
13
+ * - '+'.
14
+ * - Record a new score that is the sum of the previous two scores.
15
+ * - 'D'.
16
+ * - Record a new score that is the double of the previous score.
17
+ * - 'C'.
18
+ * - Invalidate the previous score, removing it from the record.
19
+ *
20
+ * Return the sum of all the scores on the record after applying all the operations.
21
+ *
22
+ * The test cases are generated such that the answer and all intermediate calculations fit in a
23
+ * 32-bit integer and that all operations are valid.
24
+ */
25
+
26
+ /**
27
+ * @param {string[] } ops
28
+ * @return {number }
29
+ */
30
+ var calPoints = function ( ops ) {
31
+ const stack = [ ] ;
32
+
33
+ for ( const op of ops ) {
34
+ if ( op === 'C' ) stack . pop ( ) ;
35
+ else if ( op === 'D' ) stack . push ( stack . at ( - 1 ) * 2 ) ;
36
+ else if ( op === '+' ) stack . push ( stack . at ( - 1 ) + stack . at ( - 2 ) ) ;
37
+ else stack . push ( Number ( op ) ) ;
38
+ }
39
+
40
+ return stack . reduce ( ( sum , num ) => sum + num , 0 ) ;
41
+ } ;
You can’t perform that action at this time.
0 commit comments