LeetCode Weekly Contest 25

522 Longest Uncommon Subsequence IIthis

Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.code

A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.ip

The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.ci

Example 1:
Input: "aba", "cdc", "eae"
Output: 3
["aaa","aaa","aa"]
-1element

Note:rem

All the given strings' lengths will not exceed 10.
The length of the given list will be in the range of [2, 50].input

func check(s, t string) bool {
    if len(s) > len(t) {
        return false
    }
    
    i := 0
    for j, _ := range t {
        if i< len(s) && s[i] == t[j] {
            i++
        }
    }
    
    return i == len(s)
}


func findLUSlength(strs []string) int {
    
    for i, _ := range strs {
        for j:= i+1; j<len(strs); j++ {
            if len(strs[i]) < len(strs[j]) {
                tmp := strs[i]
                strs[i] = strs[j]
                strs[j] = tmp
            } 
        }
    }
    fmt.Printf("%v\n", strs)

    for i, _ := range strs {
        found := true
        for j, _ := range strs {
            if i == j {
                continue
            }
            
            if  strs[i] == strs[j] {
                found = false
                break
            }
            
            if true == check(strs[i], strs[j]) {
                found = false
                break
            }
        }
        
        if true == found {
            return len(strs[i])
        }
    }    
    
    
    return -1
}

  1. Friend Circles My SubmissionsBack To Contest
    User Accepted: 520string

User Tried: 672
Total Accepted: 524
Total Submissions: 1212
Difficulty: Medium
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.it

Given a N*N matrix M representing the friend relationship between students in the class. If Mi = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.io

Example 1:
Input:
[[1,1,0],
[1,1,0],
[0,0,1]]
Output: 2
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
The 2nd student himself is in a friend circle. So return 2.
Example 2:
Input:
[[1,1,0],
[1,1,1],
[0,1,1]]
Output: 1
Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends,
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.
Note:
N is in range [1,200].
Mi = 1 for all students.
If Mi = 1, then Mj = 1.

type UnionFind struct {
    id    []int
    count int
}

// New initializes a new UnionFind struct and returns a pointer.
func New(count int) *UnionFind {
    id := make([]int, count)
    for i := 0; i < count; i++ {
        id[i] = i
    }
    return &UnionFind{id: id, count: count}
}

// New initializes a new UnionFind struct and returns a pointer.
func (uf UnionFind) Show() {
    for i,_ := range uf.id {
        fmt.Printf(" %v:[%v]", i, uf.id[i])
    }

    fmt.Printf(" count[%v]\n", uf.count)
}

// Find returns the set ID of a given element.
func (uf UnionFind) Find(p int) int {
    return uf.id[p]
}

// Connected determines whether two elements belong to the same set.
func (uf UnionFind) Connected(p, q int) bool {
    return uf.id[p] == uf.id[q]
}

// Union specifies two elements as belonging to the same set for future query
// with the Connected or Find methods.
func (uf *UnionFind) Union(p, q int) {
    //fmt.Printf("-- %v %v\n", p, q)
    if uf.Connected(p, q) {
        return
    }
    p_id := uf.id[p]
    for i := 0; i < len(uf.id); i++ {
        if uf.id[i] == p_id {
            uf.id[i] = uf.id[q]
        }
    }
    uf.count--
}


func findCircleNum(M [][]int) int {
    uf := New(len(M))
    for i, _ := range M {
        for j ,_ := range M[i][i+1:] {
            if M[i][j+i+1] == 1 {
                uf.Union(i, i+1+j)
            }
        }
        //uf.Show()
    }
    return uf.count
}
相關文章
相關標籤/搜索