刷题感悟 - Guess Number Game II

最近稍微有点懈怠了 刷题不勤了 勉励下自己

这道题目挺有意思的

We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I‘ll tell you whether the number I picked is higher or lower.
However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked.

自己踩了两下坑

坑1:首先没有考虑到大数的权重  以为是最少次数为最优 因此将其作为简单的二分查找来进行

错误代码如下:

        // int sum=0;
        // int m = (int) (n)/2;
        // while(n-m>1){
        //     sum=sum+m;
        //     m=(int)(n+m)/2;
        // }
        // return sum;

简单除暴的反了错误 。而后反思对代码改进踩到了第二个坑

坑2:过于关注大数的权重,解决思路为:将n个数按照和分成几乎相等的两部分 然后取小数部分来进行不断的叠加

问题所在 :无法证明小数部分最终结果一定会大于大数部分 。因此该思路部分结果错误

错误代码如下:

  //     if(n==1)return 0;
    //     if(n==2)return 1;
    //     if(n==3)return 2;
    //  int m=selectm(n,1);
    //  int sum=m;
    //  while(n-m>0){
    //      if(n-m==1)m=selectm(m,1);
    //      else
    //      m=selectm(n,m);
    //      sum=sum+m;
    //  }
    //  return sum;
    // }
    // public int selectm(int n,int m){
    //  int sum = (n-m+1)*(n+m)/2;
    //  int count=0;
    //  for(;m<n;m++){
    //      count = count+m;
    //      if(count*2>=sum){
    //          return m;}
    //  }
    //  return m;

经过反思查找相关质料后发现 其实这道题就是一个典型的Floyd算法 从1-2距离开始不断的二分 选取值与原有的数据比较 得到较小值。

注意点:

1.传入的值是n 数组的大小最好未n+1 X n+1

2.从什么时候开始迭代 起始位置当从2-1开始 3-2,3-1,4-3,4-2,4-1,如此顺序进行。

3.当两个位置相邻时 大小当为较小数。

代码如下:

public int selectn(int n){
    int[][] p = new int[n+1][n+1];
    for(int i=2;i<n+1;i++){
        for(int j=i-1;j>0;j--){
            int globalmin=10000;
            for(int k=j+1;k<i;k++){
                int leftsum = (p[j][k-1]>p[k+1][i])?p[j][k-1]:p[k+1][i];
                int sum = k+leftsum;
                globalmin=(globalmin>sum)?sum:globalmin;
            }
            p[j][i]=(j+1==i)?j:globalmin;
        }
    }
    return p[1][n];
时间: 2024-10-19 11:09:18

刷题感悟 - Guess Number Game II的相关文章

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

【leetcode刷题笔记】Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

【leetcode刷题笔记】Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

leetcode 刷题之路 77 Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. Permutations 的升级版,依旧是全排列问题,但是序列中可能会出现重复数字. 思路:采用字典序的非递归方

刷题感悟 - Longest Palindrome

最近死磕较高难度的题目 深感自己的基本数据结构掌握不够熟练 因此 刷题较慢了些 刷了一道简单的题目涨涨自信 题目:Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" i

LintCode刷题——打劫房屋I、II、III

打劫房屋I: 题目内容: 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警.给定一个非负整数列表,表示每个房子中存放的钱, 算一算,如果今晚去打劫,你最多可以得到多少钱 在不触动报警装置的情况下. 样例: 给定 [3, 8, 4], 返回 8. 挑战: O(n) 时间复杂度 且 O(1) 存储. 算法分析: 前提:对于某一间房子i,如果盗贼要打劫该房子,则房

【leetcode刷题笔记】Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

刷题142. Linked List Cycle II

一.题目说明 题目142. Linked List Cycle II,判断一个链表是否有环,如果有返回环的第一个元素,否则返回NULL. 这个题目是141. Linked List Cycle的升级版本,难度是Medium! 二.我的解答 最直观的解答就是用一个unordered_map<ListNode*,int> dp来统计节点出现的次数,如果出现2,则这个就是第一个节点. class Solution{ public: ListNode* detectCycle(ListNode* he

刷题感悟 - Convert Binary Search Tree to Doubly Linked List

将二叉查找树转化成双向链表 题目思路其实不难 ,中序遍历,然后再依次的将数据放入链表中即可 重点:新加元素前后链的配置 有个有意思的地方在于result没有赋初值 导致写代码时需要先初始化 然后删除之 . /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val =