상세 컨텐츠

본문 제목

[Go] Array and string - Pascal's Triangle

Go/Leet Code

by Gopythor 2022. 3. 29. 23:01

본문

728x90
반응형

Given an integer numRows, return the first numRows of Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:

Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2:

Input: numRows = 1
Output: [[1]]

Constraints:

  • 1 <= numRows <= 30

My code

func generate(numRows int) [][]int {
    temp := make([][]int, numRows)
    for i := 0; i < numRows; i++ {
        temp[i] = make([]int, i+1)
        temp[i][0], temp[i][i] = 1, 1
        if i > 1 {
            for j := 1; j < len(temp[i])-1; j++ {
                temp[i][j] = temp[i-1][j] + temp[i-1][j-1]
            }
        }
    }
    return temp
}

https://go.dev/play/p/iG5EHfUEGM5

  • It was a simple problem.
  • First I declare temp slice with numRows.
  • After that For loop will run based on numRows.
  • By using make function, column will be created.
  • 1 will be allocated first and end of slice.
  • from 2 rows, it starts having sum, so If condition is declared to check from row 2.
  • j will be from 1 to end -1.
  • When i checekd, there was a some pattern.
  • For example
  • temp[2][1] will have temp[1][0] + temp[1][1].
  • temp [row][col] = temp[row-1][col]+ temp[row-1][col-1]

sample 0 ms submission & sample 2000 KB submission

func generate(numRows int) [][]int {
    var res [][]int
    for i := 0;i < numRows;i++{
        var tmp []int = make([]int,i+1)
        tmp[0] = 1
        res = append(res, tmp)
        for j := 1;j <= i;j++{
            if i == j{
                res[i][j] = 1
            } else{
                res[i][j] = res[i-1][j-1] + res[i-1][j]
            }
        }
    }
    return res
}
  • I don't know why this code made temp slice to append it.
  • Every row[0] will have 1 after appended.
  • also in the second for loop, it will allocate 1 when i and j are same.
  • if not it will sum former two slices

sample 1 ms submission

func generate(numRows int) [][]int {
    if numRows == 0 {
        return nil
    }
    triangle := make([][]int, numRows)
    for i := 0; i < numRows; i++ {
        triangle[i] = make([]int, i+1)
        row := triangle[i]
        n := len(row)
        row[0] = 1
        row[n-1] = 1
        if i <= 1 {
            continue
        }
        prevRow := triangle[i-1]
        for j := 1; j < n-1; j++ {
            row[j] = prevRow[j-1] + prevRow[j]
        }
    }
    return triangle
}

sample 2 ms submission

func generate(numRows int) [][]int {
    row := []int{1}
    rows := [][]int{row}
    if numRows == 1 {
        return rows
    }
    for i:=1;i<numRows;i++ {
        rows = append(rows, generateFromAboveRow(rows[i-1])) 
    }

    return rows
}

func generateFromAboveRow(row []int) []int {
    i := 0
    res := []int{1}
    for i+1<len(row) {
        res = append(res, row[i]+row[i+1])
        i++
    }
    res = append(res, 1)
    return res
}

sample 1900 KB submission

func generate(numRows int) [][]int {
    var result [][]int = make([][]int, numRows)
    result[0] = []int{1}
    for i := 1; i < numRows; i++ {
        var row []int = make([]int, i+1)
        row[0] = 1
        row[i] = 1
        for j := 1; j < i; j++ {
            row[j] = result[i-1][j-1] + result[i-1][j]
        }
        result[i] = row
    }

    return result
}
  • This code looks a little complicated.
  • result [0] will have 1.
  • After that, First for loop will make slice row.
  • row[0] will have 1 and end of array[i] will have 1.
  • Second for loop will calculate and do like before.
  • But row will be allocated to result[i].
  • I think that will make some garbage/.
728x90
반응형

관련글 더보기

댓글 영역