美文网首页
2022-11-01 tsl 首轮笔试

2022-11-01 tsl 首轮笔试

作者: lc_fan | 来源:发表于2022-11-02 12:31 被阅读0次
    
    func Test_aa(t *testing.T) {
    
        Solutionz(1)
    }
    func Solutionz(N int) {
        var enable_print int
        enable_print = N % 10
        for N > 0 {
            if enable_print == 0 && N%10 != 0 {
                enable_print = 1
            }
            if enable_print == 1 {
                fmt.Print(N % 10)
            }
            N = N / 10
        }
    }
    
    func Test_city(t *testing.T) {
        A, B := []int{1, 2, 3, 3}, []int{2, 3, 1, 4}
        N := 4
        res := Solutiony(A, B, N)
        assert.Equal(t, 4, res)
    
        A, B = []int{1, 2, 4, 5}, []int{2, 3, 5, 6}
        N = 6
        res = Solutiony(A, B, N)
        assert.Equal(t, 2, res)
    
        A, B = []int{2, 3, 3, 3, 2}, []int{1, 2, 1, 4, 1}
        N = 4
        res = Solutiony(A, B, N)
        assert.Equal(t, 4, res)
    }
    
    func Solutiony(A []int, B []int, N int) int {
        idx, res := 0, 0
        M := len(A)
        cnt := make(map[int]int, N)
        var connected = [100][100]bool{}
        i, j := 0, 0
        for idx < M {
            i, j = A[idx], B[idx]
            if connected[i][j] || connected[j][i] {
                idx++
                continue
            }
            connected[i][j], connected[j][i] = true, true
            if _, ok := cnt[i]; !ok {
                cnt[i] = 1
            } else {
                cnt[i]++
            }
    
            if _, ok := cnt[j]; !ok {
                cnt[j] = 1
            } else {
                cnt[j]++
            }
            idx++
        }
    
        for x := 0; x < N; x++ {
            for y := x + 1; y < N; y++ {
                if connected[x][y] || connected[y][x] {
                    rank := cnt[x] + cnt[y] - 1
                    if rank > res {
                        res = rank
                    }
                }
            }
        }
        return res
    }
    
    func Test_sum(t *testing.T) {
        A := []int{2, -2, 3, 0, 4, -7}
        res := Solution(A)
        assert.Equal(t, 4, res)
    
        A = []int{1, 4, -5}
        res = Solution(A)
        assert.Equal(t, 1, res)
    
        A = []int{3, 4, -7, 3, 1, 3, 1, -4, -2, -2}
        res = Solution(A)
        assert.Equal(t, 6, res)
    
        A = []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        res = Solution(A)
        assert.Equal(t, -1, res)
    }
    
    func Solution(A []int) int {
        n := len(A)
        cnt := 0
        for i := 0; i < n; i++ {
            sum := 0
            for j := i; j < n; j++ {
                sum += A[j]
                if sum == 0 {
                    cnt++
                    if cnt > 3000 {
                        return -1
                    }
                }
            }
        }
        return cnt
    }
    
    

    相关文章

      网友评论

          本文标题:2022-11-01 tsl 首轮笔试

          本文链接:https://www.haomeiwen.com/subject/ipdjtdtx.html