From 0bf295471a645a64dd96dde6cfcde967d37137c9 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 6 Oct 2023 12:11:54 +0800 Subject: [PATCH] Add solution and test-cases for problem 2120 --- .../1.png | Bin 0 -> 1875 bytes .../2.png | Bin 0 -> 1492 bytes .../3.png | Bin 0 -> 1252 bytes .../README.md | 54 +++++++++++++----- .../Solution.go | 32 ++++++++++- .../Solution_test.go | 22 +++---- 6 files changed, 83 insertions(+), 25 deletions(-) create mode 100644 leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/1.png create mode 100644 leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/2.png create mode 100644 leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/3.png diff --git a/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/1.png b/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/1.png new file mode 100644 index 0000000000000000000000000000000000000000..9b66080efd7e3183884ec058b7a203c03ce9e071 GIT binary patch literal 1875 zcmZ`(XH=7k68#Vb1Pjr(=%PyrbzzYZI>G`PO2nWjf?^?31XLE5q97t=0YhCnQWrxt z#85&gVS!yhS|}S@Xi<7kAS9GjUfy}X-j6ru%-p#%=l+;;?wR=8_BN*_WhDUsI1RUj z-4SA%FjFK%g;gw0)>R0{y(|%y08sn86z}N?Ay;s>y@LRNH>v;-83OU^ zuz>^slMeuJ!M}j$Uti??q&S{)r7@%em#KyY|?h{0gu;^N-Ff8X5POrcP+va&vW z_`qVZs;a7@qoY54`ow0lD=RC@%gguo_qVpTQc_ZSdU^^A3yX@1va_=%CntAzcUxOq zcXoE7qN3<@Iv$VD%*>pcn&NOc?d|OZ0-?0Dbb5L^DJcny#m2|Smz0z=G&Bqk53j7O zq@|^;t*u2wL`+Of#Ky*wNF*wiy1BVoU0u!N@lsP$`}_MD3`SE^Q$|JxlgW&UiRta_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ov}3uh$uWQuDdcCfwg%MWx4~O z;=qwy+4t$BPI=iZbLi)55lKmtD2tx5NRu7N&wfurg()(t7nI3VwHne-9QtG~ndF_m z(ckitEylmeK&MGjQ*y^#`r5}wU=i!>^Kts>CLW8Y~XC;PhsM!ug z0%~_hVkDx{W91^3Oevu5k!ZLO+{@o&suD%-HNzvxQl8rE4|nXu^R3qlfiQ zDQ(jBu@g#LokRu%nCtl;L>H~#hcb_G^NS$_#e_PBM2ErvV9*$Q8+(im`Uu|M!Eyh7 qNAzK9G};l3=FqLi{v#ko62dP3{{Mo8-uuC@0C0CZ?OJv`F#aDDp9GHp literal 0 HcmV?d00001 diff --git a/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/3.png b/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/3.png new file mode 100644 index 0000000000000000000000000000000000000000..2c999be417eea0f941691272687d8acef7157489 GIT binary patch literal 1252 zcmV004R>004l5008;`004mK004C`008P>0026e000+ooVrmw0004T zP)t-s|NsB}{r&#_{`mO#s;a7nhK7fShq<}AsHmu>rl$7x_U!EJj*gDU$H(yS@PUDW z?(Xihv$K?xl-k+9X!-IbMSpudn?4{E(24%F4=`nwo@!gu1%A_xJaurKR-r^sun7i>I@fxW&@a z(uSwBm)iIJ{{H0k{jlTs`27C9==qG-^>5AdWy@xJG<|T*YQ!|?a~{@n8X-17TL!0@2m_t)|JB(v>&()1;>?jf@6BC_p3z3{Z; z`98ex@cI6`=lS&e{_6Mrp56ES{{A(&?r6*MY0L9Dy6$ex^EbKfY|Qg&%k${={Z+*A zbkFnp{r+;!^M4HQL;#2d9Y_EG00(qQO+^Rg3<(Y)9H6D}J@P8Gw4!6sRlQL(MJ0D_5K37}u`1-nbc( zLbSDa0NlDQ#kg}9n(y6zAeEAbk2)W>C|#{Gj3?c}9_VbBO6t?DFhI{UseHim7cbjh zy_REiym=dZCwmh1Ikl&s6RcK=3F^Dp zr8Svi>B)=*$V{eLDoJE$mC4jJV>2*oF=k(%TX{*h?9vx6Es+=s{+3r_Tz_n3nUd3FmaVRRO?xT7w#u@}n&a{q z>-gq{4d$hd0uJjK7B*vf9V0TnVcaH%v57;m9LDb$af2=MPH`CKo!cri_#LB8We7S( zmBvs+9V3yUi#kRkLlt$58kHgF7(?X$h2Jp}-$fI3jL0bYFiK#3KX@G@GPW@;%3(xt z*arXlvQuKadmP5zE-UT$G0eh|-#-8j0}l4{;K!&qc?^mwjKhQnB6U~^i-;Ii=aKt^ znmclB<)eUMe8+{yhG86|%@{__WuVt=lW~If#ALWqFUuh1)Q3T=*_UTHir^1idy4iH z?N2(x;rxYb3uw)X@SA^JA>-`4_~!!c#oyxj8UMI0hOS`m7`D5}F;$J&EoRmE59rAX z5N4105UK#F)cANEigG$F*G_eGdeOjD=;uRFffU= zE~5Yd03~!qSaf7zbY(hiZ)9m^c>ppnGBGVNGA%GUR53I [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +There is an `n x n` grid, with the top-left cell at (`0, 0`) and the bottom-right cell at (`n - 1, n - 1`). You are given the integer `n` and an integer array `startPos` where `startPos = [startrow, startcol]` indicates that a robot is initially at cell (startrow, startcol). + +You are also given a **0-indexed** string `s` of length `m` where `s[i]` is the ith instruction for the robot: `'L'` (move left), `'R'` (move right), `'U'` (move up), and `'D'` (move down). + +The robot can begin executing from any ith instruction in `s`. It executes the instructions one by one towards the end of `s` but it stops if either of these conditions is met: + +- The next instruction will move the robot off the grid. +- There are no more instructions left to execute. + +Return an array `answer` of length `m` where `answer[i]` is the **number of instructions** the robot can execute if the robot **begins executing from** the ith instruction in `s`. + +**Example 1:** -**Example 1:** +![example1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 3, startPos = [0,1], s = "RRDDLU" +Output: [1,5,4,3,1,0] +Explanation: Starting from startPos and beginning execution from the ith instruction: +- 0th: "RRDDLU". Only one instruction "R" can be executed before it moves off the grid. +- 1st: "RDDLU". All five instructions can be executed while it stays in the grid and ends at (1, 1). +- 2nd: "DDLU". All four instructions can be executed while it stays in the grid and ends at (1, 0). +- 3rd: "DLU". All three instructions can be executed while it stays in the grid and ends at (0, 0). +- 4th: "LU". Only one instruction "L" can be executed before it moves off the grid. +- 5th: "U". If moving up, it would move off the grid. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![example2](./2.png) -### 思路1 -> ... -Execution of All Suffix Instructions Staying in a Grid -```go ``` +Input: n = 2, startPos = [1,1], s = "LURD" +Output: [4,1,0,0] +Explanation: +- 0th: "LURD". +- 1st: "URD". +- 2nd: "RD". +- 3rd: "D". +``` + +**Example 3:** + +![example3](./3.png) +``` +Input: n = 1, startPos = [0,0], s = "LRUD" +Output: [0,0,0,0] +Explanation: No matter which instruction the robot begins execution from, it would move off the grid. +``` ## 结语 diff --git a/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/Solution.go b/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/Solution.go index d115ccf5..583a60fd 100644 --- a/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/Solution.go +++ b/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/Solution.go @@ -1,5 +1,33 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int, startPos []int, s string) []int { + ans := make([]int, len(s)) + var steps func(int, int, int) int + + steps = func(x, y, index int) int { + ans := 0 + for i := index; i < len(s); i++ { + a, b := 0, 0 + if s[i] == 'L' { + a, b = 0, -1 + } else if s[i] == 'R' { + a, b = 0, 1 + } else if s[i] == 'U' { + a, b = -1, 0 + } else { + a, b = 1, 0 + } + nx, ny := x+a, y+b + if nx < 0 || nx >= n || ny < 0 || ny >= n { + return ans + } + x, y = nx, ny + ans++ + } + return ans + } + for i := 0; i < len(s); i++ { + ans[i] = steps(startPos[0], startPos[1], i) + } + return ans } diff --git a/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/Solution_test.go b/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/Solution_test.go index 14ff50eb..5fb77a9c 100644 --- a/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/Solution_test.go +++ b/leetcode/2101-2200/2120.Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/Solution_test.go @@ -10,30 +10,32 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n int + pos []int + s string + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 3, []int{0, 1}, "RRDDLU", []int{1, 5, 4, 3, 1, 0}}, + {"TestCase2", 2, []int{1, 1}, "LURD", []int{4, 1, 0, 0}}, + {"TestCase3", 1, []int{0, 0}, "LRUD", []int{0, 0, 0, 0}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.pos, c.s) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.n, c.pos, c.s) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }