Majority Number I&II

本文介绍了一种高效算法——摩尔投票法,用于在时间复杂度为O(n)和空间复杂度为O(1)的情况下找出数组中的多数元素。文章通过两个示例展示了如何实现该算法,并分别处理了多数元素出现次数超过一半和超过三分之一的情况。

这个题的名字是在lintcode中,在leetcode中的名字是Majority Element,虽然有少许不同,但是主要都是一样的。

这个题很简单,但是如果要满足n的时间复杂度和1的空间复杂度则需要摩尔投票法。废话不多说了,直接上代码。

I:

public class Solution {
    /**
     * @param nums: a list of integers
     * @return: find a  majority number
     */
    public int majorityNumber(ArrayList<Integer> nums) {
        // write your code
        int length = nums.size();
        if (length <= 0){
            return 0;
        }
        if (length == 1){
            return nums.get(0);
        }
        int count = 1;
        int index = 0;
        for (int i = 1; i < length; i++){
            if (nums.get(index) == nums.get(i)){
                count++;
            } else {
                count--;
                if (count == 0){
                    index = i;
                    count = 1;
                }
            }
        }
        int res = nums.get(index);
        count = 0;
        for (int i = 0; i < length; i++){
            if (res == nums.get(i)){
                count++;
            }
        }
        if (count > length / 2){
            return res;
        }
        return 0;
    }
}

II:

public class Solution {
    /**
     * @param nums: A list of integers
     * @return: The majority number that occurs more than 1/3
     */
    public int majorityNumber(ArrayList<Integer> nums) {
        // write your code
        int length = nums.size();
        if (length <= 0){
            return 0;
        }
        if (length == 1){
            return nums.get(0);
        }
        int counta = 0;
        int a = 0;
        int countb = 0;
        int b = 0;
        for (int num : nums){
            if (num == a){
                counta++;
            } else if (num == b){
                countb++;
            } else {
                
                if (counta == 0){
                    counta = 1;
                    a = num;
                } else if (countb == 0){
                    countb = 1;
                    b = num;
                } else {
                    counta--;
                    countb--;
                }
                
            }
        }
        counta = 0;
        countb = 0;
        for (int num : nums){
            if (num == a){
                counta++;
            } else if (num == b){
                countb++;
            }
        }
        if (counta > length / 3){
            return a;
        }
        if (countb > length / 3){
            return b;
        }
        return 0;
        
    }
}


shuffled_idx = shuffle(np.array(range(sample_number)), random_state=seed) train_idx = shuffled_idx[:int(0.1 * sample_number)] val_idx = shuffled_idx[int(0.1 * sample_number):int(0.2 * sample_number)] test_idx = shuffled_idx[int(0.2 * sample_number):] X_smo, y_smo = balance_MLSMOTE(labeled_X, labeled_y, args.smote_num) def MLSMOTE(X, y, n_sample): """ Give the augmented data using MLSMOTE algorithm args X: pandas.DataFrame, input vector DataFrame y: pandas.DataFrame, feature vector dataframe n_sample: int, number of newly generated sample return new_X: pandas.DataFrame, augmented feature vector data target: pandas.DataFrame, augmented target vector data """ if not isinstance(X, pd.DataFrame): X = pd.DataFrame(X) if not isinstance(y, pd.DataFrame): y = pd.get_dummies(np.array(y)) indices2 = nearest_neighbour(X) n = len(indices2) new_X = np.zeros((n_sample, X.shape[1])) target = np.zeros((n_sample, y.shape[1])) for i in range(n_sample): reference = random.randint(0, n - 1) neighbour = random.choice(indices2[reference, 1:]) all_point = indices2[reference] nn_df = y[y.index.isin(all_point)] ser = nn_df.sum(axis=0, skipna=True) target[i] = np.array([1 if val > 2 else 0 for val in ser]) ratio = random.random() gap = X.loc[reference, :] - X.loc[neighbour, :] new_X[i] = np.array(X.loc[reference, :] + ratio * gap) new_X = pd.DataFrame(new_X, columns=X.columns) target = pd.DataFrame(target, columns=y.columns) new_X = pd.concat([X, new_X], axis=0) target = pd.concat([y, target], axis=0) return new_X.values, np.argmax(target.values,axis=1) def balance_MLSMOTE(labeled_X, labeled_y, n_sample): X_list = []#存储每个类别的特征,元素为列表 y_list = [] for i in range(max(labeled_y) + 1): X_list.append(labeled_X[labeled_y == i, :]) y_list.append(labeled_y[labeled_y == i]) print("lenX_list,len y_list",len(X_list),len(y_list)) num_classes = max(labeled_y) + 1#类别数 one_hot_codes = np.eye(num_classes)#单位矩阵,便于后续独热向量 df_y_list = []#将每个标签转换为独热编码 for i in range(len(y_list)): one_hot_labels = [] for label in y_list[i]: one_hot_label = one_hot_codes[label] one_hot_labels.append(one_hot_label) df_y = pd.DataFrame(np.array(one_hot_labels)) df_y_list.append(df_y) if n_sample == None: smote_num = 0 for i in range(len(y_list)): if len(y_list[i]) > smote_num: smote_num = len(y_list[i]) majority_class = i else: smote_num = n_sample for i in range(len(y_list)): if smote_num - len(y_list[i]) > 0: X_res, y_res = MLSMOTE(X_list[i], df_y_list[i], smote_num - len(y_list[i])) else: X_res, y_res = X_list[i], y_list[i] if i == 0: X_smo = X_res y_smo = y_res else: X_smo = np.concatenate([X_smo, X_res], axis=0) y_smo = np.concatenate([y_smo, y_res], axis=0) return X_smo, np.squeeze(y_smo) idx_except_train = torch.LongTensor(range(len(labels)))[~data.train_mask.cpu()] orign_idx_train = torch.tensor(np.array(range(len(train_idx))), dtype=torch.long).cuda() new_idx_train = torch.tensor(np.array(range(len(y_smo))), dtype=torch.long).cuda() new_idx_val = torch.tensor(val_idx, dtype=torch.long).cuda() + torch.tensor(len(y_smo) - len(train_idx)).cuda() new_idx_test = torch.tensor(test_idx, dtype=torch.long).cuda() + torch.tensor(len(y_smo) - len(train_idx)).cuda() X_generate = torch.FloatTensor(np.concatenate([X_smo, all_X[idx_except_train, :]], axis=0)).cuda() y_generate = torch.LongTensor(np.concatenate([y_smo, all_y[idx_except_train]], axis=0)).cuda() 这几段是合成节点前后的数据集划分,请你分析一下,正确的划分方式应该是怎样的
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值